semantic-typescript 0.0.2 → 0.0.4

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [2026] [Eloy Kim]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "semantic-typescript",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
- "files": ["dist"],
7
+ "files": ["dist", "LICENSE", "readme.md", "readme.cn.md", "readme.jp.md", "readme.ko.md", "readme.tw.md", "readme.de.md", "readme.es.md", "readme.fr.md"],
8
8
  "scripts": {
9
9
  "build": "tsc",
10
10
  "prepublishOnly": "npm run build"
package/readme.cn.md ADDED
@@ -0,0 +1,335 @@
1
+ # 📘 semantic-typescript
2
+
3
+ 一个强大、类型安全的 TypeScript 工具库,用于**语义化数据处理**。
4
+ 提供可组合的函数式风格构造,用于处理集合、流和序列 —— 支持排序、过滤、分组、统计分析等功能。
5
+
6
+ 无论您是在处理**有序或无序数据**、进行**统计分析**,还是仅仅想要**流畅地链式操作**,本库都能满足您的需求。
7
+
8
+ ---
9
+
10
+ ## 🧩 特性
11
+
12
+ - ✅ 全程**类型安全泛型**
13
+ - ✅ **函数式编程**风格(map、filter、reduce 等)
14
+ - ✅ **语义数据流**(`Semantic<E>`)支持惰性求值
15
+ - ✅ **收集器**用于将流转换为具体结构
16
+ - ✅ **有序与无序收集器** —— `toUnordered()` **不排序,最快**!其他收集器会排序
17
+ - ✅ **排序支持**通过 `sorted()`、`toOrdered()`、比较器
18
+ - ✅ **统计分析**(`Statistics`、`NumericStatistics`、`BigIntStatistics`)
19
+ - ✅ **Optional<T>** 单体模式,安全处理可空值
20
+ - ✅ 基于**迭代器与生成器**的设计 —— 适用于大型/异步数据
21
+
22
+ ---
23
+
24
+ ## 📦 安装
25
+
26
+ ```bash
27
+ npm install semantic-typescript
28
+ ```
29
+
30
+ ---
31
+
32
+ ## 🧠 核心概念
33
+
34
+ ### 1. `Optional<T>` – 安全的可空值处理
35
+
36
+ 用于包装可能为 `null` 或 `undefined` 的值的单体制容器。
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>` – 惰性数据流
73
+
74
+ 一个**惰性、可组合的序列**元素。类似于 Java Streams 或 Kotlin Sequences 的函数式流。
75
+
76
+ 您可以使用 `from()`、`range()`、`iterate()` 或 `fill()` 等辅助函数创建 `Semantic`。
77
+
78
+ #### 创建方法:
79
+
80
+ | 函数 | 说明 | 示例 |
81
+ |------|------|------|
82
+ | `from(iterable)` | 从 Array/Set/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)` | 使用收集器聚合 | `.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
+ 收集器让您能够将流**缩减为单个或复杂结构**。
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()` | 频率映射表 | `.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
+ 本库还导出许多**类型守卫**和**比较工具**:
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
+ 或使用**滑动窗口**:
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、提出问题与想法!
304
+
305
+ ---
306
+
307
+ ## 🚀 快速开始总结
308
+
309
+ | 任务 | 方法 |
310
+ |------|------|
311
+ | 安全处理空值 | `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()`)→ **会排序数据**
File without changes