semantic-typescript 0.3.0 → 0.3.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/collectable.d.ts +19 -24
- package/dist/collectable.js +329 -177
- package/dist/collector.d.ts +93 -16
- package/dist/collector.js +297 -107
- package/dist/factory.d.ts +6 -0
- package/dist/factory.js +224 -148
- package/dist/guard.d.ts +3 -1
- package/dist/guard.js +14 -2
- package/dist/hook.d.ts +14 -2
- package/dist/hook.js +85 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/semantic.d.ts +2 -6
- package/dist/semantic.js +335 -185
- package/dist/statistics.d.ts +13 -17
- package/dist/statistics.js +204 -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 +60 -0
- package/package.json +1 -1
- package/readme.md +273 -47
package/dist/collector.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Collectable } from "./collectable";
|
|
2
2
|
import { Optional } from "./optional";
|
|
3
3
|
import type { Semantic } from "./semantic";
|
|
4
|
-
import { type BiFunctional, type BiPredicate, type Functional, type Predicate, type Supplier, type TriFunctional, type Generator, type TriPredicate, type Consumer, type BiConsumer } from "./utility";
|
|
4
|
+
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
5
|
export declare class Collector<E, A, R> {
|
|
6
6
|
protected identity: Supplier<A>;
|
|
7
7
|
protected interrupt: Predicate<E> | BiPredicate<E, bigint> | TriPredicate<E, bigint, A>;
|
|
@@ -49,10 +49,20 @@ export interface UseError {
|
|
|
49
49
|
<E = unknown>(prefix: string, accumulator: BiFunctional<string, E, string>, suffix: string): Collector<E, string, string>;
|
|
50
50
|
<E = unknown>(prefix: string, accumulator: TriFunctional<string, E, bigint, string>, suffix: string): Collector<E, string, string>;
|
|
51
51
|
}
|
|
52
|
-
export declare let useError:
|
|
52
|
+
export declare let useError: UseError;
|
|
53
53
|
export declare let useFindFirst: <E>() => Collector<E, Optional<E>, Optional<E>>;
|
|
54
54
|
export declare let useFindAny: <E>() => Collector<E, Optional<E>, Optional<E>>;
|
|
55
55
|
export declare let useFindLast: <E>() => Collector<E, Optional<E>, Optional<E>>;
|
|
56
|
+
export interface UseFindMaximum {
|
|
57
|
+
<E>(): Collector<E, Optional<E>, Optional<E>>;
|
|
58
|
+
<E>(comparator: Comparator<E>): Collector<E, Optional<E>, Optional<E>>;
|
|
59
|
+
}
|
|
60
|
+
export declare let useFindMaximum: UseFindMaximum;
|
|
61
|
+
export interface UseFindMinimum {
|
|
62
|
+
<E>(): Collector<E, Optional<E>, Optional<E>>;
|
|
63
|
+
<E>(comparator: Comparator<E>): Collector<E, Optional<E>, Optional<E>>;
|
|
64
|
+
}
|
|
65
|
+
export declare let useFindMinimum: UseFindMinimum;
|
|
56
66
|
export interface UseForEach {
|
|
57
67
|
<E>(action: Consumer<E>): Collector<E, bigint, bigint>;
|
|
58
68
|
<E>(action: BiConsumer<E, bigint>): Collector<E, bigint, bigint>;
|
|
@@ -97,28 +107,95 @@ export interface UseWrite {
|
|
|
97
107
|
<E, S = string>(stream: WritableStream<S>, accumulator: TriFunctional<WritableStream<S>, E, bigint, WritableStream<S>>): Collector<E, Promise<WritableStream<S>>, Promise<WritableStream<S>>>;
|
|
98
108
|
}
|
|
99
109
|
export declare let useWrite: UseWrite;
|
|
100
|
-
export
|
|
110
|
+
export interface UseNumericSummate {
|
|
111
|
+
<E>(): Collector<E, number, number>;
|
|
112
|
+
<E>(mapper: Functional<E, number>): Collector<E, number, number>;
|
|
113
|
+
}
|
|
114
|
+
export declare let useNumericSummate: UseNumericSummate;
|
|
115
|
+
export interface UseBigIntSummate {
|
|
116
|
+
<E>(): Collector<E, bigint, bigint>;
|
|
117
|
+
<E>(mapper: Functional<E, bigint>): Collector<E, bigint, bigint>;
|
|
118
|
+
}
|
|
119
|
+
export declare let useBigIntSummate: UseBigIntSummate;
|
|
120
|
+
export interface UseNumericAverage {
|
|
121
|
+
<E>(): Collector<E, NumericAverageAccumulator, number>;
|
|
122
|
+
<E>(mapper: Functional<E, number>): Collector<E, NumericAverageAccumulator, number>;
|
|
123
|
+
}
|
|
124
|
+
export interface NumericAverageAccumulator {
|
|
101
125
|
summate: number;
|
|
102
126
|
count: number;
|
|
103
|
-
};
|
|
104
|
-
export interface UseNumericAverage {
|
|
105
|
-
(): Collector<number, NumericAverageInformation, number>;
|
|
106
|
-
<E>(mapper: Functional<E, number>): Collector<E, NumericAverageInformation, number>;
|
|
107
127
|
}
|
|
108
128
|
export declare let useNumericAverage: UseNumericAverage;
|
|
109
|
-
export
|
|
129
|
+
export interface UseBigIntAverage {
|
|
130
|
+
<E>(): Collector<E, BigIntAverageAccumulator, bigint>;
|
|
131
|
+
<E>(mapper: Functional<E, bigint>): Collector<E, BigIntAverageAccumulator, bigint>;
|
|
132
|
+
}
|
|
133
|
+
export interface BigIntAverageAccumulator {
|
|
110
134
|
summate: bigint;
|
|
111
135
|
count: bigint;
|
|
112
|
-
};
|
|
113
|
-
export interface UseBigIntAverage {
|
|
114
|
-
(): Collector<bigint, BigIntAverageInformation, bigint>;
|
|
115
|
-
<E>(mapper: Functional<E, bigint>): Collector<E, BigIntAverageInformation, bigint>;
|
|
116
136
|
}
|
|
117
137
|
export declare let useBigIntAverage: UseBigIntAverage;
|
|
118
138
|
export declare let useFrequency: <E>() => Collector<E, Map<E, bigint>, Map<E, bigint>>;
|
|
119
|
-
export interface
|
|
120
|
-
(): Collector<
|
|
121
|
-
<E>(mapper: Functional<E, number>): Collector<E, number, number>;
|
|
139
|
+
export interface UseNumericMode {
|
|
140
|
+
<E>(): Collector<E, Map<number, bigint>, number>;
|
|
141
|
+
<E>(mapper: Functional<E, number>): Collector<E, Map<number, bigint>, number>;
|
|
142
|
+
}
|
|
143
|
+
export declare let useNumericMode: UseNumericMode;
|
|
144
|
+
export interface UseBigIntMode {
|
|
145
|
+
<E>(): Collector<E, Map<bigint, bigint>, bigint>;
|
|
146
|
+
<E>(mapper: Functional<E, bigint>): Collector<E, Map<bigint, bigint>, bigint>;
|
|
147
|
+
}
|
|
148
|
+
export declare let useBigIntMode: UseBigIntMode;
|
|
149
|
+
export interface UseNumericVariance {
|
|
150
|
+
<E>(): Collector<E, VarianceAccumulator, number>;
|
|
151
|
+
<E>(mapper: Functional<E, number>): Collector<E, VarianceAccumulator, number>;
|
|
152
|
+
}
|
|
153
|
+
export interface VarianceAccumulator {
|
|
154
|
+
summate: number;
|
|
155
|
+
summateOfSquares: number;
|
|
156
|
+
count: number;
|
|
157
|
+
}
|
|
158
|
+
export declare let useNumericVariance: UseNumericVariance;
|
|
159
|
+
export interface UseBigIntVariance {
|
|
160
|
+
<E>(): Collector<E, BigIntVarianceAccumulator, bigint>;
|
|
161
|
+
<E>(mapper: Functional<E, bigint>): Collector<E, BigIntVarianceAccumulator, bigint>;
|
|
162
|
+
}
|
|
163
|
+
export interface BigIntVarianceAccumulator {
|
|
164
|
+
summate: bigint;
|
|
165
|
+
summateOfSquares: bigint;
|
|
166
|
+
count: bigint;
|
|
167
|
+
}
|
|
168
|
+
export declare let useBigIntVariance: UseBigIntVariance;
|
|
169
|
+
export interface UseNumericStandardDeviation {
|
|
170
|
+
<E>(): Collector<E, StandardDeviationAccumulator, number>;
|
|
171
|
+
<E>(mapper: Functional<E, number>): Collector<E, StandardDeviationAccumulator, number>;
|
|
172
|
+
}
|
|
173
|
+
export interface StandardDeviationAccumulator {
|
|
174
|
+
summate: number;
|
|
175
|
+
summateOfSquares: number;
|
|
176
|
+
count: number;
|
|
177
|
+
}
|
|
178
|
+
export declare let useNumericStandardDeviation: UseNumericStandardDeviation;
|
|
179
|
+
export interface UseBigIntStandardDeviation {
|
|
180
|
+
<E>(): Collector<E, BigIntStandardDeviationAccumulator, bigint>;
|
|
181
|
+
<E>(mapper: Functional<E, bigint>): Collector<E, BigIntStandardDeviationAccumulator, bigint>;
|
|
182
|
+
}
|
|
183
|
+
export interface BigIntStandardDeviationAccumulator {
|
|
184
|
+
summate: bigint;
|
|
185
|
+
summateOfSquares: bigint;
|
|
186
|
+
count: bigint;
|
|
187
|
+
}
|
|
188
|
+
export declare let useBigIntStandardDeviation: UseBigIntStandardDeviation;
|
|
189
|
+
export interface UseNumericMedian {
|
|
190
|
+
<E>(): Collector<E, number[], number>;
|
|
191
|
+
<E>(mapper: Functional<E, number>): Collector<E, number[], number>;
|
|
192
|
+
}
|
|
193
|
+
export declare let useNumericMedian: UseNumericMedian;
|
|
194
|
+
export interface UseBigIntMedian {
|
|
195
|
+
<E>(): Collector<E, bigint[], bigint>;
|
|
196
|
+
<E>(mapper: Functional<E, bigint>): Collector<E, bigint[], bigint>;
|
|
122
197
|
}
|
|
123
|
-
export declare let
|
|
198
|
+
export declare let useBigIntMedian: UseBigIntMedian;
|
|
199
|
+
export declare let useToGeneratorFunction: <E>() => Collector<E, Array<E>, globalThis.Generator<E, void, undefined>>;
|
|
200
|
+
export declare let useToAsyncGeneratorFunction: <E>() => Collector<E, Array<E>, globalThis.AsyncGenerator<E, void, undefined>>;
|
|
124
201
|
export {};
|
package/dist/collector.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isBigInt, isBoolean, isCollectable, isFunction, isIterable, isNumber, isObject, isSemantic, isString } from "./guard";
|
|
2
|
+
import { useCompare, useToBigInt, useToNumber } from "./hook";
|
|
2
3
|
import { Optional } from "./optional";
|
|
3
4
|
import { CollectableSymbol } from "./symbol";
|
|
4
5
|
import { validate, invalidate } from "./utility";
|
|
@@ -57,19 +58,7 @@ export class Collector {
|
|
|
57
58
|
else if (isCollectable(argument1)) {
|
|
58
59
|
let collectable = argument1;
|
|
59
60
|
let source = collectable.source();
|
|
60
|
-
if (
|
|
61
|
-
let iterable = source;
|
|
62
|
-
let index = 0n;
|
|
63
|
-
for (let element of iterable) {
|
|
64
|
-
if (this.interrupt(element, index, accumulator)) {
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
accumulator = this.accumulator(accumulator, element, count);
|
|
68
|
-
count++;
|
|
69
|
-
index++;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
else if (isFunction(source)) {
|
|
61
|
+
if (isFunction(source)) {
|
|
73
62
|
let generator = source;
|
|
74
63
|
generator((element, index) => {
|
|
75
64
|
accumulator = this.accumulator(accumulator, element, index);
|
|
@@ -143,30 +132,20 @@ export let useCount = () => {
|
|
|
143
132
|
};
|
|
144
133
|
;
|
|
145
134
|
export let useError = (argument1, argument2, argument3) => {
|
|
146
|
-
if (
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}, (text) => {
|
|
153
|
-
let result = text.substring(0, text.length - 1) + "]";
|
|
154
|
-
console.error(result);
|
|
155
|
-
return result;
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
159
|
-
let accumulator = argument1;
|
|
160
|
-
return Collector.full(() => "[", accumulator, (text) => {
|
|
161
|
-
let result = text.substring(0, text.length - 1) + "]";
|
|
135
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
136
|
+
let prefix = argument1;
|
|
137
|
+
let accumulator = argument2;
|
|
138
|
+
let suffix = argument3;
|
|
139
|
+
return Collector.full(() => prefix, accumulator, (text) => {
|
|
140
|
+
let result = text + suffix;
|
|
162
141
|
console.error(result);
|
|
163
142
|
return result;
|
|
164
143
|
});
|
|
165
144
|
}
|
|
166
|
-
else if (
|
|
167
|
-
let prefix =
|
|
145
|
+
else if (isFunction(argument1)) {
|
|
146
|
+
let prefix = "[";
|
|
168
147
|
let accumulator = argument2;
|
|
169
|
-
let suffix =
|
|
148
|
+
let suffix = "]";
|
|
170
149
|
return Collector.full(() => prefix, accumulator, (text) => {
|
|
171
150
|
let result = text + suffix;
|
|
172
151
|
console.error(result);
|
|
@@ -174,7 +153,16 @@ export let useError = (argument1, argument2, argument3) => {
|
|
|
174
153
|
});
|
|
175
154
|
}
|
|
176
155
|
else {
|
|
177
|
-
|
|
156
|
+
return Collector.full(() => "[", (accumulator, element) => {
|
|
157
|
+
if (isString(accumulator) && isString(element)) {
|
|
158
|
+
return accumulator + element + ",";
|
|
159
|
+
}
|
|
160
|
+
return String(accumulator) + String(element) + ",";
|
|
161
|
+
}, (text) => {
|
|
162
|
+
let result = text.substring(0, Math.max(1, text.length - 1)) + "]";
|
|
163
|
+
console.error(result);
|
|
164
|
+
return result;
|
|
165
|
+
});
|
|
178
166
|
}
|
|
179
167
|
};
|
|
180
168
|
export let useFindFirst = () => {
|
|
@@ -201,6 +189,22 @@ export let useFindLast = () => {
|
|
|
201
189
|
return accumulator;
|
|
202
190
|
}, (accumulator) => accumulator);
|
|
203
191
|
};
|
|
192
|
+
export let useFindMaximum = (comparator = (useCompare)) => {
|
|
193
|
+
return Collector.full(() => Optional.ofNullable(), (accumulator, element) => {
|
|
194
|
+
if (accumulator.isPresent()) {
|
|
195
|
+
return comparator(accumulator.get(), element) > 0 ? accumulator : Optional.ofNullable(element);
|
|
196
|
+
}
|
|
197
|
+
return Optional.ofNullable(element);
|
|
198
|
+
}, (result) => result);
|
|
199
|
+
};
|
|
200
|
+
export let useFindMinimum = (comparator = (useCompare)) => {
|
|
201
|
+
return Collector.full(() => Optional.ofNullable(), (accumulator, element) => {
|
|
202
|
+
if (accumulator.isPresent()) {
|
|
203
|
+
return comparator(accumulator.get(), element) < 0 ? accumulator : Optional.ofNullable(element);
|
|
204
|
+
}
|
|
205
|
+
return Optional.ofNullable(element);
|
|
206
|
+
}, (result) => result);
|
|
207
|
+
};
|
|
204
208
|
;
|
|
205
209
|
export let useForEach = (action) => {
|
|
206
210
|
if (isFunction(action)) {
|
|
@@ -266,30 +270,20 @@ export let useJoin = (argument1, argument2, argument3) => {
|
|
|
266
270
|
};
|
|
267
271
|
;
|
|
268
272
|
export let useLog = (argument1, argument2, argument3) => {
|
|
269
|
-
if (
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}, (text) => {
|
|
276
|
-
let result = text.substring(0, text.length - 1) + "]";
|
|
277
|
-
console.log(result);
|
|
278
|
-
return result;
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
|
|
282
|
-
let accumulator = argument1;
|
|
283
|
-
return Collector.full(() => "[", accumulator, (text) => {
|
|
284
|
-
let result = text.substring(0, text.length - 1) + "]";
|
|
273
|
+
if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
|
|
274
|
+
let prefix = argument1;
|
|
275
|
+
let accumulator = argument2;
|
|
276
|
+
let suffix = argument3;
|
|
277
|
+
return Collector.full(() => prefix, accumulator, (text) => {
|
|
278
|
+
let result = text + suffix;
|
|
285
279
|
console.log(result);
|
|
286
280
|
return result;
|
|
287
281
|
});
|
|
288
282
|
}
|
|
289
|
-
else if (
|
|
290
|
-
let prefix =
|
|
283
|
+
else if (isFunction(argument1)) {
|
|
284
|
+
let prefix = "[";
|
|
291
285
|
let accumulator = argument2;
|
|
292
|
-
let suffix =
|
|
286
|
+
let suffix = "]";
|
|
293
287
|
return Collector.full(() => prefix, accumulator, (text) => {
|
|
294
288
|
let result = text + suffix;
|
|
295
289
|
console.log(result);
|
|
@@ -297,7 +291,17 @@ export let useLog = (argument1, argument2, argument3) => {
|
|
|
297
291
|
});
|
|
298
292
|
}
|
|
299
293
|
else {
|
|
300
|
-
|
|
294
|
+
return Collector.full(() => "[", (accumulator, element) => {
|
|
295
|
+
console.log(element);
|
|
296
|
+
if (isString(accumulator) && isString(element)) {
|
|
297
|
+
return accumulator + element + ",";
|
|
298
|
+
}
|
|
299
|
+
return String(accumulator) + String(element) + ",";
|
|
300
|
+
}, (text) => {
|
|
301
|
+
let result = text.substring(0, Math.max(1, text.length - 1)) + "]";
|
|
302
|
+
console.log(result);
|
|
303
|
+
return result;
|
|
304
|
+
});
|
|
301
305
|
}
|
|
302
306
|
};
|
|
303
307
|
export let usePartition = (count) => {
|
|
@@ -427,63 +431,59 @@ export let useWrite = (argument1, argument2) => {
|
|
|
427
431
|
throw new TypeError("Invalid arguments.");
|
|
428
432
|
};
|
|
429
433
|
;
|
|
430
|
-
export let
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
434
|
+
export let useNumericSummate = (mapper = useToNumber) => {
|
|
435
|
+
return Collector.full(() => 0, (accumulator, element) => {
|
|
436
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
437
|
+
return accumulator + (isNumber(resolved) ? resolved : 0);
|
|
438
|
+
}, (result) => result);
|
|
439
|
+
};
|
|
440
|
+
;
|
|
441
|
+
export let useBigIntSummate = (mapper = useToBigInt) => {
|
|
442
|
+
return Collector.full(() => 0n, (accumulator, element) => {
|
|
443
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
444
|
+
return accumulator + (isBigInt(resolved) ? resolved : 0n);
|
|
445
|
+
}, (result) => result);
|
|
446
|
+
};
|
|
447
|
+
;
|
|
448
|
+
;
|
|
449
|
+
export let useNumericAverage = (mapper = useToNumber) => {
|
|
446
450
|
return Collector.full(() => {
|
|
447
451
|
return {
|
|
448
452
|
summate: 0,
|
|
449
453
|
count: 0
|
|
450
454
|
};
|
|
451
|
-
}, (
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
455
|
+
}, (accumulator, element) => {
|
|
456
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
457
|
+
return {
|
|
458
|
+
summate: accumulator.summate + (isNumber(resolved) ? resolved : 0),
|
|
459
|
+
count: accumulator.count + 1
|
|
460
|
+
};
|
|
461
|
+
}, (result) => {
|
|
462
|
+
if (result.count === 0) {
|
|
463
|
+
return 0;
|
|
464
|
+
}
|
|
465
|
+
return result.summate / result.count;
|
|
457
466
|
});
|
|
458
467
|
};
|
|
459
468
|
;
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
return Collector.full(() => {
|
|
463
|
-
return {
|
|
464
|
-
summate: 0n,
|
|
465
|
-
count: 0n
|
|
466
|
-
};
|
|
467
|
-
}, (information, element) => {
|
|
468
|
-
let value = mapper(element);
|
|
469
|
-
information.summate += value;
|
|
470
|
-
information.count++;
|
|
471
|
-
return information;
|
|
472
|
-
}, (information) => {
|
|
473
|
-
return information.summate / information.count;
|
|
474
|
-
});
|
|
475
|
-
}
|
|
469
|
+
;
|
|
470
|
+
export let useBigIntAverage = (mapper = useToBigInt) => {
|
|
476
471
|
return Collector.full(() => {
|
|
477
472
|
return {
|
|
478
473
|
summate: 0n,
|
|
479
474
|
count: 0n
|
|
480
475
|
};
|
|
481
|
-
}, (
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
476
|
+
}, (accumulator, element) => {
|
|
477
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
478
|
+
return {
|
|
479
|
+
summate: accumulator.summate + (isBigInt(resolved) ? resolved : 0n),
|
|
480
|
+
count: accumulator.count + 1n
|
|
481
|
+
};
|
|
482
|
+
}, (result) => {
|
|
483
|
+
if (result.count === 0n) {
|
|
484
|
+
return 0n;
|
|
485
|
+
}
|
|
486
|
+
return result.summate / result.count;
|
|
487
487
|
});
|
|
488
488
|
};
|
|
489
489
|
export let useFrequency = () => {
|
|
@@ -494,14 +494,204 @@ export let useFrequency = () => {
|
|
|
494
494
|
}, (map) => map);
|
|
495
495
|
};
|
|
496
496
|
;
|
|
497
|
-
export let
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
497
|
+
export let useNumericMode = (mapper = useToNumber) => {
|
|
498
|
+
return Collector.full(() => new Map(), (map, element) => {
|
|
499
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
500
|
+
let count = map.get(resolved) || 0n;
|
|
501
|
+
map.set(resolved, count + 1n);
|
|
502
|
+
return map;
|
|
503
|
+
}, (map) => {
|
|
504
|
+
let maxCount = 0n;
|
|
505
|
+
let mode = 0;
|
|
506
|
+
for (let [key, value] of map) {
|
|
507
|
+
if (value > maxCount) {
|
|
508
|
+
maxCount = value;
|
|
509
|
+
mode = key;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return mode;
|
|
513
|
+
});
|
|
514
|
+
};
|
|
515
|
+
;
|
|
516
|
+
export let useBigIntMode = (mapper = useToBigInt) => {
|
|
517
|
+
return Collector.full(() => new Map(), (map, element) => {
|
|
518
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
519
|
+
let count = map.get(resolved) || 0n;
|
|
520
|
+
map.set(resolved, count + 1n);
|
|
521
|
+
return map;
|
|
522
|
+
}, (map) => {
|
|
523
|
+
let maxCount = 0n;
|
|
524
|
+
let mode = 0n;
|
|
525
|
+
for (let [key, value] of map) {
|
|
526
|
+
if (value > maxCount) {
|
|
527
|
+
maxCount = value;
|
|
528
|
+
mode = key;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return mode;
|
|
532
|
+
});
|
|
533
|
+
};
|
|
534
|
+
;
|
|
535
|
+
;
|
|
536
|
+
export let useNumericVariance = (mapper = useToNumber) => {
|
|
537
|
+
return Collector.full(() => {
|
|
538
|
+
return {
|
|
539
|
+
summate: 0,
|
|
540
|
+
summateOfSquares: 0,
|
|
541
|
+
count: 0
|
|
542
|
+
};
|
|
543
|
+
}, (accumulator, element) => {
|
|
544
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
545
|
+
return {
|
|
546
|
+
summate: accumulator.summate + (isNumber(resolved) ? resolved : 0),
|
|
547
|
+
summateOfSquares: accumulator.summateOfSquares + (isNumber(resolved) ? Math.pow(resolved, 2) : 0),
|
|
548
|
+
count: accumulator.count + 1
|
|
549
|
+
};
|
|
550
|
+
}, (result) => {
|
|
551
|
+
if (result.count < 2) {
|
|
552
|
+
return 0;
|
|
553
|
+
}
|
|
554
|
+
let mean = result.summate / result.count;
|
|
555
|
+
let variance = (result.summateOfSquares / result.count) - Math.pow(mean, 2);
|
|
556
|
+
return variance;
|
|
557
|
+
});
|
|
558
|
+
};
|
|
559
|
+
;
|
|
560
|
+
;
|
|
561
|
+
export let useBigIntVariance = (mapper = useToBigInt) => {
|
|
562
|
+
return Collector.full(() => {
|
|
563
|
+
return {
|
|
564
|
+
summate: 0n,
|
|
565
|
+
summateOfSquares: 0n,
|
|
566
|
+
count: 0n
|
|
567
|
+
};
|
|
568
|
+
}, (accumulator, element) => {
|
|
569
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
570
|
+
return {
|
|
571
|
+
summate: accumulator.summate + (isBigInt(resolved) ? resolved : 0n),
|
|
572
|
+
summateOfSquares: accumulator.summateOfSquares + (isBigInt(resolved) ? resolved * resolved : 0n),
|
|
573
|
+
count: accumulator.count + 1n
|
|
574
|
+
};
|
|
575
|
+
}, (result) => {
|
|
576
|
+
if (result.count < 2n) {
|
|
577
|
+
return 0n;
|
|
578
|
+
}
|
|
579
|
+
let mean = result.summate / result.count;
|
|
580
|
+
let variance = (result.summateOfSquares / result.count) - (mean * mean);
|
|
581
|
+
return variance;
|
|
582
|
+
});
|
|
583
|
+
};
|
|
584
|
+
;
|
|
585
|
+
;
|
|
586
|
+
export let useNumericStandardDeviation = (mapper = useToNumber) => {
|
|
587
|
+
return Collector.full(() => {
|
|
588
|
+
return {
|
|
589
|
+
summate: 0,
|
|
590
|
+
summateOfSquares: 0,
|
|
591
|
+
count: 0
|
|
592
|
+
};
|
|
593
|
+
}, (accumulator, element) => {
|
|
594
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
595
|
+
return {
|
|
596
|
+
summate: accumulator.summate + (isNumber(resolved) ? resolved : 0),
|
|
597
|
+
summateOfSquares: accumulator.summateOfSquares + (isNumber(resolved) ? Math.pow(resolved, 2) : 0),
|
|
598
|
+
count: accumulator.count + 1
|
|
599
|
+
};
|
|
600
|
+
}, (result) => {
|
|
601
|
+
if (result.count < 2) {
|
|
602
|
+
return 0;
|
|
603
|
+
}
|
|
604
|
+
let mean = result.summate / result.count;
|
|
605
|
+
let variance = (result.summateOfSquares / result.count) - Math.pow(mean, 2);
|
|
606
|
+
let standardDeviation = Math.sqrt(variance);
|
|
607
|
+
return standardDeviation;
|
|
608
|
+
});
|
|
609
|
+
};
|
|
610
|
+
;
|
|
611
|
+
;
|
|
612
|
+
export let useBigIntStandardDeviation = (mapper = useToBigInt) => {
|
|
613
|
+
return Collector.full(() => {
|
|
614
|
+
return {
|
|
615
|
+
summate: 0n,
|
|
616
|
+
summateOfSquares: 0n,
|
|
617
|
+
count: 0n
|
|
618
|
+
};
|
|
619
|
+
}, (accumulator, element) => {
|
|
620
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
621
|
+
return {
|
|
622
|
+
summate: accumulator.summate + (isBigInt(resolved) ? resolved : 0n),
|
|
623
|
+
summateOfSquares: accumulator.summateOfSquares + (isBigInt(resolved) ? resolved * resolved : 0n),
|
|
624
|
+
count: accumulator.count + 1n
|
|
625
|
+
};
|
|
626
|
+
}, (result) => {
|
|
627
|
+
if (result.count < 2n) {
|
|
628
|
+
return 0n;
|
|
629
|
+
}
|
|
630
|
+
let mean = result.summate / result.count;
|
|
631
|
+
let variance = (result.summateOfSquares / result.count) - (mean * mean);
|
|
632
|
+
let standardDeviation = BigInt(Math.sqrt(Number(variance)));
|
|
633
|
+
return standardDeviation;
|
|
634
|
+
});
|
|
635
|
+
};
|
|
636
|
+
;
|
|
637
|
+
export let useNumericMedian = (mapper = useToNumber) => {
|
|
638
|
+
return Collector.full(() => [], (array, element) => {
|
|
639
|
+
let resolved = isNumber(element) ? element : mapper(element);
|
|
640
|
+
array.push(resolved);
|
|
641
|
+
array.sort((a, b) => a - b);
|
|
642
|
+
return array;
|
|
643
|
+
}, (array) => {
|
|
644
|
+
let length = array.length;
|
|
645
|
+
if (length % 2 === 0) {
|
|
646
|
+
let mid = length / 2;
|
|
647
|
+
return (array[mid - 1] + array[mid]) / 2;
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
let mid = Math.floor(length / 2);
|
|
651
|
+
return array[mid];
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
};
|
|
655
|
+
;
|
|
656
|
+
export let useBigIntMedian = (mapper = useToBigInt) => {
|
|
657
|
+
return Collector.full(() => [], (array, element) => {
|
|
658
|
+
let resolved = isBigInt(element) ? element : mapper(element);
|
|
659
|
+
array.push(resolved);
|
|
660
|
+
array.sort((a, b) => Number(a - b));
|
|
661
|
+
return array;
|
|
662
|
+
}, (array) => {
|
|
663
|
+
let length = array.length;
|
|
664
|
+
if (length % 2 === 0) {
|
|
665
|
+
let mid = length / 2;
|
|
666
|
+
return (array[Number(mid - 1)] + array[mid]) / 2n;
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
let mid = Math.floor(length / 2);
|
|
670
|
+
return array[mid];
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
};
|
|
674
|
+
export let useToGeneratorFunction = () => {
|
|
675
|
+
return Collector.full(() => [], (array, element) => {
|
|
676
|
+
array.push(element);
|
|
677
|
+
return array;
|
|
678
|
+
}, (array) => {
|
|
679
|
+
return (function* () {
|
|
680
|
+
for (let element of array) {
|
|
681
|
+
yield element;
|
|
682
|
+
}
|
|
683
|
+
})();
|
|
684
|
+
});
|
|
685
|
+
};
|
|
686
|
+
export let useToAsyncGeneratorFunction = () => {
|
|
687
|
+
return Collector.full(() => [], (array, element) => {
|
|
688
|
+
array.push(element);
|
|
689
|
+
return array;
|
|
690
|
+
}, (array) => {
|
|
691
|
+
return (async function* () {
|
|
692
|
+
for (let element of array) {
|
|
693
|
+
yield element;
|
|
694
|
+
}
|
|
695
|
+
})();
|
|
696
|
+
});
|
|
507
697
|
};
|
package/dist/factory.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Semantic } from "./semantic";
|
|
2
2
|
import type { BiFunctional, BiPredicate, Functional, Predicate, Supplier, TriFunctional, Generator } from "./utility";
|
|
3
3
|
export declare let animationFrame: Functional<number, Semantic<number>> & BiFunctional<number, number, Semantic<number>>;
|
|
4
|
+
interface Attribute<T> {
|
|
5
|
+
key: keyof T;
|
|
6
|
+
value: T[keyof T];
|
|
7
|
+
}
|
|
8
|
+
export declare let attribute: <T extends object>(target: T) => Semantic<Attribute<T>>;
|
|
4
9
|
export declare let blob: Functional<Blob, Semantic<Uint8Array>> & BiFunctional<Blob, bigint, Semantic<Uint8Array>>;
|
|
5
10
|
export declare let empty: <E>() => Semantic<E>;
|
|
6
11
|
export declare let fill: (<E>(element: E, count: bigint) => Semantic<E>) & (<E>(supplier: Supplier<E>, count: bigint) => Semantic<E>);
|
|
@@ -11,3 +16,4 @@ export declare let iterate: <E>(generator: Generator<E>) => Semantic<E>;
|
|
|
11
16
|
export declare let promise: (<T>(promise: Promise<T>) => Semantic<T>);
|
|
12
17
|
export declare let range: BiFunctional<number, number, Semantic<number>> & TriFunctional<number, number, number, Semantic<number>>;
|
|
13
18
|
export declare let websocket: Functional<WebSocket, Semantic<MessageEvent | CloseEvent | Event>>;
|
|
19
|
+
export {};
|