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.
@@ -0,0 +1,248 @@
1
+ import { isBigInt, isFunction, isIterable, isNumber } from "./guard";
2
+ import { useCompare } from "./hook";
3
+ import { Semantic } from "./semantic";
4
+ import { invalidate, validate } from "./utility";
5
+ export let blob = (blob, chunk = 64n * 1024n) => {
6
+ let size = Number(chunk);
7
+ if (size <= 0) {
8
+ throw new RangeError("Chunk size must be positive.");
9
+ }
10
+ if (invalidate(blob)) {
11
+ throw new TypeError("Blob is invalid.");
12
+ }
13
+ return new Semantic((accept, interrupt) => {
14
+ let index = 0n;
15
+ let stoppable = false;
16
+ let stream = blob.stream();
17
+ let reader = stream.getReader();
18
+ let buffer = new Uint8Array(size);
19
+ let offset = 0;
20
+ let read = async (reader) => {
21
+ if (stoppable) {
22
+ return reader.cancel().finally(() => reader.releaseLock());
23
+ }
24
+ return reader.read().then((result) => {
25
+ if (result.done) {
26
+ if (offset > 0) {
27
+ const element = buffer.subarray(0, offset);
28
+ if (interrupt(element, index)) {
29
+ stoppable = true;
30
+ }
31
+ else {
32
+ accept(element, index);
33
+ index++;
34
+ }
35
+ }
36
+ reader.releaseLock();
37
+ return;
38
+ }
39
+ let chunkData = result.value;
40
+ let length = chunkData.length;
41
+ let remaining = size - offset;
42
+ if (length > remaining) {
43
+ buffer.set(chunkData.subarray(0, remaining), offset);
44
+ if (interrupt(buffer, index)) {
45
+ stoppable = true;
46
+ }
47
+ else {
48
+ accept(buffer, index);
49
+ index++;
50
+ }
51
+ offset = 0;
52
+ const leftover = chunkData.subarray(remaining);
53
+ if (leftover.length > 0) {
54
+ buffer.set(leftover, offset);
55
+ offset += leftover.length;
56
+ if (offset === size) {
57
+ if (interrupt(buffer, index)) {
58
+ stoppable = true;
59
+ }
60
+ else {
61
+ accept(buffer, index);
62
+ index++;
63
+ offset = 0;
64
+ }
65
+ }
66
+ }
67
+ }
68
+ else {
69
+ buffer.set(chunkData, offset);
70
+ offset += length;
71
+ if (offset === size) {
72
+ if (interrupt(buffer, index)) {
73
+ stoppable = true;
74
+ }
75
+ else {
76
+ accept(buffer, index);
77
+ index++;
78
+ offset = 0;
79
+ }
80
+ }
81
+ }
82
+ if (!stoppable) {
83
+ return read(reader);
84
+ }
85
+ else {
86
+ return reader.cancel().finally(() => reader.releaseLock());
87
+ }
88
+ }).catch((error) => {
89
+ reader.releaseLock();
90
+ throw error;
91
+ });
92
+ };
93
+ read(reader).catch(() => {
94
+ reader.releaseLock();
95
+ });
96
+ });
97
+ };
98
+ export let empty = () => {
99
+ return new Semantic(() => { });
100
+ };
101
+ export let fill = (element, count) => {
102
+ if (validate(element) && count > 0n) {
103
+ return new Semantic((accept, interrupt) => {
104
+ for (let i = 0n; i < count; i++) {
105
+ let item = isFunction(element) ? element() : element;
106
+ if (interrupt(item, i)) {
107
+ break;
108
+ }
109
+ accept(item, i);
110
+ }
111
+ });
112
+ }
113
+ throw new TypeError("Invalid arguments.");
114
+ };
115
+ export let from = (iterable) => {
116
+ if (isIterable(iterable)) {
117
+ return new Semantic((accept, interrupt) => {
118
+ let index = 0n;
119
+ for (let element of iterable) {
120
+ if (interrupt(element, index)) {
121
+ break;
122
+ }
123
+ accept(element, index);
124
+ index++;
125
+ }
126
+ });
127
+ }
128
+ throw new TypeError("Invalid arguments");
129
+ };
130
+ export let generate = (supplier, interrupt) => {
131
+ if (isFunction(supplier) && isFunction(interrupt)) {
132
+ return new Semantic((accept, interrupt) => {
133
+ let index = 0n;
134
+ while (true) {
135
+ let element = supplier();
136
+ if (interrupt(element, index)) {
137
+ break;
138
+ }
139
+ accept(element, index);
140
+ index++;
141
+ }
142
+ });
143
+ }
144
+ throw new TypeError("Invalid arguments");
145
+ };
146
+ export let interval = (period, delay = 0) => {
147
+ if (period > 0 && delay >= 0) {
148
+ return new Semantic((accept, interrupt) => {
149
+ if (delay > 0) {
150
+ setTimeout(() => {
151
+ let count = 0;
152
+ let index = 0n;
153
+ let timer = setInterval(() => {
154
+ if (interrupt(count, index)) {
155
+ clearInterval(timer);
156
+ }
157
+ accept(count, BigInt(index));
158
+ index++;
159
+ count += period;
160
+ }, period);
161
+ }, delay);
162
+ }
163
+ else {
164
+ let count = 0;
165
+ let index = 0n;
166
+ let timer = setInterval(() => {
167
+ if (interrupt(count, index)) {
168
+ clearInterval(timer);
169
+ }
170
+ accept(count, BigInt(index));
171
+ index++;
172
+ count += period;
173
+ }, period);
174
+ }
175
+ });
176
+ }
177
+ throw new TypeError("Invalid arguments.");
178
+ };
179
+ export let iterate = (generator) => {
180
+ if (isFunction(generator)) {
181
+ return new Semantic(generator);
182
+ }
183
+ throw new TypeError("Invalid arguments.");
184
+ };
185
+ export let range = (start, end, step = 1) => {
186
+ if ((!isNumber(step) && !isBigInt(step)) || (isNumber(step) && useCompare(step, 0) === 0) || (isBigInt(step) && useCompare(step, 0n) === 0)) {
187
+ throw new TypeError("Step must be numeric and cannot be zero.");
188
+ }
189
+ if (isNumber(start) && isNumber(end)) {
190
+ let minimum = start, maximum = end, limit = Number(step);
191
+ let condition = limit > 0 ? (i) => i < maximum : (i) => i > maximum;
192
+ return new Semantic((accept, interrupt) => {
193
+ for (let i = minimum; condition(i); i += limit) {
194
+ let value = i;
195
+ if (interrupt(value, BigInt(i))) {
196
+ break;
197
+ }
198
+ accept(value, BigInt(i));
199
+ }
200
+ });
201
+ }
202
+ throw new TypeError("Invalid arguments.");
203
+ };
204
+ export let websocket = (websocket) => {
205
+ if (invalidate(websocket)) {
206
+ throw new TypeError("WebSocket is invalid.");
207
+ }
208
+ return new Semantic((accept, interrupt) => {
209
+ let index = 0n;
210
+ let stop = false;
211
+ websocket.addEventListener("open", (event) => {
212
+ if (stop || interrupt(event, index)) {
213
+ stop = true;
214
+ }
215
+ else {
216
+ accept(event, index);
217
+ index++;
218
+ }
219
+ });
220
+ websocket.addEventListener("message", (event) => {
221
+ if (stop || interrupt(event, index)) {
222
+ stop = true;
223
+ }
224
+ else {
225
+ accept(event, index);
226
+ index++;
227
+ }
228
+ });
229
+ websocket.addEventListener("error", (event) => {
230
+ if (stop || interrupt(event, index)) {
231
+ stop = true;
232
+ }
233
+ else {
234
+ accept(event, index);
235
+ index++;
236
+ }
237
+ });
238
+ websocket.addEventListener("close", (event) => {
239
+ if (stop || interrupt(event, index)) {
240
+ stop = true;
241
+ }
242
+ else {
243
+ accept(event, index);
244
+ index++;
245
+ }
246
+ });
247
+ });
248
+ };
@@ -0,0 +1,23 @@
1
+ import type { Collectable, OrderedCollectable, UnorderedCollectable } from "./collectable";
2
+ import type { Collector } from "./collector";
3
+ import type { Semantic } from "./semantic";
4
+ import type { Statistics } from "./statistics";
5
+ import type { MaybePrimitive, Primitive } from "./utility";
6
+ export declare let isBoolean: (t: unknown) => t is boolean;
7
+ export declare let isString: (t: unknown) => t is string;
8
+ export declare let isNumber: (t: unknown) => t is number;
9
+ export declare let isFunction: (t: unknown) => t is Function;
10
+ export declare let isObject: (t: unknown) => t is object;
11
+ export declare let isSymbol: (t: unknown) => t is symbol;
12
+ export declare let isBigInt: (t: unknown) => t is bigint;
13
+ export declare let isPrimitive: (t: MaybePrimitive<unknown>) => t is Primitive;
14
+ export declare let isIterable: (t: unknown) => t is Iterable<unknown>;
15
+ export declare let isSemantic: (t: unknown) => t is Semantic<unknown>;
16
+ export declare let isCollector: (t: unknown) => t is Collector<unknown, unknown, unknown>;
17
+ export declare let isCollectable: (t: unknown) => t is Collectable<unknown>;
18
+ export declare let isOrderedCollectable: (t: unknown) => t is OrderedCollectable<unknown>;
19
+ export declare let isWindowCollectable: (t: unknown) => t is OrderedCollectable<unknown>;
20
+ export declare let isUnorderedCollectable: (t: unknown) => t is UnorderedCollectable<unknown>;
21
+ export declare let isStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
22
+ export declare let isNumericStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
23
+ export declare let isBigIntStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
package/dist/guard.js ADDED
@@ -0,0 +1,85 @@
1
+ import { BigIntStatisticsSymbol, CollectableSymbol, CollectorsSymbol, NumericStatisticsSymbol, OrderedCollectableSymbol, SemanticSymbol, StatisticsSymbol, UnorderedCollectableSymbol } from "./symbol";
2
+ export let isBoolean = (t) => {
3
+ return typeof t === "boolean";
4
+ };
5
+ export let isString = (t) => {
6
+ return typeof t === "string";
7
+ };
8
+ export let isNumber = (t) => {
9
+ return typeof t === "number";
10
+ };
11
+ export let isFunction = (t) => {
12
+ return typeof t === "function";
13
+ };
14
+ export let isObject = (t) => {
15
+ return typeof t === "object" && t !== null;
16
+ };
17
+ export let isSymbol = (t) => {
18
+ return typeof t === "symbol";
19
+ };
20
+ export let isBigInt = (t) => {
21
+ return typeof t === "bigint";
22
+ };
23
+ export let isPrimitive = (t) => {
24
+ return isBoolean(t) || isString(t) || isNumber(t) || isSymbol(t) || isBigInt(t) || isFunction(t);
25
+ };
26
+ export let isIterable = (t) => {
27
+ if (isObject(t)) {
28
+ return isFunction(Reflect.get(t, Symbol.iterator));
29
+ }
30
+ return false;
31
+ };
32
+ export let isSemantic = (t) => {
33
+ if (isObject(t)) {
34
+ return Reflect.get(t, "Semantic") === SemanticSymbol;
35
+ }
36
+ return false;
37
+ };
38
+ export let isCollector = (t) => {
39
+ if (isObject(t)) {
40
+ return Reflect.get(t, "Collector") === CollectorsSymbol;
41
+ }
42
+ return false;
43
+ };
44
+ export let isCollectable = (t) => {
45
+ if (isObject(t)) {
46
+ return Reflect.get(t, "Collectable") === CollectableSymbol;
47
+ }
48
+ return false;
49
+ };
50
+ export let isOrderedCollectable = (t) => {
51
+ if (isObject(t)) {
52
+ return Reflect.get(t, "OrderedCollectable") === OrderedCollectableSymbol;
53
+ }
54
+ return false;
55
+ };
56
+ export let isWindowCollectable = (t) => {
57
+ if (isObject(t)) {
58
+ return Reflect.get(t, "WindowCollectable") === OrderedCollectableSymbol;
59
+ }
60
+ return false;
61
+ };
62
+ export let isUnorderedCollectable = (t) => {
63
+ if (isObject(t)) {
64
+ return Reflect.get(t, "UnorderedCollectable") === UnorderedCollectableSymbol;
65
+ }
66
+ return false;
67
+ };
68
+ export let isStatistics = (t) => {
69
+ if (isObject(t)) {
70
+ return Reflect.get(t, "Statistics") === StatisticsSymbol;
71
+ }
72
+ return false;
73
+ };
74
+ export let isNumericStatistics = (t) => {
75
+ if (isObject(t)) {
76
+ return Reflect.get(t, "NumericStatistics") === NumericStatisticsSymbol;
77
+ }
78
+ return false;
79
+ };
80
+ export let isBigIntStatistics = (t) => {
81
+ if (isObject(t)) {
82
+ return Reflect.get(t, "BigIntStatistics") === BigIntStatisticsSymbol;
83
+ }
84
+ return false;
85
+ };
package/dist/hook.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare let useCompare: <T>(t1: T, t2: T) => number;
2
+ export declare let useRandom: <T = number | bigint>(index: T) => T;
package/dist/hook.js ADDED
@@ -0,0 +1,64 @@
1
+ import { isFunction, isNumber, isPrimitive } from "./guard";
2
+ export let useCompare = (t1, t2) => {
3
+ if (t1 === t2 || Object.is(t1, t2)) {
4
+ return 0;
5
+ }
6
+ if (typeof t1 === typeof t2) {
7
+ switch (typeof t1) {
8
+ case "string":
9
+ return t1.localeCompare(t2);
10
+ case "number":
11
+ return t1 - t2;
12
+ case "bigint":
13
+ return Number(t1 - t2);
14
+ case "boolean":
15
+ return t1 === t2 ? 0 : (t1 ? 1 : -1);
16
+ case "symbol":
17
+ return Object.prototype.toString.call(t1).localeCompare(Object.prototype.toString.call(t2));
18
+ case "function":
19
+ throw new TypeError("Cannot compare functions.");
20
+ case "undefined":
21
+ return 0;
22
+ case "object":
23
+ if (isFunction(Reflect.get(t1, Symbol.toPrimitive)) && isFunction(Reflect.get(t2, Symbol.toPrimitive))) {
24
+ let a = Reflect.apply(Reflect.get(t1, Symbol.toPrimitive), t1, ["default"]);
25
+ let b = Reflect.apply(Reflect.get(t2, Symbol.toPrimitive), t2, ["default"]);
26
+ if (isPrimitive(a) && isPrimitive(b)) {
27
+ return useCompare(a, b);
28
+ }
29
+ }
30
+ let a = Object.prototype.valueOf.call(t1);
31
+ let b = Object.prototype.valueOf.call(t2);
32
+ if (isPrimitive(a) && isPrimitive(b)) {
33
+ return useCompare(a, b);
34
+ }
35
+ return useCompare(Object.prototype.toString.call(t1), Object.prototype.toString.call(t2));
36
+ default:
37
+ throw new TypeError("Invalid type.");
38
+ }
39
+ }
40
+ throw new TypeError("Cannot compare values of different types.");
41
+ };
42
+ export let useRandom = (index) => {
43
+ if (isNumber(index)) {
44
+ let x = Number(index);
45
+ let phi = (1 + Math.sqrt(5)) / 2;
46
+ let vanDerCorput = (base, n) => {
47
+ let result = 0;
48
+ let f = 1 / base;
49
+ let i = n;
50
+ while (i > 0) {
51
+ result += (i % base) * f;
52
+ i = Math.floor(i / base);
53
+ f = f / base;
54
+ }
55
+ return result;
56
+ };
57
+ let h = vanDerCorput(2, x) + vanDerCorput(3, x);
58
+ let golden = (x * phi) % 1;
59
+ let lcg = (1103515245 * x + 12345) % 2147483648;
60
+ let mixed = (h * 0.5 + golden * 0.3 + lcg / 2147483648 * 0.2);
61
+ return (mixed * 1000000);
62
+ }
63
+ throw new TypeError("Invalid input type");
64
+ };
package/dist/index.d.ts CHANGED
@@ -1 +1,10 @@
1
+ export * from "./factory";
2
+ export * from "./utility";
3
+ export * from "./symbol";
4
+ export * from "./hook";
5
+ export * from "./optional";
1
6
  export * from "./semantic";
7
+ export * from "./collectable";
8
+ export * from "./statistics";
9
+ export * from "./collector";
10
+ export * from "./guard";
package/dist/index.js CHANGED
@@ -1 +1,10 @@
1
+ export * from "./factory";
2
+ export * from "./utility";
3
+ export * from "./symbol";
4
+ export * from "./hook";
5
+ export * from "./optional";
1
6
  export * from "./semantic";
7
+ export * from "./collectable";
8
+ export * from "./statistics";
9
+ export * from "./collector";
10
+ export * from "./guard";
@@ -0,0 +1,18 @@
1
+ import { type Consumer, type Functional, type MaybeInvalid, type Predicate, type Runnable } from "./utility";
2
+ export declare class Optional<T> {
3
+ protected value: MaybeInvalid<T>;
4
+ protected readonly Optional: Symbol;
5
+ protected constructor(value: MaybeInvalid<T>);
6
+ filter(predicate: Predicate<T>): Optional<T>;
7
+ get(): T;
8
+ get(defaultValue: T): T;
9
+ ifPresent(action: Consumer<T>): void;
10
+ ifPresent(action: Consumer<T>, elseAction: Runnable): void;
11
+ isEmpty(): boolean;
12
+ isPresent(): boolean;
13
+ map<R>(mapper: Functional<T, R>): Optional<R>;
14
+ static empty<T>(): Optional<T>;
15
+ static of<T>(value: MaybeInvalid<T>): Optional<T>;
16
+ static ofNullable<T>(value?: MaybeInvalid<T>): Optional<T>;
17
+ static ofNonNull<T>(value: T): Optional<T>;
18
+ }
@@ -0,0 +1,65 @@
1
+ import { isFunction } from "./guard";
2
+ import { OptionalSymbol } from "./symbol";
3
+ import { invalidate, validate } from "./utility";
4
+ export class Optional {
5
+ value;
6
+ Optional = OptionalSymbol;
7
+ constructor(value) {
8
+ this.value = value;
9
+ }
10
+ filter(predicate) {
11
+ if (this.isPresent() && isFunction(predicate) && predicate(this.value)) {
12
+ return new Optional(this.value);
13
+ }
14
+ return new Optional((void 0));
15
+ }
16
+ get(defaultValue) {
17
+ if (this.isPresent()) {
18
+ return this.value;
19
+ }
20
+ else {
21
+ if (validate(defaultValue)) {
22
+ return defaultValue;
23
+ }
24
+ else {
25
+ throw new TypeError("Optional is empty.");
26
+ }
27
+ }
28
+ }
29
+ ifPresent(action, elseAction) {
30
+ if (this.isPresent() && isFunction(action)) {
31
+ action(this.value);
32
+ }
33
+ else if (isFunction(elseAction)) {
34
+ elseAction();
35
+ }
36
+ }
37
+ isEmpty() {
38
+ return invalidate(this.value);
39
+ }
40
+ isPresent() {
41
+ return validate(this.value);
42
+ }
43
+ map(mapper) {
44
+ if (this.isPresent() && isFunction(mapper)) {
45
+ return new Optional(mapper(this.value));
46
+ }
47
+ return new Optional(null);
48
+ }
49
+ static empty() {
50
+ return new Optional(null);
51
+ }
52
+ static of(value) {
53
+ return Optional.ofNullable(value);
54
+ }
55
+ static ofNullable(value = (void 0)) {
56
+ return new Optional(value);
57
+ }
58
+ static ofNonNull(value) {
59
+ if (validate(value)) {
60
+ return new Optional(value);
61
+ }
62
+ throw new TypeError("Value is not valid");
63
+ }
64
+ }
65
+ ;