-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add TypedArray and Array#map function
- Loading branch information
1 parent
1d5ae66
commit 22894e6
Showing
8 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]))); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]))); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |