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.
@@ -1,329 +1,179 @@
1
1
  import { OrderedCollectable } from "./collectable";
2
- import { isFunction, isIterable } from "./guard";
3
- import { useCompare } from "./hook";
4
- import { Optional } from "./optional";
2
+ import { Collector, useBigIntAverage, useBigIntMedian, useBigIntMode, useBigIntSummate, useBigIntVariance, useFrequency, useNumericAverage, useNumericMedian, useNumericMode, useNumericStandardDeviation, useNumericSummate, useNumericVariance, useToAsyncGeneratorFunction, useToGeneratorFunction } from "./collector";
3
+ import { isFunction } from "./guard";
4
+ import { useCompare, useToBigInt, useToNumber } from "./hook";
5
5
  import { StatisticsSymbol, NumericStatisticsSymbol, BigIntStatisticsSymbol } from "./symbol";
6
- import { invalidate } from "./utility";
7
6
  export class Statistics extends OrderedCollectable {
8
7
  Statistics = StatisticsSymbol;
9
8
  constructor(parameter, comparator) {
10
- if (isIterable(parameter)) {
11
- if (isFunction(comparator)) {
12
- super(parameter, comparator);
13
- }
14
- else {
15
- super(parameter);
16
- }
17
- }
18
- else if (isFunction(parameter)) {
19
- if (isFunction(comparator)) {
20
- super(parameter, comparator);
21
- }
22
- else {
23
- super(parameter);
24
- }
25
- }
9
+ super(parameter, comparator || useCompare);
10
+ Object.defineProperty(this, "Statistics", {
11
+ value: StatisticsSymbol,
12
+ enumerable: false,
13
+ writable: false,
14
+ configurable: false
15
+ });
16
+ Object.freeze(this);
26
17
  }
27
- count() {
28
- return BigInt(this.ordered.length);
29
- }
30
- maximum(argument1) {
31
- if (invalidate(argument1)) {
32
- if (!this.isEmpty()) {
33
- return this.collect(() => {
34
- return Optional.ofNullable();
35
- }, (result, element) => {
36
- if (result.isEmpty()) {
37
- return Optional.of(element);
38
- }
39
- else {
40
- let current = result.get();
41
- if (useCompare(current, element) > 0) {
42
- return Optional.of(element);
43
- }
44
- else {
45
- return result;
46
- }
47
- }
48
- }, (result) => result);
49
- }
50
- else {
51
- return Optional.ofNullable();
52
- }
18
+ *[Symbol.iterator]() {
19
+ try {
20
+ let collector = useToGeneratorFunction();
21
+ yield* collector.collect(this.source());
53
22
  }
54
- else {
55
- let comparator = argument1;
56
- if (!this.isEmpty()) {
57
- return this.collect(() => {
58
- return Optional.ofNullable();
59
- }, (result, element) => {
60
- if (result.isEmpty()) {
61
- return Optional.of(element);
62
- }
63
- else {
64
- let current = result.get();
65
- if (comparator(current, element) > 0) {
66
- return Optional.of(element);
67
- }
68
- else {
69
- return result;
70
- }
71
- }
72
- }, (result) => result);
73
- }
23
+ catch (error) {
24
+ throw new Error("Uncaught error on Generator.");
74
25
  }
75
- return Optional.ofNullable();
76
- }
77
- minimum(argument1) {
78
- if (invalidate(argument1)) {
79
- if (!this.isEmpty()) {
80
- return this.collect(() => {
81
- return Optional.ofNullable();
82
- }, (result, element) => {
83
- if (result.isEmpty()) {
84
- return Optional.of(element);
85
- }
86
- else {
87
- let current = result.get();
88
- if (useCompare(current, element) < 0) {
89
- return Optional.of(element);
90
- }
91
- else {
92
- return result;
93
- }
94
- }
95
- }, (result) => result);
96
- }
97
- else {
98
- return Optional.ofNullable();
99
- }
26
+ }
27
+ async *[Symbol.asyncIterator]() {
28
+ try {
29
+ let collector = useToAsyncGeneratorFunction();
30
+ yield* collector.collect(this.source());
100
31
  }
101
- else {
102
- let comparator = argument1;
103
- if (!this.isEmpty()) {
104
- return this.collect(() => {
105
- return Optional.ofNullable();
106
- }, (result, element) => {
107
- if (result.isEmpty()) {
108
- return Optional.of(element);
109
- }
110
- else {
111
- let current = result.get();
112
- if (comparator(current, element) < 0) {
113
- return Optional.of(element);
114
- }
115
- else {
116
- return result;
117
- }
118
- }
119
- }, (result) => result);
120
- }
121
- return Optional.ofNullable();
32
+ catch (error) {
33
+ throw new Error("Uncaught error on AsyncGenerator.");
122
34
  }
123
35
  }
36
+ count() {
37
+ return BigInt(this.buffer.length);
38
+ }
39
+ frequency() {
40
+ return useFrequency().collect(this.source());
41
+ }
124
42
  }
125
43
  ;
44
+ Object.freeze(Statistics);
45
+ Object.freeze(Statistics.prototype);
46
+ Object.freeze(Object.getPrototypeOf(Statistics));
126
47
  export class NumericStatistics extends Statistics {
127
48
  NumericStatistics = NumericStatisticsSymbol;
128
49
  constructor(parameter, comparator) {
129
- if (isIterable(parameter)) {
130
- if (isFunction(comparator)) {
131
- super(parameter, comparator);
132
- }
133
- else {
134
- super(parameter);
135
- }
50
+ super(parameter, comparator || useCompare);
51
+ Object.defineProperty(this, "NumericStatistics", {
52
+ value: NumericStatisticsSymbol,
53
+ enumerable: false,
54
+ writable: false,
55
+ configurable: false
56
+ });
57
+ Object.freeze(this);
58
+ }
59
+ *[Symbol.iterator]() {
60
+ try {
61
+ let collector = useToGeneratorFunction();
62
+ yield* collector.collect(this.source());
136
63
  }
137
- else if (isFunction(parameter)) {
138
- if (isFunction(comparator)) {
139
- super(parameter, comparator);
140
- }
141
- else {
142
- super(parameter);
64
+ catch (error) {
65
+ throw new Error("Uncaught error on Generator.");
66
+ }
67
+ }
68
+ async *[Symbol.asyncIterator]() {
69
+ try {
70
+ let collector = useToAsyncGeneratorFunction();
71
+ yield* collector.collect(this.source());
72
+ }
73
+ catch (error) {
74
+ throw new Error("Uncaught error on AsyncGenerator.");
75
+ }
76
+ }
77
+ average(mapper) {
78
+ if (this.isEmpty()) {
79
+ return 0;
80
+ }
81
+ try {
82
+ if (isFunction(mapper)) {
83
+ return useNumericAverage(mapper).collect(this.source());
143
84
  }
85
+ return useNumericAverage().collect(this.source());
86
+ }
87
+ catch (error) {
88
+ throw new Error("Uncaught error on average.");
144
89
  }
145
90
  }
146
91
  range(argument1) {
147
92
  if (this.isEmpty()) {
148
93
  return 0;
149
94
  }
150
- if (invalidate(argument1)) {
151
- let minimum = this.ordered[0].value;
152
- let maximum = this.ordered[0].value;
153
- for (let i = 1; i < this.ordered.length; i++) {
154
- let current = this.ordered[i].value;
155
- if (useCompare(current, minimum) < 0) {
156
- minimum = current;
157
- }
158
- if (useCompare(current, maximum) > 0) {
159
- maximum = current;
160
- }
161
- }
162
- return useCompare(maximum, minimum);
163
- }
164
- else if (isFunction(argument1)) {
165
- let mapper = argument1;
166
- let minimum = mapper(this.ordered[0].value);
167
- let maximum = mapper(this.ordered[0].value);
168
- for (let i = 1; i < this.ordered.length; i++) {
169
- let current = mapper(this.ordered[i].value);
170
- if (current < minimum) {
171
- minimum = current;
172
- }
173
- if (current > maximum) {
174
- maximum = current;
175
- }
95
+ try {
96
+ if (this.count() === 1n) {
97
+ return 0;
176
98
  }
177
- return maximum - minimum;
99
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
100
+ let count = this.buffer.length;
101
+ let minimum = this.buffer[0].element;
102
+ let maximum = this.buffer[count - 1].element;
103
+ return mapper(maximum) - mapper(minimum);
104
+ }
105
+ catch (error) {
106
+ throw new Error("Uncaught error on range.");
178
107
  }
179
- throw new TypeError("Invalid arguments.");
180
108
  }
181
109
  variance(argument1) {
182
110
  if (this.isEmpty() || this.count() === 1n) {
183
111
  return 0;
184
112
  }
185
- if (invalidate(argument1)) {
186
- let mean = this.mean();
187
- let summate = this.summate();
188
- return (summate / Number(this.count())) - (mean * mean);
113
+ try {
114
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
115
+ return useNumericVariance(mapper).collect(this.source());
189
116
  }
190
- else if (isFunction(argument1)) {
191
- let mapper = argument1;
192
- let mean = this.mean(mapper);
193
- let summate = this.summate(mapper);
194
- return (summate / Number(this.count())) - (mean * mean);
117
+ catch (error) {
118
+ throw new Error("Uncaught error on variance.");
195
119
  }
196
- throw new TypeError("Invalid arguments.");
197
120
  }
198
121
  standardDeviation(argument1) {
199
- if (invalidate(argument1)) {
200
- return Math.sqrt(this.variance());
122
+ try {
123
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
124
+ return useNumericStandardDeviation(mapper).collect(this.source());
201
125
  }
202
- else if (isFunction(argument1)) {
203
- let mapper = argument1;
204
- let variance = this.variance(mapper);
205
- return Math.sqrt(variance);
126
+ catch (error) {
127
+ throw new Error("Uncaught error on standardDeviation.");
206
128
  }
207
- throw new TypeError("Invalid arguments.");
208
129
  }
209
130
  mean(argument1) {
210
131
  if (this.isEmpty()) {
211
132
  return 0;
212
133
  }
213
- if (invalidate(argument1)) {
214
- let summate = this.summate();
215
- return summate / Number(this.count());
134
+ try {
135
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
136
+ return useNumericAverage(mapper).collect(this.source());
216
137
  }
217
- else if (isFunction(argument1)) {
218
- let mapper = argument1;
219
- let summate = this.summate(mapper);
220
- return summate / Number(this.count());
138
+ catch (error) {
139
+ throw new Error("Uncaught error on mean.");
221
140
  }
222
- throw new TypeError("Invalid arguments.");
223
141
  }
224
142
  median(argument1) {
225
143
  if (this.isEmpty()) {
226
144
  return 0;
227
145
  }
228
- if (invalidate(argument1)) {
229
- let count = Number(this.count());
230
- let middle = Math.floor(count / 2);
231
- let median = Number(this.ordered[middle].value);
232
- if (count % 2 === 0) {
233
- median = (median + Number(this.ordered[middle - 1].value)) / 2;
234
- }
235
- return median;
146
+ try {
147
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
148
+ return useNumericMedian(mapper).collect(this.source());
236
149
  }
237
- else if (isFunction(argument1)) {
238
- let mapper = argument1;
239
- let count = Number(this.count());
240
- let middle = Math.floor(count / 2);
241
- let median = mapper(this.ordered[middle].value);
242
- if (count % 2 === 0) {
243
- median = (median + mapper(this.ordered[middle - 1].value)) / 2;
244
- }
245
- return median;
150
+ catch (error) {
151
+ throw new Error("Uncaught error on median.");
246
152
  }
247
- throw new TypeError("Invalid arguments.");
248
153
  }
249
154
  mode(argument1) {
250
155
  if (this.isEmpty()) {
251
156
  return 0;
252
157
  }
253
- if (invalidate(argument1)) {
254
- let frequency = this.frequency();
255
- let mode = 0;
256
- let maxFrequency = 0n;
257
- for (let [value, freq] of frequency) {
258
- if (freq > maxFrequency) {
259
- mode = value;
260
- maxFrequency = freq;
261
- }
262
- }
263
- return mode;
264
- }
265
- else if (isFunction(argument1)) {
266
- let mapper = argument1;
267
- let frequency = this.frequency(mapper);
268
- let mode = 0;
269
- let maxFrequency = 0n;
270
- for (let [value, freq] of frequency) {
271
- if (freq > maxFrequency) {
272
- mode = value;
273
- maxFrequency = freq;
274
- }
275
- }
276
- return mode;
158
+ try {
159
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
160
+ return useNumericMode(mapper).collect(this.source());
277
161
  }
278
- throw new TypeError("Invalid arguments.");
279
- }
280
- frequency(argument1) {
281
- if (this.isEmpty()) {
282
- return new Map();
283
- }
284
- if (invalidate(argument1)) {
285
- let frequency = new Map();
286
- for (let i = 0; i < this.ordered.length; i++) {
287
- let current = Number(this.ordered[i].value);
288
- let count = frequency.get(current) || 0n;
289
- frequency.set(current, count + 1n);
290
- }
291
- return frequency;
292
- }
293
- else if (isFunction(argument1)) {
294
- let mapper = argument1;
295
- let frequency = new Map();
296
- for (let i = 0; i < this.ordered.length; i++) {
297
- let current = mapper(this.ordered[i].value);
298
- let count = frequency.get(current) || 0n;
299
- frequency.set(current, count + 1n);
300
- }
301
- return frequency;
162
+ catch (error) {
163
+ throw new Error("Uncaught error on mode.");
302
164
  }
303
- throw new TypeError("Invalid arguments.");
304
165
  }
305
166
  summate(argument1) {
306
167
  if (this.isEmpty()) {
307
168
  return 0;
308
169
  }
309
- if (invalidate(argument1)) {
310
- let summate = 0;
311
- for (let i = 0; i < this.ordered.length; i++) {
312
- let current = Number(this.ordered[i].value);
313
- summate += current;
314
- }
315
- return summate;
170
+ try {
171
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
172
+ return useNumericSummate(mapper).collect(this.source());
316
173
  }
317
- else if (isFunction(argument1)) {
318
- let mapper = argument1;
319
- let summate = 0;
320
- for (let i = 0; i < this.ordered.length; i++) {
321
- let current = mapper(this.ordered[i].value);
322
- summate += current;
323
- }
324
- return summate;
174
+ catch (error) {
175
+ throw new Error("Uncaught error on summate.");
325
176
  }
326
- throw new TypeError("Invalid arguments.");
327
177
  }
328
178
  quantile(quantile, argument1) {
329
179
  if (this.isEmpty()) {
@@ -332,64 +182,40 @@ export class NumericStatistics extends Statistics {
332
182
  if (quantile < 0 || quantile > 1) {
333
183
  throw new RangeError("Invalid quantile.");
334
184
  }
335
- if (invalidate(argument1)) {
185
+ try {
186
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
336
187
  let count = Number(this.count());
337
188
  let index = Math.floor(quantile * count);
338
189
  if (index === count) {
339
190
  index--;
340
191
  }
341
- let value = Number(this.ordered[index].value);
192
+ let value = mapper(this.buffer[index].element);
342
193
  return value;
343
194
  }
344
- else if (isFunction(argument1)) {
345
- let mapper = argument1;
346
- let count = Number(this.count());
347
- let index = Math.floor(quantile * count);
348
- if (index === count) {
349
- index--;
350
- }
351
- let value = mapper(this.ordered[index].value);
352
- return value;
195
+ catch (error) {
196
+ throw new Error("Uncaught error on quantile.");
353
197
  }
354
- throw new TypeError("Invalid arguments.");
355
198
  }
356
199
  interquartileRange(argument1) {
357
200
  if (this.isEmpty()) {
358
201
  return 0;
359
202
  }
360
- if (invalidate(argument1)) {
361
- let lower = this.quantile(0.25);
362
- let upper = this.quantile(0.75);
363
- return upper - lower;
364
- }
365
- else if (isFunction(argument1)) {
366
- let mapper = argument1;
203
+ try {
204
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
367
205
  let lower = this.quantile(0.25, mapper);
368
206
  let upper = this.quantile(0.75, mapper);
369
207
  return upper - lower;
370
208
  }
371
- throw new TypeError("Invalid arguments.");
209
+ catch (error) {
210
+ throw new Error("Uncaught error on interquartileRange.");
211
+ }
372
212
  }
373
213
  skewness(argument1) {
374
214
  if (this.isEmpty()) {
375
215
  return 0;
376
216
  }
377
- if (invalidate(argument1)) {
378
- let mean = this.mean();
379
- let standardDeviation = this.standardDeviation();
380
- if (standardDeviation === 0) {
381
- return 0;
382
- }
383
- let data = this.toArray();
384
- let summate = 0;
385
- for (let value of data) {
386
- let z = (value - mean) / standardDeviation;
387
- summate += Math.pow(z, 3);
388
- }
389
- return summate / data.length;
390
- }
391
- else if (isFunction(argument1)) {
392
- let mapper = argument1;
217
+ try {
218
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
393
219
  let mean = this.mean(mapper);
394
220
  let standardDeviation = this.standardDeviation(mapper);
395
221
  if (standardDeviation === 0) {
@@ -403,28 +229,16 @@ export class NumericStatistics extends Statistics {
403
229
  }
404
230
  return summate / data.length;
405
231
  }
406
- throw new TypeError("Invalid arguments.");
232
+ catch (error) {
233
+ throw new Error("Uncaught error on skewness.");
234
+ }
407
235
  }
408
236
  kurtosis(argument1) {
409
237
  if (this.isEmpty()) {
410
238
  return 0;
411
239
  }
412
- if (invalidate(argument1)) {
413
- let mean = this.mean();
414
- let standardDeviation = this.standardDeviation();
415
- if (standardDeviation === 0) {
416
- return 0;
417
- }
418
- let data = this.toArray();
419
- let summate = 0;
420
- for (let value of data) {
421
- let z = (value - mean) / standardDeviation;
422
- summate += Math.pow(z, 4);
423
- }
424
- return summate / (data.length * data.length) - 3;
425
- }
426
- else if (isFunction(argument1)) {
427
- let mapper = argument1;
240
+ try {
241
+ let mapper = isFunction(argument1) ? argument1 : useToNumber;
428
242
  let mean = this.mean(mapper);
429
243
  let standardDeviation = this.standardDeviation(mapper);
430
244
  if (standardDeviation === 0) {
@@ -438,277 +252,183 @@ export class NumericStatistics extends Statistics {
438
252
  }
439
253
  return summate / (data.length * data.length) - 3;
440
254
  }
441
- throw new TypeError("Invalid arguments.");
255
+ catch (error) {
256
+ throw new Error("Uncaught error on kurtosis.");
257
+ }
442
258
  }
443
259
  }
444
260
  ;
261
+ Object.freeze(NumericStatistics);
262
+ Object.freeze(NumericStatistics.prototype);
263
+ Object.freeze(Object.getPrototypeOf(NumericStatistics));
445
264
  export class BigIntStatistics extends Statistics {
446
265
  BigIntStatistics = BigIntStatisticsSymbol;
447
266
  constructor(parameter, comparator) {
448
- if (isIterable(parameter)) {
449
- if (isFunction(comparator)) {
450
- super(parameter, comparator);
451
- }
452
- else {
453
- super(parameter);
454
- }
267
+ super(parameter, comparator || useCompare);
268
+ Object.defineProperty(this, "BigIntStatistics", {
269
+ value: BigIntStatisticsSymbol,
270
+ enumerable: false,
271
+ writable: false,
272
+ configurable: false
273
+ });
274
+ Object.freeze(this);
275
+ }
276
+ *[Symbol.iterator]() {
277
+ try {
278
+ let collector = useToGeneratorFunction();
279
+ yield* collector.collect(this.source());
455
280
  }
456
- else if (isFunction(parameter)) {
457
- if (isFunction(comparator)) {
458
- super(parameter, comparator);
459
- }
460
- else {
461
- super(parameter);
462
- }
281
+ catch (error) {
282
+ throw new Error("Uncaught error on Generator.");
283
+ }
284
+ }
285
+ async *[Symbol.asyncIterator]() {
286
+ try {
287
+ let collector = useToAsyncGeneratorFunction();
288
+ yield* collector.collect(this.source());
289
+ }
290
+ catch (error) {
291
+ throw new Error("Uncaught error on AsyncGenerator.");
292
+ }
293
+ }
294
+ average(argument1) {
295
+ if (this.isEmpty()) {
296
+ return 0n;
297
+ }
298
+ try {
299
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
300
+ return useBigIntAverage(mapper).collect(this.source());
301
+ }
302
+ catch (error) {
303
+ throw new Error("Uncaught error on average.");
463
304
  }
464
305
  }
465
306
  range(argument1) {
466
307
  if (this.isEmpty()) {
467
308
  return 0n;
468
309
  }
469
- if (invalidate(argument1)) {
470
- let minimum = this.ordered[0].value;
471
- let maximum = this.ordered[0].value;
472
- for (let i = 1; i < this.ordered.length; i++) {
473
- let current = this.ordered[i].value;
474
- if (useCompare(current, minimum) < 0) {
475
- minimum = current;
476
- }
477
- if (useCompare(current, maximum) > 0) {
478
- maximum = current;
479
- }
480
- }
481
- return BigInt(useCompare(maximum, minimum));
482
- }
483
- else if (isFunction(argument1)) {
484
- let mapper = argument1;
485
- let minimum = mapper(this.ordered[0].value);
486
- let maximum = mapper(this.ordered[0].value);
487
- for (let i = 1; i < this.ordered.length; i++) {
488
- let current = mapper(this.ordered[i].value);
489
- if (current < minimum) {
490
- minimum = current;
491
- }
492
- if (current > maximum) {
493
- maximum = current;
494
- }
495
- }
496
- return maximum - minimum;
310
+ try {
311
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
312
+ let count = this.buffer.length;
313
+ let minimum = this.buffer[0].element;
314
+ let maximum = this.buffer[count - 1].element;
315
+ return mapper(maximum) - mapper(minimum);
316
+ }
317
+ catch (error) {
318
+ throw new Error("Uncaught error on range.");
497
319
  }
498
- throw new TypeError("Invalid arguments.");
499
320
  }
500
321
  variance(argument1) {
501
322
  if (this.isEmpty() || this.count() === 1n) {
502
323
  return 0n;
503
324
  }
504
- if (invalidate(argument1)) {
505
- let mean = this.mean();
506
- let summate = this.summate();
507
- return (summate / this.count()) - (mean * mean);
325
+ try {
326
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
327
+ return useBigIntVariance(mapper).collect(this.source());
508
328
  }
509
- else if (isFunction(argument1)) {
510
- let mapper = argument1;
511
- let mean = this.mean(mapper);
512
- let summate = this.summate(mapper);
513
- return (summate / this.count()) - (mean * mean);
329
+ catch (error) {
330
+ throw new Error("Uncaught error on variance.");
514
331
  }
515
- throw new TypeError("Invalid arguments.");
516
332
  }
517
333
  standardDeviation(argument1) {
518
- if (invalidate(argument1)) {
519
- return BigInt(Math.sqrt(Number(this.variance())));
520
- }
521
- else if (isFunction(argument1)) {
522
- let mapper = argument1;
334
+ try {
335
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
523
336
  let variance = this.variance(mapper);
524
337
  return BigInt(Math.sqrt(Number(variance)));
525
338
  }
526
- throw new TypeError("Invalid arguments.");
339
+ catch (error) {
340
+ throw new Error("Uncaught error on standardDeviation.");
341
+ }
527
342
  }
528
343
  mean(argument1) {
529
344
  if (this.isEmpty()) {
530
345
  return 0n;
531
346
  }
532
- if (invalidate(argument1)) {
533
- let summate = this.summate();
534
- return summate / this.count();
347
+ try {
348
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
349
+ return useBigIntAverage(mapper).collect(this.source());
535
350
  }
536
- else if (isFunction(argument1)) {
537
- let mapper = argument1;
538
- let summate = this.summate(mapper);
539
- return summate / this.count();
351
+ catch (error) {
352
+ throw new Error("Uncaught error on mean.");
540
353
  }
541
- throw new TypeError("Invalid arguments.");
542
354
  }
543
355
  median(argument1) {
544
356
  if (this.isEmpty()) {
545
357
  return 0n;
546
358
  }
547
- if (invalidate(argument1)) {
548
- let count = Number(this.count());
549
- let middle = Math.floor(count / 2);
550
- let median = BigInt(Number(this.ordered[middle].value));
551
- if (count % 2 === 0) {
552
- median = (median + BigInt(Number(this.ordered[middle - 1].value))) / 2n;
553
- return median;
554
- }
555
- return median;
556
- }
557
- else if (isFunction(argument1)) {
558
- let mapper = argument1;
559
- let count = this.count();
560
- let middle = count / 2n;
561
- let median = mapper(this.ordered[Number(middle)].value);
562
- if (count % 2n === 0n) {
563
- median = (median + mapper(this.ordered[Number(middle - 1n)].value)) / 2n;
564
- }
565
- return median;
359
+ try {
360
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
361
+ return useBigIntMedian(mapper).collect(this.source());
362
+ }
363
+ catch (error) {
364
+ throw new Error("Uncaught error on median.");
566
365
  }
567
- throw new TypeError("Invalid arguments.");
568
366
  }
569
367
  mode(argument1) {
570
368
  if (this.isEmpty()) {
571
369
  return 0n;
572
370
  }
573
- if (invalidate(argument1)) {
574
- let frequency = this.frequency();
575
- let mode = 0n;
576
- let maxFrequency = 0n;
577
- for (let [value, freq] of frequency) {
578
- if (freq > maxFrequency) {
579
- mode = value;
580
- maxFrequency = freq;
581
- }
582
- }
583
- return mode;
584
- }
585
- else if (isFunction(argument1)) {
586
- let mapper = argument1;
587
- let frequency = this.frequency(mapper);
588
- let mode = 0n;
589
- let maxFrequency = 0n;
590
- for (let [value, frequence] of frequency) {
591
- if (frequence > maxFrequency) {
592
- mode = value;
593
- maxFrequency = frequence;
594
- }
595
- }
596
- return mode;
371
+ try {
372
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
373
+ return useBigIntMode(mapper).collect(this.source());
597
374
  }
598
- throw new TypeError("Invalid arguments.");
599
- }
600
- frequency(argument1) {
601
- if (this.isEmpty()) {
602
- return new Map();
603
- }
604
- if (invalidate(argument1)) {
605
- let frequency = new Map();
606
- for (let i = 0; i < this.ordered.length; i++) {
607
- let current = BigInt(Number(this.ordered[i].value));
608
- let count = frequency.get(current) || 0n;
609
- frequency.set(current, count + 1n);
610
- }
611
- return frequency;
612
- }
613
- else if (isFunction(argument1)) {
614
- let mapper = argument1;
615
- let frequency = new Map();
616
- for (let i = 0; i < this.ordered.length; i++) {
617
- let current = mapper(this.ordered[i].value);
618
- let count = frequency.get(current) || 0n;
619
- frequency.set(current, count + 1n);
620
- }
621
- return frequency;
375
+ catch (error) {
376
+ throw new Error("Uncaught error on mode.");
622
377
  }
623
- throw new TypeError("Invalid arguments.");
624
378
  }
625
379
  summate(argument1) {
626
380
  if (this.isEmpty()) {
627
381
  return 0n;
628
382
  }
629
- if (invalidate(argument1)) {
630
- let summate = 0n;
631
- for (let i = 0; i < this.ordered.length; i++) {
632
- let current = BigInt(Number(this.ordered[i].value));
633
- summate += current;
634
- }
635
- return summate;
383
+ try {
384
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
385
+ return useBigIntSummate(mapper).collect(this.source());
636
386
  }
637
- else if (isFunction(argument1)) {
638
- let mapper = argument1;
639
- let summate = 0n;
640
- for (let i = 0; i < this.ordered.length; i++) {
641
- let current = mapper(this.ordered[i].value);
642
- summate += current;
643
- }
644
- return summate;
387
+ catch (error) {
388
+ throw new Error("Uncaught error on summate.");
645
389
  }
646
- throw new TypeError("Invalid arguments.");
647
390
  }
648
- quantile(quantile, mapper) {
391
+ quantile(quantile, argument1) {
649
392
  if (this.isEmpty()) {
650
393
  return 0n;
651
394
  }
652
395
  if (typeof quantile !== "number" || quantile < 0 || quantile > 1) {
653
396
  throw new RangeError("Invalid quantile.");
654
397
  }
655
- if (invalidate(mapper)) {
398
+ try {
399
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
656
400
  let count = Number(this.count());
657
401
  let index = Math.floor(quantile * count);
658
402
  if (index === count) {
659
403
  index--;
660
404
  }
661
- let value = BigInt(Number(this.ordered[index].value));
405
+ let value = mapper(this.buffer[index].element);
662
406
  return value;
663
407
  }
664
- else if (isFunction(mapper)) {
665
- let count = Number(this.count());
666
- let index = Math.floor(quantile * count);
667
- if (index === count) {
668
- index--;
669
- }
670
- let value = mapper(this.ordered[index].value);
671
- return value;
408
+ catch (error) {
409
+ throw new Error("Uncaught error on quantile.");
672
410
  }
673
- throw new TypeError("Invalid arguments.");
674
411
  }
675
412
  interquartileRange(argument1) {
676
413
  if (this.isEmpty()) {
677
414
  return 0n;
678
415
  }
679
- if (invalidate(argument1)) {
680
- let lower = this.quantile(0.25);
681
- let upper = this.quantile(0.75);
682
- return upper - lower;
683
- }
684
- else if (isFunction(argument1)) {
685
- let mapper = argument1;
416
+ try {
417
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
686
418
  let lower = this.quantile(0.25, mapper);
687
419
  let upper = this.quantile(0.75, mapper);
688
420
  return upper - lower;
689
421
  }
690
- throw new TypeError("Invalid arguments.");
422
+ catch (error) {
423
+ throw new Error("Uncaught error on interquartileRange.");
424
+ }
691
425
  }
692
426
  skewness(argument1) {
693
427
  if (this.isEmpty()) {
694
428
  return 0n;
695
429
  }
696
- if (invalidate(argument1)) {
697
- let mean = this.mean();
698
- let standardDeviation = this.standardDeviation();
699
- if (standardDeviation === 0n) {
700
- return 0n;
701
- }
702
- let data = this.toArray();
703
- let summate = 0n;
704
- for (let value of data) {
705
- let z = BigInt(Number(value)) - mean;
706
- summate += z * z * z;
707
- }
708
- return summate / BigInt(data.length);
709
- }
710
- else if (isFunction(argument1)) {
711
- let mapper = argument1;
430
+ try {
431
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
712
432
  let mean = this.mean(mapper);
713
433
  let standardDeviation = this.standardDeviation(mapper);
714
434
  if (standardDeviation === 0n) {
@@ -722,29 +442,16 @@ export class BigIntStatistics extends Statistics {
722
442
  }
723
443
  return summate / BigInt(data.length);
724
444
  }
725
- throw new TypeError("Invalid arguments.");
445
+ catch (error) {
446
+ throw new Error("Uncaught error on skewness.");
447
+ }
726
448
  }
727
449
  kurtosis(argument1) {
728
450
  if (this.isEmpty()) {
729
451
  return 0n;
730
452
  }
731
- if (invalidate(argument1)) {
732
- let mean = this.mean();
733
- let standardDeviation = this.standardDeviation();
734
- if (standardDeviation === 0n) {
735
- return 0n;
736
- }
737
- let data = this.toArray();
738
- let summate = 0n;
739
- let count = data.length;
740
- for (let value of data) {
741
- let z = BigInt(Number(value)) - mean;
742
- summate += z * z * z * z;
743
- }
744
- return summate / BigInt(count * count) - 3n;
745
- }
746
- else if (isFunction(argument1)) {
747
- let mapper = argument1;
453
+ try {
454
+ let mapper = isFunction(argument1) ? argument1 : useToBigInt;
748
455
  let mean = this.mean(mapper);
749
456
  let standardDeviation = this.standardDeviation(mapper);
750
457
  if (standardDeviation === 0n) {
@@ -759,7 +466,12 @@ export class BigIntStatistics extends Statistics {
759
466
  }
760
467
  return summate / BigInt(count * count) - 3n;
761
468
  }
762
- throw new TypeError("Invalid arguments.");
469
+ catch (error) {
470
+ throw new Error("Uncaught error on kurtosis.");
471
+ }
763
472
  }
764
473
  }
765
474
  ;
475
+ Object.freeze(BigIntStatistics);
476
+ Object.freeze(BigIntStatistics.prototype);
477
+ Object.freeze(Object.getPrototypeOf(BigIntStatistics));