Skip to content

Commit

Permalink
✨ Add TypedArray and Array#map function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jul 2, 2021
1 parent 1d5ae66 commit 22894e6
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ The module that bundles the dependencies is obtained from
`dotAll`, `exec`, `flags`, `global`, `ignoreCase`, `lastIndex`, `multiline`,
`source`, `sticky`, `test`, `unicode`
### Common(Same method or property name)
### Common(Methods or properties of multiple types)
`includes`, `length`
`includes`, `length`, `map`
## :green_heart: Supports
Expand Down
23 changes: 23 additions & 0 deletions common/map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.

import { curry } from "../deps.ts";

import { map as _map } from "../uncurry/common/map.ts";

/**
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
* @param callbackfn - callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array
* @param val - Any `Array` or `TypedArray`
* @returns The result of `val.map(callbackfn)`
*
* @example
* ```ts
* map(() => 1, [1, 2, 3]) // [1, 1, 1]
* map((a) => a + 1, new Unit8Array([2, 4, 6])) // Uint8Array(2) [3, 5, 7]
* ```
*
* @beta
*/
const map = curry(_map);

export { map };
9 changes: 9 additions & 0 deletions common/map_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import { assertEqualsType, TypedArray } from "../dev_deps.ts";
import { map } from "./map.ts";

Deno.test("map", () => {
assertEqualsType<
TypedArray | readonly unknown[]
>(map(() => 1, new Uint16Array([])));
});
1 change: 1 addition & 0 deletions common/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
export { length } from "./length.ts";
export { includes } from "./includes.ts";
export { map } from "./map.ts";
1 change: 1 addition & 0 deletions dev_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export { assertEquals } from "https://deno.land/std@0.97.0/testing/asserts.ts";
export { equal } from "https://deno.land/x/equal@v1.5.0/mod.ts";
export type {
AnyFn,
TypedArray,
} from "https://deno.land/x/fonction@v2.1.0-beta.1/mod.ts";

Expand Down
24 changes: 24 additions & 0 deletions uncurry/common/map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import { TypedArray } from "../../deps.ts";

/**
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
* @param callbackfn - callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array
* @param val - Any `Array` or `TypedArray`
* @returns The result of `val.map(callbackfn)`
*
* @example
* ```ts
* map(() => 1, [1, 2, 3]) // [1, 1, 1]
* map((a) => a + 1, new Unit8Array([2, 4, 6])) // Uint8Array(2) [3, 5, 7]
* ```
*
* @beta
*/
const map = <T extends TypedArray | readonly unknown[]>(
callbackfn: (value: T[number], index: number, array: T) => T[number],
val: T,
): T extends unknown[] ? Exclude<T, TypedArray> : Exclude<T, unknown[]> =>
val.map(callbackfn as never) as never;

export { map };
39 changes: 39 additions & 0 deletions uncurry/common/map_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import {
AnyFn,
assertEquals,
assertEqualsType,
TypedArray,
} from "../../dev_deps.ts";
import { map } from "./map.ts";

Deno.test("map", () => {
const table: [
AnyFn,
readonly unknown[] | TypedArray,
// deno-lint-ignore no-explicit-any
any,
][] = [
[() => {}, [], []],
[() => 1, [], []],
[() => 1, [1, 2, 3], [1, 1, 1]],
[(a: number) => a, [1, 2, 3], [1, 2, 3]],
[() => 1, new Uint16Array([1, 2, 3]), [1, 1, 1]],
[(a: number) => a, new Uint16Array([1, 2, 3]), [1, 2, 3]],
];

table.forEach(([callbackfn, val, expected]) => {
assertEquals(
map(callbackfn, val),
expected,
`map(${callbackfn}, ${val}) -> ${expected}`,
);
});

assertEqualsType<never[]>(map(() => [] as never, []));
assertEqualsType<string[]>(map(() => "", [""]));
assertEqualsType<readonly [""]>(map(() => "" as const, [""] as const));
assertEqualsType<Uint16Array>(map(() => 1, new Uint16Array([])));
assertEqualsType<Uint32Array>(map((a) => a, new Uint32Array([])));
assertEqualsType<Uint8ClampedArray>(map(() => 1, new Uint8ClampedArray([])));
});
1 change: 1 addition & 0 deletions uncurry/common/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
export { includes } from "./includes.ts";
export { map } from "./map.ts";

0 comments on commit 22894e6

Please sign in to comment.