semantic-typescript 0.2.9 → 0.3.3
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 +12 -7
- package/dist/collectable.js +80 -237
- package/dist/collector.d.ts +99 -1
- package/dist/collector.js +422 -1
- package/dist/factory.d.ts +6 -0
- package/dist/factory.js +23 -2
- package/dist/hook.d.ts +2 -2
- package/dist/hook.js +15 -3
- package/dist/semantic.d.ts +5 -0
- package/dist/semantic.js +13 -1
- package/package.json +3 -5
- package/readme.md +121 -34
package/dist/collector.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { isBigInt, isFunction, isIterable, isNumber, isSemantic } from "./guard";
|
|
1
|
+
import { isBigInt, isBoolean, isCollectable, isFunction, isIterable, isNumber, isObject, isSemantic, isString } from "./guard";
|
|
2
|
+
import { Optional } from "./optional";
|
|
2
3
|
import { CollectableSymbol } from "./symbol";
|
|
4
|
+
import { validate, invalidate } from "./utility";
|
|
3
5
|
export class Collector {
|
|
4
6
|
identity;
|
|
5
7
|
interrupt;
|
|
@@ -52,6 +54,29 @@ export class Collector {
|
|
|
52
54
|
throw new TypeError("Invalid arguments");
|
|
53
55
|
}
|
|
54
56
|
}
|
|
57
|
+
else if (isCollectable(argument1)) {
|
|
58
|
+
let collectable = argument1;
|
|
59
|
+
let source = collectable.source();
|
|
60
|
+
if (isIterable(source)) {
|
|
61
|
+
let iterable = source;
|
|
62
|
+
let index = 0n;
|
|
63
|
+
for (let element of iterable) {
|
|
64
|
+
if (this.interrupt(element, index, accumulator)) {
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
accumulator = this.accumulator(accumulator, element, count);
|
|
68
|
+
count++;
|
|
69
|
+
index++;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else if (isFunction(source)) {
|
|
73
|
+
let generator = source;
|
|
74
|
+
generator((element, index) => {
|
|
75
|
+
accumulator = this.accumulator(accumulator, element, index);
|
|
76
|
+
count++;
|
|
77
|
+
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
55
80
|
else if (isNumber(argument1) && isNumber(argument2)) {
|
|
56
81
|
let start = argument1 < argument2 ? argument1 : argument2;
|
|
57
82
|
let end = argument1 > argument2 ? argument1 : argument2;
|
|
@@ -84,3 +109,399 @@ export class Collector {
|
|
|
84
109
|
}
|
|
85
110
|
}
|
|
86
111
|
;
|
|
112
|
+
export let useAnyMatch = (predicate) => {
|
|
113
|
+
if (isFunction(predicate)) {
|
|
114
|
+
return Collector.shortable(() => false, (_element, _index, accumulator) => isBoolean(accumulator) && accumulator, (accumulator, element) => accumulator || predicate(element), (accumulator) => accumulator);
|
|
115
|
+
}
|
|
116
|
+
throw new TypeError("Predicate must be a function.");
|
|
117
|
+
};
|
|
118
|
+
export let useAllMatch = (predicate) => {
|
|
119
|
+
if (isFunction(predicate)) {
|
|
120
|
+
return Collector.shortable(() => true, (_element, _index, accumulator) => isBoolean(accumulator) && !accumulator, (accumulator, element) => accumulator && predicate(element), (accumulator) => accumulator);
|
|
121
|
+
}
|
|
122
|
+
throw new TypeError("Predicate must be a function.");
|
|
123
|
+
};
|
|
124
|
+
;
|
|
125
|
+
export let useCollect = (argument1, argument2, argument3, argument4) => {
|
|
126
|
+
if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3) && isFunction(argument4)) {
|
|
127
|
+
let identity = argument1;
|
|
128
|
+
let interrupt = argument2;
|
|
129
|
+
let accumulator = argument3;
|
|
130
|
+
let finisher = argument4;
|
|
131
|
+
return Collector.shortable(identity, interrupt, accumulator, finisher);
|
|
132
|
+
}
|
|
133
|
+
if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3)) {
|
|
134
|
+
let identity = argument1;
|
|
135
|
+
let accumulator = argument2;
|
|
136
|
+
let finisher = argument3;
|
|
137
|
+
return Collector.full(identity, accumulator, finisher);
|
|
138
|
+
}
|
|
139
|
+
throw new TypeError("Identity, accumulator, and finisher must be functions.");
|
|
140
|
+
};
|
|
141
|
+
export let useCount = () => {
|
|
142
|
+
return Collector.full(() => 0n, (count) => count + 1n, (count) => count);
|
|
143
|
+
};
|
|
144
|
+
;
|
|
145
|
+
export let useError = (argument1, argument2, argument3) => {
|
|
146
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
147
|
+
return Collector.full(() => "[", (accumulator, element) => {
|
|
148
|
+
if (isString(accumulator) && isString(element)) {
|
|
149
|
+
return accumulator + element + ",";
|
|
150
|
+
}
|
|
151
|
+
return String(accumulator) + String(element) + ",";
|
|
152
|
+
}, (text) => {
|
|
153
|
+
let result = text.substring(0, text.length - 1) + "]";
|
|
154
|
+
console.error(result);
|
|
155
|
+
return result;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
159
|
+
let accumulator = argument1;
|
|
160
|
+
return Collector.full(() => "[", accumulator, (text) => {
|
|
161
|
+
let result = text.substring(0, text.length - 1) + "]";
|
|
162
|
+
console.error(result);
|
|
163
|
+
return result;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
else if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
167
|
+
let prefix = argument1;
|
|
168
|
+
let accumulator = argument2;
|
|
169
|
+
let suffix = argument3;
|
|
170
|
+
return Collector.full(() => prefix, accumulator, (text) => {
|
|
171
|
+
let result = text + suffix;
|
|
172
|
+
console.error(result);
|
|
173
|
+
return result;
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
throw new TypeError("Invalid arguments.");
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
export let useFindFirst = () => {
|
|
181
|
+
return Collector.shortable(() => Optional.empty(), (_element, _index, accumulator) => validate(accumulator) && accumulator.isPresent(), (accumulator, element) => {
|
|
182
|
+
if (validate(accumulator) && accumulator.isPresent()) {
|
|
183
|
+
return accumulator;
|
|
184
|
+
}
|
|
185
|
+
return Optional.ofNullable(element);
|
|
186
|
+
}, (accumulator) => accumulator);
|
|
187
|
+
};
|
|
188
|
+
export let useFindAny = () => {
|
|
189
|
+
return Collector.shortable(() => Optional.empty(), (_element, _index, accumulator) => validate(accumulator) && accumulator.isPresent(), (accumulator, element) => {
|
|
190
|
+
if (validate(accumulator) && accumulator.isPresent() && Math.random() < 0.5) {
|
|
191
|
+
return accumulator;
|
|
192
|
+
}
|
|
193
|
+
return Optional.ofNullable(element);
|
|
194
|
+
}, (accumulator) => accumulator);
|
|
195
|
+
};
|
|
196
|
+
export let useFindLast = () => {
|
|
197
|
+
return Collector.full(() => Optional.empty(), (accumulator, element) => {
|
|
198
|
+
if (validate(accumulator) && accumulator.isPresent()) {
|
|
199
|
+
return Optional.ofNullable(element);
|
|
200
|
+
}
|
|
201
|
+
return accumulator;
|
|
202
|
+
}, (accumulator) => accumulator);
|
|
203
|
+
};
|
|
204
|
+
;
|
|
205
|
+
export let useForEach = (action) => {
|
|
206
|
+
if (isFunction(action)) {
|
|
207
|
+
return Collector.full(() => 0n, (accumulator, element, index) => {
|
|
208
|
+
action(element, index);
|
|
209
|
+
return accumulator + 1n;
|
|
210
|
+
}, (accumulator) => accumulator);
|
|
211
|
+
}
|
|
212
|
+
throw new TypeError("Action must be a function.");
|
|
213
|
+
};
|
|
214
|
+
export let useNoneMatch = (predicate) => {
|
|
215
|
+
if (isFunction(predicate)) {
|
|
216
|
+
return Collector.shortable(() => true, (_element, _index, accumulator) => !accumulator, (accumulator, element) => accumulator && !predicate(element), (accumulator) => !accumulator);
|
|
217
|
+
}
|
|
218
|
+
throw new TypeError("Predicate must be a function.");
|
|
219
|
+
};
|
|
220
|
+
export let useGroup = (classifier) => {
|
|
221
|
+
if (isFunction(classifier)) {
|
|
222
|
+
return Collector.full(() => new Map(), (accumulator, element) => {
|
|
223
|
+
let key = classifier(element);
|
|
224
|
+
let group = accumulator.get(key) || [];
|
|
225
|
+
group.push(element);
|
|
226
|
+
accumulator.set(key, group);
|
|
227
|
+
return accumulator;
|
|
228
|
+
}, (accumulator) => accumulator);
|
|
229
|
+
}
|
|
230
|
+
throw new TypeError("Classifier must be a function.");
|
|
231
|
+
};
|
|
232
|
+
export let useGroupBy = (keyExtractor, valueExtractor) => {
|
|
233
|
+
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
234
|
+
return Collector.full(() => new Map(), (accumulator, element) => {
|
|
235
|
+
let key = keyExtractor(element);
|
|
236
|
+
let group = accumulator.get(key) || [];
|
|
237
|
+
group.push(valueExtractor(element));
|
|
238
|
+
accumulator.set(key, group);
|
|
239
|
+
return accumulator;
|
|
240
|
+
}, (accumulator) => accumulator);
|
|
241
|
+
}
|
|
242
|
+
throw new TypeError("Key extractor and value extractor must be functions.");
|
|
243
|
+
};
|
|
244
|
+
;
|
|
245
|
+
export let useJoin = (argument1, argument2, argument3) => {
|
|
246
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
247
|
+
return Collector.full(() => "[", (text, element) => text + element + ",", (text) => text.substring(0, text.length - 1) + "]");
|
|
248
|
+
}
|
|
249
|
+
if (isString(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
250
|
+
let delimiter = argument1;
|
|
251
|
+
return Collector.full(() => "[", (text, element) => text + element + delimiter, (text) => text.substring(0, text.length - delimiter.length) + "]");
|
|
252
|
+
}
|
|
253
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
254
|
+
let prefix = argument1;
|
|
255
|
+
let accumulator = argument2;
|
|
256
|
+
let suffix = argument3;
|
|
257
|
+
return Collector.full(() => prefix, accumulator, (text) => text + suffix);
|
|
258
|
+
}
|
|
259
|
+
if (isString(argument1) && isString(argument2) && isString(argument3)) {
|
|
260
|
+
let prefix = argument1;
|
|
261
|
+
let delimiter = argument2;
|
|
262
|
+
let suffix = argument3;
|
|
263
|
+
return Collector.full(() => prefix, (text, element) => text + element + delimiter, (text) => text + suffix);
|
|
264
|
+
}
|
|
265
|
+
throw new TypeError("Invalid arguments.");
|
|
266
|
+
};
|
|
267
|
+
;
|
|
268
|
+
export let useLog = (argument1, argument2, argument3) => {
|
|
269
|
+
if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
270
|
+
return Collector.full(() => "[", (accumulator, element) => {
|
|
271
|
+
if (isString(accumulator) && isString(element)) {
|
|
272
|
+
return accumulator + element + ",";
|
|
273
|
+
}
|
|
274
|
+
return String(accumulator) + String(element) + ",";
|
|
275
|
+
}, (text) => {
|
|
276
|
+
let result = text.substring(0, text.length - 1) + "]";
|
|
277
|
+
console.log(result);
|
|
278
|
+
return result;
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
282
|
+
let accumulator = argument1;
|
|
283
|
+
return Collector.full(() => "[", accumulator, (text) => {
|
|
284
|
+
let result = text.substring(0, text.length - 1) + "]";
|
|
285
|
+
console.log(result);
|
|
286
|
+
return result;
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
else if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
290
|
+
let prefix = argument1;
|
|
291
|
+
let accumulator = argument2;
|
|
292
|
+
let suffix = argument3;
|
|
293
|
+
return Collector.full(() => prefix, accumulator, (text) => {
|
|
294
|
+
let result = text + suffix;
|
|
295
|
+
console.log(result);
|
|
296
|
+
return result;
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
throw new TypeError("Invalid arguments.");
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
export let usePartition = (count) => {
|
|
304
|
+
if (isBigInt(count)) {
|
|
305
|
+
let limited = count > 1n ? count : 1n;
|
|
306
|
+
return Collector.full(() => {
|
|
307
|
+
return [];
|
|
308
|
+
}, (array, element) => {
|
|
309
|
+
let index = limited % BigInt(array.length);
|
|
310
|
+
if (index === 0n) {
|
|
311
|
+
array.push([]);
|
|
312
|
+
}
|
|
313
|
+
array[Number(index)].push(element);
|
|
314
|
+
return array;
|
|
315
|
+
}, (result) => {
|
|
316
|
+
return result;
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
throw new TypeError("Count must be a BigInt.");
|
|
320
|
+
};
|
|
321
|
+
export let usePartitionBy = (classifier) => {
|
|
322
|
+
if (isFunction(classifier)) {
|
|
323
|
+
return Collector.full(() => {
|
|
324
|
+
return [];
|
|
325
|
+
}, (array, element) => {
|
|
326
|
+
let index = classifier(element);
|
|
327
|
+
while (index > BigInt(array.length) - 1n) {
|
|
328
|
+
array.push([]);
|
|
329
|
+
}
|
|
330
|
+
array[Number(index)].push(element);
|
|
331
|
+
return array;
|
|
332
|
+
}, (result) => {
|
|
333
|
+
return result;
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
throw new TypeError("Classifier must be a function.");
|
|
337
|
+
};
|
|
338
|
+
;
|
|
339
|
+
export let useReduce = (argument1, argument2, argument3) => {
|
|
340
|
+
if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
341
|
+
let accumulator = argument1;
|
|
342
|
+
return Collector.full(() => Optional.ofNullable(), (result, element, index) => {
|
|
343
|
+
if (result.isEmpty()) {
|
|
344
|
+
return Optional.of(element);
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
let current = result.get();
|
|
348
|
+
return Optional.of(accumulator(current, element, index));
|
|
349
|
+
}
|
|
350
|
+
}, (result) => result);
|
|
351
|
+
}
|
|
352
|
+
else if (validate(argument1) && isFunction(argument2) && invalidate(argument3)) {
|
|
353
|
+
let identity = argument1;
|
|
354
|
+
let accumulator = argument2;
|
|
355
|
+
return Collector.full(() => identity, accumulator, (result) => result);
|
|
356
|
+
}
|
|
357
|
+
else if (validate(argument1) && isFunction(argument2) && isFunction(argument3)) {
|
|
358
|
+
let identity = argument1;
|
|
359
|
+
let accumulator = argument2;
|
|
360
|
+
let finisher = argument3;
|
|
361
|
+
return Collector.full(() => identity, accumulator, finisher);
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
throw new TypeError("Invalid arguments.");
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
export let useToArray = () => {
|
|
368
|
+
return Collector.full(() => [], (array, element) => {
|
|
369
|
+
array.push(element);
|
|
370
|
+
return array;
|
|
371
|
+
}, (array) => array);
|
|
372
|
+
};
|
|
373
|
+
export let useToMap = (keyExtractor, valueExtractor) => {
|
|
374
|
+
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
375
|
+
return Collector.full(() => new Map(), (map, element) => {
|
|
376
|
+
let key = keyExtractor(element);
|
|
377
|
+
let value = valueExtractor(element);
|
|
378
|
+
map.set(key, value);
|
|
379
|
+
return map;
|
|
380
|
+
}, (map) => map);
|
|
381
|
+
}
|
|
382
|
+
throw new TypeError("Key extractor and value extractor must be functions.");
|
|
383
|
+
};
|
|
384
|
+
export let useToSet = () => {
|
|
385
|
+
return Collector.full(() => new Set(), (set, element) => {
|
|
386
|
+
set.add(element);
|
|
387
|
+
return set;
|
|
388
|
+
}, (set) => set);
|
|
389
|
+
};
|
|
390
|
+
;
|
|
391
|
+
export let useWrite = (argument1, argument2) => {
|
|
392
|
+
if (isObject(argument1)) {
|
|
393
|
+
if (isFunction(argument2)) {
|
|
394
|
+
let stream = argument1;
|
|
395
|
+
let accumulator = argument2;
|
|
396
|
+
return Collector.full(() => Promise.resolve(stream), (promise, element, index) => {
|
|
397
|
+
return new Promise((resolve, reject) => {
|
|
398
|
+
promise.then((stream) => {
|
|
399
|
+
try {
|
|
400
|
+
resolve(accumulator(stream, element, index));
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
reject(error);
|
|
404
|
+
}
|
|
405
|
+
}).catch(reject);
|
|
406
|
+
});
|
|
407
|
+
}, (promise) => promise);
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
let stream = argument1;
|
|
411
|
+
return Collector.full(() => Promise.resolve(stream), (promise, element) => {
|
|
412
|
+
return new Promise((resolve, reject) => {
|
|
413
|
+
promise.then((stream) => {
|
|
414
|
+
try {
|
|
415
|
+
let writer = stream.getWriter();
|
|
416
|
+
writer.write(String(element));
|
|
417
|
+
resolve(stream);
|
|
418
|
+
}
|
|
419
|
+
catch (error) {
|
|
420
|
+
reject(error);
|
|
421
|
+
}
|
|
422
|
+
}).catch(reject);
|
|
423
|
+
});
|
|
424
|
+
}, (promise) => promise);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
throw new TypeError("Invalid arguments.");
|
|
428
|
+
};
|
|
429
|
+
;
|
|
430
|
+
export let useNumericAverage = (mapper) => {
|
|
431
|
+
if (isFunction(mapper)) {
|
|
432
|
+
return Collector.full(() => {
|
|
433
|
+
return {
|
|
434
|
+
summate: 0,
|
|
435
|
+
count: 0
|
|
436
|
+
};
|
|
437
|
+
}, (information, element) => {
|
|
438
|
+
let value = mapper(element);
|
|
439
|
+
information.summate += value;
|
|
440
|
+
information.count++;
|
|
441
|
+
return information;
|
|
442
|
+
}, (information) => {
|
|
443
|
+
return information.summate / information.count;
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
return Collector.full(() => {
|
|
447
|
+
return {
|
|
448
|
+
summate: 0,
|
|
449
|
+
count: 0
|
|
450
|
+
};
|
|
451
|
+
}, (information, element) => {
|
|
452
|
+
information.summate += element;
|
|
453
|
+
information.count++;
|
|
454
|
+
return information;
|
|
455
|
+
}, (information) => {
|
|
456
|
+
return information.summate / information.count;
|
|
457
|
+
});
|
|
458
|
+
};
|
|
459
|
+
;
|
|
460
|
+
export let useBigIntAverage = (mapper) => {
|
|
461
|
+
if (isFunction(mapper)) {
|
|
462
|
+
return Collector.full(() => {
|
|
463
|
+
return {
|
|
464
|
+
summate: 0n,
|
|
465
|
+
count: 0n
|
|
466
|
+
};
|
|
467
|
+
}, (information, element) => {
|
|
468
|
+
let value = mapper(element);
|
|
469
|
+
information.summate += value;
|
|
470
|
+
information.count++;
|
|
471
|
+
return information;
|
|
472
|
+
}, (information) => {
|
|
473
|
+
return information.summate / information.count;
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
return Collector.full(() => {
|
|
477
|
+
return {
|
|
478
|
+
summate: 0n,
|
|
479
|
+
count: 0n
|
|
480
|
+
};
|
|
481
|
+
}, (information, element) => {
|
|
482
|
+
information.summate += element;
|
|
483
|
+
information.count++;
|
|
484
|
+
return information;
|
|
485
|
+
}, (information) => {
|
|
486
|
+
return information.summate / information.count;
|
|
487
|
+
});
|
|
488
|
+
};
|
|
489
|
+
export let useFrequency = () => {
|
|
490
|
+
return Collector.full(() => new Map(), (map, element) => {
|
|
491
|
+
let count = map.get(element) || 0n;
|
|
492
|
+
map.set(element, count + 1n);
|
|
493
|
+
return map;
|
|
494
|
+
}, (map) => map);
|
|
495
|
+
};
|
|
496
|
+
;
|
|
497
|
+
export let useSummate = (mapper) => {
|
|
498
|
+
if (isFunction(mapper)) {
|
|
499
|
+
return Collector.full(() => 0, (summate, element) => {
|
|
500
|
+
let value = mapper(element);
|
|
501
|
+
return summate + value;
|
|
502
|
+
}, (summate) => summate);
|
|
503
|
+
}
|
|
504
|
+
return Collector.full(() => 0, (summate, element) => {
|
|
505
|
+
return summate + element;
|
|
506
|
+
}, (summate) => summate);
|
|
507
|
+
};
|
package/dist/factory.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Semantic } from "./semantic";
|
|
2
2
|
import type { BiFunctional, BiPredicate, Functional, Predicate, Supplier, TriFunctional, Generator } from "./utility";
|
|
3
3
|
export declare let animationFrame: Functional<number, Semantic<number>> & BiFunctional<number, number, Semantic<number>>;
|
|
4
|
+
interface Attribute<T> {
|
|
5
|
+
key: keyof T;
|
|
6
|
+
value: T[keyof T];
|
|
7
|
+
}
|
|
8
|
+
export declare let attribute: <T extends object>(target: T) => Semantic<Attribute<T>>;
|
|
4
9
|
export declare let blob: Functional<Blob, Semantic<Uint8Array>> & BiFunctional<Blob, bigint, Semantic<Uint8Array>>;
|
|
5
10
|
export declare let empty: <E>() => Semantic<E>;
|
|
6
11
|
export declare let fill: (<E>(element: E, count: bigint) => Semantic<E>) & (<E>(supplier: Supplier<E>, count: bigint) => Semantic<E>);
|
|
@@ -11,3 +16,4 @@ export declare let iterate: <E>(generator: Generator<E>) => Semantic<E>;
|
|
|
11
16
|
export declare let promise: (<T>(promise: Promise<T>) => Semantic<T>);
|
|
12
17
|
export declare let range: BiFunctional<number, number, Semantic<number>> & TriFunctional<number, number, number, Semantic<number>>;
|
|
13
18
|
export declare let websocket: Functional<WebSocket, Semantic<MessageEvent | CloseEvent | Event>>;
|
|
19
|
+
export {};
|
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) => {
|
|
@@ -25,6 +25,27 @@ export let animationFrame = (period, delay = 0) => {
|
|
|
25
25
|
};
|
|
26
26
|
});
|
|
27
27
|
};
|
|
28
|
+
;
|
|
29
|
+
export let attribute = (target) => {
|
|
30
|
+
if (isObject(target)) {
|
|
31
|
+
return new Semantic((accept, interrupt) => {
|
|
32
|
+
let index = 0n;
|
|
33
|
+
useTraverse(target, (key, value) => {
|
|
34
|
+
let attribute = {
|
|
35
|
+
key: key,
|
|
36
|
+
value: value
|
|
37
|
+
};
|
|
38
|
+
if (interrupt(attribute, index)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
accept(attribute, index);
|
|
42
|
+
index++;
|
|
43
|
+
return true;
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
throw new TypeError("Target must be an object.");
|
|
48
|
+
};
|
|
28
49
|
export let blob = (blob, chunk = 64n * 1024n) => {
|
|
29
50
|
let size = Number(chunk);
|
|
30
51
|
if (size <= 0 || !Number.isSafeInteger(size)) {
|
package/dist/hook.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type BiPredicate, type DeepPropertyKey, type DeepPropertyValue } from "./utility";
|
|
2
2
|
export declare let useCompare: <T>(t1: T, t2: T) => number;
|
|
3
3
|
export declare let useRandom: <T = number | bigint>(index: T) => T;
|
|
4
|
-
export declare let useTraverse: <T extends object>(t: T, callback:
|
|
4
|
+
export declare let useTraverse: <T extends object>(t: T, callback: BiPredicate<DeepPropertyKey<T>, DeepPropertyValue<T>>) => void;
|
package/dist/hook.js
CHANGED
|
@@ -69,26 +69,38 @@ export let useTraverse = (t, callback) => {
|
|
|
69
69
|
let traverse = (target) => {
|
|
70
70
|
if (!seen.has(target)) {
|
|
71
71
|
seen.add(target);
|
|
72
|
+
let stop = false;
|
|
72
73
|
let properties = Reflect.ownKeys(target);
|
|
73
74
|
for (let property of properties) {
|
|
74
|
-
let value = target
|
|
75
|
+
let value = Reflect.get(target, property);
|
|
76
|
+
if (stop) {
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
75
79
|
if (validate(value)) {
|
|
76
80
|
if (isObject(value)) {
|
|
77
81
|
if (isIterable(value)) {
|
|
82
|
+
let index = 0;
|
|
78
83
|
for (let item of value) {
|
|
79
84
|
if (validate(item)) {
|
|
80
85
|
if (isObject(item)) {
|
|
81
86
|
traverse(item);
|
|
82
87
|
}
|
|
83
88
|
else {
|
|
84
|
-
callback(
|
|
89
|
+
if (!callback(index, item)) {
|
|
90
|
+
stop = true;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
85
93
|
}
|
|
86
94
|
}
|
|
95
|
+
index++;
|
|
87
96
|
}
|
|
88
97
|
}
|
|
89
98
|
}
|
|
90
99
|
else {
|
|
91
|
-
callback(property, value)
|
|
100
|
+
if (!callback(property, value)) {
|
|
101
|
+
stop = true;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
92
104
|
}
|
|
93
105
|
}
|
|
94
106
|
}
|
package/dist/semantic.d.ts
CHANGED
|
@@ -40,3 +40,8 @@ export declare class Semantic<E> {
|
|
|
40
40
|
translate(offset: bigint): Semantic<E>;
|
|
41
41
|
translate(translator: BiFunctional<E, bigint, bigint>): Semantic<E>;
|
|
42
42
|
}
|
|
43
|
+
export interface UseTransform {
|
|
44
|
+
<E, R>(generator: Generator<E>, mapper: Functional<E, R>): Generator<R>;
|
|
45
|
+
<E, R>(generator: Generator<E>, mapper: BiFunctional<E, bigint, R>): Generator<R>;
|
|
46
|
+
}
|
|
47
|
+
export declare let useTransform: UseTransform;
|
package/dist/semantic.js
CHANGED
|
@@ -18,7 +18,8 @@ export class Semantic {
|
|
|
18
18
|
accept(element, index);
|
|
19
19
|
count++;
|
|
20
20
|
}, interrupt);
|
|
21
|
-
other.
|
|
21
|
+
let otherGenerator = Reflect.has(other, "generator") ? Reflect.get(other, "generator") : () => { };
|
|
22
|
+
otherGenerator((element, index) => {
|
|
22
23
|
accept(element, index + count);
|
|
23
24
|
}, interrupt);
|
|
24
25
|
});
|
|
@@ -337,3 +338,14 @@ export class Semantic {
|
|
|
337
338
|
}
|
|
338
339
|
}
|
|
339
340
|
;
|
|
341
|
+
;
|
|
342
|
+
export let useTransform = (generator, mapper) => {
|
|
343
|
+
return (accept, interrupt) => {
|
|
344
|
+
generator((element, index) => {
|
|
345
|
+
let resolved = mapper(element, index);
|
|
346
|
+
accept(resolved, index);
|
|
347
|
+
}, (element, index) => {
|
|
348
|
+
return interrupt(mapper(element, index), index);
|
|
349
|
+
});
|
|
350
|
+
};
|
|
351
|
+
};
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"url": "https://github.com/eloyhere"
|
|
7
7
|
},
|
|
8
8
|
"description": "A modern type-safe stream processing library inspired by JavaScript Generator, Java Stream, and MySQL Index. Supports lazy evaluation, async streams, statistics, and IO-like operations.",
|
|
9
|
-
"version": "0.
|
|
9
|
+
"version": "0.3.3",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"readme": "readme.md",
|
|
12
12
|
"main": "dist/index.js",
|
|
@@ -52,12 +52,10 @@
|
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"build": "tsc",
|
|
55
|
+
"dev": "vite",
|
|
55
56
|
"prepublishOnly": "npm run build"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
59
|
"typescript": "~5.9.3"
|
|
59
|
-
},
|
|
60
|
-
"peerDependencies": {},
|
|
61
|
-
"dependencies": {
|
|
62
60
|
}
|
|
63
|
-
}
|
|
61
|
+
}
|