semantic-typescript 0.3.8 → 0.5.0
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 +119 -1
- package/dist/collectable.js +624 -21
- package/dist/collector.d.ts +94 -50
- package/dist/collector.js +128 -110
- package/dist/factory.d.ts +23 -35
- package/dist/factory.js +162 -68
- package/dist/guard.d.ts +43 -28
- package/dist/guard.js +144 -65
- package/dist/hash.d.ts +13 -0
- package/dist/hash.js +205 -0
- package/dist/hook.d.ts +16 -4
- package/dist/hook.js +19 -15
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/map.d.ts +76 -0
- package/dist/map.js +253 -0
- package/dist/node.d.ts +182 -0
- package/dist/node.js +918 -0
- package/dist/optional.d.ts +3 -0
- package/dist/optional.js +26 -23
- package/dist/semantic.d.ts +1 -3
- package/dist/semantic.js +43 -50
- package/dist/set.d.ts +19 -0
- package/dist/set.js +65 -0
- package/dist/statistics.js +21 -15
- package/dist/symbol.d.ts +14 -0
- package/dist/symbol.js +14 -0
- package/dist/tree.d.ts +82 -0
- package/dist/tree.js +257 -0
- package/dist/utility.d.ts +8 -1
- package/dist/utility.js +9 -0
- package/dist/window.js +7 -5
- package/package.json +1 -5
- package/readme.cn.md +381 -636
- package/readme.md +3 -1
package/dist/collector.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { Collectable } from "./collectable";
|
|
2
|
+
import { HashMap } from "./map";
|
|
2
3
|
import { Optional } from "./optional";
|
|
3
4
|
import type { Semantic } from "./semantic";
|
|
5
|
+
import { HashSet } from "./set";
|
|
4
6
|
import { type BiFunctional, type BiPredicate, type Functional, type Predicate, type Supplier, type TriFunctional, type Generator, type TriPredicate, type Consumer, type BiConsumer, type Comparator } from "./utility";
|
|
5
7
|
export declare class Collector<E, A, R> {
|
|
6
|
-
protected
|
|
7
|
-
protected
|
|
8
|
-
protected
|
|
9
|
-
protected
|
|
8
|
+
protected identity: Supplier<A>;
|
|
9
|
+
protected interrupt: Predicate<E> | BiPredicate<E, bigint> | TriPredicate<E, bigint, A>;
|
|
10
|
+
protected accumulator: BiFunctional<A, E, A> | TriFunctional<A, E, bigint, A>;
|
|
11
|
+
protected finisher: Functional<A, R>;
|
|
10
12
|
protected readonly Collector: symbol;
|
|
11
13
|
protected constructor(identity: Supplier<A>, interrupt: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>);
|
|
12
14
|
protected constructor(identity: Supplier<A>, interrupt: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>);
|
|
@@ -20,29 +22,37 @@ export declare class Collector<E, A, R> {
|
|
|
20
22
|
collect(start: bigint, end: bigint): R;
|
|
21
23
|
static full<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
22
24
|
static full<E, A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
23
|
-
static shortable<E, A, R>(identity: Supplier<A>,
|
|
24
|
-
static shortable<E, A, R>(identity: Supplier<A>,
|
|
25
|
-
static shortable<E, A, R>(identity: Supplier<A>,
|
|
26
|
-
static shortable<E, A, R>(identity: Supplier<A>,
|
|
27
|
-
static shortable<E, A, R>(identity: Supplier<A>,
|
|
28
|
-
static shortable<E, A, R>(identity: Supplier<A>,
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
static shortable<E, A, R>(identity: Supplier<A>, interrupt: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
26
|
+
static shortable<E, A, R>(identity: Supplier<A>, interrupt: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
27
|
+
static shortable<E, A, R>(identity: Supplier<A>, interrupt: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
28
|
+
static shortable<E, A, R>(identity: Supplier<A>, interrupt: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
29
|
+
static shortable<E, A, R>(identity: Supplier<A>, interrupt: TriPredicate<E, bigint, A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
30
|
+
static shortable<E, A, R>(identity: Supplier<A>, interrupt: TriPredicate<E, bigint, A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
31
|
+
}
|
|
32
|
+
interface UseAnyMatch {
|
|
33
|
+
<E>(predicate: Predicate<E>): Collector<E, boolean, boolean>;
|
|
34
|
+
<E>(predicate: BiPredicate<E, bigint>): Collector<E, boolean, boolean>;
|
|
35
|
+
}
|
|
36
|
+
export declare let useAnyMatch: UseAnyMatch;
|
|
37
|
+
interface UseAllMatch {
|
|
38
|
+
<E>(predicate: Predicate<E>): Collector<E, boolean, boolean>;
|
|
39
|
+
<E>(predicate: BiPredicate<E, bigint>): Collector<E, boolean, boolean>;
|
|
40
|
+
}
|
|
41
|
+
export declare let useAllMatch: UseAllMatch;
|
|
32
42
|
interface UseCollect {
|
|
33
43
|
<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
34
44
|
<E, A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
35
45
|
<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A> | TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
36
|
-
<E, A, R>(identity: Supplier<A>,
|
|
37
|
-
<E, A, R>(identity: Supplier<A>,
|
|
38
|
-
<E, A, R>(identity: Supplier<A>,
|
|
39
|
-
<E, A, R>(identity: Supplier<A>,
|
|
40
|
-
<E, A, R>(identity: Supplier<A>,
|
|
41
|
-
<E, A, R>(identity: Supplier<A>,
|
|
46
|
+
<E, A, R>(identity: Supplier<A>, interrupt: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
47
|
+
<E, A, R>(identity: Supplier<A>, interrupt: Predicate<E>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
48
|
+
<E, A, R>(identity: Supplier<A>, interrupt: BiPredicate<E, bigint>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
49
|
+
<E, A, R>(identity: Supplier<A>, interrupt: BiPredicate<E, bigint>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
50
|
+
<E, A, R>(identity: Supplier<A>, interrupt: TriPredicate<E, bigint, A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
51
|
+
<E, A, R>(identity: Supplier<A>, interrupt: TriPredicate<E, bigint, A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
|
|
42
52
|
}
|
|
43
53
|
export declare let useCollect: UseCollect;
|
|
44
54
|
export declare let useCount: <E = unknown>() => Collector<E, bigint, bigint>;
|
|
45
|
-
|
|
55
|
+
interface UseError {
|
|
46
56
|
<E = unknown>(): Collector<E, string, string>;
|
|
47
57
|
<E = unknown>(accumulator: BiFunctional<string, E, string>): Collector<E, string, string>;
|
|
48
58
|
<E = unknown>(accumulator: TriFunctional<string, E, bigint, string>): Collector<E, string, string>;
|
|
@@ -50,28 +60,46 @@ export interface UseError {
|
|
|
50
60
|
<E = unknown>(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): Collector<E, string, string>;
|
|
51
61
|
}
|
|
52
62
|
export declare let useError: UseError;
|
|
63
|
+
interface UseFindAt {
|
|
64
|
+
<E>(index: number): Collector<E, Array<E>, Optional<E>>;
|
|
65
|
+
<E>(index: bigint): Collector<E, Array<E>, Optional<E>>;
|
|
66
|
+
}
|
|
67
|
+
export declare let useFindAt: UseFindAt;
|
|
53
68
|
export declare let useFindFirst: <E>() => Collector<E, Optional<E>, Optional<E>>;
|
|
54
69
|
export declare let useFindAny: <E>() => Collector<E, Optional<E>, Optional<E>>;
|
|
55
70
|
export declare let useFindLast: <E>() => Collector<E, Optional<E>, Optional<E>>;
|
|
56
|
-
|
|
71
|
+
interface UseFindMaximum {
|
|
57
72
|
<E>(): Collector<E, Optional<E>, Optional<E>>;
|
|
58
73
|
<E>(comparator: Comparator<E>): Collector<E, Optional<E>, Optional<E>>;
|
|
59
74
|
}
|
|
60
75
|
export declare let useFindMaximum: UseFindMaximum;
|
|
61
|
-
|
|
76
|
+
interface UseFindMinimum {
|
|
62
77
|
<E>(): Collector<E, Optional<E>, Optional<E>>;
|
|
63
78
|
<E>(comparator: Comparator<E>): Collector<E, Optional<E>, Optional<E>>;
|
|
64
79
|
}
|
|
65
80
|
export declare let useFindMinimum: UseFindMinimum;
|
|
66
|
-
|
|
81
|
+
interface UseForEach {
|
|
67
82
|
<E>(action: Consumer<E>): Collector<E, bigint, bigint>;
|
|
68
83
|
<E>(action: BiConsumer<E, bigint>): Collector<E, bigint, bigint>;
|
|
69
84
|
}
|
|
70
85
|
export declare let useForEach: UseForEach;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
86
|
+
interface UseNonMatch {
|
|
87
|
+
<E>(predicate: Predicate<E>): Collector<E, boolean, boolean>;
|
|
88
|
+
<E>(predicate: BiPredicate<E, bigint>): Collector<E, boolean, boolean>;
|
|
89
|
+
}
|
|
90
|
+
export declare let useNoneMatch: UseNonMatch;
|
|
91
|
+
interface UseGroup {
|
|
92
|
+
<E, K>(classifier: Functional<E, K>): Collector<E, Map<K, E[]>, Map<K, E[]>>;
|
|
93
|
+
<E, K>(classifier: BiFunctional<E, bigint, K>): Collector<E, Map<K, E[]>, Map<K, E[]>>;
|
|
94
|
+
}
|
|
95
|
+
export declare let useGroup: UseGroup;
|
|
96
|
+
interface UseGroupBy {
|
|
97
|
+
<E, K>(keyExtractor: Functional<E, K>): Collector<E, Map<K, E[]>, Map<K, E[]>>;
|
|
98
|
+
<E, K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Collector<E, Map<K, V[]>, Map<K, V[]>>;
|
|
99
|
+
<E, K, V>(keyExtractor: BiFunctional<E, bigint, K>, valueExtractor: BiFunctional<E, bigint, V>): Collector<E, Map<K, V[]>, Map<K, V[]>>;
|
|
100
|
+
}
|
|
101
|
+
export declare let useGroupBy: UseGroupBy;
|
|
102
|
+
interface UseJoin {
|
|
75
103
|
<E = unknown>(): Collector<E, string, string>;
|
|
76
104
|
<E = unknown>(delimiter: string): Collector<E, string, string>;
|
|
77
105
|
<E = unknown>(prefix: string, delimiter: string, suffix: string): Collector<E, string, string>;
|
|
@@ -79,7 +107,7 @@ export interface UseJoin {
|
|
|
79
107
|
<E = unknown>(prefiex: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): Collector<E, string, string>;
|
|
80
108
|
}
|
|
81
109
|
export declare let useJoin: UseJoin;
|
|
82
|
-
|
|
110
|
+
interface UseLog {
|
|
83
111
|
<E = unknown>(): Collector<E, string, string>;
|
|
84
112
|
<E = unknown>(accumulator: BiFunctional<string, E, string>): Collector<E, string, string>;
|
|
85
113
|
<E = unknown>(accumulator: TriFunctional<string, E, bigint, string>): Collector<E, string, string>;
|
|
@@ -88,8 +116,12 @@ export interface UseLog {
|
|
|
88
116
|
}
|
|
89
117
|
export declare let useLog: UseLog;
|
|
90
118
|
export declare let usePartition: <E>(count: bigint) => Collector<E, Array<Array<E>>, Array<Array<E>>>;
|
|
91
|
-
|
|
92
|
-
|
|
119
|
+
interface UsePartitionBy {
|
|
120
|
+
<E>(classifier: Functional<E, bigint>): Collector<E, Array<E[]>, Array<E[]>>;
|
|
121
|
+
<E>(classifier: BiFunctional<E, bigint, bigint>): Collector<E, Array<E[]>, Array<E[]>>;
|
|
122
|
+
}
|
|
123
|
+
export declare let usePartitionBy: UsePartitionBy;
|
|
124
|
+
interface UseReduce {
|
|
93
125
|
<E>(accumulator: BiFunctional<E, E, E>): Collector<E, Optional<E>, Optional<E>>;
|
|
94
126
|
<E>(accumulator: TriFunctional<E, E, bigint, E>): Collector<E, Optional<E>, Optional<E>>;
|
|
95
127
|
<E>(identity: E, accumulator: BiFunctional<E, E, E>): Collector<E, E, E>;
|
|
@@ -99,99 +131,111 @@ export interface UseReduce {
|
|
|
99
131
|
}
|
|
100
132
|
export declare let useReduce: UseReduce;
|
|
101
133
|
export declare let useToArray: <E>() => Collector<E, E[], E[]>;
|
|
102
|
-
|
|
134
|
+
interface UseToMap {
|
|
135
|
+
<E, K>(keyExtractor: Functional<E, K>): Collector<E, Map<K, E>, Map<K, E>>;
|
|
136
|
+
<E, K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Collector<E, Map<K, V>, Map<K, V>>;
|
|
137
|
+
<E, K, V>(keyExtractor: BiFunctional<E, bigint, K>, valueExtractor: BiFunctional<E, bigint, V>): Collector<E, Map<K, V>, Map<K, V>>;
|
|
138
|
+
}
|
|
139
|
+
export declare let useToMap: UseToMap;
|
|
140
|
+
interface UseToHashMap {
|
|
141
|
+
<E, K>(keyExtractor: Functional<E, K>): Collector<E, HashMap<K, E>, HashMap<K, E>>;
|
|
142
|
+
<E, K, V>(keyExtractor: Functional<E, K>, valueExtractor: Functional<E, V>): Collector<E, HashMap<K, V>, HashMap<K, V>>;
|
|
143
|
+
<E, K, V>(keyExtractor: BiFunctional<E, bigint, K>, valueExtractor: BiFunctional<E, bigint, V>): Collector<E, HashMap<K, V>, HashMap<K, V>>;
|
|
144
|
+
}
|
|
145
|
+
export declare let useToHashMap: UseToHashMap;
|
|
103
146
|
export declare let useToSet: <E>() => Collector<E, Set<E>, Set<E>>;
|
|
104
|
-
export
|
|
147
|
+
export declare let useToHashSet: <E>() => Collector<E, HashSet<E>, HashSet<E>>;
|
|
148
|
+
interface UseWrite {
|
|
105
149
|
<E, S = string>(stream: WritableStream<S>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
106
150
|
<E, S = string>(stream: WritableStream<S>, accumulator: BiFunctional<WritableStream<S>, E, WritableStream<S>>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
107
151
|
<E, S = string>(stream: WritableStream<S>, accumulator: TriFunctional<WritableStream<S>, E, bigint, WritableStream<S>>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
108
152
|
}
|
|
109
153
|
export declare let useWrite: UseWrite;
|
|
110
|
-
|
|
154
|
+
interface UseNumericSummate {
|
|
111
155
|
<E>(): Collector<E, number, number>;
|
|
112
156
|
<E>(mapper: Functional<E, number>): Collector<E, number, number>;
|
|
113
157
|
}
|
|
114
158
|
export declare let useNumericSummate: UseNumericSummate;
|
|
115
|
-
|
|
159
|
+
interface UseBigIntSummate {
|
|
116
160
|
<E>(): Collector<E, bigint, bigint>;
|
|
117
161
|
<E>(mapper: Functional<E, bigint>): Collector<E, bigint, bigint>;
|
|
118
162
|
}
|
|
119
163
|
export declare let useBigIntSummate: UseBigIntSummate;
|
|
120
|
-
|
|
164
|
+
interface UseNumericAverage {
|
|
121
165
|
<E>(): Collector<E, NumericAverageAccumulator, number>;
|
|
122
166
|
<E>(mapper: Functional<E, number>): Collector<E, NumericAverageAccumulator, number>;
|
|
123
167
|
}
|
|
124
|
-
|
|
168
|
+
interface NumericAverageAccumulator {
|
|
125
169
|
summate: number;
|
|
126
170
|
count: number;
|
|
127
171
|
}
|
|
128
172
|
export declare let useNumericAverage: UseNumericAverage;
|
|
129
|
-
|
|
173
|
+
interface UseBigIntAverage {
|
|
130
174
|
<E>(): Collector<E, BigIntAverageAccumulator, bigint>;
|
|
131
175
|
<E>(mapper: Functional<E, bigint>): Collector<E, BigIntAverageAccumulator, bigint>;
|
|
132
176
|
}
|
|
133
|
-
|
|
177
|
+
interface BigIntAverageAccumulator {
|
|
134
178
|
summate: bigint;
|
|
135
179
|
count: bigint;
|
|
136
180
|
}
|
|
137
181
|
export declare let useBigIntAverage: UseBigIntAverage;
|
|
138
182
|
export declare let useFrequency: <E>() => Collector<E, Map<E, bigint>, Map<E, bigint>>;
|
|
139
|
-
|
|
183
|
+
interface UseNumericMode {
|
|
140
184
|
<E>(): Collector<E, Map<number, bigint>, number>;
|
|
141
185
|
<E>(mapper: Functional<E, number>): Collector<E, Map<number, bigint>, number>;
|
|
142
186
|
}
|
|
143
187
|
export declare let useNumericMode: UseNumericMode;
|
|
144
|
-
|
|
188
|
+
interface UseBigIntMode {
|
|
145
189
|
<E>(): Collector<E, Map<bigint, bigint>, bigint>;
|
|
146
190
|
<E>(mapper: Functional<E, bigint>): Collector<E, Map<bigint, bigint>, bigint>;
|
|
147
191
|
}
|
|
148
192
|
export declare let useBigIntMode: UseBigIntMode;
|
|
149
|
-
|
|
193
|
+
interface UseNumericVariance {
|
|
150
194
|
<E>(): Collector<E, VarianceAccumulator, number>;
|
|
151
195
|
<E>(mapper: Functional<E, number>): Collector<E, VarianceAccumulator, number>;
|
|
152
196
|
}
|
|
153
|
-
|
|
197
|
+
interface VarianceAccumulator {
|
|
154
198
|
summate: number;
|
|
155
199
|
summateOfSquares: number;
|
|
156
200
|
count: number;
|
|
157
201
|
}
|
|
158
202
|
export declare let useNumericVariance: UseNumericVariance;
|
|
159
|
-
|
|
203
|
+
interface UseBigIntVariance {
|
|
160
204
|
<E>(): Collector<E, BigIntVarianceAccumulator, bigint>;
|
|
161
205
|
<E>(mapper: Functional<E, bigint>): Collector<E, BigIntVarianceAccumulator, bigint>;
|
|
162
206
|
}
|
|
163
|
-
|
|
207
|
+
interface BigIntVarianceAccumulator {
|
|
164
208
|
summate: bigint;
|
|
165
209
|
summateOfSquares: bigint;
|
|
166
210
|
count: bigint;
|
|
167
211
|
}
|
|
168
212
|
export declare let useBigIntVariance: UseBigIntVariance;
|
|
169
|
-
|
|
213
|
+
interface UseNumericStandardDeviation {
|
|
170
214
|
<E>(): Collector<E, StandardDeviationAccumulator, number>;
|
|
171
215
|
<E>(mapper: Functional<E, number>): Collector<E, StandardDeviationAccumulator, number>;
|
|
172
216
|
}
|
|
173
|
-
|
|
217
|
+
interface StandardDeviationAccumulator {
|
|
174
218
|
summate: number;
|
|
175
219
|
summateOfSquares: number;
|
|
176
220
|
count: number;
|
|
177
221
|
}
|
|
178
222
|
export declare let useNumericStandardDeviation: UseNumericStandardDeviation;
|
|
179
|
-
|
|
223
|
+
interface UseBigIntStandardDeviation {
|
|
180
224
|
<E>(): Collector<E, BigIntStandardDeviationAccumulator, bigint>;
|
|
181
225
|
<E>(mapper: Functional<E, bigint>): Collector<E, BigIntStandardDeviationAccumulator, bigint>;
|
|
182
226
|
}
|
|
183
|
-
|
|
227
|
+
interface BigIntStandardDeviationAccumulator {
|
|
184
228
|
summate: bigint;
|
|
185
229
|
summateOfSquares: bigint;
|
|
186
230
|
count: bigint;
|
|
187
231
|
}
|
|
188
232
|
export declare let useBigIntStandardDeviation: UseBigIntStandardDeviation;
|
|
189
|
-
|
|
233
|
+
interface UseNumericMedian {
|
|
190
234
|
<E>(): Collector<E, number[], number>;
|
|
191
235
|
<E>(mapper: Functional<E, number>): Collector<E, number[], number>;
|
|
192
236
|
}
|
|
193
237
|
export declare let useNumericMedian: UseNumericMedian;
|
|
194
|
-
|
|
238
|
+
interface UseBigIntMedian {
|
|
195
239
|
<E>(): Collector<E, bigint[], bigint>;
|
|
196
240
|
<E>(mapper: Functional<E, bigint>): Collector<E, bigint[], bigint>;
|
|
197
241
|
}
|
package/dist/collector.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { isBigInt, isBoolean, isCollectable, isFunction, isIterable, isNumber, isObject, isSemantic, isString } from "./guard";
|
|
2
2
|
import { useCompare, useToBigInt, useToNumber } from "./hook";
|
|
3
|
+
import { HashMap } from "./map";
|
|
3
4
|
import { Optional } from "./optional";
|
|
5
|
+
import { HashSet } from "./set";
|
|
4
6
|
import { CollectableSymbol } from "./symbol";
|
|
5
7
|
import { validate, invalidate } from "./utility";
|
|
6
8
|
export class Collector {
|
|
@@ -17,34 +19,34 @@ export class Collector {
|
|
|
17
19
|
this.finisher = finisher;
|
|
18
20
|
Object.defineProperties(this, {
|
|
19
21
|
"identity": {
|
|
20
|
-
enumerable: false,
|
|
21
|
-
writable: false,
|
|
22
|
-
configurable: false,
|
|
23
22
|
value: identity,
|
|
23
|
+
writable: false,
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: false
|
|
24
26
|
},
|
|
25
27
|
"interrupt": {
|
|
26
|
-
enumerable: false,
|
|
27
|
-
writable: false,
|
|
28
|
-
configurable: false,
|
|
29
28
|
value: interrupt,
|
|
29
|
+
writable: false,
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: false
|
|
30
32
|
},
|
|
31
33
|
"accumulator": {
|
|
32
|
-
enumerable: false,
|
|
33
|
-
writable: false,
|
|
34
|
-
configurable: false,
|
|
35
34
|
value: accumulator,
|
|
35
|
+
writable: false,
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: false
|
|
36
38
|
},
|
|
37
39
|
"finisher": {
|
|
38
|
-
enumerable: false,
|
|
39
|
-
writable: false,
|
|
40
|
-
configurable: false,
|
|
41
40
|
value: finisher,
|
|
41
|
+
writable: false,
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: false
|
|
42
44
|
},
|
|
43
45
|
"Collector": {
|
|
44
|
-
enumerable: false,
|
|
45
|
-
writable: false,
|
|
46
|
-
configurable: false,
|
|
47
46
|
value: CollectableSymbol,
|
|
47
|
+
writable: false,
|
|
48
|
+
enumerable: false,
|
|
49
|
+
configurable: false
|
|
48
50
|
}
|
|
49
51
|
});
|
|
50
52
|
Object.freeze(this);
|
|
@@ -57,98 +59,68 @@ export class Collector {
|
|
|
57
59
|
let accumulator = this.identity();
|
|
58
60
|
let count = 0n;
|
|
59
61
|
if (isFunction(argument1)) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
throw new Error("Uncaught error on collect.");
|
|
69
|
-
}
|
|
62
|
+
let generator = argument1;
|
|
63
|
+
generator((element, index) => {
|
|
64
|
+
accumulator = this.accumulator(accumulator, element, index);
|
|
65
|
+
count++;
|
|
66
|
+
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
70
67
|
}
|
|
71
68
|
else if (isIterable(argument1)) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
accumulator = this.accumulator(accumulator, element, count);
|
|
80
|
-
count++;
|
|
81
|
-
index++;
|
|
69
|
+
let iterable = argument1;
|
|
70
|
+
let index = 0n;
|
|
71
|
+
for (let element of iterable) {
|
|
72
|
+
if (this.interrupt(element, index, accumulator)) {
|
|
73
|
+
break;
|
|
82
74
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
75
|
+
accumulator = this.accumulator(accumulator, element, count);
|
|
76
|
+
count++;
|
|
77
|
+
index++;
|
|
86
78
|
}
|
|
87
79
|
}
|
|
88
80
|
else if (isSemantic(argument1)) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
throw new TypeError("Invalid arguments");
|
|
100
|
-
}
|
|
81
|
+
let semantic = argument1;
|
|
82
|
+
let generator = Reflect.get(semantic, "generator");
|
|
83
|
+
if (isFunction(generator)) {
|
|
84
|
+
generator((element, index) => {
|
|
85
|
+
accumulator = this.accumulator(accumulator, element, index);
|
|
86
|
+
count++;
|
|
87
|
+
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
101
88
|
}
|
|
102
|
-
|
|
103
|
-
throw new
|
|
89
|
+
else {
|
|
90
|
+
throw new TypeError("Invalid arguments");
|
|
104
91
|
}
|
|
105
92
|
}
|
|
106
93
|
else if (isCollectable(argument1)) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
catch (error) {
|
|
119
|
-
throw new Error("Uncaught error on collect.");
|
|
94
|
+
let collectable = argument1;
|
|
95
|
+
let source = collectable.source();
|
|
96
|
+
if (isFunction(source)) {
|
|
97
|
+
let generator = source;
|
|
98
|
+
generator((element, index) => {
|
|
99
|
+
accumulator = this.accumulator(accumulator, element, index);
|
|
100
|
+
count++;
|
|
101
|
+
}, (element, index) => this.interrupt(element, index, accumulator));
|
|
120
102
|
}
|
|
121
103
|
}
|
|
122
104
|
else if (isNumber(argument1) && isNumber(argument2)) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
break;
|
|
129
|
-
}
|
|
130
|
-
accumulator = this.accumulator(accumulator, i, count);
|
|
131
|
-
count++;
|
|
105
|
+
let start = argument1 < argument2 ? argument1 : argument2;
|
|
106
|
+
let end = argument1 > argument2 ? argument1 : argument2;
|
|
107
|
+
for (let i = start; i < end; i++) {
|
|
108
|
+
if (this.interrupt(i, count, accumulator)) {
|
|
109
|
+
break;
|
|
132
110
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
throw new Error("Uncaught error on collect.");
|
|
111
|
+
accumulator = this.accumulator(accumulator, i, count);
|
|
112
|
+
count++;
|
|
136
113
|
}
|
|
137
114
|
}
|
|
138
115
|
else if (isBigInt(argument1) && isBigInt(argument2)) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
146
|
-
accumulator = this.accumulator(accumulator, i, count);
|
|
147
|
-
count++;
|
|
116
|
+
let start = argument1 < argument2 ? argument1 : argument2;
|
|
117
|
+
let end = argument1 > argument2 ? argument1 : argument2;
|
|
118
|
+
for (let i = start; i < end; i++) {
|
|
119
|
+
if (this.interrupt(i, count, accumulator)) {
|
|
120
|
+
break;
|
|
148
121
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
throw new Error("Uncaught error on collect.");
|
|
122
|
+
accumulator = this.accumulator(accumulator, i, count);
|
|
123
|
+
count++;
|
|
152
124
|
}
|
|
153
125
|
}
|
|
154
126
|
return this.finisher(accumulator);
|
|
@@ -156,23 +128,22 @@ export class Collector {
|
|
|
156
128
|
static full(identity, accumulator, finisher) {
|
|
157
129
|
return new Collector(identity, () => false, accumulator, finisher);
|
|
158
130
|
}
|
|
159
|
-
static shortable(identity,
|
|
160
|
-
return new Collector(identity,
|
|
131
|
+
static shortable(identity, interrupt, accumulator, finisher) {
|
|
132
|
+
return new Collector(identity, interrupt, accumulator, finisher);
|
|
161
133
|
}
|
|
162
134
|
}
|
|
163
135
|
;
|
|
164
|
-
|
|
165
|
-
Object.freeze(Collector.prototype);
|
|
166
|
-
Object.freeze(Object.getPrototypeOf(Collector));
|
|
136
|
+
;
|
|
167
137
|
export let useAnyMatch = (predicate) => {
|
|
168
138
|
if (isFunction(predicate)) {
|
|
169
|
-
return Collector.shortable(() => false, (_element, _index, accumulator) => isBoolean(accumulator) && accumulator, (accumulator, element) => accumulator || predicate(element), (accumulator) => accumulator);
|
|
139
|
+
return Collector.shortable(() => false, (_element, _index, accumulator) => isBoolean(accumulator) && accumulator, (accumulator, element, index) => accumulator || predicate(element, index), (accumulator) => accumulator);
|
|
170
140
|
}
|
|
171
141
|
throw new TypeError("Predicate must be a function.");
|
|
172
142
|
};
|
|
143
|
+
;
|
|
173
144
|
export let useAllMatch = (predicate) => {
|
|
174
145
|
if (isFunction(predicate)) {
|
|
175
|
-
return Collector.shortable(() => true, (_element, _index, accumulator) => isBoolean(accumulator) && !accumulator, (accumulator, element) => accumulator && predicate(element), (accumulator) => accumulator);
|
|
146
|
+
return Collector.shortable(() => true, (_element, _index, accumulator) => isBoolean(accumulator) && !accumulator, (accumulator, element, index) => accumulator && predicate(element, index), (accumulator) => accumulator);
|
|
176
147
|
}
|
|
177
148
|
throw new TypeError("Predicate must be a function.");
|
|
178
149
|
};
|
|
@@ -231,6 +202,31 @@ export let useError = (argument1, argument2, argument3) => {
|
|
|
231
202
|
});
|
|
232
203
|
}
|
|
233
204
|
};
|
|
205
|
+
;
|
|
206
|
+
export let useFindAt = (index) => {
|
|
207
|
+
let target = useToBigInt(index);
|
|
208
|
+
if (target < 0n) {
|
|
209
|
+
return Collector.full(() => [], (accumulator, element) => {
|
|
210
|
+
accumulator.push(element);
|
|
211
|
+
return accumulator;
|
|
212
|
+
}, (accumulator) => {
|
|
213
|
+
if (accumulator.length === 0) {
|
|
214
|
+
return Optional.empty();
|
|
215
|
+
}
|
|
216
|
+
let limited = (((BigInt(accumulator.length)) % target) + target) % target;
|
|
217
|
+
return Optional.ofNullable(accumulator[Number(limited)]);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
return Collector.shortable(() => [], (_element, _index, accumulator) => BigInt(accumulator.length) - 1n === target, (accumulator, element) => {
|
|
221
|
+
accumulator.push(element);
|
|
222
|
+
return accumulator;
|
|
223
|
+
}, (accumulator) => {
|
|
224
|
+
if (accumulator.length === 0) {
|
|
225
|
+
return Optional.empty();
|
|
226
|
+
}
|
|
227
|
+
return Optional.ofNullable(accumulator[Number(target)]);
|
|
228
|
+
});
|
|
229
|
+
};
|
|
234
230
|
export let useFindFirst = () => {
|
|
235
231
|
return Collector.shortable(() => Optional.empty(), (_element, _index, accumulator) => validate(accumulator) && accumulator.isPresent(), (accumulator, element) => {
|
|
236
232
|
if (validate(accumulator) && accumulator.isPresent()) {
|
|
@@ -281,16 +277,18 @@ export let useForEach = (action) => {
|
|
|
281
277
|
}
|
|
282
278
|
throw new TypeError("Action must be a function.");
|
|
283
279
|
};
|
|
280
|
+
;
|
|
284
281
|
export let useNoneMatch = (predicate) => {
|
|
285
282
|
if (isFunction(predicate)) {
|
|
286
|
-
return Collector.shortable(() => true, (_element, _index, accumulator) => !accumulator, (accumulator, element) => accumulator && !predicate(element), (accumulator) => !accumulator);
|
|
283
|
+
return Collector.shortable(() => true, (_element, _index, accumulator) => !accumulator, (accumulator, element, index) => accumulator && !predicate(element, index), (accumulator) => !accumulator);
|
|
287
284
|
}
|
|
288
285
|
throw new TypeError("Predicate must be a function.");
|
|
289
286
|
};
|
|
287
|
+
;
|
|
290
288
|
export let useGroup = (classifier) => {
|
|
291
289
|
if (isFunction(classifier)) {
|
|
292
|
-
return Collector.full(() => new Map(), (accumulator, element) => {
|
|
293
|
-
let key = classifier(element);
|
|
290
|
+
return Collector.full(() => new Map(), (accumulator, element, index) => {
|
|
291
|
+
let key = classifier(element, index);
|
|
294
292
|
let group = accumulator.get(key) || [];
|
|
295
293
|
group.push(element);
|
|
296
294
|
accumulator.set(key, group);
|
|
@@ -299,12 +297,12 @@ export let useGroup = (classifier) => {
|
|
|
299
297
|
}
|
|
300
298
|
throw new TypeError("Classifier must be a function.");
|
|
301
299
|
};
|
|
302
|
-
export let useGroupBy = (keyExtractor, valueExtractor) => {
|
|
300
|
+
export let useGroupBy = (keyExtractor, valueExtractor = (element) => element) => {
|
|
303
301
|
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
304
|
-
return Collector.full(() => new Map(), (accumulator, element) => {
|
|
305
|
-
let key = keyExtractor(element);
|
|
302
|
+
return Collector.full(() => new Map(), (accumulator, element, index) => {
|
|
303
|
+
let key = keyExtractor(element, index);
|
|
306
304
|
let group = accumulator.get(key) || [];
|
|
307
|
-
group.push(valueExtractor(element));
|
|
305
|
+
group.push(valueExtractor(element, index));
|
|
308
306
|
accumulator.set(key, group);
|
|
309
307
|
return accumulator;
|
|
310
308
|
}, (accumulator) => accumulator);
|
|
@@ -388,13 +386,14 @@ export let usePartition = (count) => {
|
|
|
388
386
|
}
|
|
389
387
|
throw new TypeError("Count must be a BigInt.");
|
|
390
388
|
};
|
|
389
|
+
;
|
|
391
390
|
export let usePartitionBy = (classifier) => {
|
|
392
391
|
if (isFunction(classifier)) {
|
|
393
392
|
return Collector.full(() => {
|
|
394
393
|
return [];
|
|
395
|
-
}, (array, element) => {
|
|
396
|
-
let
|
|
397
|
-
while (
|
|
394
|
+
}, (array, element, index) => {
|
|
395
|
+
let resolved = classifier(element, index);
|
|
396
|
+
while (resolved > BigInt(array.length) - 1n) {
|
|
398
397
|
array.push([]);
|
|
399
398
|
}
|
|
400
399
|
array[Number(index)].push(element);
|
|
@@ -440,11 +439,24 @@ export let useToArray = () => {
|
|
|
440
439
|
return array;
|
|
441
440
|
}, (array) => array);
|
|
442
441
|
};
|
|
443
|
-
|
|
442
|
+
;
|
|
443
|
+
export let useToMap = (keyExtractor, valueExtractor = (element) => element) => {
|
|
444
444
|
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
445
|
-
return Collector.full(() => new Map(), (map, element) => {
|
|
446
|
-
let key = keyExtractor(element);
|
|
447
|
-
let value = valueExtractor(element);
|
|
445
|
+
return Collector.full(() => new Map(), (map, element, index) => {
|
|
446
|
+
let key = keyExtractor(element, index);
|
|
447
|
+
let value = valueExtractor(element, index);
|
|
448
|
+
map.set(key, value);
|
|
449
|
+
return map;
|
|
450
|
+
}, (map) => map);
|
|
451
|
+
}
|
|
452
|
+
throw new TypeError("Key extractor and value extractor must be functions.");
|
|
453
|
+
};
|
|
454
|
+
;
|
|
455
|
+
export let useToHashMap = (keyExtractor, valueExtractor = (element) => element) => {
|
|
456
|
+
if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
|
|
457
|
+
return Collector.full(() => new HashMap(), (map, element, index) => {
|
|
458
|
+
let key = keyExtractor(element, index);
|
|
459
|
+
let value = valueExtractor(element, index);
|
|
448
460
|
map.set(key, value);
|
|
449
461
|
return map;
|
|
450
462
|
}, (map) => map);
|
|
@@ -457,6 +469,12 @@ export let useToSet = () => {
|
|
|
457
469
|
return set;
|
|
458
470
|
}, (set) => set);
|
|
459
471
|
};
|
|
472
|
+
export let useToHashSet = () => {
|
|
473
|
+
return Collector.full(() => new HashSet(), (set, element) => {
|
|
474
|
+
set.add(element);
|
|
475
|
+
return set;
|
|
476
|
+
}, (set) => set);
|
|
477
|
+
};
|
|
460
478
|
;
|
|
461
479
|
export let useWrite = (argument1, argument2) => {
|
|
462
480
|
if (isObject(argument1)) {
|