@code-essentials/utils 1.1.1 → 1.2.1

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,16 +1,16 @@
1
1
  export declare class AsyncVariable<T> implements PromiseLike<T> {
2
2
  #private;
3
+ static readonly ERR_NOT_RESOLVED = "async variable not resolved or rejected";
3
4
  get value(): T;
4
5
  set value(value: T);
5
- get error(): any;
6
- set error(error: any);
6
+ get error(): unknown;
7
+ set error(error: unknown);
7
8
  get complete(): boolean;
8
9
  constructor();
9
- init(): Promise<void>;
10
10
  read(): Promise<T>;
11
- then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2>;
12
- set(value: T, throw_if_set?: boolean): Promise<void>;
13
- reject(error: unknown, throw_if_set?: boolean): Promise<void>;
11
+ then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): AsyncVariable<TResult1 | TResult2>;
12
+ set(value: T, throw_if_set?: boolean): void;
13
+ reject(error: unknown, throw_if_set?: boolean): void;
14
14
  static performCallback<R = void>(fn: (cb: (err?: unknown, res?: R) => void) => void): AsyncVariable<R>;
15
15
  writeResult(task: PromiseLike<T>): Promise<void>;
16
16
  perform(fn: () => PromiseLike<T>): this;
@@ -2,16 +2,16 @@ export class AsyncVariable {
2
2
  #result;
3
3
  #result_res;
4
4
  #result_p;
5
- #initialization;
5
+ static ERR_NOT_RESOLVED = "async variable not resolved or rejected";
6
6
  get value() {
7
7
  if (!this.#result)
8
- throw new Error('incomplete');
8
+ throw new Error(AsyncVariable.ERR_NOT_RESOLVED);
9
9
  if (this.#result.type === "rejected")
10
10
  throw this.#result.error;
11
11
  return this.#result.value;
12
12
  }
13
13
  set value(value) {
14
- this.set(value);
14
+ void this.set(value);
15
15
  }
16
16
  get error() {
17
17
  if (!this.#result)
@@ -21,32 +21,24 @@ export class AsyncVariable {
21
21
  return this.#result.error;
22
22
  }
23
23
  set error(error) {
24
- this.reject(error);
24
+ void this.reject(error);
25
25
  }
26
26
  get complete() {
27
27
  return this.#result !== undefined;
28
28
  }
29
29
  constructor() {
30
- this.#initialization = new Promise(initialized =>
31
- // prevents unhandled rejection
32
- this.#result_p = new Promise(res => {
33
- this.#result_res = res;
34
- initialized();
35
- }));
36
- }
37
- async init() {
38
- await this.#initialization;
30
+ const { resolve, promise } = Promise.withResolvers();
31
+ this.#result_p = promise;
32
+ this.#result_res = (res => resolve(this.#result = res));
39
33
  }
40
34
  async read() {
41
- await this.#initialization;
42
35
  await this.#result_p;
43
36
  return this.value;
44
37
  }
45
38
  then(onfulfilled, onrejected) {
46
- return this.read().then(onfulfilled, onrejected);
39
+ return AsyncVariable.perform(() => this.read().then(onfulfilled, onrejected));
47
40
  }
48
- async #complete(throw_if_set = true) {
49
- await this.#initialization;
41
+ #complete(throw_if_set = true) {
50
42
  if (this.complete) {
51
43
  if (throw_if_set)
52
44
  throw new Error('already set');
@@ -55,16 +47,16 @@ export class AsyncVariable {
55
47
  }
56
48
  return true;
57
49
  }
58
- async set(value, throw_if_set = true) {
59
- if (await this.#complete(throw_if_set)) {
50
+ set(value, throw_if_set = true) {
51
+ if (this.#complete(throw_if_set)) {
60
52
  this.#result_res(this.#result = {
61
53
  type: "resolved",
62
54
  value
63
55
  });
64
56
  }
65
57
  }
66
- async reject(error, throw_if_set = true) {
67
- if (await this.#complete(throw_if_set)) {
58
+ reject(error, throw_if_set = true) {
59
+ if (this.#complete(throw_if_set)) {
68
60
  this.#result_res(this.#result = {
69
61
  type: "rejected",
70
62
  error
@@ -73,30 +65,30 @@ export class AsyncVariable {
73
65
  }
74
66
  static performCallback(fn) {
75
67
  const av = new AsyncVariable();
76
- fn(async (err, res) => {
77
- if (err)
78
- await av.error(err);
68
+ fn((err, res) => {
69
+ if (err !== undefined)
70
+ void av.reject(err);
79
71
  else
80
- await av.set(res);
72
+ void av.set(res);
81
73
  });
82
74
  return av;
83
75
  }
84
76
  async writeResult(task) {
85
77
  try {
86
- await this.set(await task);
78
+ this.set(await task);
87
79
  }
88
80
  catch (error) {
89
- await this.reject(error);
81
+ this.reject(error);
90
82
  }
91
83
  }
92
84
  perform(fn) {
93
- this.writeResult(fn());
85
+ void this.writeResult(fn());
94
86
  return this;
95
87
  }
96
88
  timeout(milliseconds) {
97
89
  AsyncVariable.wait(milliseconds).then(() => {
98
90
  if (!this.complete)
99
- this.reject("timeout");
91
+ void this.reject("timeout");
100
92
  });
101
93
  return this;
102
94
  }
@@ -105,7 +97,7 @@ export class AsyncVariable {
105
97
  }
106
98
  static wait(milliseconds) {
107
99
  const res = new AsyncVariable();
108
- setTimeout(() => res.set(), milliseconds);
100
+ setTimeout(() => void res.set(), milliseconds);
109
101
  return res;
110
102
  }
111
103
  }
package/dist/barrier.d.ts CHANGED
@@ -3,14 +3,14 @@ interface EnqueuedTask<T> {
3
3
  task: PromiseLike<T>;
4
4
  completion: AsyncVariable<T>;
5
5
  }
6
- export declare class Barrier<T = any> implements PromiseLike<T[]>, AsyncDisposable {
6
+ export declare class Barrier<T = unknown> implements PromiseLike<T[]>, AsyncDisposable {
7
7
  #private;
8
8
  readonly queue: EnqueuedTask<T>[];
9
9
  await(...asyncTasks: PromiseLike<T>[]): void;
10
- run(...asyncTasks: PromiseLike<T>[]): void;
10
+ run(...asyncFunctions: (() => PromiseLike<T>)[]): void;
11
11
  clear(): void;
12
12
  complete(): Promise<this>;
13
13
  [Symbol.asyncDispose](): Promise<void>;
14
- then<TResult1 = T[], TResult2 = never>(onfulfilled?: ((value: T[]) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
14
+ then<TResult1 = T[], TResult2 = never>(onfulfilled?: ((value: T[]) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
15
15
  }
16
16
  export {};
package/dist/barrier.js CHANGED
@@ -2,16 +2,17 @@ import { AsyncVariable } from "./async-variable.js";
2
2
  export class Barrier {
3
3
  queue = [];
4
4
  await(...asyncTasks) {
5
- this.run(...asyncTasks);
5
+ for (const asyncTask of asyncTasks)
6
+ this.#run(asyncTask);
6
7
  }
7
- async #run(task) {
8
+ #run(task) {
8
9
  const completion = new AsyncVariable();
9
10
  this.queue.push({ task, completion });
10
- completion.writeResult(task);
11
+ void completion.writeResult(task);
11
12
  }
12
- run(...asyncTasks) {
13
- for (const task of asyncTasks)
14
- this.#run(task);
13
+ run(...asyncFunctions) {
14
+ for (const func of asyncFunctions)
15
+ this.#run(func());
15
16
  }
16
17
  clear() {
17
18
  this.queue.splice(0, this.queue.length);
package/dist/index.d.ts CHANGED
@@ -5,3 +5,4 @@ export * from './barrier.js';
5
5
  export * from './observable-list.js';
6
6
  export * from './object.js';
7
7
  export * from './lock.js';
8
+ export * from './log.js';
package/dist/index.js CHANGED
@@ -5,3 +5,4 @@ export * from './barrier.js';
5
5
  export * from './observable-list.js';
6
6
  export * from './object.js';
7
7
  export * from './lock.js';
8
+ export * from './log.js';
package/dist/lock.js CHANGED
@@ -9,6 +9,7 @@ export class Lock {
9
9
  this.#isAcquired = true;
10
10
  return new LockContext(this, of, this.release.bind(this));
11
11
  }
12
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
12
13
  release(_context) {
13
14
  if (!this.isAcquired)
14
15
  throw new Error();
package/dist/log.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export declare class Log implements Disposable {
2
+ readonly msg: string;
3
+ constructor(msg: string);
4
+ [Symbol.dispose](): void;
5
+ }
6
+ export declare function log(msg: string): Log;
package/dist/log.js ADDED
@@ -0,0 +1,13 @@
1
+ export class Log {
2
+ msg;
3
+ constructor(msg) {
4
+ this.msg = msg;
5
+ console.log(msg);
6
+ }
7
+ [Symbol.dispose]() {
8
+ console.log(`${this.msg} complete`);
9
+ }
10
+ }
11
+ export function log(msg) {
12
+ return new Log(msg);
13
+ }
package/dist/object.d.ts CHANGED
@@ -8,8 +8,8 @@ export type PropertyPath<T> = [] | T extends object ? Values<{
8
8
  [K in keyof T]: [K] | [K, Values<PropertyPath<T[K]>>];
9
9
  }> : [];
10
10
  export type PropertyType<T, Property extends PropertyPath<T>> = PropertyType_<T, Property>;
11
- type PropertyType_<T, Property extends any[], Constructed extends any[] = []> = (Property & Constructed) extends never ? Values<{
12
- [K in keyof T as Property extends [...Constructed, K, ...any[]] ? K : never]: PropertyType_<T[K], Property, [...Constructed, K]>;
11
+ type PropertyType_<T, Property extends unknown[], Constructed extends unknown[] = []> = (Property & Constructed) extends never ? Values<{
12
+ [K in keyof T as Property extends [...Constructed, K, ...unknown[]] ? K : never]: PropertyType_<T[K], Property, [...Constructed, K]>;
13
13
  }> : T;
14
- export declare function replaceProperty<T, Property extends PropertyPath<T> = PropertyPath<T>>(obj: T, property: Property, value: any): T;
14
+ export declare function replaceProperty<T, Property extends PropertyPath<T> = PropertyPath<T>>(obj: T, property: Property, value: unknown): T;
15
15
  export {};
package/dist/object.js CHANGED
@@ -27,11 +27,12 @@ export function entries(o) {
27
27
  // type ac_type = PropertyType_<A, typeof ac>
28
28
  export function replaceProperty(obj, property, value) {
29
29
  if (property.length === 0)
30
- return (value ?? obj);
31
- if (typeof obj !== 'object')
30
+ return ((value) ?? obj);
31
+ if (!(obj instanceof Object))
32
32
  throw new Error();
33
33
  const prototype = Object.create(obj);
34
34
  const property0 = property[0];
35
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
35
36
  prototype[property0] = replaceProperty(obj[property0], property.slice(1), value);
36
37
  return prototype;
37
38
  }
@@ -10,7 +10,7 @@ export declare class ObservableList<T> extends Array<T> {
10
10
  push(...items: T[]): number;
11
11
  pop(): T | undefined;
12
12
  reverse(): T[];
13
- sort(compareFn?: ((a: T, b: T) => number) | undefined): this;
13
+ sort(compareFn?: ((a: T, b: T) => number)): this;
14
14
  shift(): T | undefined;
15
15
  splice(start: number, deleteCount?: number, ...items: T[]): T[];
16
16
  unshift(...items: T[]): number;
package/dist/types.d.ts CHANGED
@@ -29,3 +29,7 @@ export type Mutable<T> = {
29
29
  export type DeeplyReadonly<T> = T extends object ? T extends null ? T : {
30
30
  readonly [K in keyof T]: DeeplyReadonly<T[K]>;
31
31
  } : T;
32
+ export type NonConstructorKeys<T> = {
33
+ [P in keyof T]: T[P] extends new (...args: any[]) => any ? never : P;
34
+ }[keyof T];
35
+ export type NonConstructor<T> = Pick<T, NonConstructorKeys<T>>;
package/dist/types.js CHANGED
@@ -12,6 +12,7 @@ export function removePrefix(prefix, o) {
12
12
  export function valuesPrefixed(prefix, values) {
13
13
  return Object.fromEntries(Object.entries(values).map(([k, value]) => [k, prefix + value]));
14
14
  }
15
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
16
  export function assert() { }
16
17
  export function never() {
17
18
  throw new Error("never");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-essentials/utils",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -8,10 +8,24 @@
8
8
  "files": [
9
9
  "dist"
10
10
  ],
11
+ "scripts": {
12
+ "clean": "rm -rf dist",
13
+ "prebuild": "npm run clean && npm run lint",
14
+ "prebuild:debug": "npm run clean",
15
+ "build": "tsc -p tsconfig.prod.json",
16
+ "build:debug": "tsc -p tsconfig.debug.json",
17
+ "pretest": "npm run build:debug",
18
+ "test": "npm run test:run",
19
+ "pretest:run": "",
20
+ "test:run": "ava",
21
+ "prepublishOnly": "pnpm test && npm run build",
22
+ "lint": "eslint ."
23
+ },
11
24
  "devDependencies": {
12
25
  "@ava/typescript": "^6.0.0",
13
26
  "@code-essentials/ava-typescript": "^1.1.1",
14
27
  "@code-essentials/tsconfig": "^1.2.1",
28
+ "@code-essentials/eslint-config": "^1.0.2",
15
29
  "@types/node": "^24.10.1",
16
30
  "ava": "^6.4.1",
17
31
  "typescript": "^5.9.3"
@@ -25,16 +39,5 @@
25
39
  "type": "git",
26
40
  "url": "https://github.com/code-essentials/utils.git"
27
41
  },
28
- "license": "MIT",
29
- "scripts": {
30
- "clean": "rm -rf dist",
31
- "prebuild": "npm run clean",
32
- "prebuild:debug": "npm run clean",
33
- "build": "tsc -p tsconfig.prod.json",
34
- "build:debug": "tsc -p tsconfig.debug.json",
35
- "pretest": "npm run build:debug",
36
- "test": "npm run test:run",
37
- "pretest:run": "",
38
- "test:run": "ava"
39
- }
40
- }
42
+ "license": "MIT"
43
+ }
package/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- # Graph Mind License Agreement
2
-
3
- Copyright © 2025 Isaac Valdez. All Rights Reserved.
4
-
5
- 1. Ownership
6
- The source code and all associated intellectual property for the 'graph-math' project (hereinafter referred to as "the Software") are the exclusive private property of Isaac Valdez.
7
-
8
- 2. Permitted Use
9
- The Software may only be examined in source code form with explicit permission from Isaac Valdez. Individuals or entities wishing to review or utilize the source code must obtain this permission in writing.
10
-
11
- 3. End-User Licensing
12
- End users are permitted to run the Software on their local computer systems, provided such usage complies with a separate End-User License Agreement (EULA) that will be supplied to the end user.
13
-
14
- 4. License Modifications
15
- Isaac Valdez reserves the right to update or modify this license at any time. Changes will be effective upon modification of the license text and will apply to all users from that date onward.
16
-
17
- 5. No Warranty
18
- The Software is provided "as is," without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, or non-infringement.
19
-
20
- 6. Acceptance
21
- By using the Software, you indicate your acceptance of this license agreement. If you do not agree to abide by the terms of this agreement, you must refrain from using the Software.