Repository navigation
Expand file tree
/
Copy pathjoin.ts
More file actions
75 lines (74 loc) · 2.28 KB
/
Copy pathjoin.ts
File metadata and controls
75 lines (74 loc) · 2.28 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import * as Internal from '@app/internal/index.ts'
/**
* Join characters with a separator.
* @description Inserts a separator between every character.
* @param divider - Separator placed between characters
* @param input - String to join
* @returns Joined string
*/
export default function join(
divider: string,
input: string
): string
/**
* Join string vector characters with a separator.
* @description Inserts a separator between the characters of each string.
* @param divider - Separator or vector of separators
* @param input - String or vector of strings to join
* @returns Vector of joined strings
*/
export default function join(
divider: string | string[],
input: string | string[]
): string[]
/**
* Join string array characters with a separator.
* @description Inserts a separator between every character.
* @param divider - Separator or vector of separators
* @param input - String, vector, or matrix of strings to join
* @returns Joined strings matching the input shape
*/
export default function join(
divider: string | string[] | string[][],
input: string | string[] | string[][]
): string | string[] | string[][]
/**
* Join characters with a separator.
* @description Inserts a separator between every character.
* @param divider - Separator placed between characters
* @param input - String or array of strings to join
* @returns Joined strings matching the input shape
*/
export default function join(
divider: string | string[] | string[][],
input: string | string[] | string[][]
): string | string[] | string[][] {
if (typeof divider === 'string') {
return Internal.mapper<string, string>(
input,
(source) => {
const entry = String(source ?? '')
if (entry.length === 0) {
return ''
}
let result = ''
let cursor = 0
while (cursor < entry.length) {
const width = (entry.codePointAt(cursor) ?? 0) > 0xffff ? 2 : 1
if (cursor > 0) {
result += divider
}
result += entry.slice(cursor, cursor + width)
cursor += width
}
return result
}
)
}
return Internal.broadcast<string>(
divider,
input,
(separator, entry) => join(String(separator ?? ''), String(entry ?? '')),
'' as unknown as number
)
}