Repository navigation
Expand file tree
/
Copy pathlength.ts
More file actions
57 lines (56 loc) · 1.84 KB
/
Copy pathlength.ts
File metadata and controls
57 lines (56 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as Internal from '@app/internal/index.ts'
/**
* Measure the length of text.
* @description Counts characters in a single string.
* @param input - String to measure
* @returns Character count
*/
export default function length(input: string): number
/**
* Measure string vector lengths.
* @description Counts characters in each vector string entry.
* @param input - Vector of strings to measure
* @returns Vector of character counts
*/
export default function length(input: string[]): number[]
/**
* Measure string matrix lengths.
* @description Counts characters in each matrix string entry.
* @param input - Matrix of strings to measure
* @returns Matrix of character counts
*/
export default function length(input: string[][]): number[][]
/**
* Measure the length of text.
* @description Counts characters in each string entry.
* @param input - String, vector, or matrix of strings to measure
* @returns Character counts matching the input shape
*/
export default function length(
input: string | string[] | string[][]
): number | number[] | number[][]
/**
* Measure the length of text.
* @description Counts characters in each string entry.
* @param input - String, vector, or matrix of strings to measure
* @returns Character counts matching the input shape
*/
export default function length(
input: string | string[] | string[][]
): number | number[] | number[][] {
return Internal.mapper(input, (value) => {
const source = value ?? ''
let count = 0
for (let cursor = 0; cursor < source.length; cursor++) {
const point = source.charCodeAt(cursor)
if (point >= 0xd800 && point <= 0xdbff && cursor + 1 < source.length) {
const trailing = source.charCodeAt(cursor + 1)
if (trailing >= 0xdc00 && trailing <= 0xdfff) {
cursor++
}
}
count++
}
return count
})
}