Skip to content

Commit

Permalink
✨ Add String, Array, TypedArray, ..., #slice function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jul 2, 2021
1 parent 22894e6 commit 2c53527
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ The module that bundles the dependencies is obtained from
### Common(Methods or properties of multiple types)
`includes`, `length`, `map`
`includes`, `length`, `map`, `slice`
## :green_heart: Supports
Expand Down
1 change: 1 addition & 0 deletions common/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
export { length } from "./length.ts";
export { includes } from "./includes.ts";
export { map } from "./map.ts";
export { slice } from "./slice.ts";
24 changes: 24 additions & 0 deletions common/slice.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 { 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 };
14 changes: 14 additions & 0 deletions common/slice_test.ts
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)([]));
});
1 change: 1 addition & 0 deletions uncurry/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 { includes } from "./includes.ts";
export { map } from "./map.ts";
export { slice } from "./slice.ts";
31 changes: 31 additions & 0 deletions uncurry/common/slice.ts
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 };
74 changes: 74 additions & 0 deletions uncurry/common/slice_test.ts
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),
);
});

0 comments on commit 2c53527

Please sign in to comment.