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.
@@ -1,10 +1,9 @@
1
- import { Collector, useAllMatch, useAnyMatch, useCollect, useCount, useError, useFindAny, useFindFirst, useFindLast, useForEach, useGroup, useGroupBy, useJoin, useLog, useNoneMatch, usePartition, useReduce, useToArray, useToMap, useToSet, useWrite } from "./collector";
2
- import { from } from "./factory";
3
- import { isBigInt, isCollector, isFunction, isIterable, isObject, isString } from "./guard";
1
+ import { Collector, useAllMatch, useAnyMatch, useCollect, useCount, useError, useFindAny, useFindFirst, useFindLast, useFindMaximum, useFindMinimum, useForEach, useGroup, useGroupBy, useJoin, useLog, useNoneMatch, usePartition, usePartitionBy, useReduce, useToArray, useToAsyncGeneratorFunction, useToGeneratorFunction, useToMap, useToSet, useWrite } from "./collector";
2
+ import { isBigInt, isCollector, isFunction, isObject, isString } from "./guard";
4
3
  import { useCompare } from "./hook";
5
4
  import { Optional } from "./optional";
6
5
  import { Semantic } from "./semantic";
7
- import { CollectableSymbol, OrderedCollectableSymbol, UnorderedCollectableSymbol, WindowCollectableSymbol } from "./symbol";
6
+ import { CollectableSymbol, OrderedCollectableSymbol, UnorderedCollectableSymbol } from "./symbol";
8
7
  import { invalidate, validate } from "./utility";
9
8
  export class Collectable {
10
9
  Collectable = CollectableSymbol;
@@ -12,7 +11,12 @@ export class Collectable {
12
11
  }
13
12
  anyMatch(predicate) {
14
13
  if (isFunction(predicate)) {
15
- return useAnyMatch(predicate).collect(this);
14
+ try {
15
+ return useAnyMatch(predicate).collect(this);
16
+ }
17
+ catch (error) {
18
+ throw new Error("Uncaught error on anyMatch.");
19
+ }
16
20
  }
17
21
  throw new TypeError("Predicate must be a function.");
18
22
  }
@@ -20,43 +24,63 @@ export class Collectable {
20
24
  return useAllMatch(predicate).collect(this);
21
25
  }
22
26
  collect(argument1, argument2, argument3, argument4) {
23
- if (isCollector(argument1)) {
24
- let collector = argument1;
25
- return collector.collect(this);
26
- }
27
- if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3)) {
28
- let identity = argument1;
29
- let accumulator = argument2;
30
- let finisher = argument3;
31
- return useCollect(identity, accumulator, finisher).collect(this);
32
- }
33
- if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3) && isFunction(argument4)) {
34
- let identity = argument1;
35
- let interrupt = argument2;
36
- let accumulator = argument3;
37
- let finisher = argument4;
38
- return useCollect(identity, interrupt, accumulator, finisher).collect(this);
27
+ try {
28
+ if (isCollector(argument1)) {
29
+ let collector = argument1;
30
+ return collector.collect(this);
31
+ }
32
+ if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3)) {
33
+ let identity = argument1;
34
+ let accumulator = argument2;
35
+ let finisher = argument3;
36
+ return useCollect(identity, accumulator, finisher).collect(this);
37
+ }
38
+ if (isFunction(argument1) && isFunction(argument2) && isFunction(argument3) && isFunction(argument4)) {
39
+ let identity = argument1;
40
+ let interrupt = argument2;
41
+ let accumulator = argument3;
42
+ let finisher = argument4;
43
+ return useCollect(identity, interrupt, accumulator, finisher).collect(this);
44
+ }
45
+ }
46
+ catch (error) {
47
+ throw new Error("Uncaught error on collect.");
39
48
  }
40
49
  throw new TypeError("Invalid arguments.");
41
50
  }
42
51
  count() {
43
- return useCount().collect(this);
52
+ return useCount().collect(this.source());
44
53
  }
45
54
  error(argument1, argument2, argument3) {
46
55
  if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
47
- useError().collect(this);
56
+ try {
57
+ useError().collect(this.source());
58
+ }
59
+ catch (error) {
60
+ throw new Error("Uncaught error on error.");
61
+ }
48
62
  }
49
63
  else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
50
- let accumulator = argument1;
51
- useError(accumulator).collect(this);
64
+ try {
65
+ let accumulator = argument1;
66
+ useError(accumulator).collect(this.source());
67
+ }
68
+ catch (error) {
69
+ throw new Error("Uncaught error on error.");
70
+ }
52
71
  }
53
72
  else if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
54
- let prefix = argument1;
55
- let accumulator = argument2;
56
- let suffix = argument3;
57
- useError(prefix, accumulator, suffix).collect(this);
73
+ try {
74
+ let prefix = argument1;
75
+ let accumulator = argument2;
76
+ let suffix = argument3;
77
+ useError(prefix, accumulator, suffix).collect(this.source());
78
+ }
79
+ catch (error) {
80
+ throw new Error("Uncaught error on error.");
81
+ }
58
82
  }
59
- {
83
+ else {
60
84
  throw new TypeError("Invalid arguments.");
61
85
  }
62
86
  }
@@ -64,17 +88,55 @@ export class Collectable {
64
88
  return this.count() === 0n;
65
89
  }
66
90
  findAny() {
67
- return useFindAny().collect(this);
91
+ try {
92
+ return useFindAny().collect(this.source());
93
+ }
94
+ catch (error) {
95
+ throw new Error("Uncaught error on findAny.");
96
+ }
68
97
  }
69
98
  findFirst() {
70
- return useFindFirst().collect(this);
99
+ try {
100
+ return useFindFirst().collect(this.source());
101
+ }
102
+ catch (error) {
103
+ throw new Error("Uncaught error on findFirst.");
104
+ }
71
105
  }
72
106
  findLast() {
73
- return useFindLast().collect(this);
107
+ try {
108
+ return useFindLast().collect(this.source());
109
+ }
110
+ catch (error) {
111
+ throw new Error("Uncaught error on findLast.");
112
+ }
113
+ }
114
+ findMaximum(argument1) {
115
+ try {
116
+ let comparator = isFunction(argument1) ? argument1 : useCompare;
117
+ return useFindMaximum(comparator).collect(this.source());
118
+ }
119
+ catch (error) {
120
+ throw new Error("Uncaught error on findMaximum.");
121
+ }
122
+ }
123
+ findMinimum(argument1) {
124
+ try {
125
+ let comparator = isFunction(argument1) ? argument1 : useCompare;
126
+ return useFindMinimum(comparator).collect(this.source());
127
+ }
128
+ catch (error) {
129
+ throw new Error("Uncaught error on findMinimum.");
130
+ }
74
131
  }
75
132
  forEach(action) {
76
133
  if (isFunction(action)) {
77
- useForEach(action).collect(this);
134
+ try {
135
+ useForEach(action).collect(this);
136
+ }
137
+ catch (error) {
138
+ throw new Error("Uncaught error on forEach.");
139
+ }
78
140
  }
79
141
  else {
80
142
  throw new TypeError("Action must be a function.");
@@ -82,97 +144,162 @@ export class Collectable {
82
144
  }
83
145
  group(classifier) {
84
146
  if (isFunction(classifier)) {
85
- return useGroup(classifier).collect(this);
147
+ try {
148
+ return useGroup(classifier).collect(this.source());
149
+ }
150
+ catch (error) {
151
+ throw new Error("Uncaught error on group.");
152
+ }
86
153
  }
87
154
  throw new TypeError("Classifier must be a function.");
88
155
  }
89
156
  groupBy(keyExtractor, valueExtractor) {
90
157
  if (isFunction(keyExtractor) && isFunction(valueExtractor)) {
91
- return useGroupBy(keyExtractor, valueExtractor).collect(this);
158
+ try {
159
+ return useGroupBy(keyExtractor, valueExtractor).collect(this.source());
160
+ }
161
+ catch (error) {
162
+ throw new Error("Uncaught error on groupBy.");
163
+ }
92
164
  }
93
165
  throw new TypeError("Key and value extractors must be functions.");
94
166
  }
95
167
  join(argument1, argument2, argument3) {
96
168
  if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
97
- return useJoin().collect(this);
169
+ try {
170
+ return useJoin().collect(this.source());
171
+ }
172
+ catch (error) {
173
+ throw new Error("Uncaught error on join.");
174
+ }
98
175
  }
99
- if (isString(argument1) && invalidate(argument2) && invalidate(argument3)) {
100
- let delimiter = argument1;
101
- return useJoin(delimiter).collect(this);
176
+ else if (isString(argument1) && invalidate(argument2) && invalidate(argument3)) {
177
+ try {
178
+ let delimiter = argument1;
179
+ return useJoin(delimiter).collect(this.source());
180
+ }
181
+ catch (error) {
182
+ throw new Error("Uncaught error on join.");
183
+ }
102
184
  }
103
- if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
104
- let prefix = argument1;
105
- let accumulator = argument2;
106
- let suffix = argument3;
107
- return useJoin(prefix, accumulator, suffix).collect(this);
185
+ else if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
186
+ try {
187
+ let prefix = argument1;
188
+ let accumulator = argument2;
189
+ let suffix = argument3;
190
+ return useJoin(prefix, accumulator, suffix).collect(this.source());
191
+ }
192
+ catch (error) {
193
+ throw new Error("Uncaught error on join.");
194
+ }
108
195
  }
109
- if (isString(argument1) && isString(argument2) && isString(argument3)) {
110
- let prefix = argument1;
111
- let delimiter = argument2;
112
- let suffix = argument3;
113
- return useJoin(prefix, delimiter, suffix).collect(this);
196
+ else if (isString(argument1) && isString(argument2) && isString(argument3)) {
197
+ try {
198
+ let prefix = argument1;
199
+ let delimiter = argument2;
200
+ let suffix = argument3;
201
+ return useJoin(prefix, delimiter, suffix).collect(this.source());
202
+ }
203
+ catch (error) {
204
+ throw new Error("Uncaught error on join.");
205
+ }
114
206
  }
115
207
  throw new TypeError("Invalid arguments.");
116
208
  }
117
209
  log(argument1, argument2, argument3) {
118
- if (invalidate(argument1) && invalidate(argument2) && invalidate(argument3)) {
119
- useLog().collect(this);
120
- }
121
- else if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
122
- let accumulator = argument1;
123
- useLog(accumulator).collect(this);
210
+ if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
211
+ try {
212
+ let accumulator = argument1;
213
+ useLog(accumulator).collect(this.source());
214
+ }
215
+ catch (error) {
216
+ throw new Error("Uncaught error on log.");
217
+ }
124
218
  }
125
219
  else if (isString(argument1) && isFunction(argument2) && isString(argument3)) {
126
- let prefix = argument1;
127
- let accumulator = argument2;
128
- let suffix = argument3;
129
- useLog(prefix, accumulator, suffix).collect(this);
220
+ try {
221
+ let prefix = argument1;
222
+ let accumulator = argument2;
223
+ let suffix = argument3;
224
+ useLog(prefix, accumulator, suffix).collect(this.source());
225
+ }
226
+ catch (error) {
227
+ throw new Error("Uncaught error on log.");
228
+ }
130
229
  }
131
- {
132
- throw new TypeError("Invalid arguments.");
230
+ else {
231
+ try {
232
+ useLog().collect(this.source());
233
+ }
234
+ catch (error) {
235
+ throw new Error("Uncaught error on log.");
236
+ }
133
237
  }
134
238
  }
135
239
  nonMatch(predicate) {
136
240
  if (isFunction(predicate)) {
137
- return useNoneMatch(predicate).collect(this);
241
+ try {
242
+ return useNoneMatch(predicate).collect(this.source());
243
+ }
244
+ catch (error) {
245
+ throw new Error("Uncaught error on nonMatch.");
246
+ }
138
247
  }
139
248
  throw new TypeError("Predicate must be a function.");
140
249
  }
141
250
  partition(count) {
142
251
  if (isBigInt(count)) {
143
- return usePartition(count).collect(this);
252
+ try {
253
+ return usePartition(count).collect(this.source());
254
+ }
255
+ catch (error) {
256
+ throw new Error("Uncaught error on partition.");
257
+ }
144
258
  }
145
259
  throw new TypeError("Count must be a BigInt.");
146
260
  }
147
261
  partitionBy(classifier) {
148
- return this.collect(() => {
149
- return [];
150
- }, (array, element) => {
151
- let index = classifier(element);
152
- while (index > BigInt(array.length) - 1n) {
153
- array.push([]);
154
- }
155
- array[Number(index)].push(element);
156
- return array;
157
- }, (result) => {
158
- return result;
159
- });
262
+ if (isFunction(classifier)) {
263
+ try {
264
+ let collector = usePartitionBy(classifier);
265
+ return collector.collect(this.source());
266
+ }
267
+ catch (error) {
268
+ throw new Error("Uncaught error on partitionBy.");
269
+ }
270
+ }
271
+ throw new TypeError("Classifier must be a function.");
160
272
  }
161
273
  reduce(argument1, argument2, argument3) {
162
274
  if (isFunction(argument1) && invalidate(argument2) && invalidate(argument3)) {
163
- let accumulator = argument1;
164
- return useReduce(accumulator).collect(this);
275
+ try {
276
+ let accumulator = argument1;
277
+ return useReduce(accumulator).collect(this.source());
278
+ }
279
+ catch (error) {
280
+ throw new Error("Uncaught error on reduce.");
281
+ }
165
282
  }
166
283
  else if (validate(argument1) && isFunction(argument2) && invalidate(argument3)) {
167
- let identity = argument1;
168
- let accumulator = argument2;
169
- return useReduce(identity, accumulator).collect(this);
284
+ try {
285
+ let identity = argument1;
286
+ let accumulator = argument2;
287
+ return useReduce(identity, accumulator).collect(this.source());
288
+ }
289
+ catch (error) {
290
+ throw new Error("Uncaught error on reduce.");
291
+ }
170
292
  }
171
293
  else if (validate(argument1) && isFunction(argument2) && isFunction(argument3)) {
172
- let identity = argument1;
173
- let accumulator = argument2;
174
- let finisher = argument3;
175
- return useReduce(identity, accumulator, finisher).collect(this);
294
+ try {
295
+ let identity = argument1;
296
+ let accumulator = argument2;
297
+ let finisher = argument3;
298
+ return useReduce(identity, accumulator, finisher).collect(this.source());
299
+ }
300
+ catch (error) {
301
+ throw new Error("Uncaught error on reduce.");
302
+ }
176
303
  }
177
304
  else {
178
305
  throw new TypeError("Invalid arguments.");
@@ -180,34 +307,56 @@ export class Collectable {
180
307
  }
181
308
  semantic() {
182
309
  let source = this.source();
183
- if (isIterable(source)) {
184
- return from(source);
185
- }
186
- else if (isFunction(source)) {
187
- return new Semantic(source);
310
+ if (isFunction(source)) {
311
+ try {
312
+ return new Semantic(source);
313
+ }
314
+ catch (error) {
315
+ throw new Error("Uncaught error on semantic.");
316
+ }
188
317
  }
189
318
  else {
190
319
  throw new TypeError("Invalid source.");
191
320
  }
192
321
  }
193
322
  toArray() {
194
- return useToArray().collect(this);
323
+ try {
324
+ return useToArray().collect(this.source());
325
+ }
326
+ catch (error) {
327
+ throw new Error("Uncaught error on toArray.");
328
+ }
195
329
  }
196
330
  toMap(keyExtractor, valueExtractor) {
197
- return useToMap(keyExtractor, valueExtractor).collect(this);
331
+ try {
332
+ return useToMap(keyExtractor, valueExtractor).collect(this.source());
333
+ }
334
+ catch (error) {
335
+ throw new Error("Uncaught error on toMap.");
336
+ }
198
337
  }
199
338
  toSet() {
200
- return useToSet().collect(this);
339
+ try {
340
+ return useToSet().collect(this.source());
341
+ }
342
+ catch (error) {
343
+ throw new Error("Uncaught error on toSet.");
344
+ }
201
345
  }
202
346
  write(argument1, argument2) {
203
347
  if (isObject(argument1)) {
204
- let stream = argument1;
205
- if (isFunction(argument2)) {
206
- let accumulator = argument2;
207
- return useWrite(stream, accumulator);
348
+ try {
349
+ let stream = argument1;
350
+ if (isFunction(argument2)) {
351
+ let accumulator = argument2;
352
+ return useWrite(stream, accumulator).collect(this.source());
353
+ }
354
+ else {
355
+ return useWrite(stream).collect(this.source());
356
+ }
208
357
  }
209
- else {
210
- return useWrite(stream);
358
+ catch (error) {
359
+ throw new Error("Uncaught error on write.");
211
360
  }
212
361
  }
213
362
  throw new TypeError("Invalid arguments.");
@@ -217,106 +366,109 @@ export class Collectable {
217
366
  export class UnorderedCollectable extends Collectable {
218
367
  UnorderedCollectable = UnorderedCollectableSymbol;
219
368
  generator;
220
- constructor(generator) {
369
+ constructor(argument1) {
221
370
  super();
222
- this.generator = generator;
371
+ if (isFunction(argument1)) {
372
+ this.generator = argument1;
373
+ }
374
+ else {
375
+ throw new TypeError("Source must be an iterable or a generator function.");
376
+ }
223
377
  }
224
378
  source() {
225
379
  return this.generator;
226
380
  }
381
+ *[Symbol.iterator]() {
382
+ try {
383
+ let collector = useToGeneratorFunction();
384
+ yield* collector.collect(this.generator);
385
+ }
386
+ catch (error) {
387
+ throw new Error("Uncaught error on Generator.");
388
+ }
389
+ }
390
+ async *[Symbol.asyncIterator]() {
391
+ try {
392
+ let collector = useToAsyncGeneratorFunction();
393
+ yield* collector.collect(this.generator);
394
+ }
395
+ catch (error) {
396
+ throw new Error("Uncaught error on AsyncGenerator.");
397
+ }
398
+ }
227
399
  }
228
400
  ;
229
401
  export class OrderedCollectable extends Collectable {
230
402
  OrderedCollectable = OrderedCollectableSymbol;
231
- ordered = [];
403
+ buffer;
232
404
  constructor(argument1, argument2) {
233
405
  super();
234
- let buffer = [];
235
- if (isIterable(argument1)) {
236
- let iterable = argument1;
237
- let index = 0n;
238
- for (let element of iterable) {
239
- buffer.push({
240
- index: index,
241
- value: element
242
- });
243
- index++;
244
- }
245
- }
246
- else if (isFunction(argument1)) {
247
- let generator = argument1;
248
- generator((element, index) => {
249
- buffer.push({
250
- index: index,
251
- value: element
252
- });
253
- }, () => false);
406
+ if (isFunction(argument1)) {
407
+ try {
408
+ if (isFunction(argument2)) {
409
+ let collector = useToArray();
410
+ this.buffer = collector.collect(argument1).sort(argument2).map((element, index) => {
411
+ return {
412
+ element: element,
413
+ index: BigInt(index)
414
+ };
415
+ });
416
+ }
417
+ else {
418
+ let collector = useToArray();
419
+ this.buffer = collector.collect(argument1).map((element, index) => {
420
+ return {
421
+ element: element,
422
+ index: BigInt(index)
423
+ };
424
+ }).sort((a, b) => {
425
+ return Number(a.index - b.index);
426
+ });
427
+ }
428
+ }
429
+ catch (error) {
430
+ throw new Error("Uncaught error on creating buffer.");
431
+ }
254
432
  }
255
433
  else {
256
- throw new TypeError("Invalid arguments.");
434
+ throw new TypeError("Source must be an iterable or a generator function.");
257
435
  }
258
- buffer.map((indexed, _index, array) => {
259
- let length = BigInt(array.length);
260
- return {
261
- index: ((indexed.index % length) + length) % length,
262
- value: indexed.value
263
- };
264
- }).sort((a, b) => {
265
- if (isFunction(argument2)) {
266
- let comparator = argument2;
267
- return comparator(a.value, b.value);
268
- }
269
- else {
270
- return useCompare(a.index, b.index);
271
- }
272
- }).forEach((indexed) => {
273
- this.ordered.push(indexed);
274
- });
275
436
  }
276
- source() {
277
- return this.ordered.map((indexed) => indexed.value);
437
+ *[Symbol.iterator]() {
438
+ try {
439
+ let collector = useToGeneratorFunction();
440
+ yield* collector.collect(this.source());
441
+ }
442
+ catch (error) {
443
+ throw new Error("Uncaught error on Generator.");
444
+ }
278
445
  }
279
- }
280
- ;
281
- export class WindowCollectable extends OrderedCollectable {
282
- WindowCollectable = WindowCollectableSymbol;
283
- constructor(parameter, comparator) {
284
- if (isIterable(parameter)) {
285
- if (isFunction(comparator)) {
286
- super(parameter, comparator);
287
- }
288
- else {
289
- super(parameter);
290
- }
446
+ async *[Symbol.asyncIterator]() {
447
+ try {
448
+ let collector = useToAsyncGeneratorFunction();
449
+ yield* collector.collect(this.source());
291
450
  }
292
- else if (isFunction(parameter)) {
293
- if (isFunction(comparator)) {
294
- super(parameter, comparator);
295
- }
296
- else {
297
- super(parameter);
298
- }
451
+ catch (error) {
452
+ throw new Error("Uncaught error on AsyncGenerator.");
299
453
  }
300
454
  }
301
- slide(size, step = 1n) {
302
- if (size > 0n && step > 0n) {
303
- let source = this.toArray();
304
- let windows = [];
305
- let windowStartIndex = 0n;
306
- while (windowStartIndex < BigInt(source.length)) {
307
- let windowEnd = windowStartIndex + size;
308
- let window = source.slice(Number(windowStartIndex), Number(windowEnd));
309
- if (window.length > 0) {
310
- windows.push(window);
455
+ source() {
456
+ try {
457
+ return (accept, interrupt) => {
458
+ for (let indexed of this.buffer) {
459
+ if (interrupt(indexed.element, indexed.index)) {
460
+ break;
461
+ }
462
+ accept(indexed.element, indexed.index);
311
463
  }
312
- windowStartIndex += step;
313
- }
314
- return from(windows).map((window) => from(window));
464
+ };
465
+ }
466
+ catch (error) {
467
+ throw new Error("Uncaught error on creating source.");
315
468
  }
316
- throw new RangeError("Invalid arguments.");
317
469
  }
318
- tumble(size) {
319
- return this.slide(size, size);
470
+ isEmpty() {
471
+ return this.buffer.length === 0;
320
472
  }
321
473
  }
322
474
  ;