semantic-typescript 0.2.5 → 0.2.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.
@@ -13,8 +13,8 @@ export class Collectable {
13
13
  anyMatch(predicate) {
14
14
  return this.collect(() => {
15
15
  return false;
16
- }, (element) => {
17
- return predicate(element);
16
+ }, (_element, _index, accumulator) => {
17
+ return accumulator;
18
18
  }, (result, element) => {
19
19
  return result || predicate(element);
20
20
  }, (result) => {
@@ -24,8 +24,8 @@ export class Collectable {
24
24
  allMatch(predicate) {
25
25
  return this.collect(() => {
26
26
  return true;
27
- }, (element) => {
28
- return !predicate(element);
27
+ }, (_element, _index, accumulator) => {
28
+ return !accumulator;
29
29
  }, (result, element) => {
30
30
  return result && predicate(element);
31
31
  }, (result) => {
@@ -69,20 +69,23 @@ export class Collectable {
69
69
  }
70
70
  findAny() {
71
71
  return this.collect(() => {
72
- return Optional.ofNullable();
73
- }, () => {
74
- return true;
72
+ return Optional.empty();
73
+ }, (_element, _index, accumulator) => {
74
+ return accumulator.isPresent();
75
75
  }, (result, element) => {
76
- return result.isPresent() && Math.random() > 0.5 ? result : Optional.of(element);
76
+ if (Math.random() < 0.5) {
77
+ return Optional.of(element);
78
+ }
79
+ return result;
77
80
  }, (result) => {
78
81
  return result;
79
82
  });
80
83
  }
81
84
  findFirst() {
82
85
  return this.collect(() => {
83
- return Optional.ofNullable();
84
- }, () => {
85
- return true;
86
+ return Optional.empty();
87
+ }, (_element, _index, accumulator) => {
88
+ return accumulator.isPresent();
86
89
  }, (result, element) => {
87
90
  return result.isPresent() ? result : Optional.of(element);
88
91
  }, (result) => {
@@ -91,9 +94,9 @@ export class Collectable {
91
94
  }
92
95
  findLast() {
93
96
  return this.collect(() => {
94
- return Optional.ofNullable();
97
+ return Optional.empty();
95
98
  }, () => {
96
- return true;
99
+ return false;
97
100
  }, (result, element) => {
98
101
  return result.isPresent() ? result : Optional.of(element);
99
102
  }, (result) => {
@@ -206,10 +209,10 @@ export class Collectable {
206
209
  nonMatch(predicate) {
207
210
  return this.collect(() => {
208
211
  return true;
209
- }, (element) => {
210
- return predicate(element);
212
+ }, (_element, _index, accumulator) => {
213
+ return !accumulator;
211
214
  }, (result, element) => {
212
- return result || predicate(element);
215
+ return result || !predicate(element);
213
216
  }, (result) => {
214
217
  return result;
215
218
  });
@@ -13,6 +13,8 @@ export declare class Collector<E, A, R> {
13
13
  collect(generator: Generator<E>): R;
14
14
  collect(iterable: Iterable<E>): R;
15
15
  collect(semantic: Semantic<E>): R;
16
+ collect(start: number, end: number): R;
17
+ collect(start: bigint, end: bigint): R;
16
18
  static full<E, A, R>(identity: Supplier<A>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
17
19
  static full<E, A, R>(identity: Supplier<A>, accumulator: TriFunctional<A, E, bigint, A>, finisher: Functional<A, R>): Collector<E, A, R>;
18
20
  static shortable<E, A, R>(identity: Supplier<A>, interruptor: Predicate<E>, accumulator: BiFunctional<A, E, A>, finisher: Functional<A, R>): Collector<E, A, R>;
package/dist/collector.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isFunction, isIterable, isSemantic } from "./guard";
1
+ import { isBigInt, isFunction, isIterable, isNumber, isSemantic } from "./guard";
2
2
  import { CollectableSymbol } from "./symbol";
3
3
  export class Collector {
4
4
  identity;
@@ -17,17 +17,18 @@ export class Collector {
17
17
  throw new TypeError("Invalid arguments");
18
18
  }
19
19
  }
20
- collect(parameter) {
20
+ collect(argument1, argument2) {
21
21
  let accumulator = this.identity();
22
22
  let count = 0n;
23
- if (isFunction(parameter)) {
24
- parameter((element, index) => {
23
+ if (isFunction(argument1)) {
24
+ let generator = argument1;
25
+ generator((element, index) => {
25
26
  accumulator = this.accumulator(accumulator, element, index);
26
27
  count++;
27
28
  }, (element, index) => this.interrupt(element, index, accumulator));
28
29
  }
29
- else if (isIterable(parameter)) {
30
- let iterable = parameter;
30
+ else if (isIterable(argument1)) {
31
+ let iterable = argument1;
31
32
  let index = 0n;
32
33
  for (let element of iterable) {
33
34
  if (this.interrupt(element, index, accumulator)) {
@@ -38,8 +39,8 @@ export class Collector {
38
39
  index++;
39
40
  }
40
41
  }
41
- else if (isSemantic(parameter)) {
42
- let semantic = parameter;
42
+ else if (isSemantic(argument1)) {
43
+ let semantic = argument1;
43
44
  let generator = Reflect.get(semantic, "generator");
44
45
  if (isFunction(generator)) {
45
46
  generator((element, index) => {
@@ -51,6 +52,28 @@ export class Collector {
51
52
  throw new TypeError("Invalid arguments");
52
53
  }
53
54
  }
55
+ else if (isNumber(argument1) && isNumber(argument2)) {
56
+ let start = argument1 < argument2 ? argument1 : argument2;
57
+ let end = argument1 > argument2 ? argument1 : argument2;
58
+ for (let i = start; i < end; i++) {
59
+ if (this.interrupt(i, count, accumulator)) {
60
+ break;
61
+ }
62
+ accumulator = this.accumulator(accumulator, i, count);
63
+ count++;
64
+ }
65
+ }
66
+ else if (isBigInt(argument1) && isBigInt(argument2)) {
67
+ let start = argument1 < argument2 ? argument1 : argument2;
68
+ let end = argument1 > argument2 ? argument1 : argument2;
69
+ for (let i = start; i < end; i++) {
70
+ if (this.interrupt(i, count, accumulator)) {
71
+ break;
72
+ }
73
+ accumulator = this.accumulator(accumulator, i, count);
74
+ count++;
75
+ }
76
+ }
54
77
  return this.finisher(accumulator);
55
78
  }
56
79
  static full(identity, accumulator, finisher) {
package/dist/factory.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Semantic } from "./semantic";
2
2
  import type { BiFunctional, BiPredicate, Functional, Predicate, Supplier, TriFunctional, Generator } from "./utility";
3
+ export declare let animationFrame: Functional<number, Semantic<number>> & BiFunctional<number, number, Semantic<number>>;
3
4
  export declare let blob: Functional<Blob, Semantic<Uint8Array>> & BiFunctional<Blob, bigint, Semantic<Uint8Array>>;
4
5
  export declare let empty: <E>() => Semantic<E>;
5
6
  export declare let fill: (<E>(element: E, count: bigint) => Semantic<E>) & (<E>(supplier: Supplier<E>, count: bigint) => Semantic<E>);
@@ -7,5 +8,6 @@ export declare let from: <E>(iterable: Iterable<E>) => Semantic<E>;
7
8
  export declare let generate: (<E>(supplier: Supplier<E>, interrupt: Predicate<E>) => Semantic<E>) & (<E>(supplier: Supplier<E>, interrupt: BiPredicate<E, bigint>) => Semantic<E>);
8
9
  export declare let interval: Functional<number, Semantic<number>> & BiFunctional<number, number, Semantic<number>>;
9
10
  export declare let iterate: <E>(generator: Generator<E>) => Semantic<E>;
11
+ export declare let promise: (<T>(promise: Promise<T>) => Semantic<T>);
10
12
  export declare let range: BiFunctional<number, number, Semantic<number>> & TriFunctional<number, number, number, Semantic<number>>;
11
13
  export declare let websocket: Functional<WebSocket, Semantic<MessageEvent | CloseEvent | Event>>;
package/dist/factory.js CHANGED
@@ -1,11 +1,34 @@
1
- import { isBigInt, isFunction, isIterable, isNumber } from "./guard";
1
+ import { isBigInt, isFunction, isIterable, isNumber, isPromise } from "./guard";
2
2
  import { useCompare } from "./hook";
3
3
  import { Semantic } from "./semantic";
4
4
  import { invalidate, validate } from "./utility";
5
+ export let animationFrame = (period, delay = 0) => {
6
+ if (period <= 0 || !Number.isFinite(period) || delay < 0 || !Number.isFinite(delay)) {
7
+ throw new TypeError("Period must be positive finite number and delay must be non-negative finite number.");
8
+ }
9
+ return new Semantic((accept, interrupt) => {
10
+ let start = performance.now();
11
+ let index = 0n;
12
+ let animate = () => {
13
+ if (performance.now() - start >= delay) {
14
+ requestAnimationFrame(animate);
15
+ }
16
+ else if (performance.now() - start < period) {
17
+ requestAnimationFrame(animate);
18
+ }
19
+ else {
20
+ if (interrupt(start, index)) {
21
+ return;
22
+ }
23
+ accept(performance.now(), index);
24
+ }
25
+ };
26
+ });
27
+ };
5
28
  export let blob = (blob, chunk = 64n * 1024n) => {
6
29
  let size = Number(chunk);
7
- if (size <= 0) {
8
- throw new RangeError("Chunk size must be positive.");
30
+ if (size <= 0 || !Number.isSafeInteger(size)) {
31
+ throw new RangeError("Chunk size must be a safe positive integer.");
9
32
  }
10
33
  if (invalidate(blob)) {
11
34
  throw new TypeError("Blob is invalid.");
@@ -17,42 +40,31 @@ export let blob = (blob, chunk = 64n * 1024n) => {
17
40
  let reader = stream.getReader();
18
41
  let buffer = new Uint8Array(size);
19
42
  let offset = 0;
20
- let read = async (reader) => {
21
- if (stoppable) {
22
- return reader.cancel().finally(() => reader.releaseLock());
23
- }
24
- return reader.read().then((result) => {
25
- if (result.done) {
26
- if (offset > 0) {
27
- const element = buffer.subarray(0, offset);
28
- if (interrupt(element, index)) {
29
- stoppable = true;
30
- }
31
- else {
32
- accept(element, index);
33
- index++;
43
+ (async () => {
44
+ try {
45
+ while (!stoppable) {
46
+ let { done, value } = await reader.read();
47
+ if (done) {
48
+ if (offset > 0) {
49
+ let element = buffer.subarray(0, offset);
50
+ if (interrupt(element, index)) {
51
+ stoppable = true;
52
+ }
53
+ else {
54
+ accept(element, index);
55
+ index++;
56
+ }
34
57
  }
58
+ break;
35
59
  }
36
- reader.releaseLock();
37
- return;
38
- }
39
- let chunkData = result.value;
40
- let length = chunkData.length;
41
- let remaining = size - offset;
42
- if (length > remaining) {
43
- buffer.set(chunkData.subarray(0, remaining), offset);
44
- if (interrupt(buffer, index)) {
45
- stoppable = true;
46
- }
47
- else {
48
- accept(buffer, index);
49
- index++;
50
- }
51
- offset = 0;
52
- const leftover = chunkData.subarray(remaining);
53
- if (leftover.length > 0) {
54
- buffer.set(leftover, offset);
55
- offset += leftover.length;
60
+ let chunkData = value;
61
+ let position = 0;
62
+ while (position < chunkData.length && !stoppable) {
63
+ let space = size - offset;
64
+ let toCopy = Math.min(space, chunkData.length - position);
65
+ buffer.set(chunkData.subarray(position, position + toCopy), offset);
66
+ offset += toCopy;
67
+ position += toCopy;
56
68
  if (offset === size) {
57
69
  if (interrupt(buffer, index)) {
58
70
  stoppable = true;
@@ -60,39 +72,22 @@ export let blob = (blob, chunk = 64n * 1024n) => {
60
72
  else {
61
73
  accept(buffer, index);
62
74
  index++;
63
- offset = 0;
64
75
  }
65
- }
66
- }
67
- }
68
- else {
69
- buffer.set(chunkData, offset);
70
- offset += length;
71
- if (offset === size) {
72
- if (interrupt(buffer, index)) {
73
- stoppable = true;
74
- }
75
- else {
76
- accept(buffer, index);
77
- index++;
78
76
  offset = 0;
79
77
  }
80
78
  }
81
79
  }
82
- if (!stoppable) {
83
- return read(reader);
84
- }
85
- else {
86
- return reader.cancel().finally(() => reader.releaseLock());
80
+ }
81
+ catch (error) {
82
+ console.error(error);
83
+ }
84
+ finally {
85
+ if (stoppable) {
86
+ await reader.cancel();
87
87
  }
88
- }).catch((error) => {
89
88
  reader.releaseLock();
90
- throw error;
91
- });
92
- };
93
- read(reader).catch(() => {
94
- reader.releaseLock();
95
- });
89
+ }
90
+ })();
96
91
  });
97
92
  };
98
93
  export let empty = () => {
@@ -182,6 +177,23 @@ export let iterate = (generator) => {
182
177
  }
183
178
  throw new TypeError("Invalid arguments.");
184
179
  };
180
+ export let promise = (promise) => {
181
+ if (isPromise(promise)) {
182
+ return new Semantic((accept, interrupt) => {
183
+ promise.then((value) => {
184
+ if (interrupt(value, 0n)) {
185
+ return;
186
+ }
187
+ accept(value, 0n);
188
+ }).catch((error) => {
189
+ console.error(error);
190
+ });
191
+ });
192
+ }
193
+ else {
194
+ throw new TypeError("Invalid arguments.");
195
+ }
196
+ };
185
197
  export let range = (start, end, step = 1) => {
186
198
  if ((!isNumber(step) && !isBigInt(step)) || (isNumber(step) && useCompare(step, 0) === 0) || (isBigInt(step) && useCompare(step, 0n) === 0)) {
187
199
  throw new TypeError("Step must be numeric and cannot be zero.");
package/dist/guard.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { Collectable, OrderedCollectable, UnorderedCollectable } from "./co
2
2
  import type { Collector } from "./collector";
3
3
  import type { Semantic } from "./semantic";
4
4
  import type { Statistics } from "./statistics";
5
- import type { MaybePrimitive, Primitive } from "./utility";
5
+ import type { AsyncFunction, MaybePrimitive, Primitive } from "./utility";
6
6
  export declare let isBoolean: (t: unknown) => t is boolean;
7
7
  export declare let isString: (t: unknown) => t is string;
8
8
  export declare let isNumber: (t: unknown) => t is number;
@@ -21,3 +21,5 @@ export declare let isUnorderedCollectable: (t: unknown) => t is UnorderedCollect
21
21
  export declare let isStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
22
22
  export declare let isNumericStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
23
23
  export declare let isBigIntStatistics: (t: unknown) => t is Statistics<unknown, number | bigint>;
24
+ export declare let isPromise: (t: unknown) => t is Promise<unknown>;
25
+ export declare let isAsync: (t: unknown) => t is AsyncFunction;
package/dist/guard.js CHANGED
@@ -83,3 +83,15 @@ export let isBigIntStatistics = (t) => {
83
83
  }
84
84
  return false;
85
85
  };
86
+ export let isPromise = (t) => {
87
+ if (isObject(t)) {
88
+ return isFunction(Reflect.get(t, "then")) && isFunction(Reflect.get(t, "catch"));
89
+ }
90
+ return false;
91
+ };
92
+ export let isAsync = (t) => {
93
+ if (isFunction(t)) {
94
+ return Reflect.get(t, Symbol.toStringTag) === "AsyncFunction" && t.constructor.name === "AsyncFunction";
95
+ }
96
+ return false;
97
+ };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
+ export * from "./collectable";
2
+ export * from "./collector";
1
3
  export * from "./factory";
2
- export * from "./utility";
3
- export * from "./symbol";
4
+ export * from "./guard";
4
5
  export * from "./hook";
5
6
  export * from "./optional";
6
7
  export * from "./semantic";
7
- export * from "./collectable";
8
+ export * from "./symbol";
8
9
  export * from "./statistics";
9
- export * from "./collector";
10
- export * from "./guard";
10
+ export * from "./utility";
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
+ export * from "./collectable";
2
+ export * from "./collector";
1
3
  export * from "./factory";
2
- export * from "./utility";
3
- export * from "./symbol";
4
+ export * from "./guard";
4
5
  export * from "./hook";
5
6
  export * from "./optional";
6
7
  export * from "./semantic";
7
- export * from "./collectable";
8
+ export * from "./symbol";
8
9
  export * from "./statistics";
9
- export * from "./collector";
10
- export * from "./guard";
10
+ export * from "./utility";
@@ -1,5 +1,5 @@
1
1
  import { Collectable, OrderedCollectable, UnorderedCollectable, WindowCollectable } from "./collectable";
2
- import { type Statistics } from "./statistics";
2
+ import { BigIntStatistics, NumericStatistics } from "./statistics";
3
3
  import { type Predicate } from "./utility";
4
4
  import type { Generator, Functional, BiFunctional, Consumer, BiConsumer, Comparator } from "./utility";
5
5
  export declare class Semantic<E> {
@@ -31,10 +31,10 @@ export declare class Semantic<E> {
31
31
  takeWhile(predicate: Predicate<E>): Semantic<E>;
32
32
  toCollectable(): Collectable<E>;
33
33
  toCollectable<C extends Collectable<E>>(mapper: Functional<Generator<E>, C>): C;
34
- toBigintStatistics(): Statistics<E, bigint>;
35
- toNumericStatistics(): Statistics<E, number>;
34
+ toBigintStatistics(): BigIntStatistics<E>;
35
+ toNumericStatistics(): NumericStatistics<E>;
36
36
  toOrdered(): OrderedCollectable<E>;
37
- toUnoredered(): UnorderedCollectable<E>;
37
+ toUnordered(): UnorderedCollectable<E>;
38
38
  toWindow(): WindowCollectable<E>;
39
39
  translate(offset: number): Semantic<E>;
40
40
  translate(offset: bigint): Semantic<E>;
package/dist/semantic.js CHANGED
@@ -302,7 +302,7 @@ export class Semantic {
302
302
  toOrdered() {
303
303
  return new OrderedCollectable(this.generator);
304
304
  }
305
- toUnoredered() {
305
+ toUnordered() {
306
306
  return new UnorderedCollectable(this.generator);
307
307
  }
308
308
  toWindow() {
package/dist/utility.d.ts CHANGED
@@ -5,6 +5,7 @@ export declare let validate: <T>(t: MaybeInvalid<T>) => t is T;
5
5
  export declare let invalidate: <T>(t: MaybeInvalid<T>) => t is (null | undefined);
6
6
  export type Primitive = string | number | boolean | symbol | bigint | Function | ((...args: any[]) => any);
7
7
  export type MaybePrimitive<T> = T | Primitive;
8
+ export type AsyncFunction = (...args: any[]) => Promise<unknown>;
8
9
  export interface Runnable {
9
10
  (): void;
10
11
  }
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.2.5",
9
+ "version": "0.2.7",
10
10
  "type": "module",
11
11
  "readme": "readme.md",
12
12
  "main": "dist/index.js",