@isopodlabs/utilities 1.5.0 → 1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isopodlabs/utilities",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Various typescript helpers.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,22 +0,0 @@
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): 1 | -1 | 0;
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): 1 | -1 | 0;
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>;
@@ -1,58 +0,0 @@
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/glob.d.ts DELETED
@@ -1,20 +0,0 @@
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 DELETED
@@ -1,156 +0,0 @@
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
- }