Repository navigation
Expand file tree
/
Copy pathslice.ts
More file actions
182 lines (181 loc) · 5.71 KB
/
Copy pathslice.ts
File metadata and controls
182 lines (181 loc) · 5.71 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import * as Internal from '@app/internal/index.ts'
import length from '@app/string/length.ts'
/**
* Extract a substring from one string.
* @description Slices the string between the given bounds.
* @param input - String to slice
* @param begin - Starting index of the extracted portion
* @param finish - Ending index of the extracted portion
* @param stride - Step between extracted characters
* @returns Extracted substring
*/
export default function slice(
input: string,
begin?: number,
finish?: number,
stride?: number
): string
/**
* Extract substrings from a string vector.
* @description Slices each vector string between the given bounds.
* @param input - String or vector of strings to slice
* @param begin - Starting index or vector of starting indices
* @param finish - Ending index or vector of ending indices
* @param stride - Step or vector of steps between characters
* @returns Vector of extracted substrings
*/
export default function slice(
input: string | string[],
begin?: number | number[],
finish?: number | number[],
stride?: number | number[]
): string[]
/**
* Extract substrings from string arrays.
* @description Slices each string between the given bounds.
* @param input - String, vector, or matrix of strings to slice
* @param begin - Starting index or vector of starting indices
* @param finish - Ending index or vector of ending indices
* @param stride - Step or vector of steps between characters
* @returns Extracted substrings matching the input shape
*/
export default function slice(
input: string | string[] | string[][],
begin?: number | number[] | number[][],
finish?: number | number[] | number[][],
stride?: number | number[] | number[][]
): string | string[] | string[][]
/**
* Extract substrings from string arrays.
* @description Slices each string between the given bounds.
* @param input - String, vector, or matrix of strings to slice
* @param begin - Starting index or vector of starting indices
* @param finish - Ending index or vector of ending indices
* @param stride - Step or vector of steps between characters
* @returns Extracted substrings matching the input shape
*/
export default function slice(
input: string | string[] | string[][],
begin?: number | number[] | number[][],
finish?: number | number[] | number[][],
stride: number | number[] | number[][] = 1
): string | string[] | string[][] {
if (
(begin === undefined || typeof begin === 'number') &&
(finish === undefined || typeof finish === 'number') &&
typeof stride === 'number'
) {
if (stride === 0) {
throw new RangeError('slice step cannot be zero')
}
return Internal.mapper<string, string>(input, (source) => {
const entry = String(source ?? '')
if (stride === 1) {
const total = length(entry)
const start = begin === undefined
? 0
: begin < 0
? Math.max(0, total + begin)
: Math.min(total, begin)
const bound = finish === undefined
? total
: finish < 0
? Math.max(0, total + finish)
: Math.min(total, finish)
if (bound <= start) {
return ''
}
if (total === entry.length) {
return entry.slice(start, bound)
}
let cursor = 0
let index = 0
while (index < start && cursor < entry.length) {
cursor += (entry.codePointAt(cursor) ?? 0) > 0xffff ? 2 : 1
index++
}
const offset = cursor
while (index < bound && cursor < entry.length) {
cursor += (entry.codePointAt(cursor) ?? 0) > 0xffff ? 2 : 1
index++
}
return entry.slice(offset, cursor)
}
const symbols = Array.from(entry)
const count = symbols.length
let extracted = ''
if (stride > 0) {
for (
let cursor = Math.min(
count,
begin === undefined ? 0 : begin < 0 ? Math.max(0, count + begin) : begin
);
cursor < Math.min(
count,
finish === undefined ? count : finish < 0 ? Math.max(0, count + finish) : finish
);
cursor += stride
) {
extracted += symbols[cursor] ?? ''
}
return extracted
}
for (
let cursor = Math.min(
count - 1,
begin === undefined ? count - 1 : begin < 0 ? Math.max(-1, count + begin) : begin
);
cursor >
(finish === undefined
? -1
: finish < 0
? Math.max(-1, count + finish)
: Math.min(count, finish));
cursor += stride
) {
if (cursor >= 0 && cursor < count) {
extracted += symbols[cursor] ?? ''
}
}
return extracted
})
}
Internal.mapper(stride, (delta) => {
if ((delta ?? 1) === 0) {
throw new RangeError('slice step cannot be zero')
}
return delta
})
return Internal.broadcast<string>(
Internal.broadcast(
Internal.broadcast(
input,
begin ?? null,
(entry, first) => ({ entry: String(entry ?? ''), first }),
null as unknown as number
),
finish ?? null,
(leading, final) => ({
entry: (leading as unknown as { entry: string; first: number | null }).entry,
first: (leading as unknown as { entry: string; first: number | null }).first,
final
}),
null as unknown as number
),
stride,
(leading, delta) => {
const { entry, first, final } = leading as unknown as {
entry: string
first: number | null
final: number | null
}
return slice(
entry,
first === null ? undefined : first,
final === null ? undefined : final,
delta ?? 1
)
},
1
)
}