semantic-typescript 0.0.5 → 0.0.7
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/semantic.d.ts +9 -1
- package/dist/semantic.js +181 -19
- package/package.json +2 -2
- /package/{readme.ko.md → readme.kr.md} +0 -0
package/dist/semantic.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export type Valid<T> = T extends null | undefined ? never : T;
|
|
|
3
3
|
export type MaybeInvalid<T> = T | null | undefined;
|
|
4
4
|
export declare let validate: <T>(t: MaybeInvalid<T>) => t is T;
|
|
5
5
|
export declare let invalidate: <T>(t: MaybeInvalid<T>) => t is (null | undefined);
|
|
6
|
+
export type Primitive = string | number | boolean | symbol | bigint | Function | ((...args: any[]) => any);
|
|
7
|
+
export type MaybePrimitive<T> = T | Primitive;
|
|
6
8
|
export declare let isBoolean: (t: unknown) => t is boolean;
|
|
7
9
|
export declare let isString: (t: unknown) => t is string;
|
|
8
10
|
export declare let isNumber: (t: unknown) => t is number;
|
|
@@ -10,6 +12,7 @@ export declare let isFunction: (t: unknown) => t is Function;
|
|
|
10
12
|
export declare let isObject: (t: unknown) => t is object;
|
|
11
13
|
export declare let isSymbol: (t: unknown) => t is symbol;
|
|
12
14
|
export declare let isBigint: (t: unknown) => t is bigint;
|
|
15
|
+
export declare let isPrimitive: (t: MaybePrimitive<unknown>) => t is Primitive;
|
|
13
16
|
export declare let isIterable: (t: unknown) => t is Iterable<unknown>;
|
|
14
17
|
export declare let useCompare: <T>(t1: T, t2: T) => number;
|
|
15
18
|
export declare let useRandom: <T = number | bigint>(index: T) => T;
|
|
@@ -78,11 +81,13 @@ declare class Optional<T> {
|
|
|
78
81
|
static ofNullable<T>(value?: MaybeInvalid<T>): Optional<T>;
|
|
79
82
|
static ofNonNull<T>(value: T): Optional<T>;
|
|
80
83
|
}
|
|
84
|
+
export declare let blob: BiFunctional<Blob, bigint, Semantic<Uint8Array>>;
|
|
81
85
|
export declare let empty: <E>() => Semantic<E>;
|
|
82
86
|
export declare let fill: <E>(element: E | Supplier<E>, count: bigint) => Semantic<E>;
|
|
83
87
|
export declare let from: <E>(iterable: Iterable<E>) => Semantic<E>;
|
|
84
88
|
export declare let range: <N extends number | bigint>(start: N, end: N, step: N) => Semantic<N>;
|
|
85
|
-
export declare
|
|
89
|
+
export declare let iterate: <E>(generator: Generator<E>) => Semantic<E>;
|
|
90
|
+
export declare let websocket: Functional<WebSocket, Semantic<MessageEvent | CloseEvent | Event>>;
|
|
86
91
|
export declare class Semantic<E> {
|
|
87
92
|
protected generator: Generator<E>;
|
|
88
93
|
protected readonly Semantic: Symbol;
|
|
@@ -114,6 +119,7 @@ export declare class Semantic<E> {
|
|
|
114
119
|
toBigintStatistics(): Statistics<E, bigint>;
|
|
115
120
|
toUnoredered(): UnorderedCollectable<E>;
|
|
116
121
|
toWindow(): WindowCollectable<E>;
|
|
122
|
+
translate(offset: number): Semantic<E>;
|
|
117
123
|
translate(offset: bigint): Semantic<E>;
|
|
118
124
|
translate(translator: BiFunctional<E, bigint, bigint>): Semantic<E>;
|
|
119
125
|
}
|
|
@@ -173,6 +179,8 @@ export declare abstract class Collectable<E> {
|
|
|
173
179
|
toArray(): Array<E>;
|
|
174
180
|
toMap<K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Map<K, V>;
|
|
175
181
|
toSet(): Set<E>;
|
|
182
|
+
write(stream: WritableStream<string>): Promise<void>;
|
|
183
|
+
write(stream: WritableStream<Uint8Array>, accumulator: Functional<E, Uint8Array>): Promise<void>;
|
|
176
184
|
}
|
|
177
185
|
export declare class UnorderedCollectable<E> extends Collectable<E> {
|
|
178
186
|
protected generator: Generator<E>;
|
package/dist/semantic.js
CHANGED
|
@@ -25,6 +25,9 @@ export let isSymbol = (t) => {
|
|
|
25
25
|
export let isBigint = (t) => {
|
|
26
26
|
return typeof t === "bigint";
|
|
27
27
|
};
|
|
28
|
+
export let isPrimitive = (t) => {
|
|
29
|
+
return isBoolean(t) || isString(t) || isNumber(t) || isSymbol(t) || isBigint(t) || isFunction(t);
|
|
30
|
+
};
|
|
28
31
|
export let isIterable = (t) => {
|
|
29
32
|
if (isObject(t)) {
|
|
30
33
|
return isFunction(Reflect.get(t, Symbol.iterator));
|
|
@@ -41,7 +44,7 @@ export let useCompare = (t1, t2) => {
|
|
|
41
44
|
case "bigint":
|
|
42
45
|
return Number(t1 - t2);
|
|
43
46
|
case "boolean":
|
|
44
|
-
return t1 ? 1 : -1;
|
|
47
|
+
return t1 === t2 ? 0 : (t1 ? 1 : -1);
|
|
45
48
|
case "symbol":
|
|
46
49
|
return t1.toString().localeCompare(t2.toString());
|
|
47
50
|
case "function":
|
|
@@ -49,15 +52,19 @@ export let useCompare = (t1, t2) => {
|
|
|
49
52
|
case "undefined":
|
|
50
53
|
return 0;
|
|
51
54
|
case "object":
|
|
55
|
+
if (isFunction(Reflect.get(t1, Symbol.toPrimitive)) && isFunction(Reflect.get(t2, Symbol.toPrimitive))) {
|
|
56
|
+
let a = Reflect.apply(Reflect.get(t1, Symbol.toPrimitive), t1, ["default"]);
|
|
57
|
+
let b = Reflect.apply(Reflect.get(t2, Symbol.toPrimitive), t2, ["default"]);
|
|
58
|
+
if (isPrimitive(a) && isPrimitive(b)) {
|
|
59
|
+
return useCompare(a, b);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
52
62
|
let a = Object.prototype.valueOf.call(t1);
|
|
53
63
|
let b = Object.prototype.valueOf.call(t2);
|
|
54
|
-
if (a
|
|
55
|
-
return
|
|
56
|
-
}
|
|
57
|
-
if (a < b) {
|
|
58
|
-
return -1;
|
|
64
|
+
if (isPrimitive(a) && isPrimitive(b)) {
|
|
65
|
+
return useCompare(a, b);
|
|
59
66
|
}
|
|
60
|
-
return
|
|
67
|
+
return useCompare(Object.prototype.toString.call(t1), Object.prototype.toString.call(t2));
|
|
61
68
|
default:
|
|
62
69
|
throw new TypeError("Invalid type.");
|
|
63
70
|
}
|
|
@@ -177,7 +184,7 @@ class Optional {
|
|
|
177
184
|
this.value = value;
|
|
178
185
|
}
|
|
179
186
|
filter(predicate) {
|
|
180
|
-
if (this.isPresent() && predicate(this.value)) {
|
|
187
|
+
if (this.isPresent() && isFunction(predicate) && predicate(this.value)) {
|
|
181
188
|
return new Optional(this.value);
|
|
182
189
|
}
|
|
183
190
|
return new Optional((void 0));
|
|
@@ -198,7 +205,7 @@ class Optional {
|
|
|
198
205
|
throw new TypeError("Default value is not valid");
|
|
199
206
|
}
|
|
200
207
|
ifPresent(action) {
|
|
201
|
-
if (this.isPresent()) {
|
|
208
|
+
if (this.isPresent() && isFunction(action)) {
|
|
202
209
|
action(this.value);
|
|
203
210
|
}
|
|
204
211
|
}
|
|
@@ -209,7 +216,7 @@ class Optional {
|
|
|
209
216
|
return validate(this.value);
|
|
210
217
|
}
|
|
211
218
|
map(mapper) {
|
|
212
|
-
if (this.isPresent()) {
|
|
219
|
+
if (this.isPresent() && isFunction(mapper)) {
|
|
213
220
|
return new Optional(mapper(this.value));
|
|
214
221
|
}
|
|
215
222
|
return new Optional(null);
|
|
@@ -228,6 +235,64 @@ class Optional {
|
|
|
228
235
|
}
|
|
229
236
|
}
|
|
230
237
|
;
|
|
238
|
+
export let blob = (blob, chunk = 64n * 1024n) => {
|
|
239
|
+
let size = Number(chunk);
|
|
240
|
+
if (size <= 0) {
|
|
241
|
+
throw new RangeError("Chunk size must be positive.");
|
|
242
|
+
}
|
|
243
|
+
if (invalidate(blob)) {
|
|
244
|
+
throw new TypeError("Blob is invalid.");
|
|
245
|
+
}
|
|
246
|
+
return new Semantic((accept, interrupt) => {
|
|
247
|
+
let stream = blob.stream();
|
|
248
|
+
let reader = stream.getReader();
|
|
249
|
+
let shouldStop = false;
|
|
250
|
+
let currentIndex = 0n;
|
|
251
|
+
let currentBuffer = new Uint8Array(size);
|
|
252
|
+
let bufferPosition = 0;
|
|
253
|
+
let readNext = () => {
|
|
254
|
+
if (shouldStop) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
reader.read().then((readResult) => {
|
|
258
|
+
if (readResult.done) {
|
|
259
|
+
if (bufferPosition > 0) {
|
|
260
|
+
let finalChunk = currentBuffer.slice(0, bufferPosition);
|
|
261
|
+
if (!interrupt(finalChunk)) {
|
|
262
|
+
accept(finalChunk, currentIndex);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
shouldStop = true;
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
let chunkData = readResult.value;
|
|
269
|
+
let dataPosition = 0;
|
|
270
|
+
while (dataPosition < chunkData.length && !shouldStop) {
|
|
271
|
+
let remainingSpace = size - bufferPosition;
|
|
272
|
+
let bytesToCopy = Math.min(remainingSpace, chunkData.length - dataPosition);
|
|
273
|
+
currentBuffer.set(chunkData.subarray(dataPosition, dataPosition + bytesToCopy), bufferPosition);
|
|
274
|
+
bufferPosition += bytesToCopy;
|
|
275
|
+
dataPosition += bytesToCopy;
|
|
276
|
+
if (bufferPosition === size) {
|
|
277
|
+
let completeChunk = currentBuffer.slice();
|
|
278
|
+
if (!interrupt(completeChunk)) {
|
|
279
|
+
accept(completeChunk, currentIndex);
|
|
280
|
+
}
|
|
281
|
+
currentIndex++;
|
|
282
|
+
bufferPosition = 0;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (!shouldStop && !interrupt(chunkData)) {
|
|
286
|
+
readNext();
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
shouldStop = true;
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
};
|
|
293
|
+
readNext();
|
|
294
|
+
});
|
|
295
|
+
};
|
|
231
296
|
export let empty = () => {
|
|
232
297
|
return new Semantic(() => { });
|
|
233
298
|
};
|
|
@@ -292,9 +357,48 @@ export let range = (start, end, step = (typeof start === 'bigint' ? 1n : 1)) =>
|
|
|
292
357
|
}
|
|
293
358
|
throw new TypeError("Invalid arguments.");
|
|
294
359
|
};
|
|
295
|
-
export
|
|
296
|
-
|
|
297
|
-
|
|
360
|
+
export let iterate = (generator) => {
|
|
361
|
+
if (isFunction(generator)) {
|
|
362
|
+
return new Semantic(generator);
|
|
363
|
+
}
|
|
364
|
+
throw new TypeError("Invalid arguments.");
|
|
365
|
+
};
|
|
366
|
+
export let websocket = (websocket) => {
|
|
367
|
+
if (invalidate(websocket)) {
|
|
368
|
+
throw new TypeError("WebSocket is invalid.");
|
|
369
|
+
}
|
|
370
|
+
return new Semantic((accept, interrupt) => {
|
|
371
|
+
let index = 0n;
|
|
372
|
+
let stop = false;
|
|
373
|
+
websocket.addEventListener("message", (event) => {
|
|
374
|
+
if (stop || interrupt(event)) {
|
|
375
|
+
stop = true;
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
accept(event, index);
|
|
379
|
+
index++;
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
websocket.addEventListener("error", (event) => {
|
|
383
|
+
if (stop || interrupt(event)) {
|
|
384
|
+
stop = true;
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
accept(event, index);
|
|
388
|
+
index++;
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
websocket.addEventListener("close", (event) => {
|
|
392
|
+
if (stop || interrupt(event)) {
|
|
393
|
+
stop = true;
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
accept(event, index);
|
|
397
|
+
index++;
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
};
|
|
298
402
|
export class Semantic {
|
|
299
403
|
generator;
|
|
300
404
|
Semantic = SemanticSymbol;
|
|
@@ -591,18 +695,25 @@ export class Semantic {
|
|
|
591
695
|
toWindow() {
|
|
592
696
|
return new WindowCollectable(this.generator);
|
|
593
697
|
}
|
|
594
|
-
translate(
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
698
|
+
translate(argument1) {
|
|
699
|
+
if (isNumber(argument1)) {
|
|
700
|
+
let offset = argument1;
|
|
701
|
+
return new Semantic((accept, interrupt) => {
|
|
702
|
+
this.generator((element, index) => {
|
|
703
|
+
accept(element, index + BigInt(offset));
|
|
704
|
+
}, interrupt);
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
else if (isBigint(argument1)) {
|
|
708
|
+
let offset = argument1;
|
|
598
709
|
return new Semantic((accept, interrupt) => {
|
|
599
710
|
this.generator((element, index) => {
|
|
600
711
|
accept(element, index + offset);
|
|
601
712
|
}, interrupt);
|
|
602
713
|
});
|
|
603
714
|
}
|
|
604
|
-
else if (isFunction(
|
|
605
|
-
let translator =
|
|
715
|
+
else if (isFunction(argument1)) {
|
|
716
|
+
let translator = argument1;
|
|
606
717
|
return new Semantic((accept, interrupt) => {
|
|
607
718
|
this.generator((element, index) => {
|
|
608
719
|
accept(element, index + translator(element, index));
|
|
@@ -960,6 +1071,57 @@ export class Collectable {
|
|
|
960
1071
|
return result;
|
|
961
1072
|
});
|
|
962
1073
|
}
|
|
1074
|
+
write(stream, accumulator) {
|
|
1075
|
+
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) => {
|
|
1081
|
+
let writer = stream.getWriter();
|
|
1082
|
+
await writer.write(String(element));
|
|
1083
|
+
resolve1(stream);
|
|
1084
|
+
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
|
+
});
|
|
1095
|
+
});
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
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) => {
|
|
1104
|
+
let writer = stream.getWriter();
|
|
1105
|
+
await writer.write(accumulator(element));
|
|
1106
|
+
resolve1(stream);
|
|
1107
|
+
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
|
+
});
|
|
1118
|
+
});
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
else {
|
|
1122
|
+
throw new TypeError("Invalid arguments.");
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
963
1125
|
}
|
|
964
1126
|
export class UnorderedCollectable extends Collectable {
|
|
965
1127
|
generator;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "semantic-typescript",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
-
"files": ["dist"
|
|
7
|
+
"files": ["dist"],
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "tsc",
|
|
10
10
|
"prepublishOnly": "npm run build"
|
|
File without changes
|