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/utility.d.ts
CHANGED
package/dist/utility.js
CHANGED
|
@@ -2,7 +2,7 @@ export let validate = (t) => {
|
|
|
2
2
|
return t !== null && t !== (void 0);
|
|
3
3
|
};
|
|
4
4
|
export let invalidate = (t) => {
|
|
5
|
-
return t === null || t ===
|
|
5
|
+
return t === null || t === (void 0);
|
|
6
6
|
};
|
|
7
7
|
;
|
|
8
8
|
;
|
|
@@ -15,3 +15,4 @@ export let invalidate = (t) => {
|
|
|
15
15
|
;
|
|
16
16
|
;
|
|
17
17
|
;
|
|
18
|
+
;
|
package/dist/window.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OrderedCollectable } from "./collectable";
|
|
2
|
+
import type { Semantic } from "./semantic";
|
|
3
|
+
import type { Comparator, Generator } from "./utility";
|
|
4
|
+
export declare class WindowCollectable<E> extends OrderedCollectable<E> {
|
|
5
|
+
protected readonly WindowCollectable: symbol;
|
|
6
|
+
constructor(generator: Generator<E>);
|
|
7
|
+
constructor(generator: Generator<E>, comparator: Comparator<E>);
|
|
8
|
+
[Symbol.iterator](): globalThis.Generator<E, void, undefined>;
|
|
9
|
+
[Symbol.asyncIterator](): globalThis.AsyncGenerator<E, void, undefined>;
|
|
10
|
+
slide(size: bigint, step?: bigint): Semantic<Semantic<E>>;
|
|
11
|
+
tumble(size: bigint): Semantic<Semantic<E>>;
|
|
12
|
+
}
|
package/dist/window.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { OrderedCollectable } from "./collectable";
|
|
2
|
+
import { useToAsyncGeneratorFunction, useToGeneratorFunction } from "./collector";
|
|
3
|
+
import { from } from "./factory";
|
|
4
|
+
import { useCompare } from "./hook";
|
|
5
|
+
import { WindowCollectableSymbol } from "./symbol";
|
|
6
|
+
export class WindowCollectable extends OrderedCollectable {
|
|
7
|
+
WindowCollectable = WindowCollectableSymbol;
|
|
8
|
+
constructor(parameter, comparator = useCompare) {
|
|
9
|
+
super(parameter, comparator);
|
|
10
|
+
}
|
|
11
|
+
*[Symbol.iterator]() {
|
|
12
|
+
try {
|
|
13
|
+
let collector = useToGeneratorFunction();
|
|
14
|
+
yield* collector.collect(this.source());
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
throw new Error("Uncaught error on Generator.");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async *[Symbol.asyncIterator]() {
|
|
21
|
+
try {
|
|
22
|
+
let collector = useToAsyncGeneratorFunction();
|
|
23
|
+
yield* collector.collect(this.source());
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw new Error("Uncaught error on AsyncGenerator.");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
slide(size, step = 1n) {
|
|
30
|
+
if (size > 0n && step > 0n) {
|
|
31
|
+
try {
|
|
32
|
+
let source = this.toArray();
|
|
33
|
+
let windows = [];
|
|
34
|
+
let windowStartIndex = 0n;
|
|
35
|
+
while (windowStartIndex < BigInt(source.length)) {
|
|
36
|
+
let windowEnd = windowStartIndex + size;
|
|
37
|
+
let window = source.slice(Number(windowStartIndex), Number(windowEnd));
|
|
38
|
+
if (window.length > 0) {
|
|
39
|
+
windows.push(window);
|
|
40
|
+
}
|
|
41
|
+
windowStartIndex += step;
|
|
42
|
+
}
|
|
43
|
+
return from(windows).map((window) => from(window));
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new Error("Invalid arguments.");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
throw new RangeError("Invalid arguments.");
|
|
50
|
+
}
|
|
51
|
+
tumble(size) {
|
|
52
|
+
try {
|
|
53
|
+
return this.slide(size, size);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
throw new Error("Invalid arguments.");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
;
|
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.7",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"readme": "readme.md",
|
|
12
12
|
"main": "dist/index.js",
|
package/readme.md
CHANGED
|
@@ -2,9 +2,59 @@
|
|
|
2
2
|
|
|
3
3
|
## Introduction
|
|
4
4
|
|
|
5
|
-
Semantic-TypeScript
|
|
5
|
+
**Semantic-TypeScript: A Paradigm-Shifting Stream Processing Library for the Modern Web**
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Semantic-TypeScript represents a significant advancement in stream processing technology, synthesising the most effective concepts from JavaScript GeneratorFunction, Java Stream, and MySQL Index paradigms. Its foundational design principle is centred on constructing exceptionally efficient data processing pipelines through sophisticated data indexing methodologies. The library delivers a rigorously type-safe, functionally pure streaming operation experience specifically engineered for contemporary front-end development.
|
|
8
|
+
|
|
9
|
+
In contrast to conventional synchronous processing architectures, Semantic-TypeScript implements a fully asynchronous processing model. During data stream generation, the temporal reception of data at the terminal is determined exclusively by the upstream invocation of the `accept` and `interrupt` callback mechanisms. This deliberate architectural choice enables the library to handle with exceptional grace:
|
|
10
|
+
|
|
11
|
+
- **Real-time data streams** with deterministic latency characteristics
|
|
12
|
+
- **Large-scale datasets** through memory-efficient processing pipelines
|
|
13
|
+
- **Asynchronous data sources** with guaranteed consistency and reliability
|
|
14
|
+
|
|
15
|
+
The library's innovative approach fundamentally reimagines how developers interact with streaming data, providing both unprecedented performance characteristics and developer ergonomics in a single, cohesive package.
|
|
16
|
+
|
|
17
|
+
### Why Choose Semantic-TypeScript?
|
|
18
|
+
|
|
19
|
+
Selecting the right library for data stream processing in a TypeScript project involves balancing performance, developer experience, and architectural fit. Semantic-TypeScript is engineered to excel across all these dimensions, offering a paradigm-shifting approach for the modern web. Here’s why it stands out.
|
|
20
|
+
|
|
21
|
+
#### 1. **A Unified, Type-Safe Paradigm for Streams**
|
|
22
|
+
Semantic-TypeScript synthesises the most effective concepts from **JavaScript GeneratorFunctions**, **Java Streams API**, and **database indexing strategies**. It provides a consistent, declarative API for processing any data sequence—be it static arrays, real-time events, or asynchronous chunks—while leveraging TypeScript's full power to ensure end-to-end type safety. This eliminates a whole class of runtime errors and transforms stream manipulation into a predictable, compiler-verified activity.
|
|
23
|
+
|
|
24
|
+
#### 2. **Uncompromising Performance with Intelligent Laziness**
|
|
25
|
+
At its core, the library is built on **lazy evaluation**. Operations like `filter`, `map`, and `flatMap` merely compose a processing pipeline; no work is done until a terminal operation (like `collect` or `toArray`) is invoked. This is coupled with **short-circuiting** capabilities (via operations like `limit`, `anyMatch`, or custom `interruptor` functions), which allows the processing to stop early when the result is known, dramatically improving efficiency for large or infinite streams.
|
|
26
|
+
|
|
27
|
+
#### 3. **Architectural Distinction: `Semantic<E>` vs. `Collectable<E>`**
|
|
28
|
+
This library introduces a crucial architectural separation that others often lack:
|
|
29
|
+
* **`Semantic<E>`**: Represents the abstract, lazy stream definition—the "blueprint" of your data transformation. It is immutable and composable.
|
|
30
|
+
* **`Collectable<E>`**: Represents a materialised, executable view of the stream, offering all terminal operations.
|
|
31
|
+
|
|
32
|
+
This separation enforces a clear mental model and allows for optimisations (e.g., an `UnorderedCollectable` can skip expensive sorting steps if order is not required).
|
|
33
|
+
|
|
34
|
+
#### 4. **The Power of the `Collector<E, A, R>` Pattern**
|
|
35
|
+
Inspired by Java, the `Collector` pattern is the engine of flexibility. It decouples the *specification* of how to accumulate stream elements from the *execution* of the stream itself. The library provides a rich set of built-in collectors (`useToArray`, `useGroupBy`, `useSummate`, etc.) for everyday tasks, while the pattern makes it trivial to implement your own complex, reusable reduction logic. This is far more powerful and composable than a fixed set of terminal methods.
|
|
36
|
+
|
|
37
|
+
#### 5. **First-Class Support for Modern Web & Async Data**
|
|
38
|
+
Semantic-TypeScript is designed for contemporary front-end and Node.js development. It offers native factory methods for modern web sources:
|
|
39
|
+
* `from(iterable)`, `range()` for static data.
|
|
40
|
+
* `interval()`, `animationFrame()` for time-based streams.
|
|
41
|
+
* `blob()` for chunked binary data processing.
|
|
42
|
+
* `websocket()` for real-time message streams.
|
|
43
|
+
Its foundational support for both `Iterable` and `AsyncIterable` protocols means it handles synchronous and asynchronous data flows with the same elegant API.
|
|
44
|
+
|
|
45
|
+
#### 6. **Beyond Basic Aggregation: Built-in Statistical Analysis**
|
|
46
|
+
Move beyond simple sums and averages. The library provides dedicated `NumericStatistics` and `BigIntStatistics` classes, offering immediate access to advanced statistical measures directly from your streams—**variance, standard deviation, median, quantiles, skewness, and kurtosis**. This turns complex data analysis into a one-liner, saving you from manual implementation or integrating another specialised library.
|
|
47
|
+
|
|
48
|
+
#### 7. **Designed for Developer Ergonomics**
|
|
49
|
+
* **Fluent, Chainable API**: Write complex data pipelines as readable, sequential chains of operations.
|
|
50
|
+
* **Comprehensive Utility Suite**: Comes with essential guards (`isOptional`, `isSemantic`), type-safe utilities (`useCompare`, `useTraverse`), and functional interfaces out of the box.
|
|
51
|
+
* **`Optional<T>` Integration**: Models the absence of a value safely, eliminating null-pointer concerns in find operations.
|
|
52
|
+
* **Performance Guidance**: The documentation clearly guides you on when to use `toUnordered()` (for maximum speed) versus `toOrdered()` (when sequence matters).
|
|
53
|
+
|
|
54
|
+
#### 8. **Robust, Community-Ready Foundation**
|
|
55
|
+
As an open-source library hosted on **GitHub** and distributed via **npm**, it is built for real-world use. The extensive documentation, complete with time/space complexity annotations and practical examples, lowers the learning curve and provides clarity for performance-sensitive applications.
|
|
56
|
+
|
|
57
|
+
**In summary, choose Semantic-TypeScript if you seek a rigorously designed, type-safe, and high-performance stream processing library that brings the power of enterprise-level data transformation patterns to the TypeScript ecosystem, without compromising on the idioms and needs of modern web development.**
|
|
8
58
|
|
|
9
59
|
## Installation
|
|
10
60
|
|
|
@@ -83,7 +133,9 @@ let comparator: Comparator<number> = (a: number, b: number): number => a - b;
|
|
|
83
133
|
| `isNumericStatistics(t: unknown): t is NumericStatistics<unknown>` | Check if it is a NumericStatistics instance | O(1) | O(1) |
|
|
84
134
|
| `isBigIntStatistics(t: unknown): t is BigIntStatistics<unknown>` | Check if it is a BigIntStatistics instance | O(1) | O(1) |
|
|
85
135
|
| `isPromise(t: unknown): t is Promise<unknown>` | Check if it is a Promise object | O(1) | O(1) |
|
|
86
|
-
| `
|
|
136
|
+
| `isAsyncFunction(t: unknown): t is AsyncFunction` | Check if it is an AsyncFunction | O(1) | O(1) |
|
|
137
|
+
| `isGeneratorFunction(t: unknown): t is GeneratorFunction` | Check if it is a GeneratorFunction | O(1) | O(1) |
|
|
138
|
+
| `isAsyncGeneratorFunction(t: unknown): t is AsyncGeneratorFunction` | Check if it is an AsyncGeneratorFunction | O(1) | O(1) |
|
|
87
139
|
|
|
88
140
|
```typescript
|
|
89
141
|
// Type guard usage examples
|
|
@@ -94,7 +146,7 @@ if (isString(value)) {
|
|
|
94
146
|
}
|
|
95
147
|
|
|
96
148
|
if (isOptional(someValue)) {
|
|
97
|
-
someValue.ifPresent((value): void => console.log(
|
|
149
|
+
someValue.ifPresent((value): void => console.log(value));
|
|
98
150
|
}
|
|
99
151
|
|
|
100
152
|
if(isIterable(value)){
|
|
@@ -111,13 +163,43 @@ if(isIterable(value)){
|
|
|
111
163
|
|------|------|------------|------------|
|
|
112
164
|
| `useCompare<T>(t1: T, t2: T): number` | Generic comparison function | O(1) | O(1) |
|
|
113
165
|
| `useRandom<T = number \| bigint>(index: T): T` | Pseudo-random number generator | O(log n) | O(1) |
|
|
166
|
+
| `useTraverse(t, callback)` | Deep traverse an object without cyclic references | O(n) | O(1) |
|
|
167
|
+
| `useToNumber(t: unknown): number` | Convert a value to a number | O(1) | O(1) |
|
|
168
|
+
| `useToBigInt(t: unknown): bigint` | Convert a value to a BigInt | O(1) | O(1) |
|
|
114
169
|
|
|
115
170
|
```typescript
|
|
116
171
|
// Utility function usage examples
|
|
117
172
|
let numbers: Array<number> = [3, 1, 4, 1, 5];
|
|
118
173
|
numbers.sort(useCompare); // [1, 1, 3, 4, 5]
|
|
119
174
|
|
|
120
|
-
let randomNum = useRandom(42); // Seed-based random number
|
|
175
|
+
let randomNum: number = useRandom(42); // Seed-based random number
|
|
176
|
+
|
|
177
|
+
let o = {
|
|
178
|
+
a: 1,
|
|
179
|
+
b: {
|
|
180
|
+
c: 2,
|
|
181
|
+
d: o
|
|
182
|
+
},
|
|
183
|
+
c: [1, 3, 5, o],
|
|
184
|
+
e: undefined,
|
|
185
|
+
f: null
|
|
186
|
+
};
|
|
187
|
+
useTraverse(o, (value, key): boolean => {
|
|
188
|
+
console.log(key, value);
|
|
189
|
+
/*
|
|
190
|
+
a 1
|
|
191
|
+
c 2
|
|
192
|
+
1 3 5
|
|
193
|
+
Cyclic references, undefined and null values are not traversed.
|
|
194
|
+
*/
|
|
195
|
+
return true; // Returns true to continue traversing.
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
let toBeResolved: object = {
|
|
199
|
+
[Symbol.toPrimitive]: () => 5
|
|
200
|
+
};
|
|
201
|
+
let resolvedNumber: number = useToNumber(toBeResolved); // 5
|
|
202
|
+
let resolvedBigInt: bigint = useToBigInt(toBeResolved); // 5n
|
|
121
203
|
```
|
|
122
204
|
|
|
123
205
|
## Factory Methods
|
|
@@ -138,7 +220,7 @@ let present: Optional<number> = Optional.of(42);
|
|
|
138
220
|
let nullable: Optional<string> = Optional.ofNullable<string>(null);
|
|
139
221
|
let nonNull: Optional<string> = Optional.ofNonNull("hello");
|
|
140
222
|
|
|
141
|
-
|
|
223
|
+
present.ifPresent((value: number): void => console.log(value)); // Outputs 42
|
|
142
224
|
console.log(empty.get(100)); // Outputs 100
|
|
143
225
|
```
|
|
144
226
|
|
|
@@ -153,9 +235,16 @@ console.log(empty.get(100)); // Outputs 100
|
|
|
153
235
|
| `useCollect<E, A, R>(identity, accumulator, finisher)` | Create a full collector with identity, accumulator, finisher | O(1) | O(1) |
|
|
154
236
|
| `useCollect<E, A, R>(identity, interruptor, accumulator, finisher)` | Create a shortable collector with identity, interruptor, accumulator, finisher | O(1) | O(1) |
|
|
155
237
|
| `useCount<E>()` | Create a full collector counting elements | O(n) | O(1) |
|
|
238
|
+
| `useError<E>()` | Create a collector printing an error | O(n) | O(1) |
|
|
239
|
+
| `useError<E>(accumulator)` | Create a collector printing an accumulated error | O(n) | O(1) |
|
|
240
|
+
| `useError<E>(prefix, accumulator, suffix)` | Create a collector printing an accumulated error with prefix and suffix | O(n) | O(1) |
|
|
156
241
|
| `useFindFirst<E>()` | Create a shortable collector returning the first element | O(n) | O(1) |
|
|
157
242
|
| `useFindAny<E>()` | Create a shortable collector returning any element | O(n) | O(1) |
|
|
158
243
|
| `useFindLast<E>()` | Create a full collector returning the last element | O(n) | O(1) |
|
|
244
|
+
| `useFindMaximum<E>()` | Create a full collector returning the maximum element | O(n) | O(1) |
|
|
245
|
+
| `useFindMaximum<E>(comparator?)` | Create a full collector returning the maximum element | O(n) | O(1) |
|
|
246
|
+
| `useFindMinimum<E>()` | Create a full collector returning the minimum element | O(n) | O(1) |
|
|
247
|
+
| `useFindMinimum<E>(comparator?)` | Create a full collector returning the minimum element | O(n) | O(1) |
|
|
159
248
|
| `useForEach<E>(action)` | Create a full collector executing an action for each element | O(n) | O(1) |
|
|
160
249
|
| `useNoneMatch<E>(predicate)` | Create a shortable collector returning true if no element matches the predicate | O(n) | O(1) |
|
|
161
250
|
| `useGroup<E, K>(classifier)` | Create a full collector grouping elements by classifier key | O(n) | O(n) |
|
|
@@ -182,8 +271,33 @@ console.log(empty.get(100)); // Outputs 100
|
|
|
182
271
|
| `useBigIntAverage<E>(mapper)` | Create a full collector computing bigint average with mapper | O(n) | O(1) |
|
|
183
272
|
| `useBigIntAverage<E>()` | Create a full collector computing bigint average | O(n) | O(1) |
|
|
184
273
|
| `useFrequency<E>()` | Create a full collector counting element frequencies | O(n) | O(n) |
|
|
185
|
-
| `
|
|
186
|
-
| `
|
|
274
|
+
| `useNumericSummate<E>()` | Create a full collector summing numeric elements | O(n) | O(1) |
|
|
275
|
+
| `useNumericSummate<E>(mapper)` | Create a full collector summing mapped numeric values | O(n) | O(1) |
|
|
276
|
+
| `useBigIntSummate<E>()` | Create a full collector summing numeric elements | O(n) | O(1) |
|
|
277
|
+
| `useBigIntSummate<E>(mapper)` | Create a full collector summing mapped numeric values | O(n) | O(1) |
|
|
278
|
+
| `useNumericAverage<E>()` | Create a full collector computing numeric average | O(n) | O(1) |
|
|
279
|
+
| `useNumericAverage<E>(mapper)` | Create a full collector computing numeric average with mapper | O(n) | O(1) |
|
|
280
|
+
| `useBigIntAverage<E>()` | Create a full collector computing bigint average | O(n) | O(1) |
|
|
281
|
+
| `useBigIntAverage<E>(mapper)` | Create a full collector computing bigint average with mapper | O(n) | O(1) |
|
|
282
|
+
| `useFrequency<E>()` | Create a full collector counting element frequencies | O(n) | O(n) |
|
|
283
|
+
| `useNumericMode<E>()` | Create a full collector computing numeric mode | O(n) | O(n) |
|
|
284
|
+
| `useNumericMode<E>(mapper)` | Create a full collector computing numeric mode with mapper | O(n) | O(n) |
|
|
285
|
+
| `useBigIntMode<E>()` | Create a full collector computing bigint mode | O(n) | O(n) |
|
|
286
|
+
| `useBigIntMode<E>(mapper)` | Create a full collector computing bigint mode with mapper | O(n) | O(n) |
|
|
287
|
+
| `useNumericVariance<E>()` | Create a full collector computing numeric variance | O(n) | O(1) |
|
|
288
|
+
| `useNumericVariance<E>(mapper)` | Create a full collector computing numeric variance with mapper | O(n) | O(1) |
|
|
289
|
+
| `useBigIntVariance<E>()` | Create a full collector computing bigint variance | O(n) | O(1) |
|
|
290
|
+
| `useBigIntVariance<E>(mapper)` | Create a full collector computing bigint variance with mapper | O(n) | O(1) |
|
|
291
|
+
| `useNumericStandardDeviation<E>()` | Create a full collector computing numeric standard deviation | O(n) | O(1) |
|
|
292
|
+
| `useNumericStandardDeviation<E>(mapper)` | Create a full collector computing numeric standard deviation with mapper | O(n) | O(1) |
|
|
293
|
+
| `useBigIntStandardDeviation<E>()` | Create a full collector computing bigint standard deviation | O(n) | O(1) |
|
|
294
|
+
| `useBigIntStandardDeviation<E>(mapper)` | Create a full collector computing bigint standard deviation with mapper | O(n) | O(1) |
|
|
295
|
+
| `useNumericMedian<E>()` | Create a full collector computing numeric median | O(n) | O(1) |
|
|
296
|
+
| `useNumericMedian<E>(mapper)` | Create a full collector computing numeric median with mapper | O(n) | O(1) |
|
|
297
|
+
| `useBigIntMedian<E>()` | Create a full collector computing bigint median | O(n) | O(1) |
|
|
298
|
+
| `useBigIntMedian<E>(mapper)` | Create a full collector computing bigint median with mapper | O(n) | O(1) |
|
|
299
|
+
| `useToGeneratorFunction<E>()` | Create a full collector converting a stream to a generator function | O(n) | O(1) |
|
|
300
|
+
| `useToAsyncGeneratorFunction<E>()` | Create a full collector converting a stream to an async generator function | O(n) | O(1) |
|
|
187
301
|
|
|
188
302
|
```typescript
|
|
189
303
|
// Collector conversion examples
|
|
@@ -209,9 +323,9 @@ find.collect(from([1,2,3,4,5])); // Finds the first element
|
|
|
209
323
|
find.collect([1,2,3,4,5]); // Finds the first element
|
|
210
324
|
|
|
211
325
|
// Calculates the sum of elements
|
|
212
|
-
let
|
|
213
|
-
|
|
214
|
-
|
|
326
|
+
let summate: Collector<number, number, number> = useSummate();
|
|
327
|
+
summate.collect(from([1,2,3,4,5])); // Sums from a stream
|
|
328
|
+
summate.collect([1,2,3,4,5]); // Sums from an iterable object
|
|
215
329
|
|
|
216
330
|
// Calculates the average of elements
|
|
217
331
|
let average: Collector<number, number, number> = useNumericAverage();
|
|
@@ -219,17 +333,30 @@ average.collect(from([1,2,3,4,5])); // Averages from a stream
|
|
|
219
333
|
average.collect([1,2,3,4,5]); // Averages from an iterable object
|
|
220
334
|
```
|
|
221
335
|
|
|
336
|
+
## Collector Class Methods
|
|
337
|
+
|
|
338
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
339
|
+
|------------|------------|------------|------------|
|
|
340
|
+
| `collect(stream)` | Collect elements from a stream | O(n) | O(1) |
|
|
341
|
+
| `collect(iterable)` | Collect elements from an iterable object | O(n) | O(1) |
|
|
342
|
+
| `collect(generator)` | Collect elements from a generator | O(n) | O(1) |
|
|
343
|
+
| `collect(semantic)` | Collect elements from a semantic stream | O(n) | O(1) |
|
|
344
|
+
| `collect(collectable)` | Collect elements from a collectable stream | O(n) | O(1) |
|
|
345
|
+
| `collect(start, endExelusive)` | Collect elements from a range | O(n) | O(1) |
|
|
346
|
+
|
|
222
347
|
### Semantic Factory Methods
|
|
223
348
|
|
|
224
349
|
| Method | Description | Time Complexity | Space Complexity |
|
|
225
350
|
|------|------|------------|------------|
|
|
226
351
|
| `animationFrame(period: number, delay: number = 0)` | Create a timed animation frame stream | O(1)* | O(1) |
|
|
352
|
+
| `attribute(target)` | Create a stream from an object deep attributes | O(n) | O(1) |
|
|
227
353
|
| `blob(blob, chunkSize)` | Create a stream from a Blob | O(n) | O(chunkSize) |
|
|
228
354
|
| `empty<E>()` | Create an empty stream | O(1) | O(1) |
|
|
229
355
|
| `fill<E>(element, count)` | Create a filled stream | O(n) | O(1) |
|
|
230
356
|
| `from<E>(iterable)` | Create a stream from an iterable object | O(1) | O(1) |
|
|
231
357
|
| `interval(period, delay?)` | Create a timed interval stream | O(1)* | O(1) |
|
|
232
358
|
| `iterate<E>(generator)` | Create a stream from a generator | O(1) | O(1) |
|
|
359
|
+
| `promise<E>(promise)` | Create a stream from a promise | O(1) | O(1) |
|
|
233
360
|
| `range(start, end, step)` | Create a numerical range stream | O(n) | O(1) |
|
|
234
361
|
| `websocket(websocket)` | Create a stream from a WebSocket | O(1) | O(1) |
|
|
235
362
|
|
|
@@ -239,7 +366,7 @@ average.collect([1,2,3,4,5]); // Averages from an iterable object
|
|
|
239
366
|
// Create a stream from a timed animation frame
|
|
240
367
|
animationFrame(1000)
|
|
241
368
|
.toUnordered()
|
|
242
|
-
.forEach(frame => console.log(frame));
|
|
369
|
+
.forEach((frame): void => console.log(frame));
|
|
243
370
|
|
|
244
371
|
// Create a stream from a Blob (chunked reading)
|
|
245
372
|
blob(someBlob, 1024n)
|
|
@@ -264,7 +391,7 @@ let numberStream = from([1, 2, 3, 4, 5]);
|
|
|
264
391
|
let stringStream = from(new Set(["Alex", "Bob"]));
|
|
265
392
|
|
|
266
393
|
// Create a stream from a resolved Promise
|
|
267
|
-
let promisedStream: Semantic<Array<number>> = Promise.resolve([1, 2, 3, 4, 5]);
|
|
394
|
+
let promisedStream: Semantic<Array<number>> = promise(Promise.resolve([1, 2, 3, 4, 5]));
|
|
268
395
|
|
|
269
396
|
// Create a range stream
|
|
270
397
|
let rangeStream = range(1, 10, 2); // 1, 3, 5, 7, 9
|
|
@@ -281,7 +408,8 @@ websocket(ws)
|
|
|
281
408
|
|
|
282
409
|
| Method | Description | Time Complexity | Space Complexity |
|
|
283
410
|
|------|------|------------|------------|
|
|
284
|
-
| `concat(
|
|
411
|
+
| `concat(semantic)` | Concatenate two streams | O(n) | O(1) |
|
|
412
|
+
| `concat(iterable)` | Concatenate from an iterable object | O(n) | O(1) |
|
|
285
413
|
| `distinct()` | Remove duplicates | O(n) | O(n) |
|
|
286
414
|
| `distinct(comparator)` | Remove duplicates using a comparator | O(n²) | O(n) |
|
|
287
415
|
| `dropWhile(predicate)` | Discard elements satisfying condition | O(n) | O(1) |
|
|
@@ -329,6 +457,7 @@ let complexResult = range(1, 100, 1)
|
|
|
329
457
|
| Method | Description | Time Complexity | Space Complexity |
|
|
330
458
|
|------------|------------|------------|------------|
|
|
331
459
|
| `sorted()` | Convert to ordered collector | O(n log n) | O(n) |
|
|
460
|
+
| `sorted(comparator)` | Convert to ordered collector with comparator | O(n log n) | O(n) |
|
|
332
461
|
| `toUnordered()` | Convert to unordered collector | O(1) | O(1) |
|
|
333
462
|
| `toOrdered()` | Convert to ordered collector | O(1) | O(1) |
|
|
334
463
|
| `toNumericStatistics()` | Convert to numeric statistics | O(n) | O(1) |
|
|
@@ -389,27 +518,46 @@ let customizedCollector = from([1, 2, 3, 4, 5]).toCollectable((generator: Genera
|
|
|
389
518
|
|
|
390
519
|
| Method | Description | Time Complexity | Space Complexity |
|
|
391
520
|
|------|------|------------|------------|
|
|
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
|
-
| `
|
|
521
|
+
| `anyMatch(predicate)` | Whether any element matches the predicate | O(n) | O(1) |
|
|
522
|
+
| `allMatch(predicate)` | Whether all elements match the predicate | O(n) | O(1) |
|
|
523
|
+
| `collect(collector)` | Collect elements using the given collector | O(n) | O(1) |
|
|
524
|
+
| `collect(identity, accumulator, finisher)` | Collect elements with identity, accumulator, and finisher | O(n) | O(1) |
|
|
525
|
+
| `collect(identity, interruptor, accumulator, finisher)` | Collect elements with identity, interruptor, accumulator, and finisher | O(n) | O(1) |
|
|
526
|
+
| `count()` | Count the number of elements | O(n) | O(1) |
|
|
527
|
+
| `error()` | Log elements to console with default error format | O(n) | O(1) |
|
|
528
|
+
| `error(accumulator)` | Log elements via custom accumulator error format | O(n) | O(1) |
|
|
529
|
+
| `error(prefix, accumulator, suffix)` | Log elements with prefix, custom accumulator, and suffix | O(n) | O(1) |
|
|
530
|
+
| `isEmpty()` | Check whether the collectable is empty | O(n) | O(1) |
|
|
531
|
+
| `findAny()` | Find any element in the collectable | O(n) | O(1) |
|
|
532
|
+
| `findFirst()` | Find the first element in the collectable | O(n) | O(1) |
|
|
533
|
+
| `findLast()` | Find the last element in the collectable | O(n) | O(1) |
|
|
534
|
+
| `findMaximum()` | Find the maximum element in the collectable | O(n) | O(1) |
|
|
535
|
+
| `findMaximum(comparator)` | Find the maximum element in the collectable using a comparator | O(n) | O(1) |
|
|
536
|
+
| `findMinimum()` | Find the minimum element in the collectable | O(n) | O(1) |
|
|
537
|
+
| `findMinimum(comparator)` | Find the minimum element in the collectable using a comparator | O(n) | O(1) |
|
|
538
|
+
| `forEach(action)` | Iterate over all elements with a consumer or bi-consumer | O(n) | O(1) |
|
|
539
|
+
| `group(classifier)` | Group elements by a classifier function | O(n) | O(n) |
|
|
540
|
+
| `groupBy(keyExtractor, valueExtractor)` | Group elements by key and value extractors | O(n) | O(n) |
|
|
541
|
+
| `join()` | Join elements into a string with default format | O(n) | O(n) |
|
|
542
|
+
| `join(delimiter)` | Join elements into a string with a delimiter | O(n) | O(n) |
|
|
543
|
+
| `join(prefix, delimiter, suffix)` | Join elements with prefix, delimiter, and suffix | O(n) | O(n) |
|
|
544
|
+
| `join(prefix, accumulator, suffix)` | Join elements via custom accumulator with prefix and suffix | O(n) | O(n) |
|
|
545
|
+
| `log()` | Log elements to console with default format | O(n) | O(1) |
|
|
546
|
+
| `log(accumulator)` | Log elements via custom accumulator | O(n) | O(1) |
|
|
547
|
+
| `log(prefix, accumulator, suffix)` | Log elements with prefix, custom accumulator, and suffix | O(n) | O(1) |
|
|
548
|
+
| `nonMatch(predicate)` | Whether no elements match the predicate | O(n) | O(1) |
|
|
549
|
+
| `partition(count)` | Partition elements into chunks of specified size | O(n) | O(n) |
|
|
550
|
+
| `partitionBy(classifier)` | Partition elements by a classifier function | O(n) | O(n) |
|
|
551
|
+
| `reduce(accumulator)` | Reduce elements using an accumulator (no initial value) | O(n) | O(1) |
|
|
552
|
+
| `reduce(identity, accumulator)` | Reduce elements with an initial value and accumulator | O(n) | O(1) |
|
|
553
|
+
| `reduce(identity, accumulator, finisher)` | Reduce elements with initial value, accumulator, and finisher | O(n) | O(1) |
|
|
554
|
+
| `semantic()` | Convert the collectable to a semantic object | O(1) | O(1) |
|
|
555
|
+
| `source()` | Get the source of the collectable | O(1) | O(1) |
|
|
556
|
+
| `toArray()` | Convert elements to an array | O(n) | O(n) |
|
|
557
|
+
| `toMap(keyExtractor, valueExtractor)` | Convert elements to a Map via key and value extractors | O(n) | O(n) |
|
|
558
|
+
| `toSet()` | Convert elements to a Set | O(n) | O(n) |
|
|
559
|
+
| `write(stream)` | Write elements to a stream (default format) | O(n) | O(1) |
|
|
560
|
+
| `write(stream, accumulator)` | Write elements to a stream via custom accumulator | O(n) | O(1) |
|
|
413
561
|
|
|
414
562
|
```typescript
|
|
415
563
|
// Collectable operation examples
|
|
@@ -439,24 +587,61 @@ let sum = data.reduce(0, (accumulator: number, n: number): number => accumulator
|
|
|
439
587
|
data.join(", "); // "[2, 4, 6, 8, 10]"
|
|
440
588
|
```
|
|
441
589
|
|
|
590
|
+
## Unordered Collectable Methods
|
|
591
|
+
|
|
592
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
593
|
+
|------------|------------|------------|------------|
|
|
594
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
595
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
596
|
+
|
|
597
|
+
## Ordered Collectable Methods
|
|
598
|
+
|
|
599
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
600
|
+
|------------|------------|------------|------------|
|
|
601
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
602
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
603
|
+
|
|
442
604
|
## Statistical Analysis Methods
|
|
443
605
|
|
|
606
|
+
### Statisttics Methods
|
|
607
|
+
|
|
608
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
609
|
+
|------------|------------|------------|------------|
|
|
610
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
611
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
612
|
+
| `count()` | Count the number of elements | O(n) | O(1) |
|
|
613
|
+
| `frequency()` | Frequency distribution | O(n) | O(n) |
|
|
614
|
+
|
|
444
615
|
### NumericStatistics Methods
|
|
445
616
|
|
|
446
617
|
| Method | Description | Time Complexity | Space Complexity |
|
|
447
618
|
|------|------|------------|------------|
|
|
448
|
-
|
|
|
449
|
-
|
|
|
450
|
-
| `
|
|
451
|
-
| `
|
|
452
|
-
| `
|
|
453
|
-
| `
|
|
454
|
-
| `
|
|
455
|
-
| `
|
|
456
|
-
| `
|
|
457
|
-
| `
|
|
458
|
-
| `
|
|
459
|
-
| `
|
|
619
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
620
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
621
|
+
| `average()` | Calculate the average of elements | O(n) | O(1) |
|
|
622
|
+
| `average(mapper)` | Calculate the average of elements using a mapper | O(n) | O(1) |
|
|
623
|
+
| `range()` | Calculate the range of elements | O(n) | O(1) |
|
|
624
|
+
| `range(mapper)` | Calculate the range of elements using a mapper | O(n) | O(1) |
|
|
625
|
+
| `variance()` | Calculate the variance of elements | O(n) | O(1) |
|
|
626
|
+
| `variance(mapper)` | Calculate the variance of elements using a mapper | O(n) | O(1) |
|
|
627
|
+
| `standardDeviation()` | Calculate the standard deviation of elements | O(n) | O(1) |
|
|
628
|
+
| `standardDeviation(mapper)` | Calculate the standard deviation of elements using a mapper | O(n) | O(1) |
|
|
629
|
+
| `mean()` | Calculate the mean of elements | O(n) | O(1) |
|
|
630
|
+
| `mean(mapper)` | Calculate the mean of elements using a mapper | O(n) | O(1) |
|
|
631
|
+
| `median()` | Calculate the median of elements | O(n) | O(1) |
|
|
632
|
+
| `median(mapper)` | Calculate the median of elements using a mapper | O(n) | O(1) |
|
|
633
|
+
| `mode()` | Calculate the mode of elements | O(n) | O(1) |
|
|
634
|
+
| `mode(mapper)` | Calculate the mode of elements using a mapper | O(n) | O(1) |
|
|
635
|
+
| `summate()` | Calculate the sum of elements | O(n) | O(1) |
|
|
636
|
+
| `summate(mapper)` | Calculate the sum of elements using a mapper | O(n) | O(1) |
|
|
637
|
+
| `quantile(quantile)` | Calculate the quantile of elements | O(n) | O(1) |
|
|
638
|
+
| `quantile(quantile, mapper)` | Calculate the quantile of elements using a mapper | O(n) | O(1) |
|
|
639
|
+
| `interquartileRange()` | Calculate the interquartile range of elements | O(n) | O(1) |
|
|
640
|
+
| `interquartileRange(mapper)` | Calculate the interquartile range of elements using a mapper | O(n) | O(1) |
|
|
641
|
+
| `skewness()` | Calculate the percentile of elements | O(n) | O(1) |
|
|
642
|
+
| `skewness(mapper)` | Calculate the percentile of elements using a mapper | O(n) | O(1) |
|
|
643
|
+
| `kurtosis()` | Calculate the kurtosis of elements | O(n) | O(1) |
|
|
644
|
+
| `kurtosis(mapper)` | Calculate the kurtosis of elements using a mapper | O(n) | O(1) |
|
|
460
645
|
|
|
461
646
|
```typescript
|
|
462
647
|
// Statistical analysis examples
|
|
@@ -478,6 +663,47 @@ let objects = from([
|
|
|
478
663
|
console.log("Mapped mean:", objects.mean(obj => obj.value)); // 20
|
|
479
664
|
```
|
|
480
665
|
|
|
666
|
+
### BigintStatistics Methods
|
|
667
|
+
|
|
668
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
669
|
+
|------------|------------|------------|------------|
|
|
670
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
671
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
672
|
+
| `average()` | Calculate the average of elements | O(n) | O(1) |
|
|
673
|
+
| `average(mapper)` | Calculate the average of elements using a mapper | O(n) | O(1) |
|
|
674
|
+
| `range()` | Calculate the range of elements | O(n) | O(1) |
|
|
675
|
+
| `range(mapper)` | Calculate the range of elements using a mapper | O(n) | O(1) |
|
|
676
|
+
| `variance()` | Calculate the variance of elements | O(n) | O(1) |
|
|
677
|
+
| `variance(mapper)` | Calculate the variance of elements using a mapper | O(n) | O(1) |
|
|
678
|
+
| `standardDeviation()` | Calculate the standard deviation of elements | O(n) | O(1) |
|
|
679
|
+
| `standardDeviation(mapper)` | Calculate the standard deviation of elements using a mapper | O(n) | O(1) |
|
|
680
|
+
| `mean()` | Calculate the mean of elements | O(n) | O(1) |
|
|
681
|
+
| `mean(mapper)` | Calculate the mean of elements using a mapper | O(n) | O(1) |
|
|
682
|
+
| `median()` | Calculate the median of elements | O(n) | O(1) |
|
|
683
|
+
| `median(mapper)` | Calculate the median of elements using a mapper | O(n) | O(1) |
|
|
684
|
+
| `mode()` | Calculate the mode of elements | O(n) | O(1) |
|
|
685
|
+
| `mode(mapper)` | Calculate the mode of elements using a mapper | O(n) | O(1) |
|
|
686
|
+
| `summate()` | Calculate the sum of elements | O(n) | O(1) |
|
|
687
|
+
| `summate(mapper)` | Calculate the sum of elements using a mapper | O(n) | O(1) |
|
|
688
|
+
| `quantile(quantile)` | Calculate the quantile of elements | O(n) | O(1) |
|
|
689
|
+
| `quantile(quantile, mapper)` | Calculate the quantile of elements using a mapper | O(n) | O(1) |
|
|
690
|
+
| `interquartileRange()` | Calculate the interquartile range of elements | O(n) | O(1) |
|
|
691
|
+
| `interquartileRange(mapper)` | Calculate the interquartile range of elements using a mapper | O(n) | O(1) |
|
|
692
|
+
| `skewness()` | Calculate the percentile of elements | O(n) | O(1) |
|
|
693
|
+
| `skewness(mapper)` | Calculate the percentile of elements using a mapper | O(n) | O(1) |
|
|
694
|
+
| `kurtosis()` | Calculate the kurtosis of elements | O(n) | O(1) |
|
|
695
|
+
| `kurtosis(mapper)` | Calculate the kurtosis of elements using a mapper | O(n) | O(1) |
|
|
696
|
+
|
|
697
|
+
### Window Collectable Methods
|
|
698
|
+
|
|
699
|
+
| Method | Description | Time Complexity | Space Complexity |
|
|
700
|
+
|------------|------------|------------|------------|
|
|
701
|
+
| `*[Symbol.iterator]()` | Iterate over all elements | O(n) | O(1) |
|
|
702
|
+
| `*[Symbol.asyncIterator]()` | Iterate over all elements asynchronously | O(n) | O(1) |
|
|
703
|
+
| `slide(size)` | Slide a window of a given size | O(n) | O(1) |
|
|
704
|
+
| `slide(size, step)` | Slide a window of a given size and step | O(n) | O(1) |
|
|
705
|
+
| `tumble(size)` | Tumble a window of a given size | O(n) | O(1) |
|
|
706
|
+
|
|
481
707
|
## Performance Selection Guide
|
|
482
708
|
|
|
483
709
|
### Choose Unordered Collector (Performance First)
|