-
-
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 String, Array, TypedArray, ..., #slice function
- Loading branch information
1 parent
22894e6
commit 2c53527
Showing
7 changed files
with
146 additions
and
1 deletion.
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
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 { curry } from "../deps.ts"; | ||
|
||
import { slice as _slice } from "../uncurry/common/slice.ts"; | ||
|
||
/** | ||
* Returns a section of a value. | ||
* @param start - The index to the beginning of the specified portion | ||
* @param end - The index to the end of the specified portion | ||
* @param val - Any `String`, `Array`, `TypedArray`, `ArrayBuffer`, `SharedArrayBuffer` and `Blog` | ||
* @returns The result of `val.slice(start, end)` | ||
* | ||
* @example | ||
* ```ts | ||
* slice(0, 2, 'hello') // 'he' | ||
* slice(1, Infinity, [1, 2, 3]) // [2, 3] | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const slice = curry(_slice); | ||
|
||
export { slice }; |
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,14 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { assertEqualsType, TypedArray } from "../dev_deps.ts"; | ||
import { slice } from "./slice.ts"; | ||
|
||
Deno.test("slice", () => { | ||
assertEqualsType< | ||
| string | ||
| readonly unknown[] | ||
| TypedArray | ||
| ArrayBuffer | ||
| SharedArrayBuffer | ||
| Blob | ||
>(slice(0)(2)([])); | ||
}); |
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 { includes } from "./includes.ts"; | ||
export { map } from "./map.ts"; | ||
export { slice } from "./slice.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { TypedArray } from "../../deps.ts"; | ||
/** | ||
* Returns a section of a value. | ||
* @param start - The index to the beginning of the specified portion | ||
* @param end - The index to the end of the specified portion | ||
* @param val - Any `String`, `Array`, `TypedArray`, `ArrayBuffer`, `SharedArrayBuffer` and `Blog` | ||
* @returns The result of `val.slice(start, end)` | ||
* | ||
* @example | ||
* ```ts | ||
* slice(0, 2, 'hello') // 'he' | ||
* slice(1, Infinity, [1, 2, 3]) // [2, 3] | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const slice = < | ||
T extends | ||
| string | ||
| readonly unknown[] | ||
| TypedArray | ||
| ArrayBuffer | ||
| SharedArrayBuffer | ||
| Blob, | ||
>( | ||
start: number | undefined = 0, | ||
end: number | undefined = Infinity, | ||
val: T, | ||
): T => val.slice(start, end) as T; | ||
|
||
export { slice }; |
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,74 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { assertEquals, assertEqualsType, TypedArray } from "../../dev_deps.ts"; | ||
import { slice } from "./slice.ts"; | ||
|
||
Deno.test("slice", () => { | ||
const table: [ | ||
number | undefined, | ||
number | undefined, | ||
| string | ||
| readonly unknown[] | ||
| TypedArray | ||
| ArrayBuffer | ||
| SharedArrayBuffer | ||
| Blob, | ||
| string | ||
| readonly unknown[] | ||
| TypedArray | ||
| ArrayBuffer | ||
| SharedArrayBuffer | ||
| Blob, | ||
][] = [ | ||
[0, 0, "", ""], | ||
[undefined, undefined, "", ""], | ||
[undefined, undefined, "a", "a"], | ||
[1, undefined, "a", ""], | ||
[0, 2, "abc", "ab"], | ||
[1, 2, "abc", "b"], | ||
[1, Infinity, "abc", "bc"], | ||
[undefined, Infinity, "abc", "abc"], | ||
[undefined, undefined, "abc", "abc"], | ||
[1, undefined, "abc", "bc"], | ||
[-1, undefined, "abc", "c"], | ||
[-Infinity, undefined, "abc", "abc"], | ||
[undefined, undefined, [], []], | ||
[undefined, undefined, [1, 2, 3], [1, 2, 3]], | ||
[0, undefined, [1, 2, 3], [1, 2, 3]], | ||
[0, 1, [1, 2, 3], [1]], | ||
[0, 1, [1, 2, 3], [1]], | ||
[1, undefined, [1, 2, 3], [2, 3]], | ||
[1, 0, [1, 2, 3], []], | ||
[1, 2, [1, 2, 3], [2]], | ||
[1, Infinity, [1, 2, 3], [2, 3]], | ||
[1, undefined, new Uint16Array([1, 2, 3]), [2, 3]], | ||
[undefined, undefined, new ArrayBuffer(16), new ArrayBuffer(16)], | ||
[ | ||
undefined, | ||
undefined, | ||
new SharedArrayBuffer(16), | ||
new SharedArrayBuffer(16), | ||
], | ||
[ | ||
undefined, | ||
undefined, | ||
new Blob(), | ||
new Blob(), | ||
], | ||
]; | ||
|
||
table.forEach(([start, end, val, expected]) => { | ||
assertEquals( | ||
slice(start, end, val), | ||
expected, | ||
`slice(${start}, ${end}, ${val}) -> ${expected}`, | ||
); | ||
}); | ||
|
||
assertEqualsType<string>(slice(undefined, undefined, "")); | ||
assertEqualsType<"abc">(slice(undefined, undefined, "abc")); | ||
assertEqualsType<never[]>(slice(undefined, undefined, [])); | ||
assertEqualsType<string[]>(slice(undefined, undefined, [""])); | ||
assertEqualsType<TypedArray>( | ||
slice(undefined, undefined, new Uint16Array() as TypedArray), | ||
); | ||
}); |