semantic-typescript 0.3.0 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collectable.d.ts +19 -24
- package/dist/collectable.js +329 -177
- package/dist/collector.d.ts +93 -16
- package/dist/collector.js +297 -107
- package/dist/factory.d.ts +6 -0
- package/dist/factory.js +224 -148
- package/dist/guard.d.ts +3 -1
- package/dist/guard.js +14 -2
- package/dist/hook.d.ts +14 -2
- package/dist/hook.js +85 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/semantic.d.ts +2 -6
- package/dist/semantic.js +335 -185
- package/dist/statistics.d.ts +13 -17
- package/dist/statistics.js +204 -522
- package/dist/utility.d.ts +4 -0
- package/dist/utility.js +2 -1
- package/dist/window.d.ts +12 -0
- package/dist/window.js +60 -0
- package/package.json +1 -1
- package/readme.md +273 -47
package/dist/factory.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isBigInt, isFunction, isIterable, isNumber, isPromise } from "./guard";
|
|
2
|
-
import { useCompare } from "./hook";
|
|
1
|
+
import { isBigInt, isFunction, isIterable, isNumber, isObject, isPromise } from "./guard";
|
|
2
|
+
import { useCompare, useTraverse } from "./hook";
|
|
3
3
|
import { Semantic } from "./semantic";
|
|
4
4
|
import { invalidate, validate } from "./utility";
|
|
5
5
|
export let animationFrame = (period, delay = 0) => {
|
|
@@ -7,24 +7,55 @@ export let animationFrame = (period, delay = 0) => {
|
|
|
7
7
|
throw new TypeError("Period must be positive finite number and delay must be non-negative finite number.");
|
|
8
8
|
}
|
|
9
9
|
return new Semantic((accept, interrupt) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
else if (performance.now() - start < period) {
|
|
17
|
-
requestAnimationFrame(animate);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
if (interrupt(start, index)) {
|
|
21
|
-
return;
|
|
10
|
+
try {
|
|
11
|
+
let start = performance.now();
|
|
12
|
+
let index = 0n;
|
|
13
|
+
let animate = () => {
|
|
14
|
+
if (performance.now() - start >= delay) {
|
|
15
|
+
requestAnimationFrame(animate);
|
|
22
16
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
else if (performance.now() - start < period) {
|
|
18
|
+
requestAnimationFrame(animate);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
if (interrupt(start, index)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
accept(performance.now(), index);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
throw new Error("Uncaught error as creating animation frame semantic.");
|
|
30
|
+
}
|
|
26
31
|
});
|
|
27
32
|
};
|
|
33
|
+
;
|
|
34
|
+
export let attribute = (target) => {
|
|
35
|
+
if (isObject(target)) {
|
|
36
|
+
return new Semantic((accept, interrupt) => {
|
|
37
|
+
try {
|
|
38
|
+
let index = 0n;
|
|
39
|
+
useTraverse(target, (key, value) => {
|
|
40
|
+
let attribute = {
|
|
41
|
+
key: key,
|
|
42
|
+
value: value
|
|
43
|
+
};
|
|
44
|
+
if (interrupt(attribute, index)) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
accept(attribute, index);
|
|
48
|
+
index++;
|
|
49
|
+
return true;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
throw new Error("Uncaught error as creating attribute semantic.");
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
throw new TypeError("Target must be an object.");
|
|
58
|
+
};
|
|
28
59
|
export let blob = (blob, chunk = 64n * 1024n) => {
|
|
29
60
|
let size = Number(chunk);
|
|
30
61
|
if (size <= 0 || !Number.isSafeInteger(size)) {
|
|
@@ -34,60 +65,65 @@ export let blob = (blob, chunk = 64n * 1024n) => {
|
|
|
34
65
|
throw new TypeError("Blob is invalid.");
|
|
35
66
|
}
|
|
36
67
|
return new Semantic((accept, interrupt) => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
68
|
+
try {
|
|
69
|
+
let index = 0n;
|
|
70
|
+
let stoppable = false;
|
|
71
|
+
let stream = blob.stream();
|
|
72
|
+
let reader = stream.getReader();
|
|
73
|
+
let buffer = new Uint8Array(size);
|
|
74
|
+
let offset = 0;
|
|
75
|
+
(async () => {
|
|
76
|
+
try {
|
|
77
|
+
while (!stoppable) {
|
|
78
|
+
let { done, value } = await reader.read();
|
|
79
|
+
if (done) {
|
|
80
|
+
if (offset > 0) {
|
|
81
|
+
let element = buffer.subarray(0, offset);
|
|
82
|
+
if (interrupt(element, index)) {
|
|
83
|
+
stoppable = true;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
accept(element, index);
|
|
87
|
+
index++;
|
|
88
|
+
}
|
|
56
89
|
}
|
|
90
|
+
break;
|
|
57
91
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
92
|
+
let chunkData = value;
|
|
93
|
+
let position = 0;
|
|
94
|
+
while (position < chunkData.length && !stoppable) {
|
|
95
|
+
let space = size - offset;
|
|
96
|
+
let toCopy = Math.min(space, chunkData.length - position);
|
|
97
|
+
buffer.set(chunkData.subarray(position, position + toCopy), offset);
|
|
98
|
+
offset += toCopy;
|
|
99
|
+
position += toCopy;
|
|
100
|
+
if (offset === size) {
|
|
101
|
+
if (interrupt(buffer, index)) {
|
|
102
|
+
stoppable = true;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
accept(buffer, index);
|
|
106
|
+
index++;
|
|
107
|
+
}
|
|
108
|
+
offset = 0;
|
|
75
109
|
}
|
|
76
|
-
offset = 0;
|
|
77
110
|
}
|
|
78
111
|
}
|
|
79
112
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
console.error(error);
|
|
83
|
-
}
|
|
84
|
-
finally {
|
|
85
|
-
if (stoppable) {
|
|
86
|
-
await reader.cancel();
|
|
113
|
+
catch (error) {
|
|
114
|
+
console.error(error);
|
|
87
115
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
116
|
+
finally {
|
|
117
|
+
if (stoppable) {
|
|
118
|
+
await reader.cancel();
|
|
119
|
+
}
|
|
120
|
+
reader.releaseLock();
|
|
121
|
+
}
|
|
122
|
+
})();
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
throw new Error("Uncaught error as creating blob semantic.");
|
|
126
|
+
}
|
|
91
127
|
});
|
|
92
128
|
};
|
|
93
129
|
export let empty = () => {
|
|
@@ -96,12 +132,17 @@ export let empty = () => {
|
|
|
96
132
|
export let fill = (element, count) => {
|
|
97
133
|
if (validate(element) && count > 0n) {
|
|
98
134
|
return new Semantic((accept, interrupt) => {
|
|
99
|
-
|
|
100
|
-
let
|
|
101
|
-
|
|
102
|
-
|
|
135
|
+
try {
|
|
136
|
+
for (let i = 0n; i < count; i++) {
|
|
137
|
+
let item = isFunction(element) ? element() : element;
|
|
138
|
+
if (interrupt(item, i)) {
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
accept(item, i);
|
|
103
142
|
}
|
|
104
|
-
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
throw new Error("Uncaught error as creating fill semantic.");
|
|
105
146
|
}
|
|
106
147
|
});
|
|
107
148
|
}
|
|
@@ -110,13 +151,18 @@ export let fill = (element, count) => {
|
|
|
110
151
|
export let from = (iterable) => {
|
|
111
152
|
if (isIterable(iterable)) {
|
|
112
153
|
return new Semantic((accept, interrupt) => {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
154
|
+
try {
|
|
155
|
+
let index = 0n;
|
|
156
|
+
for (let element of iterable) {
|
|
157
|
+
if (interrupt(element, index)) {
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
accept(element, index);
|
|
161
|
+
index++;
|
|
117
162
|
}
|
|
118
|
-
|
|
119
|
-
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
throw new Error("Uncaught error as creating from semantic.");
|
|
120
166
|
}
|
|
121
167
|
});
|
|
122
168
|
}
|
|
@@ -125,14 +171,19 @@ export let from = (iterable) => {
|
|
|
125
171
|
export let generate = (supplier, interrupt) => {
|
|
126
172
|
if (isFunction(supplier) && isFunction(interrupt)) {
|
|
127
173
|
return new Semantic((accept, interrupt) => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
174
|
+
try {
|
|
175
|
+
let index = 0n;
|
|
176
|
+
while (true) {
|
|
177
|
+
let element = supplier();
|
|
178
|
+
if (interrupt(element, index)) {
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
accept(element, index);
|
|
182
|
+
index++;
|
|
133
183
|
}
|
|
134
|
-
|
|
135
|
-
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
throw new Error("Uncaught error as creating generate semantic.");
|
|
136
187
|
}
|
|
137
188
|
});
|
|
138
189
|
}
|
|
@@ -141,8 +192,22 @@ export let generate = (supplier, interrupt) => {
|
|
|
141
192
|
export let interval = (period, delay = 0) => {
|
|
142
193
|
if (period > 0 && delay >= 0) {
|
|
143
194
|
return new Semantic((accept, interrupt) => {
|
|
144
|
-
|
|
145
|
-
|
|
195
|
+
try {
|
|
196
|
+
if (delay > 0) {
|
|
197
|
+
setTimeout(() => {
|
|
198
|
+
let count = 0;
|
|
199
|
+
let index = 0n;
|
|
200
|
+
let timer = setInterval(() => {
|
|
201
|
+
if (interrupt(count, index)) {
|
|
202
|
+
clearInterval(timer);
|
|
203
|
+
}
|
|
204
|
+
accept(count, BigInt(index));
|
|
205
|
+
index++;
|
|
206
|
+
count += period;
|
|
207
|
+
}, period);
|
|
208
|
+
}, delay);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
146
211
|
let count = 0;
|
|
147
212
|
let index = 0n;
|
|
148
213
|
let timer = setInterval(() => {
|
|
@@ -153,19 +218,10 @@ export let interval = (period, delay = 0) => {
|
|
|
153
218
|
index++;
|
|
154
219
|
count += period;
|
|
155
220
|
}, period);
|
|
156
|
-
}
|
|
221
|
+
}
|
|
157
222
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
let index = 0n;
|
|
161
|
-
let timer = setInterval(() => {
|
|
162
|
-
if (interrupt(count, index)) {
|
|
163
|
-
clearInterval(timer);
|
|
164
|
-
}
|
|
165
|
-
accept(count, BigInt(index));
|
|
166
|
-
index++;
|
|
167
|
-
count += period;
|
|
168
|
-
}, period);
|
|
223
|
+
catch (error) {
|
|
224
|
+
throw new Error("Uncaught error as creating interval semantic.");
|
|
169
225
|
}
|
|
170
226
|
});
|
|
171
227
|
}
|
|
@@ -173,21 +229,31 @@ export let interval = (period, delay = 0) => {
|
|
|
173
229
|
};
|
|
174
230
|
export let iterate = (generator) => {
|
|
175
231
|
if (isFunction(generator)) {
|
|
176
|
-
|
|
232
|
+
try {
|
|
233
|
+
return new Semantic(generator);
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
throw new Error("Uncaught error as creating iterate semantic.");
|
|
237
|
+
}
|
|
177
238
|
}
|
|
178
239
|
throw new TypeError("Invalid arguments.");
|
|
179
240
|
};
|
|
180
241
|
export let promise = (promise) => {
|
|
181
242
|
if (isPromise(promise)) {
|
|
182
243
|
return new Semantic((accept, interrupt) => {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
244
|
+
try {
|
|
245
|
+
promise.then((value) => {
|
|
246
|
+
if (interrupt(value, 0n)) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
accept(value, 0n);
|
|
250
|
+
}).catch((error) => {
|
|
251
|
+
console.error(error);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
throw new Error("Uncaught error as creating promise semantic.");
|
|
256
|
+
}
|
|
191
257
|
});
|
|
192
258
|
}
|
|
193
259
|
else {
|
|
@@ -202,12 +268,17 @@ export let range = (start, end, step = 1) => {
|
|
|
202
268
|
let minimum = start, maximum = end, limit = Number(step);
|
|
203
269
|
let condition = limit > 0 ? (i) => i < maximum : (i) => i > maximum;
|
|
204
270
|
return new Semantic((accept, interrupt) => {
|
|
205
|
-
|
|
206
|
-
let
|
|
207
|
-
|
|
208
|
-
|
|
271
|
+
try {
|
|
272
|
+
for (let i = minimum; condition(i); i += limit) {
|
|
273
|
+
let value = i;
|
|
274
|
+
if (interrupt(value, BigInt(i))) {
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
accept(value, BigInt(i));
|
|
209
278
|
}
|
|
210
|
-
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
throw new Error("Uncaught error as creating range semantic.");
|
|
211
282
|
}
|
|
212
283
|
});
|
|
213
284
|
}
|
|
@@ -218,43 +289,48 @@ export let websocket = (websocket) => {
|
|
|
218
289
|
throw new TypeError("WebSocket is invalid.");
|
|
219
290
|
}
|
|
220
291
|
return new Semantic((accept, interrupt) => {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
stop
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
stop
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
stop
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
stop
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
292
|
+
try {
|
|
293
|
+
let index = 0n;
|
|
294
|
+
let stop = false;
|
|
295
|
+
websocket.addEventListener("open", (event) => {
|
|
296
|
+
if (stop || interrupt(event, index)) {
|
|
297
|
+
stop = true;
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
accept(event, index);
|
|
301
|
+
index++;
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
websocket.addEventListener("message", (event) => {
|
|
305
|
+
if (stop || interrupt(event, index)) {
|
|
306
|
+
stop = true;
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
accept(event, index);
|
|
310
|
+
index++;
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
websocket.addEventListener("error", (event) => {
|
|
314
|
+
if (stop || interrupt(event, index)) {
|
|
315
|
+
stop = true;
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
accept(event, index);
|
|
319
|
+
index++;
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
websocket.addEventListener("close", (event) => {
|
|
323
|
+
if (stop || interrupt(event, index)) {
|
|
324
|
+
stop = true;
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
accept(event, index);
|
|
328
|
+
index++;
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
throw new Error("Uncaught error as creating websocket semantic.");
|
|
334
|
+
}
|
|
259
335
|
});
|
|
260
336
|
};
|
package/dist/guard.d.ts
CHANGED
|
@@ -22,4 +22,6 @@ export declare let isStatistics: (t: unknown) => t is Statistics<unknown, number
|
|
|
22
22
|
export declare let isNumericStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
|
|
23
23
|
export declare let isBigIntStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
|
|
24
24
|
export declare let isPromise: (t: unknown) => t is Promise<unknown>;
|
|
25
|
-
export declare let
|
|
25
|
+
export declare let isAsyncFunction: (t: unknown) => t is AsyncFunction;
|
|
26
|
+
export declare let isGeneratorFunction: (t: unknown) => t is Generator<unknown, unknown, unknown>;
|
|
27
|
+
export declare let isAsyncGeneratorFunction: (t: unknown) => t is AsyncGenerator<unknown, unknown, unknown>;
|
package/dist/guard.js
CHANGED
|
@@ -6,7 +6,7 @@ export let isString = (t) => {
|
|
|
6
6
|
return typeof t === "string";
|
|
7
7
|
};
|
|
8
8
|
export let isNumber = (t) => {
|
|
9
|
-
return typeof t === "number";
|
|
9
|
+
return typeof t === "number" && !Number.isNaN(t) && Number.isFinite(t);
|
|
10
10
|
};
|
|
11
11
|
export let isFunction = (t) => {
|
|
12
12
|
return typeof t === "function";
|
|
@@ -89,9 +89,21 @@ export let isPromise = (t) => {
|
|
|
89
89
|
}
|
|
90
90
|
return false;
|
|
91
91
|
};
|
|
92
|
-
export let
|
|
92
|
+
export let isAsyncFunction = (t) => {
|
|
93
93
|
if (isFunction(t)) {
|
|
94
94
|
return Reflect.get(t, Symbol.toStringTag) === "AsyncFunction" && t.constructor.name === "AsyncFunction";
|
|
95
95
|
}
|
|
96
96
|
return false;
|
|
97
97
|
};
|
|
98
|
+
export let isGeneratorFunction = (t) => {
|
|
99
|
+
if (isObject(t)) {
|
|
100
|
+
return isFunction(t) && Reflect.get(t, "constructor").name === "GeneratorFunction";
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
};
|
|
104
|
+
export let isAsyncGeneratorFunction = (t) => {
|
|
105
|
+
if (isObject(t)) {
|
|
106
|
+
return isFunction(t) && Reflect.get(t, "constructor").name === "AsyncGeneratorFunction";
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
};
|
package/dist/hook.d.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type BiPredicate, type DeepPropertyKey, type DeepPropertyValue } from "./utility";
|
|
2
|
+
import type { Comparator, Generator } from "./utility";
|
|
2
3
|
export declare let useCompare: <T>(t1: T, t2: T) => number;
|
|
3
4
|
export declare let useRandom: <T = number | bigint>(index: T) => T;
|
|
4
|
-
export declare let useTraverse: <T extends object>(t: T, callback:
|
|
5
|
+
export declare let useTraverse: <T extends object>(t: T, callback: BiPredicate<DeepPropertyKey<T>, DeepPropertyValue<T>>) => void;
|
|
6
|
+
export declare let useGenerator: <E>(iterable: Iterable<E>) => Generator<E>;
|
|
7
|
+
interface UseArrange {
|
|
8
|
+
<E>(source: Iterable<E>): Generator<E>;
|
|
9
|
+
<E>(source: Iterable<E>, comparator: Comparator<E>): Generator<E>;
|
|
10
|
+
<E>(source: Generator<E>): Generator<E>;
|
|
11
|
+
<E>(source: Generator<E>, comparator: Comparator<E>): Generator<E>;
|
|
12
|
+
}
|
|
13
|
+
export declare let useArrange: UseArrange;
|
|
14
|
+
export declare let useToNumber: <T = unknown>(target: T) => number;
|
|
15
|
+
export declare let useToBigInt: <T = unknown>(target: T) => bigint;
|
|
16
|
+
export {};
|
package/dist/hook.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useToArray } from "./collector";
|
|
2
|
+
import { isBigInt, isFunction, isIterable, isNumber, isObject, isPrimitive } from "./guard";
|
|
2
3
|
import { validate } from "./utility";
|
|
3
4
|
export let useCompare = (t1, t2) => {
|
|
4
5
|
if (t1 === t2 || Object.is(t1, t2)) {
|
|
@@ -69,26 +70,38 @@ export let useTraverse = (t, callback) => {
|
|
|
69
70
|
let traverse = (target) => {
|
|
70
71
|
if (!seen.has(target)) {
|
|
71
72
|
seen.add(target);
|
|
73
|
+
let stop = false;
|
|
72
74
|
let properties = Reflect.ownKeys(target);
|
|
73
75
|
for (let property of properties) {
|
|
74
|
-
let value = target
|
|
76
|
+
let value = Reflect.get(target, property);
|
|
77
|
+
if (stop) {
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
75
80
|
if (validate(value)) {
|
|
76
81
|
if (isObject(value)) {
|
|
77
82
|
if (isIterable(value)) {
|
|
83
|
+
let index = 0;
|
|
78
84
|
for (let item of value) {
|
|
79
85
|
if (validate(item)) {
|
|
80
86
|
if (isObject(item)) {
|
|
81
87
|
traverse(item);
|
|
82
88
|
}
|
|
83
89
|
else {
|
|
84
|
-
callback(
|
|
90
|
+
if (!callback(index, item)) {
|
|
91
|
+
stop = true;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
85
94
|
}
|
|
86
95
|
}
|
|
96
|
+
index++;
|
|
87
97
|
}
|
|
88
98
|
}
|
|
89
99
|
}
|
|
90
100
|
else {
|
|
91
|
-
callback(property, value)
|
|
101
|
+
if (!callback(property, value)) {
|
|
102
|
+
stop = true;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
92
105
|
}
|
|
93
106
|
}
|
|
94
107
|
}
|
|
@@ -97,3 +110,71 @@ export let useTraverse = (t, callback) => {
|
|
|
97
110
|
traverse(t);
|
|
98
111
|
}
|
|
99
112
|
};
|
|
113
|
+
export let useGenerator = (iterable) => {
|
|
114
|
+
if (isIterable(iterable)) {
|
|
115
|
+
return (accept, interrupt) => {
|
|
116
|
+
let index = 0n;
|
|
117
|
+
for (let element of iterable) {
|
|
118
|
+
if (interrupt(element, index)) {
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
accept(element, index);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return () => { };
|
|
126
|
+
};
|
|
127
|
+
;
|
|
128
|
+
export let useArrange = (source, comparator) => {
|
|
129
|
+
if (isIterable(source)) {
|
|
130
|
+
let buffer = [...source];
|
|
131
|
+
if (validate(comparator) && isFunction(comparator)) {
|
|
132
|
+
return useGenerator(buffer.sort(comparator));
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
return useGenerator(buffer.map((element, index) => {
|
|
136
|
+
return {
|
|
137
|
+
element: element,
|
|
138
|
+
index: BigInt(((index % buffer.length) + buffer.length) % buffer.length)
|
|
139
|
+
};
|
|
140
|
+
}).sort((a, b) => {
|
|
141
|
+
return Number(a.index - b.index);
|
|
142
|
+
}).map((indexed) => {
|
|
143
|
+
return indexed.element;
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (isFunction(source)) {
|
|
148
|
+
let collector = useToArray();
|
|
149
|
+
let buffer = collector.collect(source);
|
|
150
|
+
if (validate(comparator) && isFunction(comparator)) {
|
|
151
|
+
return useGenerator(buffer.sort(comparator));
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
return useGenerator(buffer.map((element, index) => {
|
|
155
|
+
return {
|
|
156
|
+
element: element,
|
|
157
|
+
index: BigInt(((index % buffer.length) + buffer.length) % buffer.length)
|
|
158
|
+
};
|
|
159
|
+
}).sort((a, b) => {
|
|
160
|
+
return Number(a.index - b.index);
|
|
161
|
+
}).map((indexed) => {
|
|
162
|
+
return indexed.element;
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return useGenerator([]);
|
|
167
|
+
};
|
|
168
|
+
export let useToNumber = (target) => {
|
|
169
|
+
if (isNumber(target)) {
|
|
170
|
+
return target;
|
|
171
|
+
}
|
|
172
|
+
let resolved = Reflect.apply(Reflect.get(target, Symbol.toPrimitive), target, ["default"]);
|
|
173
|
+
return isNumber(resolved) ? resolved : 0;
|
|
174
|
+
};
|
|
175
|
+
export let useToBigInt = (target) => {
|
|
176
|
+
if (isBigInt(target)) {
|
|
177
|
+
return target;
|
|
178
|
+
}
|
|
179
|
+
return BigInt(useToNumber(target));
|
|
180
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED