semantic-typescript 0.0.7 → 0.1.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.
@@ -21,15 +21,21 @@ export declare let SemanticSymbol: symbol;
21
21
  export declare let CollectorsSymbol: symbol;
22
22
  export declare let CollectableSymbol: symbol;
23
23
  export declare let OrderedCollectableSymbol: symbol;
24
- export declare let UnorderedCollectableSymbol: symbol;
24
+ export declare let WindowCollectableSymbol: symbol;
25
25
  export declare let StatisticsSymbol: symbol;
26
+ export declare let NumericStatisticsSymbol: symbol;
27
+ export declare let BigIntStatisticsSymbol: symbol;
28
+ export declare let UnorderedCollectableSymbol: symbol;
26
29
  export declare let isOptional: (t: unknown) => t is Optional<unknown>;
27
30
  export declare let isSemantic: (t: unknown) => t is Semantic<unknown>;
28
31
  export declare let isCollector: (t: unknown) => t is Collector<unknown, unknown, unknown>;
29
32
  export declare let isCollectable: (t: unknown) => t is Collectable<unknown>;
30
33
  export declare let isOrderedCollectable: (t: unknown) => t is OrderedCollectable<unknown>;
34
+ export declare let isWindowCollectable: (t: unknown) => t is OrderedCollectable<unknown>;
31
35
  export declare let isUnorderedCollectable: (t: unknown) => t is UnorderedCollectable<unknown>;
32
36
  export declare let isStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
37
+ export declare let isNumericStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
38
+ export declare let isBigIntStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
33
39
  export interface Runnable {
34
40
  (): void;
35
41
  }
@@ -72,11 +78,13 @@ declare class Optional<T> {
72
78
  protected constructor(value: MaybeInvalid<T>);
73
79
  filter(predicate: Predicate<T>): Optional<T>;
74
80
  get(): T;
75
- getOrDefault(defaultValue: T): T;
81
+ get(defaultValue: T): T;
76
82
  ifPresent(action: Consumer<T>): void;
83
+ ifPresent(action: Consumer<T>, elseAction: Runnable): void;
77
84
  isEmpty(): boolean;
78
85
  isPresent(): boolean;
79
86
  map<R>(mapper: Functional<T, R>): Optional<R>;
87
+ static empty<T>(): Optional<T>;
80
88
  static of<T>(value: MaybeInvalid<T>): Optional<T>;
81
89
  static ofNullable<T>(value?: MaybeInvalid<T>): Optional<T>;
82
90
  static ofNonNull<T>(value: T): Optional<T>;
@@ -85,8 +93,9 @@ export declare let blob: BiFunctional<Blob, bigint, Semantic<Uint8Array>>;
85
93
  export declare let empty: <E>() => Semantic<E>;
86
94
  export declare let fill: <E>(element: E | Supplier<E>, count: bigint) => Semantic<E>;
87
95
  export declare let from: <E>(iterable: Iterable<E>) => Semantic<E>;
88
- export declare let range: <N extends number | bigint>(start: N, end: N, step: N) => Semantic<N>;
96
+ export declare let interval: Functional<number, Semantic<number>> & BiFunctional<number, number, Semantic<number>>;
89
97
  export declare let iterate: <E>(generator: Generator<E>) => Semantic<E>;
98
+ export declare let range: <N extends number | bigint>(start: N, end: N, step: N) => Semantic<N>;
90
99
  export declare let websocket: Functional<WebSocket, Semantic<MessageEvent | CloseEvent | Event>>;
91
100
  export declare class Semantic<E> {
92
101
  protected generator: Generator<E>;
@@ -139,6 +148,7 @@ export declare class Collector<E, A, R> {
139
148
  static shortable<E, A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
140
149
  }
141
150
  export declare abstract class Collectable<E> {
151
+ protected readonly Collectable: symbol;
142
152
  constructor();
143
153
  anyMatch(predicate: Predicate<E>): boolean;
144
154
  allMatch(predicate: Predicate<E>): boolean;
@@ -179,10 +189,12 @@ export declare abstract class Collectable<E> {
179
189
  toArray(): Array<E>;
180
190
  toMap<K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Map<K, V>;
181
191
  toSet(): Set<E>;
182
- write(stream: WritableStream<string>): Promise<void>;
183
- write(stream: WritableStream<Uint8Array>, accumulator: Functional<E, Uint8Array>): Promise<void>;
192
+ write(stream: WritableStream<string>): Promise<WritableStream<string>>;
193
+ write(stream: WritableStream<string>, accumulator: BiFunctional<E, bigint, string>): Promise<WritableStream<string>>;
194
+ write(stream: WritableStream<Uint8Array>, accumulator: BiFunctional<E, bigint, Uint8Array>): Promise<WritableStream<Uint8Array>>;
184
195
  }
185
196
  export declare class UnorderedCollectable<E> extends Collectable<E> {
197
+ protected readonly UnorderedCollectable: symbol;
186
198
  protected generator: Generator<E>;
187
199
  constructor(generator: Generator<E>);
188
200
  source(): Generator<E>;
@@ -192,6 +204,7 @@ type Indexed<K, V> = {
192
204
  value: V;
193
205
  };
194
206
  export declare class OrderedCollectable<E> extends Collectable<E> {
207
+ protected readonly OrderedCollectable: symbol;
195
208
  protected ordered: Array<Indexed<bigint, E>>;
196
209
  constructor(iterable: Iterable<E>);
197
210
  constructor(iterable: Iterable<E>, comparator: Comparator<E>);
@@ -200,6 +213,7 @@ export declare class OrderedCollectable<E> extends Collectable<E> {
200
213
  source(): Generator<E> | Iterable<E>;
201
214
  }
202
215
  export declare class WindowCollectable<E> extends OrderedCollectable<E> {
216
+ protected readonly WindowCollectable: symbol;
203
217
  constructor(iterable: Iterable<E>);
204
218
  constructor(iterable: Iterable<E>, comparator: Comparator<E>);
205
219
  constructor(generator: Generator<E>);
@@ -208,6 +222,7 @@ export declare class WindowCollectable<E> extends OrderedCollectable<E> {
208
222
  tumble(size: bigint): Semantic<Semantic<E>>;
209
223
  }
210
224
  export declare abstract class Statistics<E, D extends number | bigint> extends OrderedCollectable<E> {
225
+ protected readonly Statistics: symbol;
211
226
  constructor(iterable: Iterable<E>);
212
227
  constructor(iterable: Iterable<E>, comparator: Comparator<E>);
213
228
  constructor(generator: Generator<E>);
@@ -243,6 +258,7 @@ export declare abstract class Statistics<E, D extends number | bigint> extends O
243
258
  abstract kurtosis(mapper: Functional<E, D>): D;
244
259
  }
245
260
  export declare class NumericStatistics<E> extends Statistics<E, number> {
261
+ protected readonly NumericStatistics: symbol;
246
262
  constructor(iterable: Iterable<E>);
247
263
  constructor(iterable: Iterable<E>, comparator: Comparator<E>);
248
264
  constructor(generator: Generator<E>);
@@ -273,6 +289,7 @@ export declare class NumericStatistics<E> extends Statistics<E, number> {
273
289
  kurtosis(mapper: Functional<E, number>): number;
274
290
  }
275
291
  export declare class BigIntStatistics<E> extends Statistics<E, bigint> {
292
+ protected readonly BigIntStatistics: symbol;
276
293
  constructor(iterable: Iterable<E>);
277
294
  constructor(iterable: Iterable<E>, comparator: Comparator<E>);
278
295
  constructor(generator: Generator<E>);
package/dist/semantic.js CHANGED
@@ -123,8 +123,11 @@ export let SemanticSymbol = Symbol.for("Semantic");
123
123
  export let CollectorsSymbol = Symbol.for("Collector");
124
124
  export let CollectableSymbol = Symbol.for("Collectable");
125
125
  export let OrderedCollectableSymbol = Symbol.for("OrderedCollectable");
126
- export let UnorderedCollectableSymbol = Symbol.for("UnorderedCollectable");
126
+ export let WindowCollectableSymbol = Symbol.for("WindowCollectable");
127
127
  export let StatisticsSymbol = Symbol.for("Statistics");
128
+ export let NumericStatisticsSymbol = Symbol.for("NumericStatistics");
129
+ export let BigIntStatisticsSymbol = Symbol.for("BigIntStatistics");
130
+ export let UnorderedCollectableSymbol = Symbol.for("UnorderedCollectable");
128
131
  export let isOptional = (t) => {
129
132
  if (isObject(t)) {
130
133
  return Reflect.get(t, "Optional") === OptionalSymbol;
@@ -155,6 +158,12 @@ export let isOrderedCollectable = (t) => {
155
158
  }
156
159
  return false;
157
160
  };
161
+ export let isWindowCollectable = (t) => {
162
+ if (isObject(t)) {
163
+ return Reflect.get(t, "WindowCollectable") === OrderedCollectableSymbol;
164
+ }
165
+ return false;
166
+ };
158
167
  export let isUnorderedCollectable = (t) => {
159
168
  if (isObject(t)) {
160
169
  return Reflect.get(t, "UnorderedCollectable") === UnorderedCollectableSymbol;
@@ -167,6 +176,18 @@ export let isStatistics = (t) => {
167
176
  }
168
177
  return false;
169
178
  };
179
+ export let isNumericStatistics = (t) => {
180
+ if (isObject(t)) {
181
+ return Reflect.get(t, "NumericStatistics") === NumericStatisticsSymbol;
182
+ }
183
+ return false;
184
+ };
185
+ export let isBigIntStatistics = (t) => {
186
+ if (isObject(t)) {
187
+ return Reflect.get(t, "BigIntStatistics") === BigIntStatisticsSymbol;
188
+ }
189
+ return false;
190
+ };
170
191
  ;
171
192
  ;
172
193
  ;
@@ -189,25 +210,26 @@ class Optional {
189
210
  }
190
211
  return new Optional((void 0));
191
212
  }
192
- get() {
193
- if (this.isPresent()) {
194
- return this.value;
195
- }
196
- throw new TypeError("Optional is empty");
197
- }
198
- getOrDefault(defaultValue) {
213
+ get(defaultValue) {
199
214
  if (this.isPresent()) {
200
215
  return this.value;
201
216
  }
202
- if (validate(defaultValue)) {
203
- return defaultValue;
217
+ else {
218
+ if (validate(defaultValue)) {
219
+ return defaultValue;
220
+ }
221
+ else {
222
+ throw new TypeError("Invalid default value and optional is empty.");
223
+ }
204
224
  }
205
- throw new TypeError("Default value is not valid");
206
225
  }
207
- ifPresent(action) {
226
+ ifPresent(action, elseAction) {
208
227
  if (this.isPresent() && isFunction(action)) {
209
228
  action(this.value);
210
229
  }
230
+ else if (isFunction(elseAction)) {
231
+ elseAction();
232
+ }
211
233
  }
212
234
  isEmpty() {
213
235
  return invalidate(this.value);
@@ -221,6 +243,9 @@ class Optional {
221
243
  }
222
244
  return new Optional(null);
223
245
  }
246
+ static empty() {
247
+ return new Optional(null);
248
+ }
224
249
  static of(value) {
225
250
  return Optional.ofNullable(value);
226
251
  }
@@ -325,6 +350,29 @@ export let from = (iterable) => {
325
350
  }
326
351
  throw new TypeError("Invalid arguments");
327
352
  };
353
+ export let interval = (period, delay = 0) => {
354
+ if (period > 0 && delay >= 0) {
355
+ return new Semantic((accept, interrupt) => {
356
+ setTimeout(() => {
357
+ let index = 0;
358
+ let timer = setInterval(() => {
359
+ if (interrupt(index)) {
360
+ clearInterval(timer);
361
+ }
362
+ accept(period, BigInt(index));
363
+ index++;
364
+ }, period);
365
+ }, delay);
366
+ });
367
+ }
368
+ throw new TypeError("Invalid arguments.");
369
+ };
370
+ export let iterate = (generator) => {
371
+ if (isFunction(generator)) {
372
+ return new Semantic(generator);
373
+ }
374
+ throw new TypeError("Invalid arguments.");
375
+ };
328
376
  export let range = (start, end, step = (typeof start === 'bigint' ? 1n : 1)) => {
329
377
  if ((isNumber(step) && step === 0) || (isBigint(step) && step === 0n)) {
330
378
  throw new TypeError("Step cannot be zero.");
@@ -357,12 +405,6 @@ export let range = (start, end, step = (typeof start === 'bigint' ? 1n : 1)) =>
357
405
  }
358
406
  throw new TypeError("Invalid arguments.");
359
407
  };
360
- export let iterate = (generator) => {
361
- if (isFunction(generator)) {
362
- return new Semantic(generator);
363
- }
364
- throw new TypeError("Invalid arguments.");
365
- };
366
408
  export let websocket = (websocket) => {
367
409
  if (invalidate(websocket)) {
368
410
  throw new TypeError("WebSocket is invalid.");
@@ -370,6 +412,15 @@ export let websocket = (websocket) => {
370
412
  return new Semantic((accept, interrupt) => {
371
413
  let index = 0n;
372
414
  let stop = false;
415
+ websocket.addEventListener("open", (event) => {
416
+ if (stop || interrupt(event)) {
417
+ stop = true;
418
+ }
419
+ else {
420
+ accept(event, index);
421
+ index++;
422
+ }
423
+ });
373
424
  websocket.addEventListener("message", (event) => {
374
425
  if (stop || interrupt(event)) {
375
426
  stop = true;
@@ -768,6 +819,7 @@ export class Collector {
768
819
  }
769
820
  ;
770
821
  export class Collectable {
822
+ Collectable = CollectableSymbol;
771
823
  constructor() {
772
824
  }
773
825
  anyMatch(predicate) {
@@ -1073,49 +1125,45 @@ export class Collectable {
1073
1125
  }
1074
1126
  write(stream, accumulator) {
1075
1127
  if (isObject(stream) && invalidate(accumulator)) {
1076
- return this.collect(() => {
1077
- return Promise.resolve(stream);
1078
- }, (promise, element) => {
1079
- return new Promise((resolve1, reject1) => {
1080
- promise.then(async (stream) => {
1128
+ let optional = this.collect(() => {
1129
+ return Optional.ofNonNull(stream);
1130
+ }, (result, element) => {
1131
+ try {
1132
+ return result.map((stream) => {
1081
1133
  let writer = stream.getWriter();
1082
- await writer.write(String(element));
1083
- resolve1(stream);
1134
+ writer.write(String(element));
1084
1135
  return stream;
1085
- }, reject1);
1086
- });
1087
- }, (promise) => {
1088
- return new Promise((resolve, reject) => {
1089
- promise.then(async (stream) => {
1090
- await stream.getWriter().close();
1091
- resolve();
1092
- }, (reason) => {
1093
- reject(reason);
1094
1136
  });
1095
- });
1137
+ }
1138
+ catch (reason) {
1139
+ return Optional.empty();
1140
+ }
1141
+ }, (a) => {
1142
+ return a;
1143
+ });
1144
+ return new Promise((resolve, reject) => {
1145
+ optional.ifPresent(resolve, reject);
1096
1146
  });
1097
1147
  }
1098
1148
  else if (isObject(stream) && isFunction(accumulator)) {
1099
- return this.collect(() => {
1100
- return Promise.resolve(stream);
1101
- }, (promise, element) => {
1102
- return new Promise((resolve1, reject1) => {
1103
- promise.then(async (stream) => {
1149
+ let optional = this.collect(() => {
1150
+ return Optional.ofNonNull(stream);
1151
+ }, (result, element, index) => {
1152
+ try {
1153
+ return result.map((stream) => {
1104
1154
  let writer = stream.getWriter();
1105
- await writer.write(accumulator(element));
1106
- resolve1(stream);
1155
+ writer.write(accumulator(element, index));
1107
1156
  return stream;
1108
- }, reject1);
1109
- });
1110
- }, (promise) => {
1111
- return new Promise((resolve, reject) => {
1112
- promise.then(async (stream) => {
1113
- await stream.getWriter().close();
1114
- resolve();
1115
- }, (reason) => {
1116
- reject(reason);
1117
1157
  });
1118
- });
1158
+ }
1159
+ catch (reason) {
1160
+ return Optional.empty();
1161
+ }
1162
+ }, (a) => {
1163
+ return a;
1164
+ });
1165
+ return new Promise((resolve, reject) => {
1166
+ optional.ifPresent(resolve, reject);
1119
1167
  });
1120
1168
  }
1121
1169
  else {
@@ -1124,6 +1172,7 @@ export class Collectable {
1124
1172
  }
1125
1173
  }
1126
1174
  export class UnorderedCollectable extends Collectable {
1175
+ UnorderedCollectable = UnorderedCollectableSymbol;
1127
1176
  generator;
1128
1177
  constructor(generator) {
1129
1178
  super();
@@ -1134,6 +1183,7 @@ export class UnorderedCollectable extends Collectable {
1134
1183
  }
1135
1184
  }
1136
1185
  export class OrderedCollectable extends Collectable {
1186
+ OrderedCollectable = OrderedCollectableSymbol;
1137
1187
  ordered = [];
1138
1188
  constructor(argument1, argument2) {
1139
1189
  super();
@@ -1182,6 +1232,7 @@ export class OrderedCollectable extends Collectable {
1182
1232
  }
1183
1233
  }
1184
1234
  export class WindowCollectable extends OrderedCollectable {
1235
+ WindowCollectable = WindowCollectableSymbol;
1185
1236
  constructor(parameter, comparator) {
1186
1237
  if (isIterable(parameter)) {
1187
1238
  if (isFunction(comparator)) {
@@ -1223,6 +1274,7 @@ export class WindowCollectable extends OrderedCollectable {
1223
1274
  }
1224
1275
  ;
1225
1276
  export class Statistics extends OrderedCollectable {
1277
+ Statistics = StatisticsSymbol;
1226
1278
  constructor(parameter, comparator) {
1227
1279
  if (isIterable(parameter)) {
1228
1280
  if (isFunction(comparator)) {
@@ -1341,6 +1393,7 @@ export class Statistics extends OrderedCollectable {
1341
1393
  }
1342
1394
  ;
1343
1395
  export class NumericStatistics extends Statistics {
1396
+ NumericStatistics = NumericStatisticsSymbol;
1344
1397
  constructor(parameter, comparator) {
1345
1398
  if (isIterable(parameter)) {
1346
1399
  if (isFunction(comparator)) {
@@ -1659,6 +1712,7 @@ export class NumericStatistics extends Statistics {
1659
1712
  }
1660
1713
  ;
1661
1714
  export class BigIntStatistics extends Statistics {
1715
+ BigIntStatistics = BigIntStatisticsSymbol;
1662
1716
  constructor(parameter, comparator) {
1663
1717
  if (isIterable(parameter)) {
1664
1718
  if (isFunction(comparator)) {
package/package.json CHANGED
@@ -1,17 +1,62 @@
1
1
  {
2
2
  "name": "semantic-typescript",
3
- "version": "0.0.7",
3
+ "author": {
4
+ "name": "Eloy Kim",
5
+ "email": "eloy.kim@outlook.com",
6
+ "url": "https://github.com/eloyhere"
7
+ },
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.1.4",
4
10
  "type": "module",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "files": ["dist"],
11
+ "readme": "readme.md",
12
+ "main": "dist/index.js",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.js"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "types": "dist/index.d.ts",
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "keywords": [
26
+ "typescript",
27
+ "stream",
28
+ "async-stream",
29
+ "functional-programming",
30
+ "lazy-evaluation",
31
+ "data-processing",
32
+ "pipeline",
33
+ "statistics",
34
+ "generator",
35
+ "optional",
36
+ "blob-stream",
37
+ "websocket-stream",
38
+ "frontend",
39
+ "bigInt"
40
+ ],
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/eloyhere/semantic-typescript.git"
44
+ },
45
+ "bugs": {
46
+ "url": "https://github.com/eloyhere/semantic-typescript/issues"
47
+ },
48
+ "homepage": "https://github.com/eloyhere/semantic-typescript#readme",
49
+ "license": "MIT",
50
+ "engines": {
51
+ "node": ">=14.0.0"
52
+ },
8
53
  "scripts": {
9
54
  "build": "tsc",
10
55
  "prepublishOnly": "npm run build"
11
56
  },
12
57
  "devDependencies": {
13
- "typescript": "~5.9.3"
14
- },
15
- "peerDependencies": {},
16
- "dependencies": {}
58
+ "typescript": "~5.9.3"
59
+ },
60
+ "peerDependencies": {},
61
+ "dependencies": {}
17
62
  }