@isopodlabs/utilities 1.5.3 → 1.5.5

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
@@ -6,7 +6,7 @@ exports.argmin = argmin;
6
6
  exports.min = min;
7
7
  exports.argmax = argmax;
8
8
  exports.max = max;
9
- const utils_1 = require("./utils");
9
+ const object_1 = require("./object");
10
10
  function partition(array, func) {
11
11
  const partitions = {};
12
12
  for (const i of array)
@@ -24,21 +24,7 @@ function lowerBound(array, value, func) {
24
24
  }
25
25
  return i;
26
26
  }
27
- /*
28
- export function argmin<T>(array: T[], func?: (i: T) => number) {
29
- let mini = 0;
30
- let minv = func ? func(array[0]) : array[0];
31
- for (let i = 1; i < array.length; i++) {
32
- const v = func ? func(array[i]) : array[i];
33
- if (v < minv) {
34
- mini = i;
35
- minv = v;
36
- }
37
- }
38
- return mini;
39
- }
40
- */
41
- function argmin(array, func = utils_1.compare) {
27
+ function argmin(array, func = object_1.compare) {
42
28
  let mini = 0;
43
29
  for (let i = 1; i < array.length; i++) {
44
30
  const v = func(array[i], array[mini]);
@@ -47,10 +33,10 @@ function argmin(array, func = utils_1.compare) {
47
33
  }
48
34
  return mini;
49
35
  }
50
- function min(array, func = utils_1.compare) {
36
+ function min(array, func = object_1.compare) {
51
37
  return array[argmin(array, func)];
52
38
  }
53
- function argmax(array, func = utils_1.compare) {
39
+ function argmax(array, func = object_1.compare) {
54
40
  let maxi = 0;
55
41
  for (let i = 1; i < array.length; i++) {
56
42
  const v = func(array[i], array[maxi]);
@@ -59,6 +45,6 @@ function argmax(array, func = utils_1.compare) {
59
45
  }
60
46
  return maxi;
61
47
  }
62
- function max(array, func = utils_1.compare) {
48
+ function max(array, func = object_1.compare) {
63
49
  return array[argmax(array, func)];
64
50
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from './utils';
1
+ export * from './object';
2
2
  export * from './iterator';
3
3
  export * from './string';
4
4
  export * from './algorithm';
package/dist/index.js CHANGED
@@ -37,7 +37,7 @@ var __importStar = (this && this.__importStar) || (function () {
37
37
  })();
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.insensitive = void 0;
40
- __exportStar(require("./utils"), exports);
40
+ __exportStar(require("./object"), exports);
41
41
  __exportStar(require("./iterator"), exports);
42
42
  __exportStar(require("./string"), exports);
43
43
  __exportStar(require("./algorithm"), exports);
@@ -10,7 +10,7 @@ declare class CaseInsensitiveString {
10
10
  endsWith(searchString: string, position?: number): boolean;
11
11
  indexOf(searchString: string, position?: number): number;
12
12
  lastIndexOf(searchString: string, position?: number): number;
13
- compare(other: string | CaseInsensitiveString): 1 | -1 | 0;
13
+ compare(other: string | CaseInsensitiveString): 0 | 1 | -1;
14
14
  }
15
15
  declare class CaseInsensitiveString2 extends CaseInsensitiveString {
16
16
  private orig;
@@ -19,7 +19,7 @@ declare class CaseInsensitiveString2 extends CaseInsensitiveString {
19
19
  }
20
20
  export declare function String(value: string): CaseInsensitiveString;
21
21
  export declare function String2(value: string): CaseInsensitiveString2;
22
- export declare function compare(a: string, b: string): 1 | -1 | 0;
22
+ export declare function compare(a: string, b: string): 0 | 1 | -1;
23
23
  export declare function Record<T>(obj: Record<string, T>): Record<string, T>;
24
24
  export declare function Record2<T>(obj: Record<string, T>): Record<string, T>;
25
25
  export declare class Map<T> extends globalThis.Map<string, T> {
@@ -1,19 +1,18 @@
1
1
  export type SpreadType<T> = T extends Iterable<infer U> ? U[] : never;
2
- export declare function arrayAppend<T, U extends Iterable<T>>(array: T[], items: U): void;
3
2
  export declare function arrayRemove<T>(array: T[], item: T): boolean;
4
3
  export declare function arrayCompare<T>(arr1: T[], arr2: T[]): number;
5
4
  export declare function arrayEqual<T>(arr1: T[], arr2: T[]): boolean;
6
5
  export declare function arrayRotate<T>(array: T[], start: number, end: number, shift: number): void;
7
6
  export declare function arrayReverse<T>(array: T[], start: number, end: number): void;
8
- export declare function array_make<T>(n: number, constructor: new () => T): T[];
9
- export declare function eachIterable<T>(iterable: Iterable<T> | undefined, func: (v: T, i: number) => void): void;
10
- export declare function findIterable<T>(iterable: Iterable<T> | undefined, func: (v: T) => boolean): T | undefined;
11
- export declare function mapIterable<T, U>(iterable: Iterable<T> | undefined, func: (v: T, i: number) => U): U[];
7
+ export declare function arrayMake<T>(n: number, constructor: new () => T): T[];
8
+ export declare function forEach<T>(iterable: Iterable<T> | undefined, func: (v: T, i: number) => void): void;
9
+ export declare function find<T>(iterable: Iterable<T> | undefined, func: (v: T) => boolean): T | undefined;
10
+ export declare function map<T, U>(iterable: Iterable<T> | undefined, func: (v: T, i: number) => U): U[];
11
+ export declare function reduce<T, U>(iterable: Iterable<T>, func: (acc: U, v: T, i: number, iterable: Iterable<T>) => U, initialValue: U): U;
12
+ export declare function filter<T>(iterable: Iterable<T>, func: (v: T, i: number) => unknown): T[];
12
13
  export declare function asyncMap<T, U>(iterable: Iterable<T> | undefined, func: (v: T, i: number) => Promise<U>): Promise<U[]>;
13
- export declare function asyncReduce<T, U>(array: T[], func: (acc: U, v: T, i: number, array: T[]) => Promise<U>, initialValue: U): Promise<U>;
14
+ export declare function asyncMapSerial<T, U>(iterable: Iterable<T> | undefined, func: (v: T, i: number) => Promise<U>): Promise<U[]>;
15
+ export declare function asyncReduce<T, U>(iterable: Iterable<T>, func: (acc: U, v: T, i: number, iterable: Iterable<T>) => Promise<U>, initialValue: U): Promise<U>;
16
+ export declare function asyncFilter<T>(iterable: Iterable<T>, func: (v: T) => Promise<unknown>): Promise<T[]>;
14
17
  export declare function parallel(...fns: (() => any)[]): Promise<any[]>;
15
18
  export declare function serial(...fns: (() => any)[]): Promise<any[]>;
16
- export declare function filterIterable<T>(iterable: Iterable<T>, func: (v: T, i: number) => unknown): T[];
17
- export declare function asyncFilter<T>(iterable: Iterable<T>, func: (v: T) => Promise<unknown>): Promise<T[]>;
18
- export declare function mapObject<T, U>(obj: Record<string, T>, func: (x: [k: string, v: T]) => [k: string, v: U]): Record<string, U>;
19
- export declare function filterObject<T>(obj: Record<string, T>, func: (x: [k: string, v: T]) => boolean): Record<string, T>;
package/dist/iterator.js CHANGED
@@ -1,28 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.arrayAppend = arrayAppend;
4
3
  exports.arrayRemove = arrayRemove;
5
4
  exports.arrayCompare = arrayCompare;
6
5
  exports.arrayEqual = arrayEqual;
7
6
  exports.arrayRotate = arrayRotate;
8
7
  exports.arrayReverse = arrayReverse;
9
- exports.array_make = array_make;
10
- exports.eachIterable = eachIterable;
11
- exports.findIterable = findIterable;
12
- exports.mapIterable = mapIterable;
8
+ exports.arrayMake = arrayMake;
9
+ exports.forEach = forEach;
10
+ exports.find = find;
11
+ exports.map = map;
12
+ exports.reduce = reduce;
13
+ exports.filter = filter;
13
14
  exports.asyncMap = asyncMap;
15
+ exports.asyncMapSerial = asyncMapSerial;
14
16
  exports.asyncReduce = asyncReduce;
17
+ exports.asyncFilter = asyncFilter;
15
18
  exports.parallel = parallel;
16
19
  exports.serial = serial;
17
- exports.filterIterable = filterIterable;
18
- exports.asyncFilter = asyncFilter;
19
- exports.mapObject = mapObject;
20
- exports.filterObject = filterObject;
21
- const utils_1 = require("./utils");
22
- function arrayAppend(array, items) {
23
- for (const i of items)
24
- array.push(i);
25
- }
20
+ const object_1 = require("./object");
21
+ //remove element of array
26
22
  function arrayRemove(array, item) {
27
23
  const index = array.indexOf(item);
28
24
  if (index === -1)
@@ -33,7 +29,7 @@ function arrayRemove(array, item) {
33
29
  function arrayCompare(arr1, arr2) {
34
30
  const length = Math.min(arr1.length, arr2.length);
35
31
  for (let i = 0; i < length; i++) {
36
- const r = (0, utils_1.compare)(arr1[i], arr2[i]);
32
+ const r = (0, object_1.compare)(arr1[i], arr2[i]);
37
33
  if (r)
38
34
  return r;
39
35
  }
@@ -64,17 +60,17 @@ function arrayReverse(array, start, end) {
64
60
  end--;
65
61
  }
66
62
  }
67
- function array_make(n, constructor) {
63
+ function arrayMake(n, constructor) {
68
64
  return Array.from({ length: n }, () => new constructor);
69
65
  }
70
- function eachIterable(iterable, func) {
66
+ function forEach(iterable, func) {
71
67
  if (iterable) {
72
68
  let i = 0;
73
69
  for (const v of iterable)
74
70
  func(v, i++);
75
71
  }
76
72
  }
77
- function findIterable(iterable, func) {
73
+ function find(iterable, func) {
78
74
  if (iterable) {
79
75
  for (const v of iterable) {
80
76
  if (func(v))
@@ -82,39 +78,58 @@ function findIterable(iterable, func) {
82
78
  }
83
79
  }
84
80
  }
85
- function mapIterable(iterable, func) {
81
+ function map(iterable, func) {
86
82
  return iterable ? Array.from(iterable, func) : [];
87
83
  }
88
- async function asyncMap(iterable, func) {
89
- return Promise.all(mapIterable(iterable, func));
84
+ function reduce(iterable, func, initialValue) {
85
+ let i = 0;
86
+ let acc = initialValue;
87
+ for (const v of iterable)
88
+ acc = func(acc, v, i++, iterable);
89
+ return acc;
90
90
  }
91
- async function asyncReduce(array, func, initialValue) {
92
- return array.reduce(async (promise, v, i, array) => func(await promise, v, i, array), Promise.resolve(initialValue));
91
+ function filter(iterable, func) {
92
+ const array = [];
93
+ let i = 0;
94
+ for (const v of iterable)
95
+ if (func(v, i++))
96
+ array.push(v);
97
+ return array;
93
98
  }
94
- async function parallel(...fns) {
95
- return asyncMap(fns, f => f());
99
+ async function asyncMap(iterable, func) {
100
+ return Promise.all(map(iterable, func));
96
101
  }
97
- async function serial(...fns) {
102
+ async function asyncMapSerial(iterable, func) {
98
103
  const results = [];
99
- for (const f of fns)
100
- results.push(await f());
104
+ let i = 0;
105
+ for (const v of iterable || [])
106
+ results.push(await func(v, i++));
101
107
  return results;
102
108
  }
103
- function filterIterable(iterable, func) {
104
- const array = [];
109
+ async function asyncReduce(iterable, func, initialValue) {
105
110
  let i = 0;
111
+ let acc = initialValue;
106
112
  for (const v of iterable)
107
- if (func(v, i++))
108
- array.push(v);
109
- return array;
113
+ acc = await func(acc, v, i++, iterable);
114
+ return acc;
115
+ /*
116
+ return reduce<T, Promise<U>>(
117
+ iterable,
118
+ async (promise, v, i, iterable) => func(await promise, v, i, iterable),
119
+ Promise.resolve(initialValue)
120
+ );
121
+ */
110
122
  }
111
123
  async function asyncFilter(iterable, func) {
112
- const filters = await Promise.all(mapIterable(iterable, func));
113
- return filterIterable(iterable, (_, i) => filters[i]);
124
+ const filters = await Promise.all(map(iterable, func));
125
+ return filter(iterable, (_, i) => filters[i]);
114
126
  }
115
- function mapObject(obj, func) {
116
- return Object.fromEntries(Object.entries(obj).map(x => func(x)));
127
+ async function parallel(...fns) {
128
+ return asyncMap(fns, f => f());
117
129
  }
118
- function filterObject(obj, func) {
119
- return Object.fromEntries(Object.entries(obj).filter(x => func(x)));
130
+ async function serial(...fns) {
131
+ const results = [];
132
+ for (const f of fns)
133
+ results.push(await f());
134
+ return results;
120
135
  }
@@ -0,0 +1,30 @@
1
+ export declare function mapObject<T, U>(obj: Record<string, T>, func: (x: [k: string, v: T]) => [k: string, v: U]): Record<string, U>;
2
+ export declare function filterObject<T>(obj: Record<string, T>, func: (x: [k: string, v: T]) => boolean): Record<string, T>;
3
+ export declare function merge(...list: Record<string, any>[]): Record<string, any>;
4
+ export declare function isEmpty(obj: object): boolean;
5
+ export declare function clone<T extends object>(obj: T): T;
6
+ export declare function compare<T>(a: T, b: T): number;
7
+ export declare function reverse_compare<T>(a: T, b: T): number;
8
+ export declare function reverse<T, R>(func: (a: T, b: T) => R): (a: T, b: T) => R;
9
+ export declare class Lazy<T> {
10
+ private factory;
11
+ private _value;
12
+ constructor(factory: () => T);
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>;
15
+ }
16
+ export declare class CallCombiner0 {
17
+ private timeout;
18
+ combine(delay: number, func: () => void): void;
19
+ pending(): boolean;
20
+ }
21
+ export declare class CallCombiner extends CallCombiner0 {
22
+ private func;
23
+ private delay;
24
+ constructor(func: () => void, delay: number);
25
+ trigger(): void;
26
+ }
27
+ export declare function makeCache<T>(load: (key: string) => T): {
28
+ get: (fullpath: string) => T;
29
+ remove: (fullpath: string) => void;
30
+ };
package/dist/object.js ADDED
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CallCombiner = exports.CallCombiner0 = exports.Lazy = void 0;
4
+ exports.mapObject = mapObject;
5
+ exports.filterObject = filterObject;
6
+ exports.merge = merge;
7
+ exports.isEmpty = isEmpty;
8
+ exports.clone = clone;
9
+ exports.compare = compare;
10
+ exports.reverse_compare = reverse_compare;
11
+ exports.reverse = reverse;
12
+ exports.makeCache = makeCache;
13
+ function mapObject(obj, func) {
14
+ return Object.fromEntries(Object.entries(obj).map(x => func(x)));
15
+ }
16
+ function filterObject(obj, func) {
17
+ return Object.fromEntries(Object.entries(obj).filter(x => func(x)));
18
+ }
19
+ function merge(...list) {
20
+ function isObject(value) {
21
+ return typeof value === 'object' && value !== null;
22
+ }
23
+ function recurse(target, source) {
24
+ for (const key in source) {
25
+ if (isObject(source[key]) && isObject(target[key]))
26
+ recurse(target[key], source[key]);
27
+ else
28
+ target[key] = source[key];
29
+ }
30
+ return target;
31
+ }
32
+ return list.reduce((merged, r) => recurse(merged, r), {});
33
+ }
34
+ function isEmpty(obj) {
35
+ return Object.keys(obj).length === 0;
36
+ }
37
+ function clone(obj) {
38
+ return Object.assign(Object.create(Object.getPrototypeOf(obj)), obj);
39
+ }
40
+ function compare(a, b) {
41
+ return a < b ? -1 : a > b ? 1 : 0;
42
+ }
43
+ function reverse_compare(a, b) {
44
+ return compare(b, a);
45
+ }
46
+ function reverse(func) {
47
+ return (a, b) => func(b, a);
48
+ }
49
+ class Lazy {
50
+ factory;
51
+ _value;
52
+ constructor(factory) {
53
+ this.factory = factory;
54
+ }
55
+ get value() {
56
+ if (this._value === undefined)
57
+ this._value = this.factory();
58
+ return this._value;
59
+ }
60
+ // Add 'then' method only when T is a Promise
61
+ then(onFulfilled) {
62
+ return this.value.then(onFulfilled);
63
+ }
64
+ }
65
+ exports.Lazy = Lazy;
66
+ class CallCombiner0 {
67
+ timeout = null;
68
+ combine(delay, func) {
69
+ if (this.timeout)
70
+ clearTimeout(this.timeout);
71
+ this.timeout = setTimeout(() => {
72
+ this.timeout = null;
73
+ func();
74
+ }, delay);
75
+ }
76
+ pending() {
77
+ return !!this.timeout;
78
+ }
79
+ }
80
+ exports.CallCombiner0 = CallCombiner0;
81
+ class CallCombiner extends CallCombiner0 {
82
+ func;
83
+ delay;
84
+ constructor(func, delay) {
85
+ super();
86
+ this.func = func;
87
+ this.delay = delay;
88
+ }
89
+ trigger() {
90
+ super.combine(this.delay, this.func);
91
+ }
92
+ }
93
+ exports.CallCombiner = CallCombiner;
94
+ function makeCache(load) {
95
+ const cache = {};
96
+ return {
97
+ get: (fullpath) => {
98
+ if (!cache[fullpath])
99
+ cache[fullpath] = load(fullpath);
100
+ return cache[fullpath];
101
+ },
102
+ remove: (fullpath) => {
103
+ delete cache[fullpath];
104
+ },
105
+ };
106
+ }
package/dist/string.d.ts CHANGED
@@ -26,3 +26,7 @@ export declare class StringParser {
26
26
  match(re: RegExp): string | undefined;
27
27
  exec(re: RegExp): RegExpExecArray | undefined;
28
28
  }
29
+ export declare function parseGlob(glob: string): string;
30
+ export declare function anchoredRe(re: string): RegExp;
31
+ export declare function globToRe(glob: string): RegExp;
32
+ export declare function globToReMulti(globs: string[]): RegExp;
package/dist/string.js CHANGED
@@ -17,6 +17,10 @@ exports.splitEvery = splitEvery;
17
17
  exports.tag = tag;
18
18
  exports.previousChar = previousChar;
19
19
  exports.hasCustomToString = hasCustomToString;
20
+ exports.parseGlob = parseGlob;
21
+ exports.anchoredRe = anchoredRe;
22
+ exports.globToRe = globToRe;
23
+ exports.globToReMulti = globToReMulti;
20
24
  function firstOf(value, find) {
21
25
  let index = value.length;
22
26
  for (const c of find) {
@@ -161,3 +165,106 @@ class StringParser {
161
165
  }
162
166
  }
163
167
  exports.StringParser = StringParser;
168
+ const posixClasses = {
169
+ alnum: '\\p{L}\\p{Nl}\\p{Nd}',
170
+ alpha: '\\p{L}\\p{Nl}',
171
+ ascii: '\\x00-\\x7f',
172
+ blank: '\\p{Zs}\\t',
173
+ cntrl: '\\p{Cc}',
174
+ digit: '\\p{Nd}',
175
+ graph: '^\\p{Z}\\p{C}',
176
+ lower: '\\p{Ll}',
177
+ print: '\\p{C}',
178
+ punct: '\\p{P}',
179
+ space: '\\p{Z}\\t\\r\\n\\v\\f',
180
+ upper: '\\p{Lu}',
181
+ word: '\\p{L}\\p{Nl}\\p{Nd}\\p{Pc}',
182
+ xdigit: 'A-Fa-f0-9',
183
+ };
184
+ function parseGlob(glob) {
185
+ let result = '';
186
+ let depth = 0;
187
+ for (let i = 0; i < glob.length; ++i) {
188
+ let c = glob[i];
189
+ switch (c) {
190
+ case '\\':
191
+ c = glob[++i];
192
+ if ('*?+.,^$()|[]a-zA-Z'.includes(c))
193
+ result += '\\';
194
+ break;
195
+ case '*':
196
+ if (glob[i + 1] === '*') {
197
+ result += '.*';
198
+ ++i;
199
+ }
200
+ else {
201
+ result += '[^/]*';
202
+ }
203
+ continue;
204
+ case '?':
205
+ c = '.';
206
+ break;
207
+ case '+':
208
+ case '.':
209
+ case '^':
210
+ case '$':
211
+ case '(':
212
+ case ')':
213
+ case '|':
214
+ result += `\\`;
215
+ break;
216
+ case '[': {
217
+ const end = glob.indexOf(']', i + 1);
218
+ if (end > i) {
219
+ const next = glob[i + 1];
220
+ if (next === ':' && glob[end - 1] === ':') {
221
+ const p = posixClasses[glob.slice(i + 2, end - 1)];
222
+ if (p) {
223
+ result += `[${p}]`;
224
+ i = end;
225
+ continue;
226
+ }
227
+ else {
228
+ console.log(`Warning: Unknown POSIX class ${glob.slice(i + 2, end - 1)} in glob pattern ${glob}`);
229
+ }
230
+ }
231
+ const neg = next === '!' || next === '^';
232
+ result += `[${neg ? '^' : ''}${glob.slice(neg ? i + 2 : i + 1, end)}]`;
233
+ i = end;
234
+ continue;
235
+ }
236
+ result += '\\';
237
+ break;
238
+ }
239
+ case '{':
240
+ ++depth;
241
+ c = '(';
242
+ break;
243
+ case '}':
244
+ if (depth > 0) {
245
+ --depth;
246
+ c = ')';
247
+ }
248
+ break;
249
+ case ',':
250
+ if (depth > 0)
251
+ c = '|';
252
+ break;
253
+ }
254
+ result += c;
255
+ }
256
+ if (depth > 0) {
257
+ console.log(`Warning: Unmatched { in glob pattern ${glob}`);
258
+ result += ')'.repeat(depth);
259
+ }
260
+ return result;
261
+ }
262
+ function anchoredRe(re) {
263
+ return new RegExp(`^${re}$`);
264
+ }
265
+ function globToRe(glob) {
266
+ return anchoredRe(parseGlob(glob));
267
+ }
268
+ function globToReMulti(globs) {
269
+ return anchoredRe(globs.map(parseGlob).join('|'));
270
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isopodlabs/utilities",
3
- "version": "1.5.3",
3
+ "version": "1.5.5",
4
4
  "description": "Various typescript helpers.",
5
5
  "repository": {
6
6
  "type": "git",