semantic-typescript 0.3.7 → 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.
package/dist/hook.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useToArray } from "./collector";
2
2
  import { isBigInt, isFunction, isIterable, isNumber, isObject, isPrimitive } from "./guard";
3
- import { validate } from "./utility";
3
+ import { invalidate, validate } from "./utility";
4
4
  export let useCompare = (t1, t2) => {
5
5
  if (t1 === t2 || Object.is(t1, t2)) {
6
6
  return 0;
@@ -166,15 +166,58 @@ export let useArrange = (source, comparator) => {
166
166
  return useGenerator([]);
167
167
  };
168
168
  export let useToNumber = (target) => {
169
- if (isNumber(target)) {
170
- return target;
169
+ switch (typeof target) {
170
+ case "number":
171
+ return isNumber(target) ? target : 0;
172
+ case "boolean":
173
+ return target ? 1 : 0;
174
+ case "string":
175
+ let result = Number(target.trim());
176
+ return isNumber(result) ? result : 0;
177
+ case "bigint":
178
+ return Number(target);
179
+ case "object":
180
+ if (invalidate(target)) {
181
+ return 0;
182
+ }
183
+ if (target instanceof Date) {
184
+ return target.getTime();
185
+ }
186
+ if (Reflect.has(target, Symbol.toPrimitive)) {
187
+ let resolved = Reflect.apply(Reflect.get(target, Symbol.toPrimitive), target, ["default"]);
188
+ return isNumber(resolved) ? resolved : 0;
189
+ }
190
+ return 0;
191
+ default:
192
+ return 0;
171
193
  }
172
- let resolved = Reflect.apply(Reflect.get(target, Symbol.toPrimitive), target, ["default"]);
173
- return isNumber(resolved) ? resolved : 0;
174
194
  };
175
195
  export let useToBigInt = (target) => {
176
- if (isBigInt(target)) {
177
- return target;
196
+ switch (typeof target) {
197
+ case "number":
198
+ return isNumber(target) ? BigInt(target) : 0n;
199
+ case "boolean":
200
+ return target ? 1n : 0n;
201
+ case "string":
202
+ let regex = /^[-+]?\d+$/;
203
+ return regex.test(target) ? BigInt(target.trim()) : 0n;
204
+ case "bigint":
205
+ return target;
206
+ case "object":
207
+ if (invalidate(target)) {
208
+ return 0n;
209
+ }
210
+ if (Reflect.has(target, Symbol.toPrimitive)) {
211
+ let resolved = Reflect.apply(Reflect.get(target, Symbol.toPrimitive), target, ["default"]);
212
+ return isBigInt(resolved) ? resolved : BigInt(useToNumber(resolved));
213
+ }
214
+ return 0n;
215
+ default:
216
+ return 0n;
178
217
  }
179
- return BigInt(useToNumber(target));
218
+ };
219
+ export let useFunction = (target) => {
220
+ return Object.freeze((...args) => {
221
+ return target(...args);
222
+ });
180
223
  };
package/dist/index.d.ts CHANGED
@@ -5,7 +5,6 @@ export * from "./guard";
5
5
  export * from "./hook";
6
6
  export * from "./optional";
7
7
  export * from "./semantic";
8
- export * from "./symbol";
9
8
  export * from "./statistics";
10
9
  export * from "./utility";
11
10
  export * from "./window";
package/dist/index.js CHANGED
@@ -5,7 +5,6 @@ export * from "./guard";
5
5
  export * from "./hook";
6
6
  export * from "./optional";
7
7
  export * from "./semantic";
8
- export * from "./symbol";
9
8
  export * from "./statistics";
10
9
  export * from "./utility";
11
10
  export * from "./window";
package/dist/optional.js CHANGED
@@ -7,6 +7,21 @@ export class Optional {
7
7
  Optional = OptionalSymbol;
8
8
  constructor(value) {
9
9
  this.value = value;
10
+ Object.defineProperties(this, {
11
+ "value": {
12
+ value: this.value,
13
+ writable: false,
14
+ enumerable: true,
15
+ configurable: false
16
+ },
17
+ "Optional": {
18
+ value: this.Optional,
19
+ writable: false,
20
+ enumerable: false,
21
+ configurable: false
22
+ }
23
+ });
24
+ Object.freeze(this);
10
25
  }
11
26
  filter(predicate) {
12
27
  if (this.isPresent() && isFunction(predicate) && predicate(this.value)) {
@@ -29,10 +44,20 @@ export class Optional {
29
44
  }
30
45
  ifPresent(action, elseAction) {
31
46
  if (this.isPresent() && isFunction(action)) {
32
- action(this.value);
47
+ try {
48
+ action(this.value);
49
+ }
50
+ catch (error) {
51
+ throw new Error("Uncaught error on ifPresent.");
52
+ }
33
53
  }
34
54
  else if (isFunction(elseAction)) {
35
- elseAction();
55
+ try {
56
+ elseAction();
57
+ }
58
+ catch (error) {
59
+ throw new Error("Uncaught error on ifPresent elseAction.");
60
+ }
36
61
  }
37
62
  }
38
63
  isEmpty() {
@@ -43,7 +68,12 @@ export class Optional {
43
68
  }
44
69
  map(mapper) {
45
70
  if (this.isPresent() && isFunction(mapper)) {
46
- return new Optional(mapper(this.value));
71
+ try {
72
+ return new Optional(mapper(this.value));
73
+ }
74
+ catch (error) {
75
+ throw new Error("Uncaught error on map.");
76
+ }
47
77
  }
48
78
  return new Optional(null);
49
79
  }
@@ -74,3 +104,6 @@ export class Optional {
74
104
  }
75
105
  }
76
106
  ;
107
+ Object.freeze(Optional);
108
+ Object.freeze(Optional.prototype);
109
+ Object.freeze(Object.getPrototypeOf(Optional));
@@ -1,7 +1,7 @@
1
1
  import { Collectable, OrderedCollectable, UnorderedCollectable } from "./collectable";
2
2
  import { BigIntStatistics, NumericStatistics } from "./statistics";
3
3
  import { type Predicate } from "./utility";
4
- import type { Generator, Functional, BiFunctional, Consumer, BiConsumer, Comparator } from "./utility";
4
+ import type { Generator, Functional, BiFunctional, Consumer, BiConsumer, Comparator, BiPredicate } from "./utility";
5
5
  import { WindowCollectable } from "./window";
6
6
  export declare class Semantic<E> {
7
7
  protected generator: Generator<E>;
@@ -10,14 +10,24 @@ export declare class Semantic<E> {
10
10
  concat(other: Semantic<E>): Semantic<E>;
11
11
  concat(other: Iterable<E>): Semantic<E>;
12
12
  distinct(): Semantic<E>;
13
- distinct(comparator: Comparator<E>): Semantic<E>;
13
+ distinct<K>(keyExtractor: Functional<E, K>): Semantic<E>;
14
+ distinct<K>(keyExtractor: BiFunctional<E, bigint, K>): Semantic<E>;
14
15
  dropWhile(predicate: Predicate<E>): Semantic<E>;
16
+ dropWhile(predicate: BiPredicate<E, bigint>): Semantic<E>;
15
17
  filter(predicate: Predicate<E>): Semantic<E>;
16
- flat(mapper: Functional<E, Iterable<E> | Semantic<E>>): Semantic<E>;
17
- flatMap<R>(mapper: Functional<E, Iterable<R> | Semantic<R>>): Semantic<R>;
18
+ filter(predicate: BiPredicate<E, bigint>): Semantic<E>;
19
+ flat(mapper: Functional<E, Iterable<E>>): Semantic<E>;
20
+ flat(mapper: BiFunctional<E, bigint, Iterable<E>>): Semantic<E>;
21
+ flat(mapper: Functional<E, Semantic<E>>): Semantic<E>;
22
+ flat(mapper: BiFunctional<E, bigint, Semantic<E>>): Semantic<E>;
23
+ flatMap<R>(mapper: Functional<E, Iterable<R>>): Semantic<R>;
24
+ flatMap<R>(mapper: BiFunctional<E, bigint, Iterable<R>>): Semantic<R>;
25
+ flatMap<R>(mapper: Functional<E, Semantic<R>>): Semantic<R>;
26
+ flatMap<R>(mapper: BiFunctional<E, bigint, Semantic<R>>): Semantic<R>;
18
27
  limit(n: number): Semantic<E>;
19
28
  limit(n: bigint): Semantic<E>;
20
29
  map<R>(mapper: Functional<E, R>): Semantic<R>;
30
+ map<R>(mapper: BiFunctional<E, bigint, R>): Semantic<R>;
21
31
  peek(consumer: Consumer<E>): Semantic<E>;
22
32
  peek(consumer: BiConsumer<E, bigint>): Semantic<E>;
23
33
  redirect(redirector: BiFunctional<E, bigint, bigint>): Semantic<E>;
@@ -30,6 +40,7 @@ export declare class Semantic<E> {
30
40
  sorted(comparator: Comparator<E>): OrderedCollectable<E>;
31
41
  sub(start: bigint, end: bigint): Semantic<E>;
32
42
  takeWhile(predicate: Predicate<E>): Semantic<E>;
43
+ takeWhile(predicate: BiPredicate<E, bigint>): Semantic<E>;
33
44
  toCollectable(): Collectable<E>;
34
45
  toCollectable<C extends Collectable<E>>(mapper: Functional<Generator<E>, C>): C;
35
46
  toBigintStatistics(): BigIntStatistics<E>;
package/dist/semantic.js CHANGED
@@ -10,6 +10,21 @@ export class Semantic {
10
10
  Semantic = SemanticSymbol;
11
11
  constructor(generator) {
12
12
  this.generator = generator;
13
+ Object.defineProperties(this, {
14
+ "generator": {
15
+ value: generator,
16
+ writable: false,
17
+ enumerable: true,
18
+ configurable: false
19
+ },
20
+ "Semantic": {
21
+ value: SemanticSymbol,
22
+ writable: false,
23
+ enumerable: false,
24
+ configurable: false
25
+ }
26
+ });
27
+ Object.freeze(this);
13
28
  }
14
29
  concat(other) {
15
30
  if (isSemantic(other)) {
@@ -50,29 +65,15 @@ export class Semantic {
50
65
  }
51
66
  throw new TypeError("Invalid arguments.");
52
67
  }
53
- distinct(comparator) {
54
- if (validate(comparator)) {
55
- return new Semantic((accept, interrupt) => {
56
- try {
57
- let array = [];
58
- this.generator((element, index) => {
59
- if (!array.some((e) => comparator(e, element))) {
60
- array.push(element);
61
- accept(element, index);
62
- }
63
- }, interrupt);
64
- }
65
- catch (error) {
66
- throw new Error("Uncaught error on distinct.");
67
- }
68
- });
69
- }
68
+ distinct(argument1) {
69
+ let keyExtractor = validate(argument1) ? argument1 : (element) => element;
70
70
  return new Semantic((accept, interrupt) => {
71
71
  try {
72
72
  let set = new Set();
73
73
  this.generator((element, index) => {
74
- if (!set.has(element)) {
75
- set.add(element);
74
+ let key = keyExtractor(element, index);
75
+ if (!set.has(key)) {
76
+ set.add(key);
76
77
  accept(element, index);
77
78
  }
78
79
  }, interrupt);
@@ -89,7 +90,7 @@ export class Semantic {
89
90
  let dropping = true;
90
91
  this.generator((element, index) => {
91
92
  if (dropping) {
92
- if (!predicate(element)) {
93
+ if (!predicate(element, index)) {
93
94
  dropping = false;
94
95
  accept(element, index);
95
96
  }
@@ -110,7 +111,7 @@ export class Semantic {
110
111
  return new Semantic((accept, interrupt) => {
111
112
  try {
112
113
  this.generator((element, index) => {
113
- if (predicate(element)) {
114
+ if (predicate(element, index)) {
114
115
  accept(element, index);
115
116
  }
116
117
  }, interrupt);
@@ -128,8 +129,8 @@ export class Semantic {
128
129
  try {
129
130
  let count = 0n;
130
131
  let stop = false;
131
- this.generator((element) => {
132
- let result = mapper(element);
132
+ this.generator((element, index) => {
133
+ let result = mapper(element, index);
133
134
  if (isIterable(result)) {
134
135
  for (let subElement of result) {
135
136
  accept(subElement, count);
@@ -159,8 +160,8 @@ export class Semantic {
159
160
  try {
160
161
  let count = 0n;
161
162
  let stop = false;
162
- this.generator((element) => {
163
- let result = mapper(element);
163
+ this.generator((element, index) => {
164
+ let result = mapper(element, index);
164
165
  if (isIterable(result)) {
165
166
  for (let subElement of result) {
166
167
  accept(subElement, count);
@@ -227,7 +228,7 @@ export class Semantic {
227
228
  try {
228
229
  let stop = false;
229
230
  this.generator((element, index) => {
230
- let resolved = mapper(element);
231
+ let resolved = mapper(element, index);
231
232
  accept(resolved, index);
232
233
  stop = stop || interrupt(resolved, index);
233
234
  }, () => stop);
@@ -268,7 +269,7 @@ export class Semantic {
268
269
  }
269
270
  });
270
271
  }
271
- throw new TypeError("Invalid arguments.");
272
+ throw new TypeError("Invalid arguments on redirect.");
272
273
  }
273
274
  reverse() {
274
275
  return new Semantic((accept, interrupt) => {
@@ -344,7 +345,7 @@ export class Semantic {
344
345
  }
345
346
  });
346
347
  }
347
- throw new TypeError("Invalid arguments.");
348
+ throw new TypeError("Invalid arguments on skip.");
348
349
  }
349
350
  sorted(comparator) {
350
351
  if (isFunction(comparator)) {
@@ -363,38 +364,44 @@ export class Semantic {
363
364
  }
364
365
  }
365
366
  sub(start, end) {
366
- return new Semantic((accept, interrupt) => {
367
- try {
368
- let count = 0n;
369
- this.generator((element, index) => {
370
- if (count < end) {
371
- count++;
372
- if (count >= start) {
373
- accept(element, index);
367
+ if (isBigInt(start) && isBigInt(end)) {
368
+ return new Semantic((accept, interrupt) => {
369
+ try {
370
+ let count = 0n;
371
+ this.generator((element, index) => {
372
+ if (count < end) {
373
+ count++;
374
+ if (count >= start) {
375
+ accept(element, index);
376
+ }
374
377
  }
375
- }
376
- }, interrupt);
377
- }
378
- catch (error) {
379
- throw new Error("Uncaught error on sub.");
380
- }
381
- });
378
+ }, interrupt);
379
+ }
380
+ catch (error) {
381
+ throw new Error("Uncaught error on sub.");
382
+ }
383
+ });
384
+ }
385
+ throw new TypeError("Invalid arguments on sub.");
382
386
  }
383
387
  takeWhile(predicate) {
384
- return new Semantic((accept, interrupt) => {
385
- try {
386
- this.generator((element, index) => {
387
- if (!predicate(element)) {
388
- interrupt(element, index);
389
- return;
390
- }
391
- accept(element, index);
392
- }, interrupt);
393
- }
394
- catch (error) {
395
- throw new Error("Uncaught error on takeWhile.");
396
- }
397
- });
388
+ if (isFunction(predicate)) {
389
+ return new Semantic((accept, interrupt) => {
390
+ try {
391
+ this.generator((element, index) => {
392
+ if (!predicate(element, index)) {
393
+ interrupt(element, index);
394
+ return;
395
+ }
396
+ accept(element, index);
397
+ }, interrupt);
398
+ }
399
+ catch (error) {
400
+ throw new Error("Uncaught error on takeWhile.");
401
+ }
402
+ });
403
+ }
404
+ throw new TypeError("Invalid arguments.");
398
405
  }
399
406
  toCollectable(mapper) {
400
407
  if (isFunction(mapper)) {
@@ -469,7 +476,7 @@ export class Semantic {
469
476
  }
470
477
  });
471
478
  }
472
- else if (isBigInt(argument1)) {
479
+ if (isBigInt(argument1)) {
473
480
  let offset = argument1;
474
481
  return new Semantic((accept, interrupt) => {
475
482
  try {
@@ -482,7 +489,7 @@ export class Semantic {
482
489
  }
483
490
  });
484
491
  }
485
- else if (isFunction(argument1)) {
492
+ if (isFunction(argument1)) {
486
493
  let translator = argument1;
487
494
  return new Semantic((accept, interrupt) => {
488
495
  try {
@@ -499,3 +506,6 @@ export class Semantic {
499
506
  }
500
507
  }
501
508
  ;
509
+ Object.freeze(Semantic);
510
+ Object.freeze(Semantic.prototype);
511
+ Object.freeze(Object.getPrototypeOf(Semantic.prototype));
@@ -7,6 +7,13 @@ export class Statistics extends OrderedCollectable {
7
7
  Statistics = StatisticsSymbol;
8
8
  constructor(parameter, comparator) {
9
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);
10
17
  }
11
18
  *[Symbol.iterator]() {
12
19
  try {
@@ -34,10 +41,20 @@ export class Statistics extends OrderedCollectable {
34
41
  }
35
42
  }
36
43
  ;
44
+ Object.freeze(Statistics);
45
+ Object.freeze(Statistics.prototype);
46
+ Object.freeze(Object.getPrototypeOf(Statistics));
37
47
  export class NumericStatistics extends Statistics {
38
48
  NumericStatistics = NumericStatisticsSymbol;
39
49
  constructor(parameter, comparator) {
40
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);
41
58
  }
42
59
  *[Symbol.iterator]() {
43
60
  try {
@@ -241,10 +258,20 @@ export class NumericStatistics extends Statistics {
241
258
  }
242
259
  }
243
260
  ;
261
+ Object.freeze(NumericStatistics);
262
+ Object.freeze(NumericStatistics.prototype);
263
+ Object.freeze(Object.getPrototypeOf(NumericStatistics));
244
264
  export class BigIntStatistics extends Statistics {
245
265
  BigIntStatistics = BigIntStatisticsSymbol;
246
266
  constructor(parameter, comparator) {
247
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);
248
275
  }
249
276
  *[Symbol.iterator]() {
250
277
  try {
@@ -445,3 +472,6 @@ export class BigIntStatistics extends Statistics {
445
472
  }
446
473
  }
447
474
  ;
475
+ Object.freeze(BigIntStatistics);
476
+ Object.freeze(BigIntStatistics.prototype);
477
+ Object.freeze(Object.getPrototypeOf(BigIntStatistics));
package/dist/window.js CHANGED
@@ -7,6 +7,13 @@ export class WindowCollectable extends OrderedCollectable {
7
7
  WindowCollectable = WindowCollectableSymbol;
8
8
  constructor(parameter, comparator = useCompare) {
9
9
  super(parameter, comparator);
10
+ Object.defineProperty(this, "WindowCollectable", {
11
+ value: WindowCollectableSymbol,
12
+ enumerable: false,
13
+ writable: false,
14
+ configurable: false
15
+ });
16
+ Object.freeze(this);
10
17
  }
11
18
  *[Symbol.iterator]() {
12
19
  try {
@@ -58,3 +65,6 @@ export class WindowCollectable extends OrderedCollectable {
58
65
  }
59
66
  }
60
67
  ;
68
+ Object.freeze(WindowCollectable);
69
+ Object.freeze(WindowCollectable.prototype);
70
+ Object.freeze(Object.getPrototypeOf(WindowCollectable));
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "url": "https://github.com/eloyhere"
7
7
  },
8
8
  "description": "A modern type-safe stream processing library inspired by JavaScript Generator, Java Stream, and MySQL Index. Supports lazy evaluation, async streams, statistics, and IO-like operations.",
9
- "version": "0.3.7",
9
+ "version": "0.3.8",
10
10
  "type": "module",
11
11
  "readme": "readme.md",
12
12
  "main": "dist/index.js",