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/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
|
|
@@ -148,6 +170,42 @@ console.log(empty.get(100)); // Outputs 100
|
|
|
148
170
|
|------|------|------------|------------|
|
|
149
171
|
| `Collector.full(identity, accumulator, finisher)` | Create a full collector | O(1) | O(1) |
|
|
150
172
|
| `Collector.shortable(identity, interruptor, accumulator, finisher)` | Create an interruptible collector | O(1) | O(1) |
|
|
173
|
+
| `useAnyMatch<E>(predicate)` | Create a shortable collector returning true if any element matches the predicate | O(n) | O(1) |
|
|
174
|
+
| `useAllMatch<E>(predicate)` | Create a shortable collector returning true if all elements match the predicate | O(n) | O(1) |
|
|
175
|
+
| `useCollect<E, A, R>(identity, accumulator, finisher)` | Create a full collector with identity, accumulator, finisher | O(1) | O(1) |
|
|
176
|
+
| `useCollect<E, A, R>(identity, interruptor, accumulator, finisher)` | Create a shortable collector with identity, interruptor, accumulator, finisher | O(1) | O(1) |
|
|
177
|
+
| `useCount<E>()` | Create a full collector counting elements | O(n) | O(1) |
|
|
178
|
+
| `useFindFirst<E>()` | Create a shortable collector returning the first element | O(n) | O(1) |
|
|
179
|
+
| `useFindAny<E>()` | Create a shortable collector returning any element | O(n) | O(1) |
|
|
180
|
+
| `useFindLast<E>()` | Create a full collector returning the last element | O(n) | O(1) |
|
|
181
|
+
| `useForEach<E>(action)` | Create a full collector executing an action for each element | O(n) | O(1) |
|
|
182
|
+
| `useNoneMatch<E>(predicate)` | Create a shortable collector returning true if no element matches the predicate | O(n) | O(1) |
|
|
183
|
+
| `useGroup<E, K>(classifier)` | Create a full collector grouping elements by classifier key | O(n) | O(n) |
|
|
184
|
+
| `useGroupBy<E, K, V>(keyExtractor, valueExtractor)` | Create a full collector grouping elements by key with extracted values | O(n) | O(n) |
|
|
185
|
+
| `useJoin<E>()` | Create a full collector joining elements into a string with default format | O(n) | O(1) |
|
|
186
|
+
| `useJoin<E>(delimiter)` | Create a full collector joining elements with delimiter | O(n) | O(1) |
|
|
187
|
+
| `useJoin<E>(prefix, delimiter, suffix)` | Create a full collector joining elements with prefix, delimiter, suffix | O(n) | O(1) |
|
|
188
|
+
| `useJoin<E>(prefix, accumulator, suffix)` | Create a full collector joining elements via custom accumulator | O(n) | O(1) |
|
|
189
|
+
| `useLog<E>()` | Create a full collector logging elements to console with default format | O(n) | O(1) |
|
|
190
|
+
| `useLog<E>(accumulator)` | Create a full collector logging elements via custom accumulator | O(n) | O(1) |
|
|
191
|
+
| `useLog<E>(prefix, accumulator, suffix)` | Create a full collector logging elements with prefix/suffix via accumulator | O(n) | O(1) |
|
|
192
|
+
| `usePartition<E>(count)` | Create a full collector partitioning elements into chunks of specified size | O(n) | O(n) |
|
|
193
|
+
| `usePartitionBy<E>(classifier)` | Create a full collector partitioning elements by classifier result | O(n) | O(n) |
|
|
194
|
+
| `useReduce<E>(accumulator)` | Create a full collector reducing elements without identity | O(n) | O(1) |
|
|
195
|
+
| `useReduce<E>(identity, accumulator)` | Create a full collector reducing elements with identity | O(n) | O(1) |
|
|
196
|
+
| `useReduce<E, R>(identity, accumulator, finisher)` | Create a full collector reducing elements with identity, accumulator, finisher | O(n) | O(1) |
|
|
197
|
+
| `useToArray<E>()` | Create a full collector gathering elements into an array | O(n) | O(n) |
|
|
198
|
+
| `useToMap<E, K, V>(keyExtractor, valueExtractor)` | Create a full collector gathering elements into a Map | O(n) | O(n) |
|
|
199
|
+
| `useToSet<E>()` | Create a full collector gathering elements into a Set | O(n) | O(n) |
|
|
200
|
+
| `useWrite<E, S>(stream)` | Create a full collector writing elements to a stream | O(n) | O(1) |
|
|
201
|
+
| `useWrite<E, S>(stream, accumulator)` | Create a full collector writing elements via custom accumulator | O(n) | O(1) |
|
|
202
|
+
| `useNumericAverage<E>(mapper)` | Create a full collector computing numeric average with mapper | O(n) | O(1) |
|
|
203
|
+
| `useNumericAverage<E>()` | Create a full collector computing numeric average | O(n) | O(1) |
|
|
204
|
+
| `useBigIntAverage<E>(mapper)` | Create a full collector computing bigint average with mapper | O(n) | O(1) |
|
|
205
|
+
| `useBigIntAverage<E>()` | Create a full collector computing bigint average | O(n) | O(1) |
|
|
206
|
+
| `useFrequency<E>()` | Create a full collector counting element frequencies | O(n) | O(n) |
|
|
207
|
+
| `useSummate<E>(mapper)` | Create a full collector summing mapped numeric values | O(n) | O(1) |
|
|
208
|
+
| `useSummate<E>()` | Create a full collector summing numeric elements | O(n) | O(1) |
|
|
151
209
|
|
|
152
210
|
```typescript
|
|
153
211
|
// Collector conversion examples
|
|
@@ -163,29 +221,43 @@ let ordered: OrderedCollectable<number> = from([3, 1, 4, 1, 5, 9, 2, 6, 5])
|
|
|
163
221
|
.sorted();
|
|
164
222
|
|
|
165
223
|
// Counts the number of elements
|
|
166
|
-
let count: Collector<number, number, number> =
|
|
167
|
-
(): number => 0, // Initial value
|
|
168
|
-
(accumulator: number, element: number): number => accumulator + element, // Accumulate
|
|
169
|
-
(accumulator: number): number => accumulator // Finish
|
|
170
|
-
);
|
|
224
|
+
let count: Collector<number, number, number> = useCount();
|
|
171
225
|
count.collect(from([1,2,3,4,5])); // Counts from a stream
|
|
172
226
|
count.collect([1,2,3,4,5]); // Counts from an iterable object
|
|
173
227
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
(element: number, index: bigint, accumulator: Optional<number>): Optional<number> => accumulator.isPresent(), // Interrupt
|
|
177
|
-
(accumulator: Optional<number>, element: number, index: bigint): Optional<number> => Optional.of(element), // Accumulate
|
|
178
|
-
(accumulator: Optional<number>): Optional<number> => accumulator // Finish
|
|
179
|
-
);
|
|
228
|
+
// Finds the first element
|
|
229
|
+
let findFirst: Collector<number, number, number> = useFindFirst();
|
|
180
230
|
find.collect(from([1,2,3,4,5])); // Finds the first element
|
|
181
231
|
find.collect([1,2,3,4,5]); // Finds the first element
|
|
232
|
+
|
|
233
|
+
// Calculates the sum of elements
|
|
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
|
|
237
|
+
|
|
238
|
+
// Calculates the average of elements
|
|
239
|
+
let average: Collector<number, number, number> = useNumericAverage();
|
|
240
|
+
average.collect(from([1,2,3,4,5])); // Averages from a stream
|
|
241
|
+
average.collect([1,2,3,4,5]); // Averages from an iterable object
|
|
182
242
|
```
|
|
183
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
|
+
|
|
184
255
|
### Semantic Factory Methods
|
|
185
256
|
|
|
186
257
|
| Method | Description | Time Complexity | Space Complexity |
|
|
187
258
|
|------|------|------------|------------|
|
|
188
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) |
|
|
189
261
|
| `blob(blob, chunkSize)` | Create a stream from a Blob | O(n) | O(chunkSize) |
|
|
190
262
|
| `empty<E>()` | Create an empty stream | O(1) | O(1) |
|
|
191
263
|
| `fill<E>(element, count)` | Create a filled stream | O(n) | O(1) |
|
|
@@ -243,7 +315,8 @@ websocket(ws)
|
|
|
243
315
|
|
|
244
316
|
| Method | Description | Time Complexity | Space Complexity |
|
|
245
317
|
|------|------|------------|------------|
|
|
246
|
-
| `concat(
|
|
318
|
+
| `concat(semantic)` | Concatenate two streams | O(n) | O(1) |
|
|
319
|
+
| `concat(iterable)` | Concatenate from an iterable object | O(n) | O(1) |
|
|
247
320
|
| `distinct()` | Remove duplicates | O(n) | O(n) |
|
|
248
321
|
| `distinct(comparator)` | Remove duplicates using a comparator | O(n²) | O(n) |
|
|
249
322
|
| `dropWhile(predicate)` | Discard elements satisfying condition | O(n) | O(1) |
|
|
@@ -351,27 +424,41 @@ let customizedCollector = from([1, 2, 3, 4, 5]).toCollectable((generator: Genera
|
|
|
351
424
|
|
|
352
425
|
| Method | Description | Time Complexity | Space Complexity |
|
|
353
426
|
|------|------|------------|------------|
|
|
354
|
-
| `anyMatch(predicate)` | Whether any element matches | O(n) | O(1) |
|
|
355
|
-
| `allMatch(predicate)` | Whether all elements match | O(n) | O(1) |
|
|
356
|
-
| `
|
|
357
|
-
| `
|
|
358
|
-
| `
|
|
359
|
-
| `
|
|
360
|
-
| `
|
|
361
|
-
| `
|
|
362
|
-
| `
|
|
363
|
-
| `
|
|
364
|
-
| `
|
|
365
|
-
| `
|
|
366
|
-
| `
|
|
367
|
-
| `
|
|
368
|
-
| `
|
|
369
|
-
| `
|
|
370
|
-
| `
|
|
371
|
-
| `
|
|
372
|
-
| `
|
|
373
|
-
| `
|
|
374
|
-
| `
|
|
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) |
|
|
375
462
|
|
|
376
463
|
```typescript
|
|
377
464
|
// Collectable operation examples
|