utilium 2.4.0 → 2.5.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.
package/LICENSE.md ADDED
@@ -0,0 +1,157 @@
1
+ # GNU LESSER GENERAL PUBLIC LICENSE
2
+
3
+ Version 3, 29 June 2007
4
+
5
+ Copyright (C) 2007 Free Software Foundation, Inc.
6
+ <https://fsf.org/>
7
+
8
+ Everyone is permitted to copy and distribute verbatim copies of this
9
+ license document, but changing it is not allowed.
10
+
11
+ This version of the GNU Lesser General Public License incorporates the
12
+ terms and conditions of version 3 of the GNU General Public License,
13
+ supplemented by the additional permissions listed below.
14
+
15
+ ## 0. Additional Definitions.
16
+
17
+ As used herein, "this License" refers to version 3 of the GNU Lesser
18
+ General Public License, and the "GNU GPL" refers to version 3 of the
19
+ GNU General Public License.
20
+
21
+ "The Library" refers to a covered work governed by this License, other
22
+ than an Application or a Combined Work as defined below.
23
+
24
+ An "Application" is any work that makes use of an interface provided
25
+ by the Library, but which is not otherwise based on the Library.
26
+ Defining a subclass of a class defined by the Library is deemed a mode
27
+ of using an interface provided by the Library.
28
+
29
+ A "Combined Work" is a work produced by combining or linking an
30
+ Application with the Library. The particular version of the Library
31
+ with which the Combined Work was made is also called the "Linked
32
+ Version".
33
+
34
+ The "Minimal Corresponding Source" for a Combined Work means the
35
+ Corresponding Source for the Combined Work, excluding any source code
36
+ for portions of the Combined Work that, considered in isolation, are
37
+ based on the Application, and not on the Linked Version.
38
+
39
+ The "Corresponding Application Code" for a Combined Work means the
40
+ object code and/or source code for the Application, including any data
41
+ and utility programs needed for reproducing the Combined Work from the
42
+ Application, but excluding the System Libraries of the Combined Work.
43
+
44
+ ## 1. Exception to Section 3 of the GNU GPL.
45
+
46
+ You may convey a covered work under sections 3 and 4 of this License
47
+ without being bound by section 3 of the GNU GPL.
48
+
49
+ ## 2. Conveying Modified Versions.
50
+
51
+ If you modify a copy of the Library, and, in your modifications, a
52
+ facility refers to a function or data to be supplied by an Application
53
+ that uses the facility (other than as an argument passed when the
54
+ facility is invoked), then you may convey a copy of the modified
55
+ version:
56
+
57
+ - a) under this License, provided that you make a good faith effort
58
+ to ensure that, in the event an Application does not supply the
59
+ function or data, the facility still operates, and performs
60
+ whatever part of its purpose remains meaningful, or
61
+ - b) under the GNU GPL, with none of the additional permissions of
62
+ this License applicable to that copy.
63
+
64
+ ## 3. Object Code Incorporating Material from Library Header Files.
65
+
66
+ The object code form of an Application may incorporate material from a
67
+ header file that is part of the Library. You may convey such object
68
+ code under terms of your choice, provided that, if the incorporated
69
+ material is not limited to numerical parameters, data structure
70
+ layouts and accessors, or small macros, inline functions and templates
71
+ (ten or fewer lines in length), you do both of the following:
72
+
73
+ - a) Give prominent notice with each copy of the object code that
74
+ the Library is used in it and that the Library and its use are
75
+ covered by this License.
76
+ - b) Accompany the object code with a copy of the GNU GPL and this
77
+ license document.
78
+
79
+ ## 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that, taken
82
+ together, effectively do not restrict modification of the portions of
83
+ the Library contained in the Combined Work and reverse engineering for
84
+ debugging such modifications, if you also do each of the following:
85
+
86
+ - a) Give prominent notice with each copy of the Combined Work that
87
+ the Library is used in it and that the Library and its use are
88
+ covered by this License.
89
+ - b) Accompany the Combined Work with a copy of the GNU GPL and this
90
+ license document.
91
+ - c) For a Combined Work that displays copyright notices during
92
+ execution, include the copyright notice for the Library among
93
+ these notices, as well as a reference directing the user to the
94
+ copies of the GNU GPL and this license document.
95
+ - d) Do one of the following:
96
+ - 0) Convey the Minimal Corresponding Source under the terms of
97
+ this License, and the Corresponding Application Code in a form
98
+ suitable for, and under terms that permit, the user to
99
+ recombine or relink the Application with a modified version of
100
+ the Linked Version to produce a modified Combined Work, in the
101
+ manner specified by section 6 of the GNU GPL for conveying
102
+ Corresponding Source.
103
+ - 1) Use a suitable shared library mechanism for linking with
104
+ the Library. A suitable mechanism is one that (a) uses at run
105
+ time a copy of the Library already present on the user's
106
+ computer system, and (b) will operate properly with a modified
107
+ version of the Library that is interface-compatible with the
108
+ Linked Version.
109
+ - e) Provide Installation Information, but only if you would
110
+ otherwise be required to provide such information under section 6
111
+ of the GNU GPL, and only to the extent that such information is
112
+ necessary to install and execute a modified version of the
113
+ Combined Work produced by recombining or relinking the Application
114
+ with a modified version of the Linked Version. (If you use option
115
+ 4d0, the Installation Information must accompany the Minimal
116
+ Corresponding Source and Corresponding Application Code. If you
117
+ use option 4d1, you must provide the Installation Information in
118
+ the manner specified by section 6 of the GNU GPL for conveying
119
+ Corresponding Source.)
120
+
121
+ ## 5. Combined Libraries.
122
+
123
+ You may place library facilities that are a work based on the Library
124
+ side by side in a single library together with other library
125
+ facilities that are not Applications and are not covered by this
126
+ License, and convey such a combined library under terms of your
127
+ choice, if you do both of the following:
128
+
129
+ - a) Accompany the combined library with a copy of the same work
130
+ based on the Library, uncombined with any other library
131
+ facilities, conveyed under the terms of this License.
132
+ - b) Give prominent notice with the combined library that part of it
133
+ is a work based on the Library, and explaining where to find the
134
+ accompanying uncombined form of the same work.
135
+
136
+ ## 6. Revised Versions of the GNU Lesser General Public License.
137
+
138
+ The Free Software Foundation may publish revised and/or new versions
139
+ of the GNU Lesser General Public License from time to time. Such new
140
+ versions will be similar in spirit to the present version, but may
141
+ differ in detail to address new problems or concerns.
142
+
143
+ Each version is given a distinguishing version number. If the Library
144
+ as you received it specifies that a certain numbered version of the
145
+ GNU Lesser General Public License "or any later version" applies to
146
+ it, you have the option of following the terms and conditions either
147
+ of that published version or of any later version published by the
148
+ Free Software Foundation. If the Library as you received it does not
149
+ specify a version number of the GNU Lesser General Public License, you
150
+ may choose any version of the GNU Lesser General Public License ever
151
+ published by the Free Software Foundation.
152
+
153
+ If the Library as you received it specifies that a proxy can decide
154
+ whether future versions of the GNU Lesser General Public License shall
155
+ apply, that proxy's public statement of acceptance of any version is
156
+ permanent authorization for you to choose that version for the
157
+ Library.
package/dist/buffer.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  /* eslint-disable @typescript-eslint/no-unused-expressions */
2
4
  /**
3
5
  * Grows a buffer if it isn't large enough
package/dist/cache.js CHANGED
@@ -1,4 +1,8 @@
1
- /** A ranged cache */
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ /**
3
+ * A ranged cache
4
+ * Copyright (c) 2025 James Prevett
5
+ */
2
6
  import { extendBuffer } from './buffer.js';
3
7
  /**
4
8
  * The cache for a specific resource
@@ -1,4 +1,7 @@
1
- /** Utilities for computing checksums */
1
+ /**
2
+ * Utilities for computing checksums
3
+ * Copyright (c) 2025 James Prevett
4
+ */
2
5
  /**
3
6
  * Computes the CRC32C checksum of a Uint8Array.
4
7
  */
package/dist/checksum.js CHANGED
@@ -1,4 +1,8 @@
1
- /** Utilities for computing checksums */
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ /**
3
+ * Utilities for computing checksums
4
+ * Copyright (c) 2025 James Prevett
5
+ */
2
6
  // Precompute CRC32C table
3
7
  const crc32cTable = new Uint32Array(256);
4
8
  for (let i = 0; i < 256; i++) {
package/dist/debugging.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  function defaultStringify(value) {
2
4
  switch (typeof value) {
3
5
  case 'undefined':
package/dist/dom.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  /* eslint-disable @typescript-eslint/no-unused-expressions */
2
4
  /**
3
5
  * Upload a file
package/dist/fs.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  import { isJSON } from './objects.js';
2
4
  import * as fs from 'fs';
3
5
  export class FileMap {
package/dist/index.d.ts CHANGED
@@ -4,5 +4,6 @@ export * from './numbers.js';
4
4
  export * from './objects.js';
5
5
  export * from './random.js';
6
6
  export * from './string.js';
7
+ export * from './type-math.js';
7
8
  export * from './types.js';
8
9
  export * as version from './version.js';
package/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  // For better tree shaking, import from whichever file is actually needed
2
4
  export * from './list.js';
3
5
  export * from './misc.js';
@@ -5,5 +7,6 @@ export * from './numbers.js';
5
7
  export * from './objects.js';
6
8
  export * from './random.js';
7
9
  export * from './string.js';
10
+ export * from './type-math.js';
8
11
  export * from './types.js';
9
12
  export * as version from './version.js';
package/dist/list.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  import { EventEmitter } from 'eventemitter3';
2
4
  export class List extends EventEmitter {
3
5
  [Symbol.toStringTag] = 'List';
package/dist/misc.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  import { resolveConstructors } from './objects.js';
2
4
  export function wait(time) {
3
5
  return new Promise(resolve => setTimeout(resolve, time));
package/dist/numbers.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  export function range(min, max) {
2
4
  const a = [];
3
5
  for (let i = min; i < max; i++) {
package/dist/objects.d.ts CHANGED
@@ -65,3 +65,70 @@ export declare function bindFunctions<T extends object, This = any>(fns: T, this
65
65
  export type Mutable<T> = {
66
66
  -readonly [P in keyof T]: T[P];
67
67
  };
68
+ /**
69
+ * Makes properties with keys assignable to K in T required
70
+ * @see https://stackoverflow.com/a/69328045/17637456
71
+ */
72
+ export type WithRequired<T, K extends keyof T> = T & {
73
+ [P in K]-?: T[P];
74
+ };
75
+ /**
76
+ * Makes properties with keys assignable to K in T optional
77
+ */
78
+ export type WithOptional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
79
+ export type ClassLike<Instance = any> = abstract new (...args: any[]) => Instance;
80
+ export type InstancesFor<T extends readonly ClassLike[]> = T extends [] ? [] : T extends readonly [infer C extends ClassLike, ...infer Rest extends readonly ClassLike[]] ? [InstanceType<C>, ...InstancesFor<Rest>] : never;
81
+ export type ConstructorsFor<T extends readonly unknown[]> = T extends [] ? [] : T extends readonly [infer I, ...infer Rest extends readonly unknown[]] ? [new (...args: any[]) => I, ...ConstructorsFor<Rest>] : never;
82
+ export type Concrete<T extends ClassLike> = Pick<T, keyof T> & (new (...args: any[]) => InstanceType<T>);
83
+ /**
84
+ * Extracts an object with properties assignable to P from an object T
85
+ * @see https://stackoverflow.com/a/71532723/17637456
86
+ */
87
+ export type ExtractProperties<T, P> = {
88
+ [K in keyof T as T[K] extends infer Prop ? (Prop extends P ? K : never) : never]: T[K];
89
+ };
90
+ /**
91
+ * Extract the keys of T which are required
92
+ * @see https://stackoverflow.com/a/55247867/17637456
93
+ */
94
+ export type RequiredKeys<T> = {
95
+ [K in keyof T]-?: {} extends {
96
+ [P in K]: T[K];
97
+ } ? never : K;
98
+ }[keyof T];
99
+ /**
100
+ * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
101
+ */
102
+ export type RequiredProperties<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Required<Pick<T, K>>;
103
+ /**
104
+ * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
105
+ */
106
+ export type DeepRequired<T> = {
107
+ [K in keyof T]-?: DeepRequired<T[K]>;
108
+ };
109
+ /**
110
+ * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
111
+ */
112
+ export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
113
+ /**
114
+ * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
115
+ */
116
+ export type NestedKeys<T extends object> = {
117
+ [P in keyof T & (string | number)]: T[P] extends Date ? `${P}` : T[P] extends Record<string, unknown> ? `${P}` | `${P}.${NestedKeys<T[P]>}` : `${P}`;
118
+ }[keyof T & (string | number)];
119
+ /**
120
+ * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
121
+ */
122
+ export type PartialRecursive<T> = {
123
+ [P in keyof T]?: T[P] extends (infer U)[] ? PartialRecursive<U>[] : T[P] extends object | undefined ? PartialRecursive<T[P]> : T[P];
124
+ };
125
+ /**
126
+ * Nothing in T
127
+ */
128
+ export type Never<T> = {
129
+ [K in keyof T]?: never;
130
+ };
131
+ /**
132
+ * All of the properties in T or none of them
133
+ */
134
+ export type AllOrNone<T> = T | Never<T>;
package/dist/objects.js CHANGED
@@ -1,3 +1,6 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
3
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
4
  export function filterObject(object, predicate) {
2
5
  const entries = Object.entries(object);
3
6
  return Object.fromEntries(entries.filter(([key, value]) => predicate(key, value)));
package/dist/random.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  export function randomFloat(min = 0, max = 1) {
2
4
  return Math.random() * (max - min) + min;
3
5
  }
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Utilities for `fetch` when using range requests.
3
+ * It also allows you to handle errors easier
4
+ * Copyright (c) 2025 James Prevett
5
+ */
1
6
  import * as cache from './cache.js';
2
7
  /** @deprecated Use `cache.Options` */
3
8
  export type ResourceCacheOptions = cache.Options;
package/dist/requests.js CHANGED
@@ -1,4 +1,9 @@
1
- /* Utilities for `fetch` when using range requests. It also allows you to handle errors easier */
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ /**
3
+ * Utilities for `fetch` when using range requests.
4
+ * It also allows you to handle errors easier
5
+ * Copyright (c) 2025 James Prevett
6
+ */
2
7
  import * as cache from './cache.js';
3
8
  /** @deprecated Use `cache.Resource` */
4
9
  export const ResourceCache = cache.Resource;
package/dist/shell.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * A simple wrapper for xterm.js that makes implementing shells easier
3
+ * Copyright (c) 2025 James Prevett
4
+ */
1
5
  import type { Terminal } from '@xterm/xterm';
2
6
  export interface ShellOptions {
3
7
  /**
package/dist/shell.js CHANGED
@@ -1,3 +1,8 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ /**
3
+ * A simple wrapper for xterm.js that makes implementing shells easier
4
+ * Copyright (c) 2025 James Prevett
5
+ */
1
6
  async function handleData($, data) {
2
7
  if ($.index == -1) {
3
8
  $.currentInput = $.input;
package/dist/string.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { $Max, w_subtract } from './type-math.js';
1
2
  export declare function capitalize<T extends string>(value: T): Capitalize<T>;
2
3
  export declare function uncapitalize<T extends string>(value: T): Uncapitalize<T>;
3
4
  export type ConcatString<T extends string[]> = T extends [infer F extends string, ...infer R extends string[]] ? `${F}${ConcatString<R>}` : '';
@@ -22,3 +23,11 @@ export declare function decodeUUID(uuid: Uint8Array): UUID;
22
23
  export declare function encodeUUID(uuid: UUID): Uint8Array<ArrayBuffer>;
23
24
  export declare function stringifyUUID(uuid: bigint): UUID;
24
25
  export declare function parseUUID(uuid: UUID): bigint;
26
+ /**
27
+ * Split a string.
28
+ */
29
+ export type Split<T extends string, Delimiter extends string = ''> = string extends T ? string[] : T extends '' ? [] : T extends `${infer Left}${Delimiter}${infer Right}` ? [Left, ...Split<Right, Delimiter>] : [T];
30
+ export type StringLength<T extends string> = Split<T>['length'];
31
+ export type Repeat<T extends string, N extends number, Init extends string = ''> = N extends 0 ? Init : Repeat<T, w_subtract<N, StringLength<T>>, `${Init}${T}`>;
32
+ export type PadRight<Init extends string, R extends string, N extends number> = Repeat<R, $Max<0, w_subtract<N, StringLength<Init>>>, Init>;
33
+ export type PadLeft<Init extends string, R extends string, N extends number> = `${Repeat<R, $Max<0, w_subtract<N, StringLength<Init>>>>}${Init}`;
package/dist/string.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  export function capitalize(value) {
2
4
  return (value.at(0).toUpperCase() + value.slice(1));
3
5
  }
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Type-system arithmetic.
3
+ * Copyright (c) 2025 James Prevett
4
+ *
5
+ * Notes on implementation:
6
+ *
7
+ * Snake case is used for internal types to reduce visual conflict/confusion with type variables.
8
+ * Type naming convention:
9
+ * `w_*` => internals that operate on whole numbers (i.e. n >= 0)
10
+ * `i_*` => internals that operate on integers
11
+ * `_*` => general purpose internals
12
+ *
13
+ */
14
+ import type { Repeat, StringLength } from './string.js';
15
+ import type { $drain, Length } from './types.js';
16
+ /**
17
+ * Maps a numeric literal type to a tuple type with that length
18
+ */
19
+ type __tuple<L extends number, T extends any[] = []> = T extends {
20
+ length: L;
21
+ } ? T : __tuple<L, [...T, any]>;
22
+ /**
23
+ * Sum A and B (both must be whole numbers)
24
+ */
25
+ type w_add<A extends number, B extends number> = Length<[...__tuple<A>, ...__tuple<B>]>;
26
+ /**
27
+ * Subtracts B from A (both must be whole numbers)
28
+ */
29
+ export type w_subtract<A extends number, B extends number> = __tuple<A> extends [...infer U, ...__tuple<B>] ? Length<U> : never;
30
+ /**
31
+ * Is A strictly equal to B?
32
+ */
33
+ export type Equal<A, B> = A extends B ? (B extends A ? true : false) : false;
34
+ /**
35
+ * Is A or B zero?
36
+ */
37
+ type _either_zero<A extends number, B extends number> = A extends 0 ? true : B extends 0 ? true : false;
38
+ /**
39
+ * Is A less than B? (whole numbers)
40
+ */
41
+ type w_less<A extends number, B extends number> = _either_zero<A, B> extends true ? Equal<A, B> extends true ? false : A extends 0 ? true : false : w_less<w_subtract<A, 1>, w_subtract<B, 1>>;
42
+ /**
43
+ * Negate *any* number.
44
+ */
45
+ export type Negate<N extends number> = N extends 0 ? 0 : `-${N}` extends `${infer V extends number}` ? V : `${N}` extends `-${infer V extends number}` ? V : never;
46
+ export type Absolute<N extends number> = N extends 0 ? 0 : `${N}` extends `-${infer V extends number}` ? V : N;
47
+ export type Is_Negative<N extends number> = `${N}` extends `-${number}` ? true : false;
48
+ export type Is_Positive<N extends number> = `-${N}` extends `${number}` ? true : false;
49
+ type _is_int<N extends number> = `${N}` extends `${number}.${number}` ? false : true;
50
+ type _are_ints<A extends number, B extends number> = _is_int<A> extends true ? (_is_int<B> extends true ? true : false) : false;
51
+ /**
52
+ * While `i_sum` is just as convenient and also more robust, this has a depth of 1 instead of 3.
53
+ */
54
+ type i_increment<N extends number, I extends number = 1> = `${N}` extends `-${infer Abs extends number}` ? Negate<w_add<Abs, I>> : w_add<N, I>;
55
+ /**
56
+ * While `i_sum` is just as convenient and also more robust, this has a depth of 1 instead of 3.
57
+ */
58
+ type i_decrement<N extends number, I extends number = 1> = `${N}` extends `-${infer Abs extends number}` ? Negate<w_subtract<Abs, I>> : w_subtract<N, I>;
59
+ /**
60
+ * Sum integers A and B.
61
+ *
62
+ * Does not perform integer validation!
63
+ * @internal
64
+ */
65
+ type i_sum<A extends number, B extends number> = `${A}` extends `-${infer A_abs extends number}` ? `${B}` extends `-${infer B_abs extends number}` ? Negate<w_add<A_abs, B_abs>> : w_less<A_abs, B> extends true ? w_subtract<B, A_abs> : Negate<w_subtract<A_abs, B>> : `${B}` extends `-${infer B_abs extends number}` ? w_less<A, B_abs> extends true ? Negate<w_subtract<B_abs, A>> : w_subtract<A, B_abs> : w_add<A, B>;
66
+ /**
67
+ * Add integers A and B.
68
+ * @internal
69
+ */
70
+ export type $Add<A extends number, B extends number> = _are_ints<A, B> extends true ? i_sum<A, B> : never;
71
+ /**
72
+ * Subtract integer B from A.
73
+ * @internal
74
+ */
75
+ export type $Subtract<A extends number, B extends number> = _are_ints<A, B> extends true ? i_sum<A, Negate<B>> : never;
76
+ /**
77
+ * Is A less than B?
78
+ * @internal
79
+ */
80
+ type i_less<A extends number, B extends number> = $drain<_either_zero<A, B> extends true ? Equal<A, B> extends true ? false : A extends 0 ? true : false : i_less<i_decrement<A>, i_decrement<B>>>;
81
+ export type $LessThan<A extends number, B extends number> = _are_ints<A, B> extends true ? i_less<A, B> : never;
82
+ type i_max<A extends number, B extends number> = i_less<A, B> extends true ? B : A;
83
+ export type $Max<A extends number, B extends number> = _are_ints<A, B> extends true ? i_max<A, B> : never;
84
+ /**
85
+ * Accumulative subtraction:
86
+ * N: initial value
87
+ * D: divisor
88
+ * Q: quotient
89
+ */
90
+ type w_divide<N extends number, D extends number, Q extends number> = $drain<D extends 0 ? never : i_less<N, D> extends true ? Q : w_divide<i_sum<N, Negate<D>>, D, i_increment<Q>>>;
91
+ /**
92
+ * Integer division
93
+ */
94
+ type i_divide<A extends number, B extends number> = `${A}` extends `-${infer A_abs extends number}` ? `${B}` extends `-${infer B_abs extends number}` ? w_divide<A_abs, B_abs, 0> : Negate<w_divide<A_abs, B, 0>> : `${B}` extends `-${infer B_abs extends number}` ? Negate<w_divide<A, B_abs, 0>> : w_divide<A, B, 0>;
95
+ /**
96
+ * Integer division
97
+ */
98
+ export type Divide<A extends number, B extends number> = _are_ints<A, B> extends true ? i_divide<A, B> : never;
99
+ /**
100
+ * Modulo
101
+ */
102
+ export type Modulo<A extends number, B extends number> = _are_ints<A, B> extends true ? (i_less<A, B> extends true ? A : Modulo<i_sum<A, Negate<B>>, B>) : never;
103
+ /**
104
+ * Get the integer part of a number
105
+ */
106
+ export type Integer<N extends number> = `${N}` extends `${infer I extends number}.${string}` ? I : N;
107
+ /**
108
+ * Get the fractional part of a number
109
+ */
110
+ export type Fraction<N extends number> = `${N}` extends `${number}.${infer S extends string}` ? `0.${S}` extends `${infer F extends number}` ? F : never : 0;
111
+ type _f_sum_str<A_s extends string, B_s extends string, _A_neg extends boolean, _B_neg extends boolean, _A_len extends number = StringLength<A_s>, _B_len extends number = StringLength<B_s>> = $drain<Repeat<'0', i_max<0, w_subtract<_B_len, _A_len>>, A_s>> extends `${infer A_f extends number}` ? $drain<Repeat<'0', i_max<0, w_subtract<_A_len, _B_len>>, B_s>> extends `${infer B_f extends number}` ? i_sum<_A_neg extends true ? Negate<A_f> : A_f, _B_neg extends true ? Negate<B_f> : B_f> : never : never;
112
+ /**
113
+ * Sum the fractional parts A and B of two numbers
114
+ */
115
+ type f_sum<A extends number, B extends number, _A_neg extends boolean, _B_neg extends boolean> = `${A}` extends `0.${infer A_s extends string}` ? `${B}` extends `0.${infer B_s extends string}` ? `0.${_f_sum_str<A_s, B_s, _A_neg, _B_neg>}` extends `${infer F extends number}` ? F : never : never : never;
116
+ type _sum_with_f<A extends number, B extends number, F extends number> = `${F}` extends `${infer C extends number}.${infer S}` ? `${i_sum<i_sum<Integer<A>, Integer<B>>, C>}.${S}` extends `${infer V extends number}` ? V : never : never;
117
+ export type Add<A extends number, B extends number, __fA extends number = Fraction<A>, __fB extends number = Fraction<B>> = __fA extends 0 ? __fB extends 0 ? i_sum<A, B> : _sum_with_f<A, B, __fB> : __fB extends 0 ? _sum_with_f<A, B, __fA> : _sum_with_f<A, B, f_sum<__fA, __fB, Is_Negative<A>, Is_Negative<B>>>;
118
+ export type Subtract<A extends number, B extends number> = Add<A, Negate<B>>;
119
+ /**
120
+ * Accumulative addition:
121
+ * N: increment
122
+ * I: iterations (must be a whole number)
123
+ * A: sum
124
+ */
125
+ type _mul_accumulate<N extends number, I extends number, A extends number> = $drain<I extends 0 ? A : _mul_accumulate<N, i_decrement<I>, Add<N, A>>>;
126
+ /**
127
+ * Multiplication.
128
+ *
129
+ * Current limitation: B must be an integer.
130
+ */
131
+ export type Multiply<A extends number, B extends number> = `${B}` extends `${number}.${number}` ? never : `${B}` extends `-${infer B_abs extends number}` ? Negate<_mul_accumulate<A, B_abs, 0>> : _mul_accumulate<A, B, 0>;
132
+ export {};
@@ -0,0 +1,15 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ /**
3
+ * Type-system arithmetic.
4
+ * Copyright (c) 2025 James Prevett
5
+ *
6
+ * Notes on implementation:
7
+ *
8
+ * Snake case is used for internal types to reduce visual conflict/confusion with type variables.
9
+ * Type naming convention:
10
+ * `w_*` => internals that operate on whole numbers (i.e. n >= 0)
11
+ * `i_*` => internals that operate on integers
12
+ * `_*` => general purpose internals
13
+ *
14
+ */
15
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,38 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
3
+ // addition
4
+ const add_positive = 3;
5
+ const add_zero = 0;
6
+ const add_diff_signs = -1;
7
+ const add_negative = -3;
8
+ const add_float = 1.7;
9
+ // @ts-expect-error 2 + 2 != 5
10
+ let fail = 5;
11
+ fail = 4;
12
+ // subtraction for sanity
13
+ const subtract_normal = -18;
14
+ const subtract_from_neg = -9;
15
+ const subtract_both_neg = 5;
16
+ const subtract_zero = -1;
17
+ const subtract_floats = 3.3;
18
+ // were doing multiplication in the type system now?!
19
+ // yup.
20
+ const multiply_normal = 6;
21
+ const multiply_by_zero = 0;
22
+ const multiply_negative = -12;
23
+ const multiply_both_negative = 21;
24
+ const multiply_large = 896;
25
+ // division
26
+ const divide_normal = 5;
27
+ const divide_by_zero = NaN;
28
+ const divide_negative = -4;
29
+ const divide_both_negative = 6;
30
+ // floor/fraction
31
+ const int_normal = 3;
32
+ const int_negative = -3;
33
+ const fraction_normal = 0.1415;
34
+ const fraction_negative = 0.1415;
35
+ // With type variable
36
+ const pi = 3.141;
37
+ const octo_pi = 24.1128;
38
+ export {};
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { Subtract } from './type-math.js';
1
2
  /**
2
3
  * Expands the type T (for intellisense and debugging)
3
4
  * @see https://stackoverflow.com/a/69288824/17637456
@@ -12,48 +13,6 @@ export type Expand<T> = T extends (...args: infer A) => infer R ? (...args: Expa
12
13
  export type ExpandRecursively<T> = T extends (...args: infer A) => infer R ? (...args: ExpandRecursively<A>) => ExpandRecursively<R> : T extends object ? T extends infer O ? {
13
14
  [K in keyof O]: ExpandRecursively<O[K]>;
14
15
  } : never : T;
15
- /**
16
- * Extracts an object with properties assignable to P from an object T
17
- * @see https://stackoverflow.com/a/71532723/17637456
18
- */
19
- export type ExtractProperties<T, P> = {
20
- [K in keyof T as T[K] extends infer Prop ? (Prop extends P ? K : never) : never]: T[K];
21
- };
22
- /**
23
- * Extract the keys of T which are required
24
- * @see https://stackoverflow.com/a/55247867/17637456
25
- */
26
- export type RequiredKeys<T> = {
27
- [K in keyof T]-?: {} extends {
28
- [P in K]: T[K];
29
- } ? never : K;
30
- }[keyof T];
31
- /**
32
- * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
33
- */
34
- export type RequiredProperties<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Required<Pick<T, K>>;
35
- /**
36
- * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
37
- */
38
- export type DeepRequired<T> = {
39
- [K in keyof T]-?: DeepRequired<T[K]>;
40
- };
41
- /**
42
- * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
43
- */
44
- export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
45
- /**
46
- * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
47
- */
48
- export type NestedKeys<T extends object> = {
49
- [P in keyof T & (string | number)]: T[P] extends Date ? `${P}` : T[P] extends Record<string, unknown> ? `${P}` | `${P}.${NestedKeys<T[P]>}` : `${P}`;
50
- }[keyof T & (string | number)];
51
- /**
52
- * @see https://dev.to/tmhao2005/ts-useful-advanced-types-3k5e
53
- */
54
- export type PartialRecursive<T> = {
55
- [P in keyof T]?: T[P] extends (infer U)[] ? PartialRecursive<U>[] : T[P] extends object | undefined ? PartialRecursive<T[P]> : T[P];
56
- };
57
16
  /**
58
17
  * Get the keys of a union of objects
59
18
  * @see https://stackoverflow.com/a/65805753/17637456
@@ -106,9 +65,9 @@ export type Remove<A extends unknown[], B extends unknown[]> = Empty extends B ?
106
65
  /**
107
66
  * The length of T
108
67
  */
109
- export type Length<T extends {
110
- length: number;
111
- }> = T['length'];
68
+ export type Length<T extends unknown[]> = T extends {
69
+ length: infer L extends number;
70
+ } ? L : never;
112
71
  type _FromLength<N extends number, R extends unknown[] = Empty> = Length<R> extends N ? R : _FromLength<N, Unshift<R, 0>>;
113
72
  /**
114
73
  * Creates a tuple of length N
@@ -116,24 +75,18 @@ type _FromLength<N extends number, R extends unknown[] = Empty> = Length<R> exte
116
75
  export type FromLength<N extends number> = _FromLength<N>;
117
76
  /**
118
77
  * Increments N
78
+ * @deprecated Use Add<N, 1> instead
119
79
  */
120
80
  export type Increment<N extends number> = Length<Unshift<_FromLength<N>, 0>>;
121
81
  /**
122
82
  * Decrements N
83
+ * @deprecated Use Subtract<N, 1> instead
123
84
  */
124
- export type Decrement<N extends number> = Length<Shift<_FromLength<N>>>;
125
- /**
126
- * Gets the sum of A and B
127
- */
128
- export type Add<A extends number, B extends number> = Length<Concat<_FromLength<A>, _FromLength<B>>>;
129
- /**
130
- * Subtracts B from A
131
- */
132
- export type Subtract<A extends number, B extends number> = Length<Remove<_FromLength<A>, _FromLength<B>>>;
85
+ export type Decrement<N extends number> = Subtract<N, 1>;
133
86
  /**
134
87
  * Gets the type of an array's members
135
88
  */
136
- export type Member<T, D = null> = D extends 0 ? T : T extends (infer U)[] ? Member<U, D extends number ? Decrement<D> : null> : T;
89
+ export type Member<T, D = null> = D extends 0 ? T : T extends (infer U)[] ? Member<U, D extends number ? Subtract<D, 1> : null> : T;
137
90
  /**
138
91
  * Flattens an array
139
92
  */
@@ -165,10 +118,6 @@ export type OptionalTuple<T extends unknown[]> = T extends [infer Head, ...infer
165
118
  * Keys of a Map
166
119
  */
167
120
  export type MapKeys<T> = T extends Map<infer K, any> ? K : never;
168
- export type ClassLike<Instance = any> = abstract new (...args: any[]) => Instance;
169
- export type InstancesFor<T extends readonly ClassLike[]> = T extends [] ? [] : T extends readonly [infer C extends ClassLike, ...infer Rest extends readonly ClassLike[]] ? [InstanceType<C>, ...InstancesFor<Rest>] : never;
170
- export type ConstructorsFor<T extends readonly unknown[]> = T extends [] ? [] : T extends readonly [infer I, ...infer Rest extends readonly unknown[]] ? [new (...args: any[]) => I, ...ConstructorsFor<Rest>] : never;
171
- export type Concrete<T extends ClassLike> = Pick<T, keyof T> & (new (...args: any[]) => InstanceType<T>);
172
121
  /**
173
122
  * Converts a union to an intersection
174
123
  * @see https://stackoverflow.com/a/55128956/17637456
@@ -185,24 +134,9 @@ export type LastOfUnion<T> = UnionToIntersection<T extends any ? () => T : never
185
134
  */
186
135
  export type UnionToTuple<T, L = LastOfUnion<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple<Exclude<T, L>>, L>;
187
136
  /**
188
- * Makes properties with keys assignable to K in T required
189
- * @see https://stackoverflow.com/a/69328045/17637456
190
- */
191
- export type WithRequired<T, K extends keyof T> = T & {
192
- [P in K]-?: T[P];
193
- };
194
- /**
195
- * Makes properties with keys assignable to K in T optional
196
- */
197
- export type WithOptional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
198
- /**
199
- * Nothing in T
200
- */
201
- export type Never<T> = {
202
- [K in keyof T]?: never;
203
- };
204
- /**
205
- * All of the properties in T or none of them
137
+ * Utility to reduce depth of TypeScript's internal type instantiation stack.
138
+ * Some wizard on the Kysely team came up with this.
139
+ * It improves performance for the math types by an order of magnitude.
206
140
  */
207
- export type AllOrNone<T> = T | Never<T>;
141
+ export type $drain<T> = [T] extends [unknown] ? T : never;
208
142
  export {};
package/dist/types.js CHANGED
@@ -1 +1,3 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  export {};
package/dist/version.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  import { capitalize } from './string.js';
2
4
  export const regex = /^(?<core>\d+\.\d+\.\d+)(?:[-_](?<type>[^-_.]+)[-_.](?<pre>\d*(?:\.\d+)*))?/;
3
5
  export function parse(full, stripCore) {
package/eslint.shared.js CHANGED
@@ -1,4 +1,8 @@
1
- /* Shared eslint rules */
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ /**
3
+ * Shared eslint rules
4
+ * Copyright (c) 2025 James Prevett
5
+ */
2
6
  import eslint from '@eslint/js';
3
7
  import globals from 'globals';
4
8
  import tseslint from 'typescript-eslint';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "2.4.0",
3
+ "version": "2.5.1",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -15,6 +15,9 @@
15
15
  "./eslint": "./eslint.shared.js",
16
16
  "./typedoc-no-ref": "./scripts/typedoc-no-ref.js"
17
17
  },
18
+ "bin": {
19
+ "lice": "./scripts/lice.js"
20
+ },
18
21
  "files": [
19
22
  "dist",
20
23
  "scripts",
@@ -31,7 +34,7 @@
31
34
  "url": "git+https://github.com/james-pre/utilium.git"
32
35
  },
33
36
  "author": "James Prevett <jp@jamespre.dev> (https://jamespre.dev)",
34
- "license": "MIT",
37
+ "license": "LGPL-3.0-or-later",
35
38
  "bugs": {
36
39
  "url": "https://github.com/james-pre/utilium/issues"
37
40
  },
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/env node
2
+ // SPDX-License-Identifier: LGPL-3.0-or-later
3
+ // Copyright (c) 2025 James Prevett
4
+
5
+ import { readdirSync } from 'node:fs';
6
+ import { readFile, writeFile } from 'node:fs/promises';
7
+ import { join, matchesGlob, relative } from 'node:path';
8
+ import { parseArgs, styleText } from 'node:util';
9
+
10
+ const {
11
+ positionals: dirs,
12
+ values: { license: expectedLicense, write, verbose, force, exclude },
13
+ } = parseArgs({
14
+ allowPositionals: true,
15
+ options: {
16
+ license: { type: 'string', short: 'L' },
17
+ write: { type: 'boolean', short: 'w', default: false },
18
+ verbose: { type: 'boolean', short: 'v', default: false },
19
+ force: { type: 'boolean', short: 'f', default: false },
20
+ exclude: { type: 'string', short: 'x', default: '' },
21
+ },
22
+ });
23
+
24
+ if (write && !expectedLicense) {
25
+ console.error(styleText('red', 'You must specify a license to write with --license/-L'));
26
+ process.exit(1);
27
+ }
28
+
29
+ const licenseSpec = /^\s*\/(?:\/|\*) SPDX-License-Identifier: (.+)/;
30
+
31
+ async function check_file(path, display) {
32
+ if (matchesGlob(path, exclude)) {
33
+ console.log(styleText('whiteBright', ' (skipped)'));
34
+ return 'skipped';
35
+ }
36
+
37
+ const content = await readFile(path, 'utf-8');
38
+
39
+ const match = licenseSpec.exec(content);
40
+
41
+ if (!match) {
42
+ console.error(styleText('red', 'Missing:'), display);
43
+ return 'missing';
44
+ }
45
+
46
+ if (!expectedLicense) {
47
+ if (verbose) console.log(styleText(['dim'], 'Found: ') + display);
48
+ return 'with license';
49
+ }
50
+
51
+ const [, license] = match;
52
+
53
+ if (license == expectedLicense) {
54
+ if (verbose) console.log(styleText(['green', 'dim'], 'Correct: ') + display);
55
+ return 'correct';
56
+ }
57
+
58
+ console.warn(styleText('yellow', 'Mismatch:'), display);
59
+ return 'mismatched';
60
+ }
61
+
62
+ async function write_file(path, display) {
63
+ if (matchesGlob(path, exclude)) {
64
+ console.log(styleText('whiteBright', ' (skipped)'));
65
+ return 'skipped';
66
+ }
67
+
68
+ const content = await readFile(path, 'utf-8');
69
+
70
+ const match = licenseSpec.exec(content);
71
+
72
+ if (!match) {
73
+ await writeFile(path, `// SPDX-License-Identifier: ${expectedLicense}\n${content}`, 'utf-8');
74
+
75
+ console.log('Added: ' + display);
76
+ return 'added';
77
+ }
78
+
79
+ const [, license] = match;
80
+
81
+ if (license == expectedLicense) {
82
+ if (verbose) console.log(styleText(['green', 'dim'], 'Correct: ') + display);
83
+ return 'correct';
84
+ }
85
+
86
+ process.stdout.write(styleText('yellow', 'Mismatch: ') + display);
87
+
88
+ if (!force) {
89
+ console.log(styleText('whiteBright', ' (skipped)'));
90
+ return 'skipped';
91
+ }
92
+
93
+ await writeFile(path, content.replace(licenseSpec, `// SPDX-License-Identifier: ${expectedLicense}`), 'utf-8');
94
+ console.log(styleText('whiteBright', ' (overwritten)'));
95
+ return 'overwritten';
96
+ }
97
+
98
+ function check_dir(dir, display) {
99
+ const entries = readdirSync(dir, { withFileTypes: true });
100
+
101
+ const results = [];
102
+
103
+ for (const entry of entries) {
104
+ if (entry.isDirectory()) {
105
+ results.push(...check_dir(join(dir, entry.name), join(display, entry.name)));
106
+ continue;
107
+ }
108
+
109
+ if (!entry.isFile()) continue;
110
+
111
+ const op = write ? write_file : check_file;
112
+ results.push(op(join(dir, entry.name), join(display, entry.name)));
113
+ }
114
+
115
+ return results;
116
+ }
117
+
118
+ if (!dirs.length) {
119
+ console.error(styleText('red', 'No directories specified'));
120
+ process.exit(1);
121
+ }
122
+
123
+ if (verbose) console.log('Checking:', dirs.join(', '));
124
+
125
+ const promises = [];
126
+
127
+ for (const dir of dirs) {
128
+ const rel = relative(process.cwd(), dir);
129
+ promises.push(...check_dir(dir, rel.startsWith('..') ? dir : rel));
130
+ }
131
+
132
+ const styles = Object.assign(Object.create(null), {
133
+ missing: 'red',
134
+ 'with license': 'white',
135
+ correct: 'green',
136
+ mismatched: 'yellow',
137
+ skipped: 'white',
138
+ added: 'cyan',
139
+ overwritten: 'magenta',
140
+ });
141
+
142
+ try {
143
+ const raw_results = await Promise.all(promises);
144
+ const results = Object.create(null);
145
+ for (const result of raw_results) {
146
+ if (!(result in results)) results[result] = 0;
147
+ results[result]++;
148
+ }
149
+ console.log(
150
+ write ? 'Wrote' : 'Checked',
151
+ styleText('blueBright', raw_results.length.toString()),
152
+ 'files:',
153
+ Object.entries(results)
154
+ .map(([key, value]) => `${key in styles ? styleText(styles[key], value.toString()) : value} ${key}`)
155
+ .join(', ')
156
+ );
157
+ } catch (error) {
158
+ console.error(styleText('red', error.toString()));
159
+ process.exit(1);
160
+ }
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2025 James Prevett
1
3
  import { Converter, ReflectionKind } from 'typedoc';
2
4
 
3
5
  export function load({ application }) {
File without changes