Skip to content

Commit aec546b

Browse files
committed
✨ MAP
1 parent 45fe36f commit aec546b

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

src/language/filter.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ const data = [1, 2, 3, 4, 5];
55

66
const cb = (item: number) => item % 2 === 0;
77

8-
Deno.test("[filter]", () => {
8+
Deno.test("[filter] Main", () => {
99
const result = filter(data, cb);
1010

1111
assertEquals(result, [2, 4]);
1212
});
1313

14-
Deno.test("[filter vs Array.filter]", () => {
14+
Deno.test("[filter] Object", () => {
15+
const example = [{ hey: true, value: "x" }, { hey: false }];
16+
17+
const cb = (item: typeof example[number]) => item.hey;
18+
19+
const result = filter(example, cb);
20+
const expected = example.filter(cb);
21+
22+
assertEquals(result, [{ hey: true, value: "x" }]);
23+
assertEquals(result, expected);
24+
});
25+
26+
Deno.test("[filter] Versus Array.filter", () => {
1527
const expected = data.filter(cb);
1628
const result = filter(data, cb);
1729

src/language/filter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ export type FilterCallback<T = never> = (
55
) => unknown;
66

77
export function filter<T>(array: readonly T[], test: FilterCallback<T>) {
8-
const result: T[] = [];
8+
const result = [];
99

10-
for (const [index, value] of array.entries()) {
10+
const entries = array.entries();
11+
12+
for (const [index, value] of entries) {
1113
if (test(value, index, array)) {
1214
result.push(value);
1315
}

src/language/map.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { map } from "./map.ts";
2+
import { assert, assertEquals } from "../../deps.ts";
3+
4+
const TEST_DATA = [1, 2, 3, 4, 5];
5+
6+
Deno.test("[map] Versus Array.map", () => {
7+
const result = map(TEST_DATA, (x) => x * 2);
8+
const expected = TEST_DATA.map((x) => x * 2);
9+
10+
assertEquals(result, expected);
11+
});
12+
13+
Deno.test("[map] Chaning Types", () => {
14+
const result = map(TEST_DATA, (x) => `${x}`);
15+
const expected = TEST_DATA.map((x) => `${x}`);
16+
17+
assert(result.every((x) => typeof x === "string"));
18+
19+
assertEquals(result, expected);
20+
});

src/language/map.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
export type MapCallback<T = never> = (
1+
export type MapCallback<T> = (
22
value: T,
33
index: number,
4-
array: T[],
4+
array: readonly T[],
55
) => unknown;
66

7-
export function map<T>(array: T[], transform: MapCallback<T>) {
8-
const result: unknown[] = [];
7+
export function map<T, F extends MapCallback<T>>(array: T[], transform: F) {
8+
const result = [];
99

10-
for (const [index, value] of array.entries()) {
10+
const entries = array.entries();
11+
12+
for (const [index, value] of entries) {
1113
result.push(transform(value, index, array));
1214
}
1315

14-
return result;
16+
return result as ReturnType<F>[];
1517
}
16-
17-
[].map;

0 commit comments

Comments
 (0)