semantic-typescript 0.3.0 → 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/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/package.json +1 -1
- package/readme.md +75 -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
|
-
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/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.3",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"readme": "readme.md",
|
|
12
12
|
"main": "dist/index.js",
|
package/readme.md
CHANGED
|
@@ -111,13 +111,35 @@ 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
|
+
e: undefined,
|
|
131
|
+
f: null
|
|
132
|
+
};
|
|
133
|
+
useTraverse(o, (value, key): boolean => {
|
|
134
|
+
console.log(key, value);
|
|
135
|
+
/*
|
|
136
|
+
a 1
|
|
137
|
+
c 2
|
|
138
|
+
1 3 5
|
|
139
|
+
Cyclic references, undefined and null values are not traversed.
|
|
140
|
+
*/
|
|
141
|
+
return true; // Returns true to continue traversing.
|
|
142
|
+
});
|
|
121
143
|
```
|
|
122
144
|
|
|
123
145
|
## Factory Methods
|
|
@@ -209,9 +231,9 @@ find.collect(from([1,2,3,4,5])); // Finds the first element
|
|
|
209
231
|
find.collect([1,2,3,4,5]); // Finds the first element
|
|
210
232
|
|
|
211
233
|
// Calculates the sum of elements
|
|
212
|
-
let
|
|
213
|
-
|
|
214
|
-
|
|
234
|
+
let summate: Collector<number, number, number> = useSummate();
|
|
235
|
+
summate.collect(from([1,2,3,4,5])); // Sums from a stream
|
|
236
|
+
summate.collect([1,2,3,4,5]); // Sums from an iterable object
|
|
215
237
|
|
|
216
238
|
// Calculates the average of elements
|
|
217
239
|
let average: Collector<number, number, number> = useNumericAverage();
|
|
@@ -219,11 +241,23 @@ average.collect(from([1,2,3,4,5])); // Averages from a stream
|
|
|
219
241
|
average.collect([1,2,3,4,5]); // Averages from an iterable object
|
|
220
242
|
```
|
|
221
243
|
|
|
244
|
+
## Collector Class Methods
|
|
245
|
+
|
|
246
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
247
|
+
|------------|------------|------------|------------|
|
|
248
|
+
| `collect(stream)` | Collect elements from a stream | O(n) | O(1) |
|
|
249
|
+
| `collect(iterable)` | Collect elements from an iterable object | O(n) | O(1) |
|
|
250
|
+
| `collect(generator)` | Collect elements from a generator | O(n) | O(1) |
|
|
251
|
+
| `collect(semantic)` | Collect elements from a semantic stream | O(n) | O(1) |
|
|
252
|
+
| `collect(collectable)` | Collect elements from a collectable stream | O(n) | O(1) |
|
|
253
|
+
| `collect(start, endExelusive)` | Collect elements from a range | O(n) | O(1) |
|
|
254
|
+
|
|
222
255
|
### Semantic Factory Methods
|
|
223
256
|
|
|
224
257
|
| Method | Description | Time Complexity | Space Complexity |
|
|
225
258
|
|------|------|------------|------------|
|
|
226
259
|
| `animationFrame(period: number, delay: number = 0)` | Create a timed animation frame stream | O(1)* | O(1) |
|
|
260
|
+
| `attribute(target)` | Create a stream from an object deep attributes | O(n) | O(1) |
|
|
227
261
|
| `blob(blob, chunkSize)` | Create a stream from a Blob | O(n) | O(chunkSize) |
|
|
228
262
|
| `empty<E>()` | Create an empty stream | O(1) | O(1) |
|
|
229
263
|
| `fill<E>(element, count)` | Create a filled stream | O(n) | O(1) |
|
|
@@ -281,7 +315,8 @@ websocket(ws)
|
|
|
281
315
|
|
|
282
316
|
| Method | Description | Time Complexity | Space Complexity |
|
|
283
317
|
|------|------|------------|------------|
|
|
284
|
-
| `concat(
|
|
318
|
+
| `concat(semantic)` | Concatenate two streams | O(n) | O(1) |
|
|
319
|
+
| `concat(iterable)` | Concatenate from an iterable object | O(n) | O(1) |
|
|
285
320
|
| `distinct()` | Remove duplicates | O(n) | O(n) |
|
|
286
321
|
| `distinct(comparator)` | Remove duplicates using a comparator | O(n²) | O(n) |
|
|
287
322
|
| `dropWhile(predicate)` | Discard elements satisfying condition | O(n) | O(1) |
|
|
@@ -389,27 +424,41 @@ let customizedCollector = from([1, 2, 3, 4, 5]).toCollectable((generator: Genera
|
|
|
389
424
|
|
|
390
425
|
| Method | Description | Time Complexity | Space Complexity |
|
|
391
426
|
|------|------|------------|------------|
|
|
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
|
-
| `
|
|
427
|
+
| `anyMatch(predicate)` | Whether any element matches the predicate | O(n) | O(1) |
|
|
428
|
+
| `allMatch(predicate)` | Whether all elements match the predicate | O(n) | O(1) |
|
|
429
|
+
| `collect(collector)` | Collect elements using the given collector | O(n) | O(1) |
|
|
430
|
+
| `collect(identity, accumulator, finisher)` | Collect elements with identity, accumulator, and finisher | O(n) | O(1) |
|
|
431
|
+
| `collect(identity, interruptor, accumulator, finisher)` | Collect elements with identity, interruptor, accumulator, and finisher | O(n) | O(1) |
|
|
432
|
+
| `count()` | Count the number of elements | O(n) | O(1) |
|
|
433
|
+
| `error()` | Log elements to console with default error format | O(n) | O(1) |
|
|
434
|
+
| `error(accumulator)` | Log elements via custom accumulator error format | O(n) | O(1) |
|
|
435
|
+
| `error(prefix, accumulator, suffix)` | Log elements with prefix, custom accumulator, and suffix | O(n) | O(1) |
|
|
436
|
+
| `isEmpty()` | Check whether the collectable is empty | O(n) | O(1) |
|
|
437
|
+
| `findAny()` | Find any element in the collectable | O(n) | O(1) |
|
|
438
|
+
| `findFirst()` | Find the first element in the collectable | O(n) | O(1) |
|
|
439
|
+
| `findLast()` | Find the last element in the collectable | O(n) | O(1) |
|
|
440
|
+
| `forEach(action)` | Iterate over all elements with a consumer or bi-consumer | O(n) | O(1) |
|
|
441
|
+
| `group(classifier)` | Group elements by a classifier function | O(n) | O(n) |
|
|
442
|
+
| `groupBy(keyExtractor, valueExtractor)` | Group elements by key and value extractors | O(n) | O(n) |
|
|
443
|
+
| `join()` | Join elements into a string with default format | O(n) | O(n) |
|
|
444
|
+
| `join(delimiter)` | Join elements into a string with a delimiter | O(n) | O(n) |
|
|
445
|
+
| `join(prefix, delimiter, suffix)` | Join elements with prefix, delimiter, and suffix | O(n) | O(n) |
|
|
446
|
+
| `join(prefix, accumulator, suffix)` | Join elements via custom accumulator with prefix and suffix | O(n) | O(n) |
|
|
447
|
+
| `log()` | Log elements to console with default format | O(n) | O(1) |
|
|
448
|
+
| `log(accumulator)` | Log elements via custom accumulator | O(n) | O(1) |
|
|
449
|
+
| `log(prefix, accumulator, suffix)` | Log elements with prefix, custom accumulator, and suffix | O(n) | O(1) |
|
|
450
|
+
| `nonMatch(predicate)` | Whether no elements match the predicate | O(n) | O(1) |
|
|
451
|
+
| `partition(count)` | Partition elements into chunks of specified size | O(n) | O(n) |
|
|
452
|
+
| `partitionBy(classifier)` | Partition elements by a classifier function | O(n) | O(n) |
|
|
453
|
+
| `reduce(accumulator)` | Reduce elements using an accumulator (no initial value) | O(n) | O(1) |
|
|
454
|
+
| `reduce(identity, accumulator)` | Reduce elements with an initial value and accumulator | O(n) | O(1) |
|
|
455
|
+
| `reduce(identity, accumulator, finisher)` | Reduce elements with initial value, accumulator, and finisher | O(n) | O(1) |
|
|
456
|
+
| `semantic()` | Convert the collectable to a semantic object | O(1) | O(1) |
|
|
457
|
+
| `toArray()` | Convert elements to an array | O(n) | O(n) |
|
|
458
|
+
| `toMap(keyExtractor, valueExtractor)` | Convert elements to a Map via key and value extractors | O(n) | O(n) |
|
|
459
|
+
| `toSet()` | Convert elements to a Set | O(n) | O(n) |
|
|
460
|
+
| `write(stream)` | Write elements to a stream (default format) | O(n) | O(1) |
|
|
461
|
+
| `write(stream, accumulator)` | Write elements to a stream via custom accumulator | O(n) | O(1) |
|
|
413
462
|
|
|
414
463
|
```typescript
|
|
415
464
|
// Collectable operation examples
|