@isopodlabs/utilities 1.8.0 → 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.js CHANGED
@@ -73,3 +73,4 @@ function* lazySlice(array, start, end) {
73
73
  for (let i = start; i < end; i++)
74
74
  yield array[i];
75
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
@@ -1323,3 +1323,4 @@ class SparseBits2 extends SparseBits {
1323
1323
  }
1324
1324
  exports.SparseBits2 = SparseBits2;
1325
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
@@ -13,6 +13,7 @@ export declare class Lazy<T> {
13
13
  get value(): T;
14
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.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Various typescript helpers.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,6 +17,7 @@
17
17
  ],
18
18
  "scripts": {
19
19
  "build": "tsc",
20
+ "watch": "tsc -watch -p ./",
20
21
  "lint": "eslint \"src/**/*.ts\""
21
22
  },
22
23
  "keywords": [