semantic-typescript 0.6.0 → 0.7.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.
@@ -1,8 +1,8 @@
1
- import { isFunction, isIterable, isNumber, isBigInt, isBoolean, isString, isObject } from "../guard";
1
+ import { isFunction, isNumber, isBigInt, isBoolean, isString, isObject, isAsyncFunction, isIterable } from "../guard";
2
2
  import { useCompare, useToBigInt, useToNumber } from "../hook";
3
3
  import { Optional } from "../optional";
4
4
  import { SynchronousCollectorSymbol } from "../symbol";
5
- import { validate, invalidate } from "../utility";
5
+ import { invalidate, validate } from "../utility";
6
6
  export class SynchronousCollector {
7
7
  identity;
8
8
  interrupt;
@@ -54,7 +54,7 @@ export class SynchronousCollector {
54
54
  }
55
55
  }
56
56
  collect(argument1, argument2) {
57
- if (isFunction(argument1)) {
57
+ if (isAsyncFunction(argument1) || isFunction(argument1)) {
58
58
  try {
59
59
  let generator = argument1;
60
60
  let accumulator = this.identity();
@@ -66,29 +66,25 @@ export class SynchronousCollector {
66
66
  return this.finisher(accumulator);
67
67
  }
68
68
  catch (error) {
69
- console.error(error);
70
- throw new Error("Uncaught error on collect.");
69
+ throw error;
71
70
  }
72
71
  }
73
72
  else if (isIterable(argument1)) {
74
73
  try {
75
74
  let iterable = argument1;
76
- let index = 0n;
77
75
  let accumulator = this.identity();
78
76
  let count = 0n;
79
77
  for (let element of iterable) {
80
- if (this.interrupt(element, index, accumulator)) {
78
+ if (this.interrupt(element, count, accumulator)) {
81
79
  break;
82
80
  }
83
81
  accumulator = this.accumulator(accumulator, element, count);
84
82
  count++;
85
- index++;
86
83
  }
87
84
  return this.finisher(accumulator);
88
85
  }
89
86
  catch (error) {
90
- console.error(error);
91
- throw new Error("Uncaught error n collect.");
87
+ throw error;
92
88
  }
93
89
  }
94
90
  else if (isNumber(argument1) && isNumber(argument2)) {
@@ -107,8 +103,7 @@ export class SynchronousCollector {
107
103
  return this.finisher(accumulator);
108
104
  }
109
105
  catch (error) {
110
- console.error(error);
111
- throw new Error("Uncaught error on collect.");
106
+ throw error;
112
107
  }
113
108
  }
114
109
  else if (isBigInt(argument1) && isBigInt(argument2)) {
@@ -127,12 +122,23 @@ export class SynchronousCollector {
127
122
  return this.finisher(accumulator);
128
123
  }
129
124
  catch (error) {
130
- console.error(error);
131
- throw new Error("Uncaught error on collect.");
125
+ throw error;
132
126
  }
133
127
  }
134
128
  throw new Error("Invalid arguments.");
135
129
  }
130
+ getIdentity() {
131
+ return this.identity;
132
+ }
133
+ getInterrupt() {
134
+ return this.interrupt;
135
+ }
136
+ getAccumulator() {
137
+ return this.accumulator;
138
+ }
139
+ getFinisher() {
140
+ return this.finisher;
141
+ }
136
142
  static full(identity, accumulator, finisher) {
137
143
  return new SynchronousCollector(identity, () => false, accumulator, finisher);
138
144
  }
@@ -173,7 +179,7 @@ export let useSynchronousCollect = (argument1, argument2, argument3, argument4)
173
179
  throw new TypeError("Identity, accumulator, and finisher must be functions.");
174
180
  };
175
181
  export let useSynchronousCount = () => {
176
- return SynchronousCollector.full(() => 0n, (count) => count + 1n, (count) => count);
182
+ return SynchronousCollector.full(() => 0n, (count) => isBigInt(count) ? (count + 1n) : 1n, (count) => isBigInt(count) ? count : 0n);
177
183
  };
178
184
  ;
179
185
  export let useSynchronousError = (argument1, argument2, argument3) => {
@@ -215,65 +221,78 @@ export let useSynchronousFindAt = (index) => {
215
221
  let target = useToBigInt(index);
216
222
  if (target < 0n) {
217
223
  return SynchronousCollector.full(() => [], (accumulator, element) => {
218
- accumulator.push(element);
219
- return accumulator;
220
- }, (accumulator) => {
221
- if (accumulator.length === 0) {
222
- return Optional.empty();
224
+ let accumulated = Array.isArray(accumulator) ? accumulator : [];
225
+ accumulated.push(element);
226
+ return accumulated;
227
+ }, (result) => {
228
+ if (Array.isArray(result) && result.length > 0) {
229
+ let index = ((Number(target) % result.length) + result.length) % result.length;
230
+ return Optional.of(result[index]);
223
231
  }
224
- let limited = (((BigInt(accumulator.length)) % target) + target) % target;
225
- return Optional.ofNullable(accumulator[Number(limited)]);
232
+ return Optional.empty();
226
233
  });
227
234
  }
228
235
  return SynchronousCollector.shortable(() => [], (_element, _index, accumulator) => BigInt(accumulator.length) - 1n === target, (accumulator, element) => {
229
- accumulator.push(element);
230
- return accumulator;
231
- }, (accumulator) => {
232
- if (accumulator.length === 0) {
233
- return Optional.empty();
236
+ let accumulated = Array.isArray(accumulator) ? accumulator : [];
237
+ accumulated.push(element);
238
+ return accumulated;
239
+ }, (result) => {
240
+ let index = ((Number(target) % result.length) + result.length) % result.length;
241
+ if (Array.isArray(result) && result.length > 0) {
242
+ return Optional.of(result[index]);
234
243
  }
235
- return Optional.ofNullable(accumulator[Number(target)]);
244
+ return Optional.empty();
236
245
  });
237
246
  };
238
247
  export let useSynchronousFindFirst = () => {
239
248
  return SynchronousCollector.shortable(() => Optional.empty(), (_element, _index, accumulator) => validate(accumulator) && accumulator.isPresent(), (accumulator, element) => {
240
- if (validate(accumulator) && accumulator.isPresent()) {
241
- return accumulator;
249
+ if (validate(accumulator) && accumulator.isPresent() && validate(element)) {
250
+ return Optional.of(element);
242
251
  }
243
- return Optional.ofNullable(element);
244
- }, (accumulator) => accumulator);
252
+ return accumulator;
253
+ }, (result) => result);
245
254
  };
246
255
  export let useSynchronousFindAny = () => {
247
256
  return SynchronousCollector.shortable(() => Optional.empty(), (_element, _index, accumulator) => validate(accumulator) && accumulator.isPresent(), (accumulator, element) => {
248
- if (validate(accumulator) && accumulator.isPresent() && Math.random() < 0.5) {
249
- return accumulator;
257
+ if (validate(accumulator) && accumulator.isPresent() && validate(element) && Math.random() < 0.5) {
258
+ return Optional.of(element);
250
259
  }
251
- return Optional.ofNullable(element);
252
- }, (accumulator) => accumulator);
260
+ return accumulator;
261
+ }, (result) => result);
253
262
  };
254
263
  export let useSynchronousFindLast = () => {
255
264
  return SynchronousCollector.full(() => Optional.empty(), (accumulator, element) => {
256
- if (validate(accumulator) && accumulator.isPresent()) {
257
- return Optional.ofNullable(element);
265
+ if (validate(accumulator) && accumulator.isPresent() && validate(element)) {
266
+ return Optional.of(element);
258
267
  }
259
268
  return accumulator;
260
- }, (accumulator) => accumulator);
269
+ }, (result) => result);
261
270
  };
262
271
  export let useSynchronousFindMaximum = (comparator = (useCompare)) => {
263
- return SynchronousCollector.full(() => Optional.ofNullable(), (accumulator, element) => {
264
- if (accumulator.isPresent()) {
265
- return comparator(accumulator.get(), element) > 0 ? accumulator : Optional.ofNullable(element);
266
- }
267
- return Optional.ofNullable(element);
268
- }, (result) => result);
272
+ if (isFunction(comparator)) {
273
+ return SynchronousCollector.full(() => Optional.empty(), (accumulator, element) => {
274
+ if (validate(accumulator) && accumulator.isPresent()) {
275
+ return accumulator.map((previous) => {
276
+ return comparator(previous, element) > 0 ? previous : element;
277
+ });
278
+ }
279
+ return accumulator;
280
+ }, (result) => result);
281
+ }
282
+ throw new TypeError("Invalid argument.");
269
283
  };
270
284
  export let useSynchronousFindMinimum = (comparator = (useCompare)) => {
271
- return SynchronousCollector.full(() => Optional.ofNullable(), (accumulator, element) => {
272
- if (accumulator.isPresent()) {
273
- return comparator(accumulator.get(), element) < 0 ? accumulator : Optional.ofNullable(element);
274
- }
275
- return Optional.ofNullable(element);
276
- }, (result) => result);
285
+ if (isFunction(comparator)) {
286
+ return SynchronousCollector.full(() => Optional.empty(), (accumulator, element) => {
287
+ if (validate(accumulator) && accumulator.isPresent()) {
288
+ return accumulator.map((previous) => {
289
+ return comparator(previous, element) < 0 ? previous : element;
290
+ });
291
+ }
292
+ return accumulator;
293
+ }, (result) => result);
294
+ }
295
+ throw new TypeError("Invalid argument.");
277
296
  };
278
297
  ;
279
298
  export let useSynchronousForEach = (action) => {
@@ -364,7 +383,6 @@ export let useSynchronousLog = (argument1, argument2, argument3) => {
364
383
  }
365
384
  else {
366
385
  return SynchronousCollector.full(() => "[", (accumulator, element) => {
367
- console.log(element);
368
386
  if (isString(accumulator) && isString(element)) {
369
387
  return accumulator + element + ",";
370
388
  }
@@ -416,14 +434,11 @@ export let useSynchronousPartitionBy = (classifier) => {
416
434
  export let useSynchronousReduce = (argument1, argument2, argument3) => {
417
435
  if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
418
436
  let accumulator = argument1;
419
- return SynchronousCollector.full(() => Optional.ofNullable(), (result, element, index) => {
420
- if (result.isEmpty()) {
421
- return Optional.of(element);
422
- }
423
- else {
424
- let current = result.get();
425
- return Optional.of(accumulator(current, element, index));
437
+ return SynchronousCollector.full(() => Optional.empty(), (result, element, index) => {
438
+ if (validate(result) && result.isPresent()) {
439
+ return result.map((previous) => accumulator(previous, element, index));
426
440
  }
441
+ return result;
427
442
  }, (result) => result);
428
443
  }
429
444
  else if (validate(argument1) && isFunction(argument2) && invalidate(argument3)) {
@@ -1,9 +1,10 @@
1
+ import { type BiConsumer, type BiFunctional, type BiPredicate, type Comparator, type Consumer, type Functional, type Indexed, type Predicate, type Supplier, type SynchronousGenerator, type TriFunctional, type TriPredicate } from "../utility";
1
2
  import { SynchronousCollector } from "./collector";
2
3
  import type { Optional } from "../optional";
3
- import type { BiConsumer, BiFunctional, BiPredicate, Comparator, Consumer, Functional, Indexed, Predicate, Supplier, SynchronousGenerator, TriFunctional, TriPredicate } from "../utility";
4
4
  export declare class SynchronousSemantic<E> {
5
5
  protected generator: SynchronousGenerator<E>;
6
6
  protected readonly SynchronousSemantic: Symbol;
7
+ [Symbol.toStringTag]: string;
7
8
  constructor(generator: SynchronousGenerator<E>);
8
9
  concat(other: SynchronousSemantic<E>): SynchronousSemantic<E>;
9
10
  concat(other: Iterable<E>): SynchronousSemantic<E>;
@@ -22,8 +23,8 @@ export declare class SynchronousSemantic<E> {
22
23
  flatMap<R>(mapper: BiFunctional<E, bigint, Iterable<R>>): SynchronousSemantic<R>;
23
24
  flatMap<R>(mapper: Functional<E, SynchronousSemantic<R>>): SynchronousSemantic<R>;
24
25
  flatMap<R>(mapper: BiFunctional<E, bigint, SynchronousSemantic<R>>): SynchronousSemantic<R>;
25
- limit(n: number): SynchronousSemantic<E>;
26
- limit(n: bigint): SynchronousSemantic<E>;
26
+ limit(count: number): SynchronousSemantic<E>;
27
+ limit(count: bigint): SynchronousSemantic<E>;
27
28
  map<R>(mapper: Functional<E, R>): SynchronousSemantic<R>;
28
29
  map<R>(mapper: BiFunctional<E, bigint, R>): SynchronousSemantic<R>;
29
30
  peek(consumer: Consumer<E>): SynchronousSemantic<E>;
@@ -32,16 +33,15 @@ export declare class SynchronousSemantic<E> {
32
33
  reverse(): SynchronousSemantic<E>;
33
34
  shuffle(): SynchronousSemantic<E>;
34
35
  shuffle(mapper: BiFunctional<E, bigint, bigint>): SynchronousSemantic<E>;
35
- skip(n: number): SynchronousSemantic<E>;
36
- skip(n: bigint): SynchronousSemantic<E>;
36
+ skip(count: number): SynchronousSemantic<E>;
37
+ skip(count: bigint): SynchronousSemantic<E>;
37
38
  source(): SynchronousGenerator<E>;
38
- sorted(): SynchronousOrderedCollectable<E>;
39
- sorted(comparator: Comparator<E>): SynchronousOrderedCollectable<E>;
39
+ sub(start: number, end: number): SynchronousSemantic<E>;
40
40
  sub(start: bigint, end: bigint): SynchronousSemantic<E>;
41
41
  takeWhile(predicate: Predicate<E>): SynchronousSemantic<E>;
42
42
  takeWhile(predicate: BiPredicate<E, bigint>): SynchronousSemantic<E>;
43
- toSynchronousCollectable(): SynchronousCollectable<E>;
44
- toSynchronousCollectable<C extends SynchronousCollectable<E>>(mapper: Functional<SynchronousGenerator<E>, C>): C;
43
+ toCollectable(): SynchronousCollectable<E>;
44
+ toCollectable<C extends SynchronousCollectable<E>>(mapper: Functional<SynchronousGenerator<E>, C>): C;
45
45
  toBigintStatistics(): SynchronousBigIntStatistics<E>;
46
46
  toNumericStatistics(): SynchronousNumericStatistics<E>;
47
47
  toOrdered(): SynchronousOrderedCollectable<E>;
@@ -51,13 +51,15 @@ export declare class SynchronousSemantic<E> {
51
51
  translate(offset: bigint): SynchronousSemantic<E>;
52
52
  translate(translator: BiFunctional<E, bigint, bigint>): SynchronousSemantic<E>;
53
53
  }
54
- export declare abstract class SynchronousCollectable<E> {
54
+ export declare abstract class SynchronousCollectable<E> implements Iterable<E> {
55
55
  protected readonly SynchronousCollectable: symbol;
56
+ [Symbol.toStringTag]: string;
56
57
  constructor();
57
- abstract [Symbol.iterator](): globalThis.Generator<E, void, undefined>;
58
- abstract [Symbol.asyncIterator](): globalThis.AsyncGenerator<E, void, undefined>;
58
+ [Symbol.iterator](): globalThis.Generator<E, void, undefined>;
59
59
  anyMatch(predicate: Predicate<E>): boolean;
60
+ anyMatch(predicate: BiPredicate<E, bigint>): boolean;
60
61
  allMatch(predicate: Predicate<E>): boolean;
62
+ allMatch(predicate: BiPredicate<E, bigint>): boolean;
61
63
  collect<A, R>(collector: SynchronousCollector<E, A, R>): R;
62
64
  collect<A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): R;
63
65
  collect<A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): R;
@@ -104,8 +106,8 @@ export declare abstract class SynchronousCollectable<E> {
104
106
  partition(count: bigint): Array<Array<E>>;
105
107
  partitionBy(classifier: Functional<E, bigint>): Array<Array<E>>;
106
108
  partitionBy(classifier: BiFunctional<E, bigint, bigint>): Array<Array<E>>;
107
- reduce(accumulator: BiFunctional<E, E, E>): Optional<E>;
108
- reduce(accumulator: TriFunctional<E, E, bigint, E>): Optional<E>;
109
+ reduce(accumulator: BiFunctional<E, E, E>): E;
110
+ reduce(accumulator: TriFunctional<E, E, bigint, E>): E;
109
111
  reduce(identity: E, accumulator: BiFunctional<E, E, E>): E;
110
112
  reduce(identity: E, accumulator: TriFunctional<E, E, bigint, E>): E;
111
113
  reduce<R>(identity: R, accumulator: BiFunctional<R, E, R>, finisher: Functional<R, R>): R;
@@ -122,30 +124,32 @@ export declare abstract class SynchronousCollectable<E> {
122
124
  write<S = string>(stream: WritableStream<S>, accumulator: TriFunctional<WritableStream<S>, E, bigint, WritableStream<S>>): Promise<WritableStream<S>>;
123
125
  }
124
126
  export declare class SynchronousOrderedCollectable<E> extends SynchronousCollectable<E> {
125
- protected readonly SynchronousOrderedCollectable: symbol;
127
+ protected readonly OrderedCollectable: symbol;
126
128
  protected buffer: Array<Indexed<E>>;
129
+ [Symbol.toStringTag]: string;
127
130
  constructor(generator: SynchronousGenerator<E>);
128
131
  constructor(generator: SynchronousGenerator<E>, comparator: Comparator<E>);
129
132
  [Symbol.iterator](): globalThis.Generator<E, void, undefined>;
130
- [Symbol.asyncIterator](): globalThis.AsyncGenerator<E, void, undefined>;
133
+ allMatch(predicate: Predicate<E>): boolean;
134
+ allMatch(predicate: BiPredicate<E, bigint>): boolean;
131
135
  source(): SynchronousGenerator<E>;
132
136
  isEmpty(): boolean;
137
+ count(): bigint;
133
138
  }
134
139
  export declare class SynchronousUnorderedCollectable<E> extends SynchronousCollectable<E> {
135
140
  protected readonly SynchronousUnorderedCollectable: symbol;
141
+ [Symbol.toStringTag]: string;
136
142
  protected buffer: Map<bigint, E>;
137
143
  constructor(generator: SynchronousGenerator<E>);
138
- source(): SynchronousGenerator<E>;
139
144
  [Symbol.iterator](): globalThis.Generator<E, void, undefined>;
140
- [Symbol.asyncIterator](): globalThis.AsyncGenerator<E, void, undefined>;
145
+ source(): SynchronousGenerator<E>;
141
146
  }
142
147
  export declare abstract class SynchronousStatistics<E, D extends number | bigint> extends SynchronousOrderedCollectable<E> {
143
148
  protected readonly SynchronousStatistics: symbol;
149
+ [Symbol.toStringTag]: string;
144
150
  constructor(generator: SynchronousGenerator<E>);
145
151
  constructor(generator: SynchronousGenerator<E>, comparator: Comparator<E>);
146
152
  [Symbol.iterator](): globalThis.Generator<E, void, undefined>;
147
- [Symbol.asyncIterator](): globalThis.AsyncGenerator<E, void, undefined>;
148
- count(): bigint;
149
153
  abstract average(): D;
150
154
  abstract average(mapper: Functional<E, D>): D;
151
155
  abstract range(): D;
@@ -174,10 +178,10 @@ export declare abstract class SynchronousStatistics<E, D extends number | bigint
174
178
  }
175
179
  export declare class SynchronousBigIntStatistics<E> extends SynchronousStatistics<E, bigint> {
176
180
  protected readonly BigIntStatistics: symbol;
181
+ [Symbol.toStringTag]: string;
177
182
  constructor(generator: SynchronousGenerator<E>);
178
183
  constructor(generator: SynchronousGenerator<E>, comparator: Comparator<E>);
179
184
  [Symbol.iterator](): globalThis.Generator<E, void, undefined>;
180
- [Symbol.asyncIterator](): globalThis.AsyncGenerator<E, void, undefined>;
181
185
  average(): bigint;
182
186
  average(mapper: Functional<E, bigint>): bigint;
183
187
  range(): bigint;
@@ -205,10 +209,10 @@ export declare class SynchronousBigIntStatistics<E> extends SynchronousStatistic
205
209
  }
206
210
  export declare class SynchronousNumericStatistics<E> extends SynchronousStatistics<E, number> {
207
211
  protected readonly SynchronousNumericStatistics: symbol;
212
+ [Symbol.toStringTag]: string;
208
213
  constructor(generator: SynchronousGenerator<E>);
209
214
  constructor(generator: SynchronousGenerator<E>, comparator: Comparator<E>);
210
215
  [Symbol.iterator](): globalThis.Generator<E, void, undefined>;
211
- [Symbol.asyncIterator](): globalThis.AsyncGenerator<E, void, undefined>;
212
216
  average(): number;
213
217
  average(mapper: Functional<E, number>): number;
214
218
  range(): number;
@@ -236,10 +240,10 @@ export declare class SynchronousNumericStatistics<E> extends SynchronousStatisti
236
240
  }
237
241
  export declare class SynchronousWindowCollectable<E> extends SynchronousOrderedCollectable<E> {
238
242
  protected readonly WindowCollectable: symbol;
243
+ [Symbol.toStringTag]: string;
239
244
  constructor(generator: SynchronousGenerator<E>);
240
245
  constructor(generator: SynchronousGenerator<E>, comparator: Comparator<E>);
241
246
  [Symbol.iterator](): globalThis.Generator<E, void, undefined>;
242
- [Symbol.asyncIterator](): globalThis.AsyncGenerator<E, void, undefined>;
243
247
  slide(size: bigint, step?: bigint): SynchronousSemantic<SynchronousSemantic<E>>;
244
248
  tumble(size: bigint): SynchronousSemantic<SynchronousSemantic<E>>;
245
249
  }