semantic-typescript 0.3.0 → 0.3.2
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/factory.d.ts +6 -0
- package/dist/factory.js +23 -2
- package/dist/hook.d.ts +2 -2
- package/dist/hook.js +14 -2
- package/package.json +1 -1
- package/readme.md +73 -26
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
75
|
let value = 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/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.3.
|
|
9
|
+
"version": "0.3.2",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"readme": "readme.md",
|
|
12
12
|
"main": "dist/index.js",
|
package/readme.md
CHANGED
|
@@ -111,13 +111,33 @@ if(isIterable(value)){
|
|
|
111
111
|
|------|------|------------|------------|
|
|
112
112
|
| `useCompare<T>(t1: T, t2: T): number` | Generic comparison function | O(1) | O(1) |
|
|
113
113
|
| `useRandom<T = number \| bigint>(index: T): T` | Pseudo-random number generator | O(log n) | O(1) |
|
|
114
|
+
| `useTraverse(t, callback)` | Deep traverse an object without cyclic references | O(n) | O(1) |
|
|
114
115
|
|
|
115
116
|
```typescript
|
|
116
117
|
// Utility function usage examples
|
|
117
118
|
let numbers: Array<number> = [3, 1, 4, 1, 5];
|
|
118
119
|
numbers.sort(useCompare); // [1, 1, 3, 4, 5]
|
|
119
120
|
|
|
120
|
-
let randomNum = useRandom(42); // Seed-based random number
|
|
121
|
+
let randomNum: number = useRandom(42); // Seed-based random number
|
|
122
|
+
|
|
123
|
+
let o = {
|
|
124
|
+
a: 1,
|
|
125
|
+
b: {
|
|
126
|
+
c: 2,
|
|
127
|
+
d: o
|
|
128
|
+
},
|
|
129
|
+
c: [1, 3, 5, o]
|
|
130
|
+
};
|
|
131
|
+
useTraverse(o, (value, key): boolean => {
|
|
132
|
+
console.log(key, value);
|
|
133
|
+
/*
|
|
134
|
+
a 1
|
|
135
|
+
c 2
|
|
136
|
+
1 3 5
|
|
137
|
+
Cyclic references are not traversed.
|
|
138
|
+
*/
|
|
139
|
+
return true; // Returns true to continue traversing.
|
|
140
|
+
});
|
|
121
141
|
```
|
|
122
142
|
|
|
123
143
|
## Factory Methods
|
|
@@ -209,9 +229,9 @@ find.collect(from([1,2,3,4,5])); // Finds the first element
|
|
|
209
229
|
find.collect([1,2,3,4,5]); // Finds the first element
|
|
210
230
|
|
|
211
231
|
// Calculates the sum of elements
|
|
212
|
-
let
|
|
213
|
-
|
|
214
|
-
|
|
232
|
+
let summate: Collector<number, number, number> = useSummate();
|
|
233
|
+
summate.collect(from([1,2,3,4,5])); // Sums from a stream
|
|
234
|
+
summate.collect([1,2,3,4,5]); // Sums from an iterable object
|
|
215
235
|
|
|
216
236
|
// Calculates the average of elements
|
|
217
237
|
let average: Collector<number, number, number> = useNumericAverage();
|
|
@@ -219,11 +239,23 @@ average.collect(from([1,2,3,4,5])); // Averages from a stream
|
|
|
219
239
|
average.collect([1,2,3,4,5]); // Averages from an iterable object
|
|
220
240
|
```
|
|
221
241
|
|
|
242
|
+
## Collector Class Methods
|
|
243
|
+
|
|
244
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
245
|
+
|------------|------------|------------|------------|
|
|
246
|
+
| `collect(stream)` | Collect elements from a stream | O(n) | O(1) |
|
|
247
|
+
| `collect(iterable)` | Collect elements from an iterable object | O(n) | O(1) |
|
|
248
|
+
| `collect(generator)` | Collect elements from a generator | O(n) | O(1) |
|
|
249
|
+
| `collect(semantic)` | Collect elements from a semantic stream | O(n) | O(1) |
|
|
250
|
+
| `collect(collectable)` | Collect elements from a collectable stream | O(n) | O(1) |
|
|
251
|
+
| `collect(start, endExelusive)` | Collect elements from a range | O(n) | O(1) |
|
|
252
|
+
|
|
222
253
|
### Semantic Factory Methods
|
|
223
254
|
|
|
224
255
|
| Method | Description | Time Complexity | Space Complexity |
|
|
225
256
|
|------|------|------------|------------|
|
|
226
257
|
| `animationFrame(period: number, delay: number = 0)` | Create a timed animation frame stream | O(1)* | O(1) |
|
|
258
|
+
| `attribute(target)` | Create a stream from an object deep attributes | O(n) | O(1) |
|
|
227
259
|
| `blob(blob, chunkSize)` | Create a stream from a Blob | O(n) | O(chunkSize) |
|
|
228
260
|
| `empty<E>()` | Create an empty stream | O(1) | O(1) |
|
|
229
261
|
| `fill<E>(element, count)` | Create a filled stream | O(n) | O(1) |
|
|
@@ -281,7 +313,8 @@ websocket(ws)
|
|
|
281
313
|
|
|
282
314
|
| Method | Description | Time Complexity | Space Complexity |
|
|
283
315
|
|------|------|------------|------------|
|
|
284
|
-
| `concat(
|
|
316
|
+
| `concat(semantic)` | Concatenate two streams | O(n) | O(1) |
|
|
317
|
+
| `concat(iterable)` | Concatenate from an iterable object | O(n) | O(1) |
|
|
285
318
|
| `distinct()` | Remove duplicates | O(n) | O(n) |
|
|
286
319
|
| `distinct(comparator)` | Remove duplicates using a comparator | O(n²) | O(n) |
|
|
287
320
|
| `dropWhile(predicate)` | Discard elements satisfying condition | O(n) | O(1) |
|
|
@@ -389,27 +422,41 @@ let customizedCollector = from([1, 2, 3, 4, 5]).toCollectable((generator: Genera
|
|
|
389
422
|
|
|
390
423
|
| Method | Description | Time Complexity | Space Complexity |
|
|
391
424
|
|------|------|------------|------------|
|
|
392
|
-
| `anyMatch(predicate)` | Whether any element matches | O(n) | O(1) |
|
|
393
|
-
| `allMatch(predicate)` | Whether all elements match | O(n) | O(1) |
|
|
394
|
-
| `
|
|
395
|
-
| `
|
|
396
|
-
| `
|
|
397
|
-
| `
|
|
398
|
-
| `
|
|
399
|
-
| `
|
|
400
|
-
| `
|
|
401
|
-
| `
|
|
402
|
-
| `
|
|
403
|
-
| `
|
|
404
|
-
| `
|
|
405
|
-
| `
|
|
406
|
-
| `
|
|
407
|
-
| `
|
|
408
|
-
| `
|
|
409
|
-
| `
|
|
410
|
-
| `
|
|
411
|
-
| `
|
|
412
|
-
| `
|
|
425
|
+
| `anyMatch(predicate)` | Whether any element matches the predicate | O(n) | O(1) |
|
|
426
|
+
| `allMatch(predicate)` | Whether all elements match the predicate | O(n) | O(1) |
|
|
427
|
+
| `collect(collector)` | Collect elements using the given collector | O(n) | O(1) |
|
|
428
|
+
| `collect(identity, accumulator, finisher)` | Collect elements with identity, accumulator, and finisher | O(n) | O(1) |
|
|
429
|
+
| `collect(identity, interruptor, accumulator, finisher)` | Collect elements with identity, interruptor, accumulator, and finisher | O(n) | O(1) |
|
|
430
|
+
| `count()` | Count the number of elements | O(n) | O(1) |
|
|
431
|
+
| `error()` | Log elements to console with default error format | O(n) | O(1) |
|
|
432
|
+
| `error(accumulator)` | Log elements via custom accumulator error format | O(n) | O(1) |
|
|
433
|
+
| `error(prefix, accumulator, suffix)` | Log elements with prefix, custom accumulator, and suffix | O(n) | O(1) |
|
|
434
|
+
| `isEmpty()` | Check whether the collectable is empty | O(n) | O(1) |
|
|
435
|
+
| `findAny()` | Find any element in the collectable | O(n) | O(1) |
|
|
436
|
+
| `findFirst()` | Find the first element in the collectable | O(n) | O(1) |
|
|
437
|
+
| `findLast()` | Find the last element in the collectable | O(n) | O(1) |
|
|
438
|
+
| `forEach(action)` | Iterate over all elements with a consumer or bi-consumer | O(n) | O(1) |
|
|
439
|
+
| `group(classifier)` | Group elements by a classifier function | O(n) | O(n) |
|
|
440
|
+
| `groupBy(keyExtractor, valueExtractor)` | Group elements by key and value extractors | O(n) | O(n) |
|
|
441
|
+
| `join()` | Join elements into a string with default format | O(n) | O(n) |
|
|
442
|
+
| `join(delimiter)` | Join elements into a string with a delimiter | O(n) | O(n) |
|
|
443
|
+
| `join(prefix, delimiter, suffix)` | Join elements with prefix, delimiter, and suffix | O(n) | O(n) |
|
|
444
|
+
| `join(prefix, accumulator, suffix)` | Join elements via custom accumulator with prefix and suffix | O(n) | O(n) |
|
|
445
|
+
| `log()` | Log elements to console with default format | O(n) | O(1) |
|
|
446
|
+
| `log(accumulator)` | Log elements via custom accumulator | O(n) | O(1) |
|
|
447
|
+
| `log(prefix, accumulator, suffix)` | Log elements with prefix, custom accumulator, and suffix | O(n) | O(1) |
|
|
448
|
+
| `nonMatch(predicate)` | Whether no elements match the predicate | O(n) | O(1) |
|
|
449
|
+
| `partition(count)` | Partition elements into chunks of specified size | O(n) | O(n) |
|
|
450
|
+
| `partitionBy(classifier)` | Partition elements by a classifier function | O(n) | O(n) |
|
|
451
|
+
| `reduce(accumulator)` | Reduce elements using an accumulator (no initial value) | O(n) | O(1) |
|
|
452
|
+
| `reduce(identity, accumulator)` | Reduce elements with an initial value and accumulator | O(n) | O(1) |
|
|
453
|
+
| `reduce(identity, accumulator, finisher)` | Reduce elements with initial value, accumulator, and finisher | O(n) | O(1) |
|
|
454
|
+
| `semantic()` | Convert the collectable to a semantic object | O(1) | O(1) |
|
|
455
|
+
| `toArray()` | Convert elements to an array | O(n) | O(n) |
|
|
456
|
+
| `toMap(keyExtractor, valueExtractor)` | Convert elements to a Map via key and value extractors | O(n) | O(n) |
|
|
457
|
+
| `toSet()` | Convert elements to a Set | O(n) | O(n) |
|
|
458
|
+
| `write(stream)` | Write elements to a stream (default format) | O(n) | O(1) |
|
|
459
|
+
| `write(stream, accumulator)` | Write elements to a stream via custom accumulator | O(n) | O(1) |
|
|
413
460
|
|
|
414
461
|
```typescript
|
|
415
462
|
// Collectable operation examples
|