semantic-typescript 0.0.1 → 0.0.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/readme.tw.md ADDED
@@ -0,0 +1,335 @@
1
+ # 📘 semantic-typescript
2
+
3
+ 一個強大且**類型安全**的 TypeScript 工具庫,專為**語意化資料處理(Semantic Data Processing)**而設計。
4
+ 提供可組合的函數式結構,用於處理集合(Collections)、資料流(Streams)與序列(Sequences),並支援排序、篩選、分組、統計分析等多種功能。
5
+
6
+ 無論您是處理**已排序或未排序的資料**、進行**統計分析**,還是僅想**流暢地鏈式操作資料**,這個套件都能滿足您的需求。
7
+
8
+ ---
9
+
10
+ ## 🧩 功能特色
11
+
12
+ - ✅ 全面採用**類型安全的泛型(Generics)**
13
+ - ✅ **函數式編程風格**(例如:map、filter、reduce)
14
+ - ✅ **語意化資料流(Semantic<E>)**,支援延遲求值(Lazy Evaluation)
15
+ - ✅ 可將資料流轉換為實體資料結構的**收集器(Collectors)**
16
+ - ✅ **已排序與未排序的收集器(Collectables)** — 其中 `toUnordered()` 是**最快的(不會排序)**
17
+ - ✅ 支援透過 `sorted()`、`toOrdered()` 與自訂比較器進行**排序**
18
+ - ✅ **統計分析功能**(`Statistics`、`NumericStatistics`、`BigIntStatistics`)
19
+ - ✅ **Optional<T>** — 安全處理可能為 `null` 或 `undefined` 值的 Monad
20
+ - ✅ 採用**迭代器(Iterator)與生成器(Generator)**架構,適用於大型或非同步資料
21
+
22
+ ---
23
+
24
+ ## 📦 安裝方式
25
+
26
+ ```bash
27
+ npm install semantic-typescript
28
+ ```
29
+
30
+ ---
31
+
32
+ ## 🧠 核心概念
33
+
34
+ ### 1. `Optional<T>` — 安全處理可為空(Nullable)的值
35
+
36
+ 用來包裝可能為 `null` 或 `undefined` 的值的 Monad 容器。
37
+
38
+ #### 提供的方法:
39
+
40
+ | 方法 | 說明 | 範例 |
41
+ |------|------|------|
42
+ | `of(value)` | 包裝一個值(可為空) | `Optional.of(null)` |
43
+ | `ofNullable(v)` | 包裝,允許為空值 | `Optional.ofNullable(someVar)` |
44
+ | `ofNonNull(v)` | 包裝,若為 null/undefined 則拋出例外 | `Optional.ofNonNull(5)` |
45
+ | `get()` | 取得值(若為空則拋出例外) | `opt.get()` |
46
+ | `getOrDefault(d)` | 取得值,若無則回傳預設值 | `opt.getOrDefault(0)` |
47
+ | `ifPresent(fn)` | 若有值則執行副作用 | `opt.ifPresent(x => console.log(x))` |
48
+ | `map(fn)` | 若有值則對其進行轉換 | `opt.map(x => x + 1)` |
49
+ | `filter(fn)` | 僅保留符合條件的值 | `opt.filter(x => x > 0)` |
50
+ | `isEmpty()` | 判斷是否為空 | `opt.isEmpty()` |
51
+ | `isPresent()` | 判斷是否有值 | `opt.isPresent()` |
52
+
53
+ #### 範例:
54
+
55
+ ```typescript
56
+ import { Optional } from 'semantic-typescript';
57
+
58
+ const value: number | null = Math.random() > 0.5 ? 10 : null;
59
+
60
+ const opt = Optional.ofNullable(value);
61
+
62
+ const result = opt
63
+ .filter(v => v > 5)
64
+ .map(v => v * 2)
65
+ .getOrDefault(0);
66
+
67
+ console.log(result); // 20 或 0
68
+ ```
69
+
70
+ ---
71
+
72
+ ### 2. `Semantic<E>` — 延遲資料流(Lazy Data Stream)
73
+
74
+ 一個**可組合、延遲求值的資料序列**,類似於 Java 的 Streams 或 Kotlin 的 Sequences。
75
+
76
+ 您可使用 `from()`、`range()`、`iterate()` 或 `fill()` 等輔助函數建立 `Semantic` 資料流。
77
+
78
+ #### 建立方式:
79
+
80
+ | 函數 | 說明 | 範例 |
81
+ |------|------|------|
82
+ | `from(iterable)` | 從陣列、集合或可迭代物件建立 | `from([1, 2, 3])` |
83
+ | `range(start, end, step?)` | 產生數字範圍 | `range(0, 5)` → 0,1,2,3,4 |
84
+ | `fill(element, count)` | 重複某個元素 N 次 | `fill('a', 3n)` |
85
+ | `iterate(gen)` | 使用自訂的生成器函數 | `iterate(genFn)` |
86
+
87
+ #### 常見操作:
88
+
89
+ | 方法 | 說明 | 範例 |
90
+ |------|------|------|
91
+ | `map(fn)` | 對每個元素進行轉換 | `.map(x => x * 2)` |
92
+ | `filter(fn)` | 僅保留符合條件的元素 | `.filter(x => x > 10)` |
93
+ | `limit(n)` | 限制回傳前 N 個元素 | `.limit(5)` |
94
+ | `skip(n)` | 跳過前 N 個元素 | `.skip(2)` |
95
+ | `distinct()` | 移除重複項(預設使用 Set) | `.distinct()` |
96
+ | `sorted()` | 對元素進行排序(自然順序) | `.sorted()` |
97
+ | `sorted(comparator)` | 使用自訂比較器排序 | `.sorted((a, b) => a - b)` |
98
+ | `toOrdered()` | 排序後回傳 OrderedCollectable | `.toOrdered()` |
99
+ | `toUnordered()` | **不進行排序(最快的方法)** | `.toUnordered()` ✅ |
100
+ | `collect(collector)` | 使用 Collector 進行聚合 | `.collect(Collector.full(...))` |
101
+ | `toArray()` | 轉為陣列 | `.toArray()` |
102
+ | `toSet()` | 轉為 Set | `.toSet()` |
103
+ | `toMap(keyFn, valFn)` | 轉為 Map | `.toMap(x => x.id, x => x)` |
104
+
105
+ ---
106
+
107
+ ### 3. `toUnordered()` — 🚀 最快,不排序
108
+
109
+ 如果您**不需要排序**,且希望獲得**最快的效能**,請使用:
110
+
111
+ ```typescript
112
+ const fastest = semanticStream.toUnordered();
113
+ ```
114
+
115
+ 🔥 **不會套用任何排序演算法。**
116
+ 當資料的順序不重要,而您追求極致效能時,這是最佳選擇。
117
+
118
+ ---
119
+
120
+ ### 4. `toOrdered()` 與 `sorted()` — 排序後的資料
121
+
122
+ 如果您需要**排序後的資料**,可使用以下方法:
123
+
124
+ ```typescript
125
+ const ordered = semanticStream.sorted(); // 自然排序
126
+ const customSorted = semanticStream.sorted((a, b) => a - b); // 自訂比較器
127
+ const orderedCollectable = semanticStream.toOrdered(); // 也會排序
128
+ ```
129
+
130
+ ⚠️ 這些方法**會對資料進行排序**,可選擇使用自然排序或自訂比較器。
131
+
132
+ ---
133
+
134
+ ### 5. `Collector<E, A, R>` — 資料聚合
135
+
136
+ Collector 可讓您將資料流**縮減為單一值或複雜的資料結構**。
137
+
138
+ 內建靜態工廠:
139
+
140
+ ```typescript
141
+ Collector.full(identity, accumulator, finisher)
142
+ Collector.shortable(identity, interruptor, accumulator, finisher)
143
+ ```
144
+
145
+ 但您通常會透過 `Collectable` 類別提供的高階方法來使用它們。
146
+
147
+ ---
148
+
149
+ ### 6. `Collectable<E>`(抽象類別)
150
+
151
+ 以下類別的基礎:
152
+
153
+ - `OrderedCollectable<E>` — 排序後的資料
154
+ - `UnorderedCollectable<E>` — 不排序,速度最快
155
+ - `WindowCollectable<E>` — 滑動視窗資料
156
+ - `Statistics<E, D>` — 統計資料聚合
157
+
158
+ #### 常見方法(繼承而來):
159
+
160
+ | 方法 | 說明 | 範例 |
161
+ |------|------|------|
162
+ | `count()` | 計算元素數量 | `.count()` |
163
+ | `toArray()` | 轉為陣列 | `.toArray()` |
164
+ | `toSet()` | 轉為 Set | `.toSet()` |
165
+ | `toMap(k, v)` | 轉為 Map | `.toMap(x => x.id, x => x)` |
166
+ | `group(k)` | 依鍵值分組 | `.group(x => x.category)` |
167
+ | `findAny()` | 找出任意符合的元素(Optional) | `.findAny()` |
168
+ | `findFirst()` | 找出第一個元素(Optional) | `.findFirst()` |
169
+ | `reduce(...)` | 自訂縮減邏輯 | `.reduce((a,b) => a + b, 0)` |
170
+
171
+ ---
172
+
173
+ ### 7. `OrderedCollectable<E>` — 排序資料
174
+
175
+ 如果您希望資料**自動排序**,可使用此類別。
176
+
177
+ 可接受**自訂比較器**,或使用自然排序。
178
+
179
+ ```typescript
180
+ const sorted = new OrderedCollectable(stream);
181
+ const customSorted = new OrderedCollectable(stream, (a, b) => b - a);
182
+ ```
183
+
184
+ 🔒 **保證輸出為排序後的資料。**
185
+
186
+ ---
187
+
188
+ ### 8. `UnorderedCollectable<E>` — 不排序(🚀 最快)
189
+
190
+ 如果您**不在意資料順序**,且希望獲得**最佳效能**,可使用:
191
+
192
+ ```typescript
193
+ const unordered = new UnorderedCollectable(stream);
194
+ // 或
195
+ const fastest = semanticStream.toUnordered();
196
+ ```
197
+
198
+ ✅ **不執行任何排序演算法**
199
+ ✅ **當資料順序不重要時,提供最快速的處理方式**
200
+
201
+ ---
202
+
203
+ ### 9. `Statistics<E, D>` — 統計分析
204
+
205
+ 用於分析數值資料的抽象基礎類別。
206
+
207
+ #### 子類別:
208
+
209
+ - `NumericStatistics<E>` — 適用於 `number` 類型
210
+ - `BigIntStatistics<E>` — 適用於 `bigint` 類型
211
+
212
+ ##### 常見統計方法:
213
+
214
+ | 方法 | 說明 | 範例 |
215
+ |------|------|------|
216
+ | `mean()` | 平均值 | `.mean()` |
217
+ | `median()` | 中位數 | `.median()` |
218
+ | `mode()` | 眾數(最常出現的值) | `.mode()` |
219
+ | `minimum()` | 最小值 | `.minimum()` |
220
+ | `maximum()` | 最大值 | `.maximum()` |
221
+ | `range()` | 範圍(最大值 - 最小值) | `.range()` |
222
+ | `variance()` | 變異數 | `.variance()` |
223
+ | `standardDeviation()` | 標準差 | `.standardDeviation()` |
224
+ | `summate()` | 總和 | `.summate()` |
225
+ | `quantile(q)` | 第 q 百分位數(0–1) | `.quantile(0.5)` → 中位數 |
226
+ | `frequency()` | 頻率統計(Map) | `.frequency()` |
227
+
228
+ ---
229
+
230
+ ## 🧪 完整範例
231
+
232
+ ```typescript
233
+ import { from, toUnordered, toOrdered, sorted, NumericStatistics } from 'semantic-typescript';
234
+
235
+ // 範例資料
236
+ const numbers = from([10, 2, 8, 4, 5, 6]);
237
+
238
+ // 🚀 最快:不排序
239
+ const fastest = numbers.toUnordered();
240
+ console.log(fastest.toArray()); // 例如:[10, 2, 8, 4, 5, 6](原始順序)
241
+
242
+ // 🔢 自然排序
243
+ const ordered = numbers.sorted();
244
+ console.log(ordered.toArray()); // [2, 4, 5, 6, 8, 10]
245
+
246
+ // 📊 統計分析
247
+ const stats = new NumericStatistics(numbers);
248
+ console.log('平均值:', stats.mean());
249
+ console.log('中位數:', stats.median());
250
+ console.log('眾數:', stats.mode());
251
+ console.log('範圍:', stats.range());
252
+ console.log('標準差:', stats.standardDeviation());
253
+ ```
254
+
255
+ ---
256
+
257
+ ## 🛠️ 工具函數
258
+
259
+ 本套件也提供多種**型別守衛(Type Guards)**與**比較工具**:
260
+
261
+ | 函數 | 用途 |
262
+ |------|------|
263
+ | `isString(x)` | 判斷是否為 `string` |
264
+ | `isNumber(x)` | 判斷是否為 `number` |
265
+ | `isBoolean(x)` | 判斷是否為 `boolean` |
266
+ | `isIterable(x)` | 判斷是否為可迭代物件 |
267
+ | `useCompare(a, b)` | 通用比較函數 |
268
+ | `useRandom(x)` | 偽隨機數生成器(趣味用途) |
269
+
270
+ ---
271
+
272
+ ## 🧩 進階:自訂生成器與滑動視窗
273
+
274
+ 您可以建立**自訂的資料生成器**,用於控制或無限的資料流:
275
+
276
+ ```typescript
277
+ const gen = (accept: BiConsumer<number, bigint>, interrupt: Predicate<number>) => {
278
+ for (let i = 0; i < 10; i++) {
279
+ accept(i, BigInt(i));
280
+ if (i === 5) interrupt(i);
281
+ }
282
+ };
283
+
284
+ const s = new Semantic(gen);
285
+ ```
286
+
287
+ 或者使用**滑動視窗(Sliding Window)**:
288
+
289
+ ```typescript
290
+ const windowed = ordered.slide(3n, 2n); // 視窗大小為 3,每次移動 2
291
+ ```
292
+
293
+ ---
294
+
295
+ ## 📄 授權
296
+
297
+ 本專案採用 **MIT 授權**,可免費用於商業或個人用途。
298
+
299
+ ---
300
+
301
+ ## 🙌 貢獻
302
+
303
+ 歡迎提交 Pull Request、提出問題(Issues)或分享想法!
304
+
305
+ ---
306
+
307
+ ## 🚀 快速入門總覽
308
+
309
+ | 任務 | 方法 |
310
+ |------|------|
311
+ | 安全處理 null/undefined | `Optional<T>` |
312
+ | 建立資料流 | `from([...])`、`range()`、`fill()` |
313
+ | 資料轉換 | `map()`、`filter()` |
314
+ | 資料排序 | `sorted()`、`toOrdered()` |
315
+ | 不排序(最快) | `toUnordered()` ✅ |
316
+ | 分組 / 聚合 | `toMap()`、`group()`、`Collector` |
317
+ | 統計分析 | `NumericStatistics`、`mean()`、`median()` 等 |
318
+
319
+ ---
320
+
321
+ ## 🔗 相關連結
322
+
323
+ - 📦 npm: https://www.npmjs.com/package/semantic-typescript
324
+ - 🐙 GitHub: https://github.com/eloyhere/semantic-typescript
325
+ - 📘 文件:請參考原始碼 / 類型定義
326
+
327
+ ---
328
+
329
+ **享受在 TypeScript 中進行函數式、類型安全且可組合的資料處理吧!** 🚀
330
+
331
+ ---
332
+
333
+ ✅ **請記住:**
334
+ - `toUnordered()` → **不排序,最快**
335
+ - 其他方法(如 `sorted()`、`toOrdered()`)→ **會進行排序**