-
-
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.
- Loading branch information
1 parent
79851d6
commit f9ffce7
Showing
4 changed files
with
51 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
/** | ||
* Returns length property. | ||
* | ||
* @param val - Value with length property | ||
* @returns The result of `val.length` | ||
* | ||
* @example | ||
* ```ts | ||
* length('hello') // 5 | ||
* length(['hello', 'world', 1]) // 3 | ||
* length({length: 5, text: 'hello'}) // 5 | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const length = <T extends { length: number }>(val: T): T["length"] => | ||
val.length; | ||
|
||
export { length }; |
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,28 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
import { assertEquals, assertEqualsType } from "../dev_deps.ts"; | ||
import { length } from "./length.ts"; | ||
|
||
Deno.test("length", () => { | ||
const table: [{ length: number } | string, number][] = [ | ||
["", 0], | ||
[" ", 1], | ||
[" ", 2], | ||
["a", 1], | ||
["ab", 2], | ||
["hello", 5], | ||
[[], 0], | ||
[[""], 1], | ||
[["", 0], 2], | ||
[["", 0, "1"], 3], | ||
[{ length: 3 }, 3], | ||
]; | ||
|
||
table.forEach(([val, expected]) => { | ||
assertEquals(length(val), expected, `length(${val}) -> ${expected}`); | ||
}); | ||
|
||
assertEqualsType<number>(length("")); | ||
assertEqualsType<number>(length([])); | ||
assertEqualsType<number>(length({ length: 1 })); | ||
assertEqualsType<1>(length({ length: 1 } as const)); | ||
}); |
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,2 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
export { length } from "./length.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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license. | ||
export * from "./string/mod.ts"; | ||
export * from "./common/mod.ts"; |