semantic-typescript 0.2.6 → 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.
package/dist/factory.d.ts CHANGED
@@ -1,12 +1,13 @@
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
- export declare let event: BiFunctional<HTMLElement, Array<keyof HTMLElementEventMap>, Semantic<Event>>;
6
6
  export declare let fill: (<E>(element: E, count: bigint) => Semantic<E>) & (<E>(supplier: Supplier<E>, count: bigint) => Semantic<E>);
7
7
  export declare let from: <E>(iterable: Iterable<E>) => Semantic<E>;
8
8
  export declare let generate: (<E>(supplier: Supplier<E>, interrupt: Predicate<E>) => Semantic<E>) & (<E>(supplier: Supplier<E>, interrupt: BiPredicate<E, bigint>) => Semantic<E>);
9
9
  export declare let interval: Functional<number, Semantic<number>> & BiFunctional<number, number, Semantic<number>>;
10
10
  export declare let iterate: <E>(generator: Generator<E>) => Semantic<E>;
11
+ export declare let promise: (<T>(promise: Promise<T>) => Semantic<T>);
11
12
  export declare let range: BiFunctional<number, number, Semantic<number>> & TriFunctional<number, number, number, Semantic<number>>;
12
13
  export declare let websocket: Functional<WebSocket, Semantic<MessageEvent | CloseEvent | Event>>;
package/dist/factory.js CHANGED
@@ -1,7 +1,30 @@
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
30
  if (size <= 0 || !Number.isSafeInteger(size)) {
@@ -20,10 +43,10 @@ export let blob = (blob, chunk = 64n * 1024n) => {
20
43
  (async () => {
21
44
  try {
22
45
  while (!stoppable) {
23
- const { done, value } = await reader.read();
46
+ let { done, value } = await reader.read();
24
47
  if (done) {
25
48
  if (offset > 0) {
26
- const element = buffer.subarray(0, offset);
49
+ let element = buffer.subarray(0, offset);
27
50
  if (interrupt(element, index)) {
28
51
  stoppable = true;
29
52
  }
@@ -37,8 +60,8 @@ export let blob = (blob, chunk = 64n * 1024n) => {
37
60
  let chunkData = value;
38
61
  let position = 0;
39
62
  while (position < chunkData.length && !stoppable) {
40
- const space = size - offset;
41
- const toCopy = Math.min(space, chunkData.length - position);
63
+ let space = size - offset;
64
+ let toCopy = Math.min(space, chunkData.length - position);
42
65
  buffer.set(chunkData.subarray(position, position + toCopy), offset);
43
66
  offset += toCopy;
44
67
  position += toCopy;
@@ -56,7 +79,7 @@ export let blob = (blob, chunk = 64n * 1024n) => {
56
79
  }
57
80
  }
58
81
  catch (error) {
59
- // Handle error
82
+ console.error(error);
60
83
  }
61
84
  finally {
62
85
  if (stoppable) {
@@ -70,26 +93,6 @@ export let blob = (blob, chunk = 64n * 1024n) => {
70
93
  export let empty = () => {
71
94
  return new Semantic(() => { });
72
95
  };
73
- export let event = (element, events) => {
74
- if (validate(element) && isIterable(events)) {
75
- return new Semantic((accept, interrupt) => {
76
- let index = 0n;
77
- let stop = false;
78
- for (let name of events) {
79
- if (!stop) {
80
- element.addEventListener(name, (event) => {
81
- if (interrupt(event, index)) {
82
- stop = true;
83
- }
84
- accept(event, index);
85
- index++;
86
- });
87
- }
88
- }
89
- });
90
- }
91
- throw new TypeError("Invalid arguments.");
92
- };
93
96
  export let fill = (element, count) => {
94
97
  if (validate(element) && count > 0n) {
95
98
  return new Semantic((accept, interrupt) => {
@@ -174,6 +177,23 @@ export let iterate = (generator) => {
174
177
  }
175
178
  throw new TypeError("Invalid arguments.");
176
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
+ };
177
197
  export let range = (start, end, step = 1) => {
178
198
  if ((!isNumber(step) && !isBigInt(step)) || (isNumber(step) && useCompare(step, 0) === 0) || (isBigInt(step) && useCompare(step, 0n) === 0)) {
179
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";
@@ -34,7 +34,7 @@ export declare class Semantic<E> {
34
34
  toBigintStatistics(): BigIntStatistics<E>;
35
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.6",
9
+ "version": "0.2.7",
10
10
  "type": "module",
11
11
  "readme": "readme.md",
12
12
  "main": "dist/index.js",