@isopodlabs/utilities 1.4.1 → 1.5.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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Adrian Stephens
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,24 +1,27 @@
1
- # @isopodlabs/utilities
2
-
3
- This package provides a set of utilities for TypeScript.
4
-
5
- ## ☕ Support My Work
6
- If you use this package, consider [buying me a tea](https://coff.ee/adrianstephens) to support future updates!
7
-
8
- ## Usage
9
-
10
- Here is a basic example of how to use the package:
11
-
12
- ```typescript
13
- import * as utils from '@isopodlabs/utilities';
14
- ````
15
-
16
- **Interfaces**:
17
-
18
-
19
- **Functions**:
20
-
21
-
22
- ## License
23
-
1
+ # @isopodlabs/utilities
2
+ [![npm version](https://img.shields.io/npm/v/@isopodlabs/utilities.svg)](https://www.npmjs.com/package/@isopodlabs/utilities)
3
+ [![GitHub stars](https://img.shields.io/github/stars/adrianstephens/utilities.svg?style=social)](https://github.com/adrianstephens/utilities)
4
+ [![License](https://img.shields.io/npm/l/@isopodlabs/utilities.svg)](LICENSE.txt)
5
+
6
+ This package provides a set of utilities for TypeScript.
7
+
8
+ ## ☕ Support My Work
9
+ If you use this package, consider [buying me a cup of tea](https://coff.ee/adrianstephens) to support future updates!
10
+
11
+ ## Usage
12
+
13
+ Here is a basic example of how to use the package:
14
+
15
+ ```typescript
16
+ import * as utils from '@isopodlabs/utilities';
17
+ ````
18
+
19
+ **Interfaces**:
20
+
21
+
22
+ **Functions**:
23
+
24
+
25
+ ## License
26
+
24
27
  This project is licensed under the MIT License.
package/dist/glob.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ export interface Patcher {
2
+ patch(input: string): string;
3
+ }
4
+ export declare function patch<T>(patcher: Patcher, input: T): T;
5
+ export declare class MapPatcher implements Patcher {
6
+ matches: Record<string, string>;
7
+ constructor(matches: Record<string, string>);
8
+ add(key: string, value: string): this;
9
+ patch(input: string): string;
10
+ }
11
+ export declare function globRe(glob: string): RegExp;
12
+ export declare class Glob {
13
+ glob: string;
14
+ re: RegExp;
15
+ constructor(glob: string);
16
+ test(match: string): boolean;
17
+ star(match: string): string;
18
+ }
19
+ export declare function isWild(glob: string): boolean;
20
+ export declare function expandGlobs(patterns: string[], cwd: string): Promise<string[]>;
package/dist/glob.js ADDED
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.Glob = exports.MapPatcher = void 0;
37
+ exports.patch = patch;
38
+ exports.globRe = globRe;
39
+ exports.isWild = isWild;
40
+ exports.expandGlobs = expandGlobs;
41
+ const path = __importStar(require("path"));
42
+ const fs = __importStar(require("fs"));
43
+ function patch(patcher, input) {
44
+ if (typeof input === 'string')
45
+ return patcher.patch(input);
46
+ if (typeof input === 'object' && input !== null) {
47
+ if (Array.isArray(input))
48
+ return input.map(value => patch(patcher, value));
49
+ return Object.entries(input).reduce((acc, [key, value]) => {
50
+ acc[key] = patch(patcher, value);
51
+ return acc;
52
+ }, Object.create(Object.getPrototypeOf(input)));
53
+ }
54
+ return input;
55
+ }
56
+ class MapPatcher {
57
+ matches;
58
+ constructor(matches) {
59
+ this.matches = matches;
60
+ }
61
+ add(key, value) {
62
+ this.matches[key] = value;
63
+ return this;
64
+ }
65
+ patch(input) {
66
+ for (const [key, value] of Object.entries(this.matches))
67
+ input = input.replaceAll(key, value);
68
+ return input;
69
+ }
70
+ }
71
+ exports.MapPatcher = MapPatcher;
72
+ function globRe(glob) {
73
+ const regex = glob
74
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&') // Escape regex chars except * and ?
75
+ .replace(/\*/g, '[^/]*') // * matches any chars except dir separator
76
+ .replace(/\*\*/g, '.*') // ** matches any chars
77
+ .replace(/\?/g, '.'); // ? matches single char
78
+ return new RegExp(`^${regex}$`);
79
+ }
80
+ class Glob {
81
+ glob;
82
+ re;
83
+ constructor(glob) {
84
+ this.glob = glob;
85
+ this.re = globRe(glob);
86
+ }
87
+ test(match) {
88
+ return this.re.test(match);
89
+ }
90
+ star(match) {
91
+ const parts = this.glob.split('*');
92
+ return match.slice(parts[0].length, match.length - parts[1].length);
93
+ }
94
+ }
95
+ exports.Glob = Glob;
96
+ function isWild(glob) {
97
+ return glob.includes('*') || glob.includes('?');
98
+ }
99
+ async function getDirs(dir, glob) {
100
+ const star = dir.indexOf('*');
101
+ if (star >= 0) {
102
+ const startDir = dir.lastIndexOf(path.sep, star);
103
+ const endDir = dir.indexOf(path.sep, star);
104
+ const dirDone = dir.substring(0, startDir);
105
+ const dirWild = dir.substring(startDir + 1, endDir >= 0 ? endDir : undefined);
106
+ const dirRest = endDir >= 0 ? dir.substring(endDir + 1) : '';
107
+ const entries = await fs.promises.readdir(dirDone, { withFileTypes: true });
108
+ if (dirWild === '**') {
109
+ if (dirRest) {
110
+ return (await Promise.all(entries.filter(i => i.isDirectory()).map(async (i) => [
111
+ ...await getDirs(path.join(i.parentPath, i.name, '**', dirRest), glob),
112
+ ...await getDirs(path.join(i.parentPath, i.name, dirRest), glob)
113
+ ]))).flat();
114
+ }
115
+ else {
116
+ /*
117
+ return (await Promise.all(entries.filter(i => i.isDirectory()).map(i =>
118
+ getDirs(path.join(i.parentPath, i.name, '**'), glob)
119
+ ))).flat().concat(
120
+ entries.filter(i => !i.isDirectory() && glob.test(i.name))
121
+ .map(i => path.join(i.parentPath, i.name))
122
+ );*/
123
+ return (await Promise.all(entries.map(i => i.isDirectory() ? getDirs(path.join(i.parentPath, i.name, '**'), glob)
124
+ : glob.test(i.name) ? path.join(i.parentPath, i.name)
125
+ : []))).flat();
126
+ }
127
+ }
128
+ else {
129
+ const dirGlob = globRe(dirWild);
130
+ return (await Promise.all(entries
131
+ .filter(i => i.isDirectory() && dirGlob.test(i.name))
132
+ .map(i => getDirs(path.join(dirDone, i.name, dirRest), glob)))).flat();
133
+ }
134
+ }
135
+ else {
136
+ try {
137
+ const entries = await fs.promises.readdir(dir, { withFileTypes: true });
138
+ return entries
139
+ .filter(i => !i.isDirectory() && glob.test(i.name))
140
+ .map(i => path.join(i.parentPath, i.name));
141
+ }
142
+ catch (error) {
143
+ console.log(`Warning: Cannot read directory ${dir}: ${error}`);
144
+ return [];
145
+ }
146
+ }
147
+ }
148
+ async function expandGlobs(patterns, cwd) {
149
+ const files = (await Promise.all(patterns.map(async (i) => {
150
+ const pattern = path.resolve(cwd, i);
151
+ return isWild(i)
152
+ ? await getDirs(path.dirname(pattern), globRe(path.basename(pattern)))
153
+ : pattern;
154
+ }))).flat();
155
+ return [...new Set(files)]; // Remove duplicates
156
+ }
@@ -0,0 +1,22 @@
1
+ export declare class CaseInsensitiveString {
2
+ private value;
3
+ constructor(value: string);
4
+ get length(): number;
5
+ toString(): string;
6
+ includes(searchString: string, position?: number): boolean;
7
+ startsWith(searchString: string, position?: number): boolean;
8
+ endsWith(searchString: string, position?: number): boolean;
9
+ indexOf(searchString: string, position?: number): number;
10
+ lastIndexOf(searchString: string, position?: number): number;
11
+ compare(other: string): 0 | 1 | -1;
12
+ }
13
+ export declare class CaseInsensitiveString2 extends CaseInsensitiveString {
14
+ private orig;
15
+ constructor(orig: string);
16
+ toString(): string;
17
+ }
18
+ export declare function String(value: string): CaseInsensitiveString;
19
+ export declare function String2(value: string): CaseInsensitiveString2;
20
+ export declare function compare(a: string, b: string): 0 | 1 | -1;
21
+ export declare function Record<T>(obj: Record<string, T>): Record<string, T>;
22
+ export declare function Record2<T>(obj: Record<string, T>): Record<string, T>;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CaseInsensitiveString2 = exports.CaseInsensitiveString = void 0;
4
+ exports.String = String;
5
+ exports.String2 = String2;
6
+ exports.compare = compare;
7
+ exports.Record = Record;
8
+ exports.Record2 = Record2;
9
+ class CaseInsensitiveString {
10
+ value;
11
+ constructor(value) { this.value = value.toLowerCase(); }
12
+ get length() { return this.value.length; }
13
+ toString() { return this.value; }
14
+ includes(searchString, position) { return this.value.includes(searchString.toLowerCase(), position); }
15
+ startsWith(searchString, position) { return this.value.startsWith(searchString.toLowerCase(), position); }
16
+ endsWith(searchString, position) { return this.value.endsWith(searchString.toLowerCase(), position); }
17
+ indexOf(searchString, position) { return this.value.indexOf(searchString.toLowerCase(), position); }
18
+ lastIndexOf(searchString, position) { return this.value.lastIndexOf(searchString.toLowerCase(), position); }
19
+ compare(other) { const bi = other.toLowerCase(); return this.value < bi ? -1 : this.value > bi ? 1 : 0; }
20
+ }
21
+ exports.CaseInsensitiveString = CaseInsensitiveString;
22
+ // keeps original string
23
+ class CaseInsensitiveString2 extends CaseInsensitiveString {
24
+ orig;
25
+ constructor(orig) {
26
+ super(orig);
27
+ this.orig = orig;
28
+ }
29
+ toString() { return this.orig; }
30
+ }
31
+ exports.CaseInsensitiveString2 = CaseInsensitiveString2;
32
+ function String(value) {
33
+ return new CaseInsensitiveString(value);
34
+ }
35
+ function String2(value) {
36
+ return new CaseInsensitiveString2(value);
37
+ }
38
+ function compare(a, b) {
39
+ const ai = a.toUpperCase(), bi = b.toUpperCase();
40
+ return ai < bi ? -1 : ai > bi ? 1 : 0;
41
+ }
42
+ function Record(obj) {
43
+ return new Proxy(obj, {
44
+ get: (target, name) => target[name.toUpperCase()],
45
+ set: (target, name, value) => (target[name.toUpperCase()] = value, true),
46
+ has: (target, name) => name.toUpperCase() in target,
47
+ });
48
+ }
49
+ // keeps original record
50
+ function Record2(obj) {
51
+ return new Proxy(Object.entries(obj).reduce((acc, [key, value]) => ((acc[key.toUpperCase()] = value), acc), {}), {
52
+ get: (target, name) => target[name.toUpperCase()],
53
+ set: (target, name, value) => (target[name.toUpperCase()] = value, true),
54
+ has: (target, name) => name.toUpperCase() in target,
55
+ ownKeys: (target) => Object.keys(obj),
56
+ getOwnPropertyDescriptor: (target, name) => Object.getOwnPropertyDescriptor(obj, name)
57
+ });
58
+ }
package/dist/string.js CHANGED
@@ -45,25 +45,35 @@ function trim0(value) {
45
45
  return index === -1 ? value : value.substring(0, index);
46
46
  }
47
47
  function replace(value, re, process) {
48
- let m;
49
48
  let result = "";
50
49
  let i = 0;
51
- while ((m = re.exec(value))) {
50
+ for (let m; (m = re.exec(value));) {
52
51
  result += value.substring(i, m.index) + process(m);
53
52
  i = re.lastIndex;
54
53
  }
55
54
  return result + value.substring(i);
56
55
  }
57
- async function async_replace(value, re, process) {
58
- let m;
56
+ /*
57
+ export async function async_replace(value: string, re: RegExp, process: (match: RegExpExecArray)=>Promise<string>): Promise<string> {
59
58
  let result = "";
60
59
  let i = 0;
61
- while ((m = re.exec(value))) {
60
+ for (let m; (m = re.exec(value));) {
62
61
  result += value.substring(i, m.index) + await process(m);
63
62
  i = re.lastIndex;
64
63
  }
65
64
  return result + value.substring(i);
66
65
  }
66
+ */
67
+ async function async_replace(value, re, process) {
68
+ const combine = async (m) => value.substring(i, m.index) + await process(m);
69
+ const promises = [];
70
+ let i = 0;
71
+ for (let m; (m = re.exec(value));) {
72
+ promises.push(combine(m));
73
+ i = re.lastIndex;
74
+ }
75
+ return (await Promise.all(promises)).join('') + value.substring(i);
76
+ }
67
77
  function replace_back(value, re, process) {
68
78
  const start = re.lastIndex;
69
79
  const m = re.exec(value);
@@ -88,10 +98,10 @@ function splitEvery(s, n) {
88
98
  return Array.from({ length: Math.ceil(s.length / n) }, (_, i) => s.slice(i * n, (i + 1) * n));
89
99
  }
90
100
  function tag(strings, ...keys) {
91
- return ((...values) => {
101
+ return (...values) => {
92
102
  const dict = values.at(-1) || {};
93
103
  return keys.map((key, i) => (Number.isInteger(key) ? values[key] : dict[key]) + strings[i + 1]).join('');
94
- });
104
+ };
95
105
  }
96
106
  function previousChar(str, pos) {
97
107
  return pos === 0
package/package.json CHANGED
@@ -1,28 +1,36 @@
1
- {
2
- "name": "@isopodlabs/utilities",
3
- "version": "1.4.1",
4
- "description": "Various typescript helpers.",
5
- "main": "dist/index.js",
6
- "files": [
7
- "dist",
8
- "README.md"
9
- ],
10
- "scripts": {
11
- "build": "tsc",
12
- "test": "echo \"No tests specified\" && exit 0"
13
- },
14
- "keywords": [
15
- "typescript",
16
- "string",
17
- "iterator",
18
- "algorithm"
19
- ],
20
- "author": "Adrian Stephens",
21
- "license": "MIT",
22
- "devDependencies": {
23
- "@types/node": "^22.13.8",
24
- "eslint": "^9.21.0",
25
- "typescript": "^5.8.2",
26
- "typescript-eslint": "^8.26.0"
27
- }
28
- }
1
+ {
2
+ "name": "@isopodlabs/utilities",
3
+ "version": "1.5.0",
4
+ "description": "Various typescript helpers.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/adrianstephens/ts-utilities.git"
8
+ },
9
+ "main": "dist/index.js",
10
+ "exports": {
11
+ ".": "./dist/index.js",
12
+ "./insensitive": "./dist/insensitive.js"
13
+ },
14
+ "files": [
15
+ "dist/**/*.{js,d.ts}",
16
+ "README.md"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "test": "echo \"No tests specified\" && exit 0"
21
+ },
22
+ "keywords": [
23
+ "typescript",
24
+ "string",
25
+ "iterator",
26
+ "algorithm"
27
+ ],
28
+ "author": "Adrian Stephens",
29
+ "license": "MIT",
30
+ "devDependencies": {
31
+ "@types/node": "^22.13.8",
32
+ "eslint": "^9.21.0",
33
+ "typescript": "^5.8.2",
34
+ "typescript-eslint": "^8.26.0"
35
+ }
36
+ }