semantic-typescript 0.1.4 → 0.2.5
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 +84 -0
- package/dist/collectable.js +476 -0
- package/dist/collector.d.ts +24 -0
- package/dist/collector.js +63 -0
- package/dist/factory.d.ts +11 -0
- package/dist/factory.js +248 -0
- package/dist/guard.d.ts +23 -0
- package/dist/guard.js +85 -0
- package/dist/hook.d.ts +2 -0
- package/dist/hook.js +64 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/optional.d.ts +18 -0
- package/dist/optional.js +65 -0
- package/dist/semantic.d.ts +9 -289
- package/dist/semantic.js +38 -1733
- package/dist/statistics.d.ts +101 -0
- package/dist/statistics.js +765 -0
- package/dist/symbol.d.ts +10 -0
- package/dist/symbol.js +10 -0
- package/dist/utility.d.ts +49 -0
- package/dist/utility.js +17 -0
- package/package.json +3 -2
- package/readme.cn.md +102 -57
- package/readme.de.md +120 -73
- package/readme.es.md +114 -67
- package/readme.fr.md +117 -70
- package/readme.jp.md +115 -70
- package/readme.kr.md +116 -71
- package/readme.md +114 -67
- package/readme.ru.md +115 -70
- package/readme.tw.md +131 -69
package/readme.tw.md
CHANGED
|
@@ -15,34 +15,40 @@ npm install semantic-typescript
|
|
|
15
15
|
## 基礎類型
|
|
16
16
|
|
|
17
17
|
| 類型 | 描述 |
|
|
18
|
-
|
|
19
|
-
| `Invalid<T>` | 擴展 null 或 undefined 的類型 |
|
|
20
|
-
| `Valid<T>` | 排除 null
|
|
21
|
-
| `MaybeInvalid<T>` | 可能為 null 或 undefined 的類型 |
|
|
22
|
-
| `Primitive` |
|
|
18
|
+
|------|-------------|
|
|
19
|
+
| `Invalid<T>` | 擴展 `null` 或 `undefined` 的類型 |
|
|
20
|
+
| `Valid<T>` | 排除 `null` 及 `undefined` 的類型 |
|
|
21
|
+
| `MaybeInvalid<T>` | 可能為 `null` 或 `undefined` 的類型 |
|
|
22
|
+
| `Primitive` | 原始類型的集合 |
|
|
23
23
|
| `MaybePrimitive<T>` | 可能為原始類型的類型 |
|
|
24
|
-
| `OptionalSymbol` | Optional
|
|
25
|
-
| `SemanticSymbol` | Semantic
|
|
26
|
-
| `CollectorsSymbol` | Collector
|
|
27
|
-
| `CollectableSymbol` | Collectable
|
|
28
|
-
| `OrderedCollectableSymbol` | OrderedCollectable
|
|
29
|
-
| `WindowCollectableSymbol` | WindowCollectable
|
|
30
|
-
| `StatisticsSymbol` | Statistics
|
|
31
|
-
| `NumericStatisticsSymbol` | NumericStatistics
|
|
32
|
-
| `BigIntStatisticsSymbol` | BigIntStatistics
|
|
33
|
-
| `UnorderedCollectableSymbol` | UnorderedCollectable
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
|
38
|
-
|
|
39
|
-
| `
|
|
40
|
-
| `
|
|
41
|
-
| `
|
|
42
|
-
| `
|
|
43
|
-
| `
|
|
44
|
-
| `
|
|
45
|
-
| `
|
|
24
|
+
| `OptionalSymbol` | `Optional` 類別的符號識別 |
|
|
25
|
+
| `SemanticSymbol` | `Semantic` 類別的符號識別 |
|
|
26
|
+
| `CollectorsSymbol` | `Collector` 類別的符號識別 |
|
|
27
|
+
| `CollectableSymbol` | `Collectable` 類別的符號識別 |
|
|
28
|
+
| `OrderedCollectableSymbol` | `OrderedCollectable` 類別的符號識別 |
|
|
29
|
+
| `WindowCollectableSymbol` | `WindowCollectable` 類別的符號識別 |
|
|
30
|
+
| `StatisticsSymbol` | `Statistics` 類別的符號識別 |
|
|
31
|
+
| `NumericStatisticsSymbol` | `NumericStatistics` 類別的符號識別 |
|
|
32
|
+
| `BigIntStatisticsSymbol` | `BigIntStatistics` 類別的符號識別 |
|
|
33
|
+
| `UnorderedCollectableSymbol` | `UnorderedCollectable` 類別的符號識別 |
|
|
34
|
+
|
|
35
|
+
## 函式介面
|
|
36
|
+
|
|
37
|
+
| 介面 | 描述 |
|
|
38
|
+
|------|-------------|
|
|
39
|
+
| `Runnable` | 無參數且無回傳值的函式 |
|
|
40
|
+
| `Supplier<R>` | 無參數並回傳 `R` 的函式 |
|
|
41
|
+
| `Functional<T, R>` | 單參數轉換函式 |
|
|
42
|
+
| `BiFunctional<T, U, R>` | 雙參數轉換函式 |
|
|
43
|
+
| `TriFunctional<T, U, V, R>` | 三參數轉換函式 |
|
|
44
|
+
| `Predicate<T>` | 單參數判斷函式 |
|
|
45
|
+
| `BiPredicate<T, U>` | 雙參數判斷函式 |
|
|
46
|
+
| `TriPredicate<T, U, V>` | 三參數判斷函式 |
|
|
47
|
+
| `Consumer<T>` | 單參數消費函式 |
|
|
48
|
+
| `BiConsumer<T, U>` | 雙參數消費函式 |
|
|
49
|
+
| `TriConsumer<T, U, V>` | 三參數消費函式 |
|
|
50
|
+
| `Comparator<T>` | 雙參數比較函式 |
|
|
51
|
+
| `Generator<T>` | 產生器函式(核心與基礎) |
|
|
46
52
|
|
|
47
53
|
```typescript
|
|
48
54
|
// 類型使用示例
|
|
@@ -136,15 +142,50 @@ console.log(emptyOpt.orElse(100)); // 輸出 100
|
|
|
136
142
|
| `Collector.shortable(identity, interruptor, accumulator, finisher)` | 創建可中斷收集器 | O(1) | O(1) |
|
|
137
143
|
|
|
138
144
|
```typescript
|
|
139
|
-
//
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
145
|
+
// 收集器轉換範例
|
|
146
|
+
const numbers = from([3, 1, 4, 1, 5, 9, 2, 6, 5]);
|
|
147
|
+
|
|
148
|
+
// 效能優先:使用無序收集器
|
|
149
|
+
const unordered = numbers
|
|
150
|
+
.filter(n => n > 3)
|
|
151
|
+
.toUnoredered();
|
|
152
|
+
|
|
153
|
+
// 需要排序:使用有序收集器
|
|
154
|
+
const ordered = numbers.sorted();
|
|
155
|
+
|
|
156
|
+
// 計算元素數量
|
|
157
|
+
let count = Collector.full(
|
|
158
|
+
() => 0, // 初始值
|
|
159
|
+
(accumulator, element) => accumulator + element, // 累積
|
|
160
|
+
(accumulator) => accumulator // 完成
|
|
144
161
|
);
|
|
162
|
+
count.collect(from([1,2,3,4,5])); // 從串流中計數
|
|
163
|
+
count.collect([1,2,3,4,5]); // 從可迭代物件中計數
|
|
164
|
+
|
|
165
|
+
let find = Collector.shortable(
|
|
166
|
+
() => Optional.empty(), // 初始值
|
|
167
|
+
(element, index, accumulator) => accumulator.isPresent(), // 中斷
|
|
168
|
+
(accumulator, element, index) => Optional.of(element), // 累積
|
|
169
|
+
(accumulator) => accumulator // 完成
|
|
170
|
+
);
|
|
171
|
+
find.collect(from([1,2,3,4,5])); // 尋找第一個元素
|
|
172
|
+
find.collect([1,2,3,4,5]); // 尋找第一個元素
|
|
173
|
+
|
|
174
|
+
// 統計分析:使用統計收集器
|
|
175
|
+
const stats = numbers.toNumericStatistics();
|
|
145
176
|
|
|
146
|
-
|
|
147
|
-
|
|
177
|
+
console.log(stats.mean()); // 平均值
|
|
178
|
+
console.log(stats.median()); // 中位數
|
|
179
|
+
console.log(stats.standardDeviation()); // 標準差
|
|
180
|
+
|
|
181
|
+
// 視窗操作
|
|
182
|
+
const windowed = numbers
|
|
183
|
+
.toWindow()
|
|
184
|
+
.tumble(3n); // 每 3 個元素形成一個視窗
|
|
185
|
+
|
|
186
|
+
windowed.forEach(window => {
|
|
187
|
+
console.log(window.toArray()); // 每個視窗的內容
|
|
188
|
+
});
|
|
148
189
|
```
|
|
149
190
|
|
|
150
191
|
### Semantic 工廠方法
|
|
@@ -155,6 +196,7 @@ const total = numbers.toUnoredered().collect(sumCollector); // 15
|
|
|
155
196
|
| `empty<E>()` | 建立空串流 | O(1) | O(1) |
|
|
156
197
|
| `fill<E>(element, count)` | 建立填充串流 | O(n) | O(1) |
|
|
157
198
|
| `from<E>(iterable)` | 從可迭代物件建立串流 | O(1) | O(1) |
|
|
199
|
+
| `generate<E>(element, interrupt)` | 建立產生器串流 | O(1) | O(1) |
|
|
158
200
|
| `interval(period, delay?)` | 建立定時間隔串流 | O(1)* | O(1) |
|
|
159
201
|
| `iterate<E>(generator)` | 從產生器建立串流 | O(1) | O(1) |
|
|
160
202
|
| `range(start, end, step)` | 建立數值範圍串流 | O(n) | O(1) |
|
|
@@ -243,45 +285,65 @@ const complexResult = range(1, 100, 1)
|
|
|
243
285
|
.toArray(); // 轉換為陣列
|
|
244
286
|
```
|
|
245
287
|
|
|
246
|
-
##
|
|
288
|
+
## Semantic轉換方法
|
|
247
289
|
|
|
248
290
|
| 方法 | 描述 | 時間複雜度 | 空間複雜度 |
|
|
249
|
-
|
|
250
|
-
| `
|
|
251
|
-
| `
|
|
252
|
-
| `
|
|
253
|
-
| `
|
|
254
|
-
| `
|
|
255
|
-
| `
|
|
291
|
+
|------------|------------|------------|------------|
|
|
292
|
+
| `sorted()` | 轉為有序收集器 | O(n log n) | O(n) |
|
|
293
|
+
| `toUnordered()` | 轉為無序收集器 | O(1) | O(1) |
|
|
294
|
+
| `toOrdered()` | 轉為有序收集器 | O(1) | O(1) |
|
|
295
|
+
| `toNumericStatistics()` | 轉為數值統計 | O(n) | O(1) |
|
|
296
|
+
| `toBigintStatistics()` | 轉為 bigint 統計 | O(n) | O(1) |
|
|
297
|
+
| `toWindow()` | 轉為視窗收集器 | O(1) | O(1) |
|
|
298
|
+
| `toCollectable()` | 轉為 `UnorderdCollectable` | O(n) | O(1) |
|
|
299
|
+
| `toCollectable(mapper)` | 轉為自訂收集器 | O(n) | O(1) |
|
|
256
300
|
|
|
257
301
|
```typescript
|
|
258
|
-
//
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
//
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
//
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
.
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
//
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
302
|
+
// 轉為升序排序陣列
|
|
303
|
+
from([6,4,3,5,2]) // 建立串流
|
|
304
|
+
.sorted() // 依升序排序串流
|
|
305
|
+
.toArray(); // [2, 3, 4, 5, 6]
|
|
306
|
+
|
|
307
|
+
// 轉為降序排序陣列
|
|
308
|
+
from([6,4,3,5,2]) // 建立串流
|
|
309
|
+
.soted((a, b) => b - a) // 依降序排序串流
|
|
310
|
+
.toArray(); // [6, 5, 4, 3, 2]
|
|
311
|
+
|
|
312
|
+
// 重新導向為反轉陣列
|
|
313
|
+
from([6,4,3,5,2])
|
|
314
|
+
.redirect((element, index) => -index) // 重新導向為反轉順序
|
|
315
|
+
.toOrderd() // 保持重新導向後的順序
|
|
316
|
+
.toArray(); // [2, 5, 3, 4, 6]
|
|
317
|
+
|
|
318
|
+
// 忽略反轉陣列的重新導向
|
|
319
|
+
from([6,4,3,5,2])
|
|
320
|
+
.redirect((element, index) => -index) // 重新導向為反轉順序
|
|
321
|
+
.toUnorderd() // 捨棄重新導向的順序。此操作會忽略 `redirect`、`reverse`、`shuffle` 與 `translate` 操作
|
|
322
|
+
.toArray(); // [2, 5, 3, 4, 6]
|
|
323
|
+
|
|
324
|
+
// 將串流反轉為陣列
|
|
325
|
+
from([6, 4, 3, 5, 2])
|
|
326
|
+
.reverse() // 反轉串流
|
|
327
|
+
.toOrdered() // 保證反轉後的順序
|
|
328
|
+
.toArray(); // [2, 5, 3, 4, 6]
|
|
329
|
+
|
|
330
|
+
// 覆寫洗牌後的串流為陣列
|
|
331
|
+
from([6, 4, 3, 5, 2])
|
|
332
|
+
.shuffle() // 打亂串流
|
|
333
|
+
.sorted() // 覆寫洗牌順序。此操作會覆寫 `redirect`、`reverse`、`shuffle` 與 `translate` 操作
|
|
334
|
+
.toArray(); // [2, 5, 3, 4, 6]
|
|
335
|
+
|
|
336
|
+
// 轉為視窗收集器
|
|
337
|
+
from([6, 4, 3, 5, 2]).toWindow();
|
|
338
|
+
|
|
339
|
+
// 轉為數值統計
|
|
340
|
+
from([6, 4, 3, 5, 2]).toNumericStatistics();
|
|
341
|
+
|
|
342
|
+
// 轉為 bigint 統計
|
|
343
|
+
from([6n, 4n, 3n, 5n, 2n]).toBigintStatistics();
|
|
344
|
+
|
|
345
|
+
// 定義自訂收集器以收集資料
|
|
346
|
+
let customizedCollector = from([1, 2, 3, 4, 5]).toCollectable((generator: Generator<E>) => new CustomizedCollector(generator));
|
|
285
347
|
```
|
|
286
348
|
|
|
287
349
|
## Collectable 收集方法
|