semantic-typescript 0.6.0 → 0.7.0
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/asynchronous/collector.d.ts +62 -58
- package/dist/asynchronous/collector.js +16 -5
- package/dist/asynchronous/semantic.d.ts +2 -2
- package/dist/asynchronous/semantic.js +16 -6
- package/dist/factory.d.ts +8 -0
- package/dist/factory.js +112 -36
- package/dist/hook.d.ts +6 -1
- package/dist/hook.js +20 -3
- package/dist/main.d.ts +1 -0
- package/dist/main.js +6 -0
- package/dist/synchronous/collector.d.ts +8 -4
- package/dist/synchronous/collector.js +74 -59
- package/dist/synchronous/semantic.d.ts +27 -23
- package/dist/synchronous/semantic.js +215 -286
- package/package.json +3 -2
- package/readme.cn.md +213 -214
- package/readme.de.md +172 -173
- package/readme.es.md +177 -172
- package/readme.fr.md +181 -172
- package/readme.jp.md +187 -169
- package/readme.kr.md +182 -169
- package/readme.md +213 -214
- package/readme.ru.md +188 -169
- package/readme.tw.md +178 -169
package/readme.md
CHANGED
|
@@ -1,214 +1,213 @@
|
|
|
1
|
-
# Semantic-TypeScript
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
- **
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
###
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
//
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
//
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
[](https://github.com/eloyhere/semantic-typescript) [](https://www.npmjs.com/package/semantic-typescript)
|
|
1
|
+
# **Semantic-TypeScript**
|
|
2
|
+
**Flow, Indexed.** Your data, under precise control.
|
|
3
|
+
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
### Overview
|
|
7
|
+
|
|
8
|
+
Semantic-TypeScript marks a significant leap forward in stream processing technology, **synthesising** the most effective concepts from JavaScript `GeneratorFunction`, Java Streams, and MySQL-style indexing. Its core philosophy is both simple and powerful: construct exceptionally efficient data-processing pipelines through intelligent indexing, not through brute-force iteration.
|
|
9
|
+
|
|
10
|
+
Where conventional libraries impose synchronous loops or unwieldy promise chains, Semantic-TypeScript delivers a **fully asynchronous**, functionally pure, and rigorously type-safe experience, designed for the demands of modern front-end development.
|
|
11
|
+
|
|
12
|
+
In its elegant model, data only reaches the consumer when the upstream pipeline explicitly invokes the `accept` (and optionally `interrupt`) callbacks. You have complete control over the timing—exactly when it is needed.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
### Why Developers Prefer It
|
|
17
|
+
|
|
18
|
+
- **Zero-Boilerplate Indexing** — every element carries its natural or bespoke index.
|
|
19
|
+
- **Pure Functional Style** — with full TypeScript inference.
|
|
20
|
+
- **Leak-Proof Event Streams** — `useWindow`, `useDocument`, `useHTMLElement`, and `useWebSocket` are built with safety in mind. You define the boundary—using `limit(n)`, `sub(start, end)`, or `takeWhile(predicate)`—and the library manages the cleanup. No lingering listeners, no memory leaks.
|
|
21
|
+
- **Built-in Statistics** — comprehensive numeric and bigint analytics including averages, medians, modes, variance, skewness, and kurtosis.
|
|
22
|
+
- **Predictable Performance** — choose between ordered or unordered collectors based on your requirements.
|
|
23
|
+
- **Memory-Efficient** — streams are evaluated lazily, alleviating memory concerns.
|
|
24
|
+
- **No Undefined Behaviour** — TypeScript guarantees type safety and nullability. Input data remains unmodified unless explicitly altered within your callback functions.
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
### Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install semantic-typescript
|
|
32
|
+
```
|
|
33
|
+
or
|
|
34
|
+
```bash
|
|
35
|
+
yarn add semantic-typescript
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
### Quick Start
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { useOf, useFrom, useRange, useWindow, useHTMLElement, useWebSocket, useText, useStringify } from "semantic-typescript";
|
|
44
|
+
|
|
45
|
+
// Numeric statistics
|
|
46
|
+
let summate: number = useOf(10, 20, 30, 40)
|
|
47
|
+
.map((n: number): number => n * 2)
|
|
48
|
+
.toNumericStatistics() // Required before terminal operation
|
|
49
|
+
.summate(); // 200
|
|
50
|
+
|
|
51
|
+
// Bigint statistics
|
|
52
|
+
let summate: bigint = useOf(10n, 20n, 30n, 40n)
|
|
53
|
+
.map((n: bigint): bigint => n * 2)
|
|
54
|
+
.toBigIntStatistics() // Required before terminal operation
|
|
55
|
+
.summate(); // 200n
|
|
56
|
+
|
|
57
|
+
// Reverse a stream by index
|
|
58
|
+
useFrom([1, 2, 3, 4, 5])
|
|
59
|
+
.redirect((element: E, index: bigint): bigint => -index) // Negative index for reversal
|
|
60
|
+
.toOrdered() // Call toOrdered() to preserve index order
|
|
61
|
+
.toArray(); // [5, 4, 3, 2, 1]
|
|
62
|
+
|
|
63
|
+
// Shuffle a stream
|
|
64
|
+
useFrom([1, 2, 3, 4, 5])
|
|
65
|
+
.shuffle()
|
|
66
|
+
.toOrdered()
|
|
67
|
+
.toArray(); // e.g., [2, 5, 1, 4, 3]
|
|
68
|
+
|
|
69
|
+
// Translate elements within a stream
|
|
70
|
+
useFrom([1, 2, 3, 4, 5])
|
|
71
|
+
.translate(2) // Shift elements right by 2 positions
|
|
72
|
+
.toOrdered()
|
|
73
|
+
.toArray(); // [4, 5, 1, 2, 3]
|
|
74
|
+
|
|
75
|
+
useFrom([1, 2, 3, 4, 5])
|
|
76
|
+
.translate(-2) // Shift elements left by 2 positions
|
|
77
|
+
.toOrdered()
|
|
78
|
+
.toArray(); // [3, 4, 5, 1, 2]
|
|
79
|
+
|
|
80
|
+
// Infinite range with early termination
|
|
81
|
+
useRange(0n, 1_000_000n)
|
|
82
|
+
.filter(n => n % 17n === 0n)
|
|
83
|
+
.limit(10n) // Stop after 10 elements
|
|
84
|
+
.toUnordered()
|
|
85
|
+
.toArray();
|
|
86
|
+
|
|
87
|
+
// Real-time window resize (stops automatically after 5 events)
|
|
88
|
+
useWindow("resize")
|
|
89
|
+
.limit(5n) // Crucial for event streams
|
|
90
|
+
.toUnordered()
|
|
91
|
+
.forEach((ev, idx) => console.log(`Resize #${idx}`));
|
|
92
|
+
|
|
93
|
+
// Listen to an HTML element
|
|
94
|
+
// <input id="input" type="text"/>
|
|
95
|
+
useHTMLElement("#input", "change")
|
|
96
|
+
.limit(1)
|
|
97
|
+
.toUnordered()
|
|
98
|
+
.forEach((event: Event) => submit(event));
|
|
99
|
+
|
|
100
|
+
// Listen to multiple elements and events
|
|
101
|
+
useHTMLElement("input", ["change", "keyup"])
|
|
102
|
+
.takeWhile((event: Event): boolean => validate(event))
|
|
103
|
+
.toUnordered()
|
|
104
|
+
.forEach((event: Event) => submit(event));
|
|
105
|
+
|
|
106
|
+
// Listen to a WebSocket
|
|
107
|
+
let webSocket = new WebSocket("ws://localhost:8080");
|
|
108
|
+
webSocket.addEventListener("close", (): void => {
|
|
109
|
+
webSocket.close(); // Manage the WebSocket lifecycle manually
|
|
110
|
+
});
|
|
111
|
+
useWebSocket(webSocket, "message")
|
|
112
|
+
.limit(1)
|
|
113
|
+
.toUnordered()
|
|
114
|
+
.forEach((message: MessageEvent) => console.log(message.data));
|
|
115
|
+
|
|
116
|
+
// Iterate over a string by code point
|
|
117
|
+
useText("My emotion now is: 😊, and semantic is 👍")
|
|
118
|
+
.toUnordered()
|
|
119
|
+
.log(); // Outputs the string
|
|
120
|
+
|
|
121
|
+
// Safely stringify an object with circular references
|
|
122
|
+
let o = {
|
|
123
|
+
a: 1,
|
|
124
|
+
b: "text",
|
|
125
|
+
c: [o.a, o.b, o.c] // Circular reference
|
|
126
|
+
};
|
|
127
|
+
// let text: string = JSON.stringify(o); // Throws an error
|
|
128
|
+
let text: string = useStringify(o); // Safely yields `{a: 1, b: "text", c: []}`
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
### Core Concepts
|
|
134
|
+
|
|
135
|
+
| Concept | Purpose | When to Use |
|
|
136
|
+
| :--- | :--- | :--- |
|
|
137
|
+
| `AsynchronousSemantic` | Core builder for asynchronous streams, events, and lazy pipelines. | Real-time events, WebSockets, DOM listeners, long-running or infinite streams. |
|
|
138
|
+
| `SynchronousSemantic` | Builder for synchronous, in-memory, or loop-based streams. | Static data, ranges, immediate iteration. |
|
|
139
|
+
| `toUnordered()` | Fastest terminal collector (Map-based indexing). | Performance-critical paths (O(n) time & space, no sorting). |
|
|
140
|
+
| `toOrdered()` | Sorted, index-stable collector. | When stable ordering or indexed access is required. |
|
|
141
|
+
| `toNumericStatistics()` | Rich numeric statistical analysis (mean, median, variance, skewness, kurtosis, etc.). | Data analytics and statistical computations. |
|
|
142
|
+
| `toBigIntStatistics()` | Rich bigint statistical analysis. | Data analytics and statistical computations for large integers. |
|
|
143
|
+
| `toWindow()` | Sliding and tumbling window support. | Time-series processing, batching, and windowed operations. |
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
**Important Usage Rules**
|
|
148
|
+
|
|
149
|
+
1. **Event streams** (`useWindow`, `useDocument`, `useHTMLElement`, `useWebSocket`, …) return an `AsynchronousSemantic`.
|
|
150
|
+
→ You **must** call `.limit(n)`, `.sub(start, end)`, or `.takeWhile()` to cease listening. Otherwise, the listener remains active.
|
|
151
|
+
|
|
152
|
+
2. **Terminal operations** (`.toArray()`, `.count()`, `.average()`, `.reduce()`, `.findFirst()`, etc.) are **only available after** conversion to a collector:
|
|
153
|
+
```typescript
|
|
154
|
+
.toUnordered() // O(n) time & space, no sorting
|
|
155
|
+
// or
|
|
156
|
+
.toOrdered() // Sorted, maintains order
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
### Performance Characteristics
|
|
162
|
+
|
|
163
|
+
| Collector | Time Complexity | Space Complexity | Sorted? | Best For |
|
|
164
|
+
| :--- | :--- | :--- | :--- | :--- |
|
|
165
|
+
| `toUnordered()` | O(n) | O(n) | No | Raw speed, order not required. |
|
|
166
|
+
| `toOrdered()` | O(2n) | O(n) | Yes | Stable ordering, indexed access, analytics. |
|
|
167
|
+
| `toNumericStatistics()` | O(2n) | O(n) | Yes | Statistical operations requiring sorted data. |
|
|
168
|
+
| `toBigIntStatistics()` | O(2n) | O(n) | Yes | Statistical operations for bigint. |
|
|
169
|
+
| `toWindow()` | O(2n) | O(n) | Yes | Time-based windowing operations. |
|
|
170
|
+
|
|
171
|
+
Opt for `toUnordered()` when speed is paramount. Use `toOrdered()` only when you require stable ordering or statistical methods that depend on sorted data.
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
**Comparison with Other Front-End Stream Processors**
|
|
176
|
+
|
|
177
|
+
| Feature | Semantic-TypeScript | RxJS | Native Async Iterators / Generators | Most.js |
|
|
178
|
+
| :--- | :--- | :--- | :--- | :--- |
|
|
179
|
+
| **TypeScript Integration** | First-class, deeply typed with native index awareness. | Excellent, but involves complex generics. | Good, requires manual typing. | Strong, functional-first style. |
|
|
180
|
+
| **Built-in Statistical Analysis** | Comprehensive native support for `number` and `bigint`. | Not available natively (requires custom operators). | None. | None. |
|
|
181
|
+
| **Indexing & Position Awareness** | Native, powerful bigint indexing on every element. | Requires custom operators (`scan`, `withLatestFrom`). | Manual counter required. | Basic, no built-in index. |
|
|
182
|
+
| **Event Stream Management** | Dedicated, type-safe factories with explicit early-stop control. | Powerful but requires manual subscription management. | Manual event listener + cancellation. | Good `fromEvent`, lightweight. |
|
|
183
|
+
| **Performance & Memory Efficiency** | Exceptional – optimised `toUnordered()` and `toOrdered()` collectors. | Very good, but operator chains add overhead. | Excellent (zero overhead). | Excellent. |
|
|
184
|
+
| **Bundle Size** | Very lightweight. | Large (even with tree-shaking). | Zero (native). | Small. |
|
|
185
|
+
| **API Design Philosophy** | Functional collector pattern with explicit indexing. | Reactive Observable pattern. | Iterator / Generator pattern. | Functional, point-free. |
|
|
186
|
+
| **Early Termination & Control** | Explicit (`interrupt`, `.limit()`, `.takeWhile()`, `.sub()`). | Good (`take`, `takeUntil`, `first`). | Manual (`break` in `for await…of`). | Good (`take`, `until`). |
|
|
187
|
+
| **Synchronous & Asynchronous Support** | Unified API – first-class support for both. | Primarily asynchronous. | Both, but manual. | Primarily asynchronous. |
|
|
188
|
+
| **Learning Curve** | Gentle for developers familiar with functional and indexed pipelines. | Steeper (many operators, hot/cold observables). | Low. | Moderate. |
|
|
189
|
+
|
|
190
|
+
**Key Advantages of Semantic-TypeScript**
|
|
191
|
+
|
|
192
|
+
* Unique built-in statistical and indexing capabilities, eliminating the need for manual `reduce` or external libraries.
|
|
193
|
+
* Explicit control over event streams prevents the memory leaks common in RxJS.
|
|
194
|
+
* A unified synchronous/asynchronous design provides a single, consistent API for diverse use cases.
|
|
195
|
+
|
|
196
|
+
This comparison illustrates why Semantic-TypeScript is particularly well-suited for modern TypeScript front-end applications that demand performance, type safety, and rich analytics without the ceremony of traditional reactive libraries.
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
### Ready to Explore?
|
|
201
|
+
|
|
202
|
+
Semantic-TypeScript transforms complex data flows into readable, composable, and high-performance pipelines. Whether you are handling real-time UI events, processing large datasets, or building analytics dashboards, it provides the power of database-grade indexing with the elegance of functional programming.
|
|
203
|
+
|
|
204
|
+
**Next Steps:**
|
|
205
|
+
|
|
206
|
+
* Browse the fully typed API in your IDE (all exports are from the main package).
|
|
207
|
+
* Join the growing community of developers who have replaced convoluted async iterators with clean Semantic pipelines.
|
|
208
|
+
|
|
209
|
+
**Semantic-TypeScript** — where streams meet structure.
|
|
210
|
+
|
|
211
|
+
Begin building today and experience the difference that thoughtful indexing delivers.
|
|
212
|
+
|
|
213
|
+
**Build with clarity, proceed with confidence, and transform data with intent.**
|