-
Notifications
You must be signed in to change notification settings - Fork 64
/
split.ts
135 lines (119 loc) · 2.86 KB
/
split.ts
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
import { isAsyncIterable, isIterable } from "../_internal/utils";
import type ReturnIterableIteratorType from "../types/ReturnIterableIteratorType";
import concurrent, { isConcurrent } from "./concurrent";
function* sync(sep: string, iterable: Iterable<string>) {
if (sep === "") {
return yield* iterable;
}
let acc = "";
let chr = "";
for (chr of iterable) {
if (chr === sep) {
yield acc;
acc = "";
} else {
acc += chr;
}
}
if (chr === sep) {
yield "";
} else if (acc.length > 0) {
yield acc;
}
}
async function* asyncSequential(sep: string, iterable: AsyncIterable<string>) {
if (sep === "") {
return yield* iterable;
}
let acc = "";
let chr = "";
for await (chr of iterable) {
if (chr === sep) {
yield acc;
acc = "";
} else {
acc += chr;
}
}
if (chr === sep) {
yield "";
} else if (acc.length > 0) {
yield acc;
}
}
function async(
sep: string,
iterable: AsyncIterable<string>,
): AsyncIterableIterator<string> {
let _iterator: AsyncIterator<string>;
return {
async next(_concurrent: any) {
if (_iterator === undefined) {
_iterator = isConcurrent(_concurrent)
? asyncSequential(sep, concurrent(_concurrent.length, iterable))
: asyncSequential(sep, iterable);
}
return _iterator.next(_concurrent);
},
[Symbol.asyncIterator]() {
return this;
},
};
}
/**
* Splits string by separator.
*
* @example
* ```ts
* const iter = split(',', '1,2,3,4');
* iter.next(); // 1
* iter.next(); // 2
* iter.next(); // 3
* iter.next(); // 4
* iter.next(); // undefined
*
* // with pipe
* pipe(
* "1,2,3,4,5",
* split(','),
* toArray,
* ); // ["1", "2", "3", "4", "5"]
* ```
*
* see {@link https://fxts.dev/docs/pipe | pipe}, {@link https://fxts.dev/docs/toArray | toArray}
*/
function split(
sep: string,
iterable: Iterable<string>,
): IterableIterator<string>;
function split(
sep: string,
iterable: AsyncIterable<string>,
): AsyncIterableIterator<string>;
function split<A extends Iterable<string> | AsyncIterable<string>>(
sep: string,
): (iterable: A) => ReturnIterableIteratorType<A, string>;
function split<A extends Iterable<string> | AsyncIterable<string>>(
sep: string,
iterable?: A,
):
| IterableIterator<string>
| AsyncIterableIterator<string>
| ((iterable: A) => ReturnIterableIteratorType<A, string>) {
if (iterable === undefined) {
return (iterable: A): ReturnIterableIteratorType<A, string> => {
return split(sep, iterable as any) as ReturnIterableIteratorType<
A,
string
>;
};
}
if (isIterable<string>(iterable)) {
return sync(sep, iterable);
}
if (isAsyncIterable<string>(iterable)) {
return async(sep, iterable);
}
throw new TypeError("'iterable' must be type of Iterable or AsyncIterable");
}
export default split;