semantic-typescript 0.3.3 → 0.3.8
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 +19 -24
- package/dist/collectable.js +373 -175
- package/dist/collector.d.ts +101 -24
- package/dist/collector.js +402 -146
- package/dist/factory.d.ts +38 -8
- package/dist/factory.js +300 -163
- package/dist/guard.d.ts +8 -3
- package/dist/guard.js +29 -5
- package/dist/hook.d.ts +13 -0
- package/dist/hook.js +114 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/optional.js +36 -3
- package/dist/semantic.d.ts +17 -10
- package/dist/semantic.js +355 -195
- package/dist/statistics.d.ts +13 -17
- package/dist/statistics.js +234 -522
- package/dist/utility.d.ts +4 -0
- package/dist/utility.js +2 -1
- package/dist/window.d.ts +12 -0
- package/dist/window.js +70 -0
- package/package.json +1 -1
- package/readme.cn.md +732 -210
- package/readme.md +426 -150
package/dist/guard.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BigIntStatisticsSymbol, CollectableSymbol, CollectorsSymbol, NumericStatisticsSymbol, OrderedCollectableSymbol, SemanticSymbol, StatisticsSymbol, UnorderedCollectableSymbol } from "./symbol";
|
|
1
|
+
import { BigIntStatisticsSymbol, CollectableSymbol, CollectorsSymbol, NumericStatisticsSymbol, OrderedCollectableSymbol, SemanticSymbol, StatisticsSymbol, UnorderedCollectableSymbol, WindowCollectableSymbol } from "./symbol";
|
|
2
2
|
export let isBoolean = (t) => {
|
|
3
3
|
return typeof t === "boolean";
|
|
4
4
|
};
|
|
@@ -6,7 +6,7 @@ export let isString = (t) => {
|
|
|
6
6
|
return typeof t === "string";
|
|
7
7
|
};
|
|
8
8
|
export let isNumber = (t) => {
|
|
9
|
-
return typeof t === "number";
|
|
9
|
+
return typeof t === "number" && !Number.isNaN(t) && Number.isFinite(t);
|
|
10
10
|
};
|
|
11
11
|
export let isFunction = (t) => {
|
|
12
12
|
return typeof t === "function";
|
|
@@ -23,6 +23,12 @@ export let isBigInt = (t) => {
|
|
|
23
23
|
export let isPrimitive = (t) => {
|
|
24
24
|
return isBoolean(t) || isString(t) || isNumber(t) || isSymbol(t) || isBigInt(t) || isFunction(t);
|
|
25
25
|
};
|
|
26
|
+
export let isAsyncIterable = (t) => {
|
|
27
|
+
if (isObject(t)) {
|
|
28
|
+
return isFunction(Reflect.get(t, Symbol.asyncIterator));
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
};
|
|
26
32
|
export let isIterable = (t) => {
|
|
27
33
|
if (isObject(t)) {
|
|
28
34
|
return isFunction(Reflect.get(t, Symbol.iterator));
|
|
@@ -55,7 +61,7 @@ export let isOrderedCollectable = (t) => {
|
|
|
55
61
|
};
|
|
56
62
|
export let isWindowCollectable = (t) => {
|
|
57
63
|
if (isObject(t)) {
|
|
58
|
-
return Reflect.get(t, "WindowCollectable") ===
|
|
64
|
+
return Reflect.get(t, "WindowCollectable") === WindowCollectableSymbol;
|
|
59
65
|
}
|
|
60
66
|
return false;
|
|
61
67
|
};
|
|
@@ -89,9 +95,27 @@ export let isPromise = (t) => {
|
|
|
89
95
|
}
|
|
90
96
|
return false;
|
|
91
97
|
};
|
|
92
|
-
export let
|
|
98
|
+
export let isGenerator = (t) => {
|
|
99
|
+
if (isFunction(t)) {
|
|
100
|
+
return t.length === 2;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
};
|
|
104
|
+
export let isAsyncFunction = (t) => {
|
|
105
|
+
if (isFunction(t)) {
|
|
106
|
+
return Reflect.get(t, Symbol.toStringTag) === "AsyncFunction";
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
};
|
|
110
|
+
export let isGeneratorFunction = (t) => {
|
|
111
|
+
if (isFunction(t)) {
|
|
112
|
+
return Reflect.get(t, Symbol.toStringTag) === "GeneratorFunction";
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
export let isAsyncGeneratorFunction = (t) => {
|
|
93
117
|
if (isFunction(t)) {
|
|
94
|
-
return Reflect.get(t, Symbol.toStringTag) === "
|
|
118
|
+
return Reflect.get(t, Symbol.toStringTag) === "AsyncGeneratorFunction";
|
|
95
119
|
}
|
|
96
120
|
return false;
|
|
97
121
|
};
|
package/dist/hook.d.ts
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
import { type BiPredicate, type DeepPropertyKey, type DeepPropertyValue } from "./utility";
|
|
2
|
+
import type { Comparator, Generator } from "./utility";
|
|
2
3
|
export declare let useCompare: <T>(t1: T, t2: T) => number;
|
|
3
4
|
export declare let useRandom: <T = number | bigint>(index: T) => T;
|
|
4
5
|
export declare let useTraverse: <T extends object>(t: T, callback: BiPredicate<DeepPropertyKey<T>, DeepPropertyValue<T>>) => void;
|
|
6
|
+
export declare let useGenerator: <E>(iterable: Iterable<E>) => Generator<E>;
|
|
7
|
+
interface UseArrange {
|
|
8
|
+
<E>(source: Iterable<E>): Generator<E>;
|
|
9
|
+
<E>(source: Iterable<E>, comparator: Comparator<E>): Generator<E>;
|
|
10
|
+
<E>(source: Generator<E>): Generator<E>;
|
|
11
|
+
<E>(source: Generator<E>, comparator: Comparator<E>): Generator<E>;
|
|
12
|
+
}
|
|
13
|
+
export declare let useArrange: UseArrange;
|
|
14
|
+
export declare let useToNumber: <T = unknown>(target: T) => number;
|
|
15
|
+
export declare let useToBigInt: <T = unknown>(target: T) => bigint;
|
|
16
|
+
export declare let useFunction: <T extends ((...args: any[]) => any)>(target: T) => T;
|
|
17
|
+
export {};
|
package/dist/hook.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useToArray } from "./collector";
|
|
2
|
+
import { isBigInt, isFunction, isIterable, isNumber, isObject, isPrimitive } from "./guard";
|
|
3
|
+
import { invalidate, validate } from "./utility";
|
|
3
4
|
export let useCompare = (t1, t2) => {
|
|
4
5
|
if (t1 === t2 || Object.is(t1, t2)) {
|
|
5
6
|
return 0;
|
|
@@ -109,3 +110,114 @@ export let useTraverse = (t, callback) => {
|
|
|
109
110
|
traverse(t);
|
|
110
111
|
}
|
|
111
112
|
};
|
|
113
|
+
export let useGenerator = (iterable) => {
|
|
114
|
+
if (isIterable(iterable)) {
|
|
115
|
+
return (accept, interrupt) => {
|
|
116
|
+
let index = 0n;
|
|
117
|
+
for (let element of iterable) {
|
|
118
|
+
if (interrupt(element, index)) {
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
accept(element, index);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return () => { };
|
|
126
|
+
};
|
|
127
|
+
;
|
|
128
|
+
export let useArrange = (source, comparator) => {
|
|
129
|
+
if (isIterable(source)) {
|
|
130
|
+
let buffer = [...source];
|
|
131
|
+
if (validate(comparator) && isFunction(comparator)) {
|
|
132
|
+
return useGenerator(buffer.sort(comparator));
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
return useGenerator(buffer.map((element, index) => {
|
|
136
|
+
return {
|
|
137
|
+
element: element,
|
|
138
|
+
index: BigInt(((index % buffer.length) + buffer.length) % buffer.length)
|
|
139
|
+
};
|
|
140
|
+
}).sort((a, b) => {
|
|
141
|
+
return Number(a.index - b.index);
|
|
142
|
+
}).map((indexed) => {
|
|
143
|
+
return indexed.element;
|
|
144
|
+
}));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (isFunction(source)) {
|
|
148
|
+
let collector = useToArray();
|
|
149
|
+
let buffer = collector.collect(source);
|
|
150
|
+
if (validate(comparator) && isFunction(comparator)) {
|
|
151
|
+
return useGenerator(buffer.sort(comparator));
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
return useGenerator(buffer.map((element, index) => {
|
|
155
|
+
return {
|
|
156
|
+
element: element,
|
|
157
|
+
index: BigInt(((index % buffer.length) + buffer.length) % buffer.length)
|
|
158
|
+
};
|
|
159
|
+
}).sort((a, b) => {
|
|
160
|
+
return Number(a.index - b.index);
|
|
161
|
+
}).map((indexed) => {
|
|
162
|
+
return indexed.element;
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return useGenerator([]);
|
|
167
|
+
};
|
|
168
|
+
export let useToNumber = (target) => {
|
|
169
|
+
switch (typeof target) {
|
|
170
|
+
case "number":
|
|
171
|
+
return isNumber(target) ? target : 0;
|
|
172
|
+
case "boolean":
|
|
173
|
+
return target ? 1 : 0;
|
|
174
|
+
case "string":
|
|
175
|
+
let result = Number(target.trim());
|
|
176
|
+
return isNumber(result) ? result : 0;
|
|
177
|
+
case "bigint":
|
|
178
|
+
return Number(target);
|
|
179
|
+
case "object":
|
|
180
|
+
if (invalidate(target)) {
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
if (target instanceof Date) {
|
|
184
|
+
return target.getTime();
|
|
185
|
+
}
|
|
186
|
+
if (Reflect.has(target, Symbol.toPrimitive)) {
|
|
187
|
+
let resolved = Reflect.apply(Reflect.get(target, Symbol.toPrimitive), target, ["default"]);
|
|
188
|
+
return isNumber(resolved) ? resolved : 0;
|
|
189
|
+
}
|
|
190
|
+
return 0;
|
|
191
|
+
default:
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
export let useToBigInt = (target) => {
|
|
196
|
+
switch (typeof target) {
|
|
197
|
+
case "number":
|
|
198
|
+
return isNumber(target) ? BigInt(target) : 0n;
|
|
199
|
+
case "boolean":
|
|
200
|
+
return target ? 1n : 0n;
|
|
201
|
+
case "string":
|
|
202
|
+
let regex = /^[-+]?\d+$/;
|
|
203
|
+
return regex.test(target) ? BigInt(target.trim()) : 0n;
|
|
204
|
+
case "bigint":
|
|
205
|
+
return target;
|
|
206
|
+
case "object":
|
|
207
|
+
if (invalidate(target)) {
|
|
208
|
+
return 0n;
|
|
209
|
+
}
|
|
210
|
+
if (Reflect.has(target, Symbol.toPrimitive)) {
|
|
211
|
+
let resolved = Reflect.apply(Reflect.get(target, Symbol.toPrimitive), target, ["default"]);
|
|
212
|
+
return isBigInt(resolved) ? resolved : BigInt(useToNumber(resolved));
|
|
213
|
+
}
|
|
214
|
+
return 0n;
|
|
215
|
+
default:
|
|
216
|
+
return 0n;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
export let useFunction = (target) => {
|
|
220
|
+
return Object.freeze((...args) => {
|
|
221
|
+
return target(...args);
|
|
222
|
+
});
|
|
223
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/optional.js
CHANGED
|
@@ -7,6 +7,21 @@ export class Optional {
|
|
|
7
7
|
Optional = OptionalSymbol;
|
|
8
8
|
constructor(value) {
|
|
9
9
|
this.value = value;
|
|
10
|
+
Object.defineProperties(this, {
|
|
11
|
+
"value": {
|
|
12
|
+
value: this.value,
|
|
13
|
+
writable: false,
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: false
|
|
16
|
+
},
|
|
17
|
+
"Optional": {
|
|
18
|
+
value: this.Optional,
|
|
19
|
+
writable: false,
|
|
20
|
+
enumerable: false,
|
|
21
|
+
configurable: false
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.freeze(this);
|
|
10
25
|
}
|
|
11
26
|
filter(predicate) {
|
|
12
27
|
if (this.isPresent() && isFunction(predicate) && predicate(this.value)) {
|
|
@@ -29,10 +44,20 @@ export class Optional {
|
|
|
29
44
|
}
|
|
30
45
|
ifPresent(action, elseAction) {
|
|
31
46
|
if (this.isPresent() && isFunction(action)) {
|
|
32
|
-
|
|
47
|
+
try {
|
|
48
|
+
action(this.value);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
throw new Error("Uncaught error on ifPresent.");
|
|
52
|
+
}
|
|
33
53
|
}
|
|
34
54
|
else if (isFunction(elseAction)) {
|
|
35
|
-
|
|
55
|
+
try {
|
|
56
|
+
elseAction();
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
throw new Error("Uncaught error on ifPresent elseAction.");
|
|
60
|
+
}
|
|
36
61
|
}
|
|
37
62
|
}
|
|
38
63
|
isEmpty() {
|
|
@@ -43,7 +68,12 @@ export class Optional {
|
|
|
43
68
|
}
|
|
44
69
|
map(mapper) {
|
|
45
70
|
if (this.isPresent() && isFunction(mapper)) {
|
|
46
|
-
|
|
71
|
+
try {
|
|
72
|
+
return new Optional(mapper(this.value));
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
throw new Error("Uncaught error on map.");
|
|
76
|
+
}
|
|
47
77
|
}
|
|
48
78
|
return new Optional(null);
|
|
49
79
|
}
|
|
@@ -74,3 +104,6 @@ export class Optional {
|
|
|
74
104
|
}
|
|
75
105
|
}
|
|
76
106
|
;
|
|
107
|
+
Object.freeze(Optional);
|
|
108
|
+
Object.freeze(Optional.prototype);
|
|
109
|
+
Object.freeze(Object.getPrototypeOf(Optional));
|
package/dist/semantic.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Collectable, OrderedCollectable, UnorderedCollectable
|
|
1
|
+
import { Collectable, OrderedCollectable, UnorderedCollectable } from "./collectable";
|
|
2
2
|
import { BigIntStatistics, NumericStatistics } from "./statistics";
|
|
3
3
|
import { type Predicate } from "./utility";
|
|
4
|
-
import type { Generator, Functional, BiFunctional, Consumer, BiConsumer, Comparator } from "./utility";
|
|
4
|
+
import type { Generator, Functional, BiFunctional, Consumer, BiConsumer, Comparator, BiPredicate } from "./utility";
|
|
5
|
+
import { WindowCollectable } from "./window";
|
|
5
6
|
export declare class Semantic<E> {
|
|
6
7
|
protected generator: Generator<E>;
|
|
7
8
|
protected readonly Semantic: Symbol;
|
|
@@ -9,14 +10,24 @@ export declare class Semantic<E> {
|
|
|
9
10
|
concat(other: Semantic<E>): Semantic<E>;
|
|
10
11
|
concat(other: Iterable<E>): Semantic<E>;
|
|
11
12
|
distinct(): Semantic<E>;
|
|
12
|
-
distinct(
|
|
13
|
+
distinct<K>(keyExtractor: Functional<E, K>): Semantic<E>;
|
|
14
|
+
distinct<K>(keyExtractor: BiFunctional<E, bigint, K>): Semantic<E>;
|
|
13
15
|
dropWhile(predicate: Predicate<E>): Semantic<E>;
|
|
16
|
+
dropWhile(predicate: BiPredicate<E, bigint>): Semantic<E>;
|
|
14
17
|
filter(predicate: Predicate<E>): Semantic<E>;
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
filter(predicate: BiPredicate<E, bigint>): Semantic<E>;
|
|
19
|
+
flat(mapper: Functional<E, Iterable<E>>): Semantic<E>;
|
|
20
|
+
flat(mapper: BiFunctional<E, bigint, Iterable<E>>): Semantic<E>;
|
|
21
|
+
flat(mapper: Functional<E, Semantic<E>>): Semantic<E>;
|
|
22
|
+
flat(mapper: BiFunctional<E, bigint, Semantic<E>>): Semantic<E>;
|
|
23
|
+
flatMap<R>(mapper: Functional<E, Iterable<R>>): Semantic<R>;
|
|
24
|
+
flatMap<R>(mapper: BiFunctional<E, bigint, Iterable<R>>): Semantic<R>;
|
|
25
|
+
flatMap<R>(mapper: Functional<E, Semantic<R>>): Semantic<R>;
|
|
26
|
+
flatMap<R>(mapper: BiFunctional<E, bigint, Semantic<R>>): Semantic<R>;
|
|
17
27
|
limit(n: number): Semantic<E>;
|
|
18
28
|
limit(n: bigint): Semantic<E>;
|
|
19
29
|
map<R>(mapper: Functional<E, R>): Semantic<R>;
|
|
30
|
+
map<R>(mapper: BiFunctional<E, bigint, R>): Semantic<R>;
|
|
20
31
|
peek(consumer: Consumer<E>): Semantic<E>;
|
|
21
32
|
peek(consumer: BiConsumer<E, bigint>): Semantic<E>;
|
|
22
33
|
redirect(redirector: BiFunctional<E, bigint, bigint>): Semantic<E>;
|
|
@@ -29,6 +40,7 @@ export declare class Semantic<E> {
|
|
|
29
40
|
sorted(comparator: Comparator<E>): OrderedCollectable<E>;
|
|
30
41
|
sub(start: bigint, end: bigint): Semantic<E>;
|
|
31
42
|
takeWhile(predicate: Predicate<E>): Semantic<E>;
|
|
43
|
+
takeWhile(predicate: BiPredicate<E, bigint>): Semantic<E>;
|
|
32
44
|
toCollectable(): Collectable<E>;
|
|
33
45
|
toCollectable<C extends Collectable<E>>(mapper: Functional<Generator<E>, C>): C;
|
|
34
46
|
toBigintStatistics(): BigIntStatistics<E>;
|
|
@@ -40,8 +52,3 @@ export declare class Semantic<E> {
|
|
|
40
52
|
translate(offset: bigint): Semantic<E>;
|
|
41
53
|
translate(translator: BiFunctional<E, bigint, bigint>): Semantic<E>;
|
|
42
54
|
}
|
|
43
|
-
export interface UseTransform {
|
|
44
|
-
<E, R>(generator: Generator<E>, mapper: Functional<E, R>): Generator<R>;
|
|
45
|
-
<E, R>(generator: Generator<E>, mapper: BiFunctional<E, bigint, R>): Generator<R>;
|
|
46
|
-
}
|
|
47
|
-
export declare let useTransform: UseTransform;
|