@isopodlabs/utilities 1.7.1 → 1.9.0

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/algorithm.js CHANGED
@@ -52,3 +52,4 @@ function argmax(array, func = object_1.compare) {
52
52
  function max(array, func = object_1.compare) {
53
53
  return array[argmax(array, func)];
54
54
  }
55
+ //# sourceMappingURL=algorithm.js.map
package/dist/array.d.ts CHANGED
@@ -4,3 +4,4 @@ export declare function equal<T>(arr1: T[], arr2: T[]): boolean;
4
4
  export declare function reverse<T>(array: T[], start: number, end: number): void;
5
5
  export declare function rotate<T>(array: T[], start: number, end: number, shift: number): void;
6
6
  export declare function make<T>(n: number, constructor: new () => T): T[];
7
+ export declare function lazySlice<T>(array: T[], start?: number, end?: number): Generator<T>;
package/dist/array.js CHANGED
@@ -6,6 +6,7 @@ exports.equal = equal;
6
6
  exports.reverse = reverse;
7
7
  exports.rotate = rotate;
8
8
  exports.make = make;
9
+ exports.lazySlice = lazySlice;
9
10
  const object_1 = require("./object");
10
11
  //-----------------------------------------------------------------------------
11
12
  // array
@@ -55,3 +56,21 @@ function rotate(array, start, end, shift) {
55
56
  function make(n, constructor) {
56
57
  return Array.from({ length: n }, () => new constructor);
57
58
  }
59
+ function* lazySlice(array, start, end) {
60
+ const len = array.length;
61
+ if (start === undefined)
62
+ start = 0;
63
+ else if (start < 0)
64
+ start = Math.max(len + start, 0);
65
+ else
66
+ start = Math.min(start, len);
67
+ if (end === undefined)
68
+ end = len;
69
+ else if (end < 0)
70
+ end = Math.max(len + start, 0);
71
+ else
72
+ end = Math.min(end, len);
73
+ for (let i = start; i < end; i++)
74
+ yield array[i];
75
+ }
76
+ //# sourceMappingURL=array.js.map
package/dist/async.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare function map<T, U>(iterable: Iterable<T> | undefined, func: (v: T, i: number) => Promise<U>): Promise<U[]>;
2
+ export declare function mapObject<T, U>(obj: Record<string, T>, func: (x: [k: string, v: T]) => Promise<[k: string, v: U]>): Promise<Record<string, U>>;
2
3
  export declare function mapSerial<T, U>(iterable: Iterable<T> | undefined, func: (v: T, i: number) => Promise<U>): Promise<U[]>;
3
4
  export declare function reduce<T, U>(iterable: Iterable<T>, func: (acc: U, v: T, i: number, iterable: Iterable<T>) => Promise<U>, initialValue: U): Promise<U>;
4
5
  export declare function filter<T>(iterable: Iterable<T>, func: (v: T) => Promise<unknown>): Promise<T[]>;
package/dist/async.js CHANGED
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.map = map;
37
+ exports.mapObject = mapObject;
37
38
  exports.mapSerial = mapSerial;
38
39
  exports.reduce = reduce;
39
40
  exports.filter = filter;
@@ -45,6 +46,9 @@ const iterator = __importStar(require("./iterator"));
45
46
  async function map(iterable, func) {
46
47
  return Promise.all(iterator.map(iterable, func));
47
48
  }
49
+ async function mapObject(obj, func) {
50
+ return Object.fromEntries(await Promise.all(Object.entries(obj).map(x => func(x))));
51
+ }
48
52
  async function mapSerial(iterable, func) {
49
53
  const results = [];
50
54
  let i = 0;
@@ -63,3 +67,4 @@ async function filter(iterable, func) {
63
67
  const filters = await map(iterable, func);
64
68
  return iterator.filter(iterable, (_, i) => filters[i]);
65
69
  }
70
+ //# sourceMappingURL=async.js.map
package/dist/bits.js CHANGED
@@ -28,7 +28,7 @@ function highestSet32(x) {
28
28
  return x ? 32 - Math.clz32(x) : 0;
29
29
  }
30
30
  function highestSet1024(x) {
31
- let b = Math.floor(Math.log2(x));
31
+ const b = Math.floor(Math.log2(x));
32
32
  return 1n << BigInt(b) <= x ? b + 1 : b;
33
33
  }
34
34
  // Returns the number of set bits
@@ -42,7 +42,7 @@ function nthSet32(x, i) {
42
42
  let b2 = x - ((x >> 1) & 0x55555555);
43
43
  let b4 = (b2 & 0x33333333) + ((b2 >> 2) & 0x33333333);
44
44
  let b8 = (b4 + (b4 >> 4) & 0xF0F0F0F);
45
- let b16 = (b8 + (b8 >> 8)) & 0xff;
45
+ const b16 = (b8 + (b8 >> 8)) & 0xff;
46
46
  let n = 0;
47
47
  if (i >= b16) {
48
48
  i -= b16;
@@ -744,7 +744,8 @@ function sparseClearMask(bits, i, m, undef = 0) {
744
744
  bits[i] = ~m;
745
745
  }
746
746
  function sparseSetRange(bits, a, b, undef = 0) {
747
- let i = a >> 5, j = b >> 5;
747
+ let i = a >> 5;
748
+ const j = b >> 5;
748
749
  if (i === j) {
749
750
  sparseSetMask(bits, i, (1 << (b & 0x1f)) - (1 << (a & 0x1f)), undef);
750
751
  }
@@ -762,7 +763,8 @@ function sparseSetRange(bits, a, b, undef = 0) {
762
763
  }
763
764
  }
764
765
  function sparseClearRange(bits, a, b, undef = 0) {
765
- let i = a >> 5, j = b >> 5;
766
+ let i = a >> 5;
767
+ const j = b >> 5;
766
768
  if (i === j) {
767
769
  sparseClearMask(bits, i, (1 << (b & 0x1f)) - (1 << (a & 0x1f)), undef);
768
770
  }
@@ -821,36 +823,32 @@ function sparseComplement(bits) {
821
823
  function sparseIntersect(bits, other) {
822
824
  const result = [];
823
825
  for (const i in bits) {
824
- if (other[i] !== undefined) {
826
+ if (other[i] !== undefined)
825
827
  result[i] = bits[i] & other[i];
826
- }
827
828
  }
828
829
  return result;
829
830
  }
830
831
  function sparseUnion(bits, other) {
831
832
  const result = [];
832
833
  for (const i in bits) {
833
- if (other[i] !== undefined) {
834
+ if (other[i] !== undefined)
834
835
  result[i] = bits[i] | other[i];
835
- }
836
836
  }
837
837
  return result;
838
838
  }
839
839
  function sparseXor(bits, other) {
840
840
  const result = [];
841
841
  for (const i in bits) {
842
- if (other[i] !== undefined) {
842
+ if (other[i] !== undefined)
843
843
  result[i] = bits[i] ^ other[i];
844
- }
845
844
  }
846
845
  return result;
847
846
  }
848
847
  function sparseDifference(bits, other) {
849
848
  const result = [];
850
849
  for (const i in bits) {
851
- if (other[i] !== undefined) {
850
+ if (other[i] !== undefined)
852
851
  result[i] = bits[i] & ~other[i];
853
- }
854
852
  }
855
853
  return result;
856
854
  }
@@ -975,7 +973,7 @@ function* sparseWhere(bits, set, from = -1, to, undef = 0) {
975
973
  }
976
974
  function* sparseRanges(bits, set, undef = 0) {
977
975
  let start = -1, end = 0;
978
- let other = undef ? set : !set;
976
+ const other = undef ? set : !set;
979
977
  for (const i in bits) {
980
978
  let b = bits[i] ^ undef;
981
979
  const c0 = +i * 32;
@@ -1325,3 +1323,4 @@ class SparseBits2 extends SparseBits {
1325
1323
  }
1326
1324
  exports.SparseBits2 = SparseBits2;
1327
1325
  ;
1326
+ //# sourceMappingURL=bits.js.map
package/dist/glob.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare function parseGlob(glob: string): string;
1
+ export declare function parse(glob: string): string;
2
2
  export declare function anchoredRe(re: string): RegExp;
3
- export declare function globToRe(glob: string): RegExp;
4
- export declare function globToReMulti(globs: string[]): RegExp;
3
+ export declare function toRe(glob: string): RegExp;
4
+ export declare function toReMulti(globs: string[]): RegExp;
package/dist/glob.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseGlob = parseGlob;
3
+ exports.parse = parse;
4
4
  exports.anchoredRe = anchoredRe;
5
- exports.globToRe = globToRe;
6
- exports.globToReMulti = globToReMulti;
5
+ exports.toRe = toRe;
6
+ exports.toReMulti = toReMulti;
7
7
  const posixClasses = {
8
8
  alnum: '\\p{L}\\p{Nl}\\p{Nd}',
9
9
  alpha: '\\p{L}\\p{Nl}',
@@ -23,7 +23,7 @@ const posixClasses = {
23
23
  //-----------------------------------------------------------------------------
24
24
  // Glob pattern to regex
25
25
  //-----------------------------------------------------------------------------
26
- function parseGlob(glob) {
26
+ function parse(glob) {
27
27
  let result = '';
28
28
  let depth = 0;
29
29
  for (let i = 0; i < glob.length; ++i) {
@@ -104,9 +104,10 @@ function parseGlob(glob) {
104
104
  function anchoredRe(re) {
105
105
  return new RegExp(`^${re}$`);
106
106
  }
107
- function globToRe(glob) {
108
- return anchoredRe(parseGlob(glob));
107
+ function toRe(glob) {
108
+ return anchoredRe(parse(glob));
109
109
  }
110
- function globToReMulti(globs) {
111
- return anchoredRe(globs.map(parseGlob).join('|'));
110
+ function toReMulti(globs) {
111
+ return anchoredRe(globs.map(parse).join('|'));
112
112
  }
113
+ //# sourceMappingURL=glob.js.map
package/dist/index.d.ts CHANGED
@@ -2,10 +2,23 @@ export * from './object';
2
2
  export * from './iterator';
3
3
  export * from './string';
4
4
  export * from './algorithm';
5
- export * from './glob';
5
+ export * as glob from './glob';
6
6
  export * as bits from './bits';
7
7
  export * as array from './array';
8
8
  export * as async from './async';
9
9
  export * as insensitive from './insensitive';
10
10
  export declare function parallel(...fns: (() => any)[]): Promise<any[]>;
11
11
  export declare function serial(...fns: (() => any)[]): Promise<any[]>;
12
+ export declare function regex(strings: TemplateStringsArray, ...args: any[]): RegExp;
13
+ export declare function regex(flags: string): (strings: TemplateStringsArray, ...args: any[]) => RegExp;
14
+ export declare function reDup(re: RegExp): RegExp;
15
+ export declare class DeferredPromise<T> {
16
+ private promise;
17
+ private resolver?;
18
+ private rejecter?;
19
+ constructor(t?: T);
20
+ resolve(value: T): void;
21
+ reset(): void;
22
+ then<T2 = void>(onfulfilled?: (t: T) => T2 | PromiseLike<T2>): Promise<T2>;
23
+ reject(error: any): void;
24
+ }
package/dist/index.js CHANGED
@@ -36,15 +36,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
36
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.insensitive = exports.async = exports.array = exports.bits = void 0;
39
+ exports.DeferredPromise = exports.insensitive = exports.async = exports.array = exports.bits = exports.glob = void 0;
40
40
  exports.parallel = parallel;
41
41
  exports.serial = serial;
42
+ exports.regex = regex;
43
+ exports.reDup = reDup;
42
44
  const async = __importStar(require("./async"));
43
45
  __exportStar(require("./object"), exports);
44
46
  __exportStar(require("./iterator"), exports);
45
47
  __exportStar(require("./string"), exports);
46
48
  __exportStar(require("./algorithm"), exports);
47
- __exportStar(require("./glob"), exports);
49
+ exports.glob = __importStar(require("./glob"));
48
50
  exports.bits = __importStar(require("./bits"));
49
51
  exports.array = __importStar(require("./array"));
50
52
  exports.async = __importStar(require("./async"));
@@ -55,3 +57,48 @@ async function parallel(...fns) {
55
57
  async function serial(...fns) {
56
58
  return async.mapSerial(fns, f => f());
57
59
  }
60
+ function regex(param, ...args) {
61
+ const flags = typeof param === 'string' ? param : 'g';
62
+ if (typeof param === 'string')
63
+ return inner;
64
+ return inner(param, ...args);
65
+ function inner(strings, ...args) {
66
+ const s = strings.raw.reduce((s, str, i) => s + str + (args[i] || ''), '');
67
+ const lines = s.split('\n');
68
+ return new RegExp(lines.map(line => line.split('#')[0].trim()).join(''), flags);
69
+ }
70
+ ;
71
+ }
72
+ function reDup(re) { return new RegExp(re.source, re.flags); }
73
+ class DeferredPromise {
74
+ promise;
75
+ resolver;
76
+ rejecter;
77
+ constructor(t) {
78
+ this.promise = t === undefined ? new Promise((resolve, reject) => {
79
+ this.resolver = resolve;
80
+ this.rejecter = reject;
81
+ }) : Promise.resolve(t);
82
+ }
83
+ resolve(value) {
84
+ if (this.resolver) {
85
+ this.resolver(value);
86
+ this.resolver = undefined;
87
+ }
88
+ }
89
+ reset() {
90
+ this.promise = new Promise((resolve, reject) => {
91
+ this.resolver = resolve;
92
+ this.rejecter = reject;
93
+ });
94
+ }
95
+ then(onfulfilled) {
96
+ return this.promise.then(onfulfilled);
97
+ }
98
+ reject(error) {
99
+ if (this.rejecter)
100
+ this.rejecter(error);
101
+ }
102
+ }
103
+ exports.DeferredPromise = DeferredPromise;
104
+ //# sourceMappingURL=index.js.map
@@ -77,3 +77,4 @@ class Set extends globalThis.Set {
77
77
  has(value) { return super.has(value.toUpperCase()); }
78
78
  }
79
79
  exports.Set = Set;
80
+ //# sourceMappingURL=insensitive.js.map
package/dist/iterator.js CHANGED
@@ -76,3 +76,4 @@ function filter(iterable, func) {
76
76
  array.push(v);
77
77
  return array;
78
78
  }
79
+ //# sourceMappingURL=iterator.js.map
package/dist/object.d.ts CHANGED
@@ -11,8 +11,9 @@ export declare class Lazy<T> {
11
11
  private _value;
12
12
  constructor(factory: () => T);
13
13
  get value(): T;
14
- then<U>(this: T extends Promise<infer R> ? Lazy<T> : never, onFulfilled: (value: T extends Promise<infer R> ? R : never) => U): Promise<U>;
14
+ then<U>(this: T extends Promise<infer _R> ? Lazy<T> : never, onFulfilled: (value: T extends Promise<infer R> ? R : never) => U): Promise<U>;
15
15
  }
16
+ export declare function Memoize<T>(func: () => T): () => T;
16
17
  export declare class CallCombiner0 {
17
18
  private timeout?;
18
19
  combine(delay: number, func: () => void): void;
package/dist/object.js CHANGED
@@ -9,6 +9,7 @@ exports.clone = clone;
9
9
  exports.compare = compare;
10
10
  exports.reverse_compare = reverse_compare;
11
11
  exports.reverse = reverse;
12
+ exports.Memoize = Memoize;
12
13
  exports.makeCache = makeCache;
13
14
  exports.union = union;
14
15
  exports.difference = difference;
@@ -65,6 +66,10 @@ class Lazy {
65
66
  }
66
67
  }
67
68
  exports.Lazy = Lazy;
69
+ function Memoize(func) {
70
+ const lazy = new Lazy(func);
71
+ return () => lazy.value;
72
+ }
68
73
  class CallCombiner0 {
69
74
  timeout;
70
75
  combine(delay, func) {
@@ -117,3 +122,4 @@ function difference(a, b) {
117
122
  }
118
123
  return [remaining, removed];
119
124
  }
125
+ //# sourceMappingURL=object.js.map
package/dist/string.d.ts CHANGED
@@ -5,6 +5,7 @@ export declare function splitLastOf(value: string, find: string): (string | unde
5
5
  export declare function trim0(value: string): string;
6
6
  export declare function replace(value: string, re: RegExp, process: (match: RegExpExecArray) => string): string;
7
7
  export declare function async_replace(value: string, re: RegExp, process: (match: RegExpExecArray) => Promise<string>): Promise<string>;
8
+ export declare function async_replace_async(value: string, re: RegExp, process: (match: RegExpExecArray) => Promise<string>): Promise<string>;
8
9
  export declare function replace_back(value: string, re: RegExp, process: (match: RegExpExecArray, right: string) => string): string;
9
10
  export declare function async_replace_back(value: string, re: RegExp, process: (match: RegExpExecArray, right: string) => Promise<string>): Promise<string>;
10
11
  export declare function splitEvery(s: string, n: number): string[];
package/dist/string.js CHANGED
@@ -11,6 +11,7 @@ exports.splitLastOf = splitLastOf;
11
11
  exports.trim0 = trim0;
12
12
  exports.replace = replace;
13
13
  exports.async_replace = async_replace;
14
+ exports.async_replace_async = async_replace_async;
14
15
  exports.replace_back = replace_back;
15
16
  exports.async_replace_back = async_replace_back;
16
17
  exports.splitEvery = splitEvery;
@@ -54,17 +55,29 @@ function replace(value, re, process) {
54
55
  return result + value.substring(i);
55
56
  }
56
57
  /*
57
- export async function async_replace(value: string, re: RegExp, process: (match: RegExpExecArray)=>Promise<string>): Promise<string> {
58
+ export async function async_replace<T = undefined>(value: string, re: RegExp, process: (match: RegExpExecArray, right: (context?: T)=>Promise<string>, context?: T)=>Promise<string>, context?: T): Promise<string> {
59
+ const right = async (context?: T) => async_replace(value, re, process, context);
58
60
  let result = "";
59
- let i = 0;
61
+ let i = re.lastIndex;
60
62
  for (let m; (m = re.exec(value));) {
61
- result += value.substring(i, m.index) + await process(m);
63
+ result += value.substring(i, m.index) + await process(m, right, context);
62
64
  i = re.lastIndex;
63
65
  }
66
+ re.lastIndex = value.length;
64
67
  return result + value.substring(i);
65
68
  }
66
69
  */
67
70
  async function async_replace(value, re, process) {
71
+ let result = "";
72
+ let i = re.lastIndex;
73
+ for (let m; (m = re.exec(value));) {
74
+ result += value.substring(i, m.index) + await process(m);
75
+ i = re.lastIndex;
76
+ }
77
+ re.lastIndex = value.length;
78
+ return result + value.substring(i);
79
+ }
80
+ async function async_replace_async(value, re, process) {
68
81
  const combine = async (m) => value.substring(i, m.index) + await process(m);
69
82
  const promises = [];
70
83
  let i = 0;
@@ -161,3 +174,4 @@ class StringParser {
161
174
  }
162
175
  }
163
176
  exports.StringParser = StringParser;
177
+ //# sourceMappingURL=string.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isopodlabs/utilities",
3
- "version": "1.7.1",
3
+ "version": "1.9.0",
4
4
  "description": "Various typescript helpers.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,7 +16,9 @@
16
16
  "README.md"
17
17
  ],
18
18
  "scripts": {
19
- "build": "tsc"
19
+ "build": "tsc",
20
+ "watch": "tsc -watch -p ./",
21
+ "lint": "eslint \"src/**/*.ts\""
20
22
  },
21
23
  "keywords": [
22
24
  "typescript",
@@ -29,7 +31,6 @@
29
31
  "devDependencies": {
30
32
  "@types/node": "^22.13.8",
31
33
  "eslint": "^9.21.0",
32
- "typescript": "^5.8.2",
33
34
  "typescript-eslint": "^8.26.0"
34
35
  }
35
36
  }