@zenstackhq/common-helpers 3.0.0-alpha.10
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 +21 -0
- package/dist/index.cjs +133 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +92 -0
- package/dist/index.js.map +1 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ZenStack
|
|
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/dist/index.cjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var src_exports = {};
|
|
33
|
+
__export(src_exports, {
|
|
34
|
+
findUp: () => findUp,
|
|
35
|
+
invariant: () => invariant,
|
|
36
|
+
isPlainObject: () => isPlainObject,
|
|
37
|
+
lowerCaseFirst: () => lowerCaseFirst,
|
|
38
|
+
paramCase: () => paramCase,
|
|
39
|
+
sleep: () => sleep,
|
|
40
|
+
upperCaseFirst: () => upperCaseFirst
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(src_exports);
|
|
43
|
+
|
|
44
|
+
// src/find-up.ts
|
|
45
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
46
|
+
var import_path = __toESM(require("path"), 1);
|
|
47
|
+
function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
|
|
48
|
+
if (!names.some((name) => !!name)) return void 0;
|
|
49
|
+
const target = names.find((name) => import_fs.default.existsSync(import_path.default.join(cwd, name)));
|
|
50
|
+
if (multiple === false && target) return import_path.default.join(cwd, target);
|
|
51
|
+
if (target) result.push(import_path.default.join(cwd, target));
|
|
52
|
+
const up = import_path.default.resolve(cwd, "..");
|
|
53
|
+
if (up === cwd) return multiple && result.length > 0 ? result : void 0;
|
|
54
|
+
return findUp(names, up, multiple, result);
|
|
55
|
+
}
|
|
56
|
+
__name(findUp, "findUp");
|
|
57
|
+
|
|
58
|
+
// src/is-plain-object.ts
|
|
59
|
+
function isObject(o) {
|
|
60
|
+
return Object.prototype.toString.call(o) === "[object Object]";
|
|
61
|
+
}
|
|
62
|
+
__name(isObject, "isObject");
|
|
63
|
+
function isPlainObject(o) {
|
|
64
|
+
if (isObject(o) === false) return false;
|
|
65
|
+
const ctor = o.constructor;
|
|
66
|
+
if (ctor === void 0) return true;
|
|
67
|
+
const prot = ctor.prototype;
|
|
68
|
+
if (isObject(prot) === false) return false;
|
|
69
|
+
if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
__name(isPlainObject, "isPlainObject");
|
|
75
|
+
|
|
76
|
+
// src/lower-case-first.ts
|
|
77
|
+
function lowerCaseFirst(input) {
|
|
78
|
+
return input.charAt(0).toLowerCase() + input.slice(1);
|
|
79
|
+
}
|
|
80
|
+
__name(lowerCaseFirst, "lowerCaseFirst");
|
|
81
|
+
|
|
82
|
+
// src/param-case.ts
|
|
83
|
+
var DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;
|
|
84
|
+
var DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;
|
|
85
|
+
var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
|
|
86
|
+
function paramCase(input) {
|
|
87
|
+
const result = input.replace(DEFAULT_SPLIT_REGEXP_1, "$1\0$2").replace(DEFAULT_SPLIT_REGEXP_2, "$1\0$2").replace(DEFAULT_STRIP_REGEXP, "\0");
|
|
88
|
+
let start = 0;
|
|
89
|
+
let end = result.length;
|
|
90
|
+
while (result.charAt(start) === "\0") start++;
|
|
91
|
+
while (result.charAt(end - 1) === "\0") end--;
|
|
92
|
+
return result.slice(start, end).split("\0").map((str) => str.toLowerCase()).join("-");
|
|
93
|
+
}
|
|
94
|
+
__name(paramCase, "paramCase");
|
|
95
|
+
|
|
96
|
+
// src/sleep.ts
|
|
97
|
+
function sleep(timeout) {
|
|
98
|
+
return new Promise((resolve) => {
|
|
99
|
+
setTimeout(() => resolve(), timeout);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
__name(sleep, "sleep");
|
|
103
|
+
|
|
104
|
+
// src/tiny-invariant.ts
|
|
105
|
+
var isProduction = process.env["NODE_ENV"] === "production";
|
|
106
|
+
var prefix = "Invariant failed";
|
|
107
|
+
function invariant(condition, message) {
|
|
108
|
+
if (condition) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (isProduction) {
|
|
112
|
+
throw new Error(prefix);
|
|
113
|
+
}
|
|
114
|
+
throw new Error(message ? `${prefix}: ${message}` : prefix);
|
|
115
|
+
}
|
|
116
|
+
__name(invariant, "invariant");
|
|
117
|
+
|
|
118
|
+
// src/upper-case-first.ts
|
|
119
|
+
function upperCaseFirst(input) {
|
|
120
|
+
return input.charAt(0).toUpperCase() + input.slice(1);
|
|
121
|
+
}
|
|
122
|
+
__name(upperCaseFirst, "upperCaseFirst");
|
|
123
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
124
|
+
0 && (module.exports = {
|
|
125
|
+
findUp,
|
|
126
|
+
invariant,
|
|
127
|
+
isPlainObject,
|
|
128
|
+
lowerCaseFirst,
|
|
129
|
+
paramCase,
|
|
130
|
+
sleep,
|
|
131
|
+
upperCaseFirst
|
|
132
|
+
});
|
|
133
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/find-up.ts","../src/is-plain-object.ts","../src/lower-case-first.ts","../src/param-case.ts","../src/sleep.ts","../src/tiny-invariant.ts","../src/upper-case-first.ts"],"sourcesContent":["export * from './find-up';\nexport * from './is-plain-object';\nexport * from './lower-case-first';\nexport * from './param-case';\nexport * from './sleep';\nexport * from './tiny-invariant';\nexport * from './upper-case-first';\n","import fs from 'fs';\nimport path from 'path';\n\n/**\n * A type named FindUp that takes a type parameter e which extends boolean.\n */\nexport type FindUpResult<Multiple extends boolean> = Multiple extends true ? string[] | undefined : string | undefined;\n\n/**\n * Find and return file paths by searching parent directories based on the given names list and current working directory (cwd) path.\n * Optionally return a single path or multiple paths.\n * If multiple allowed, return all paths found.\n * If no paths are found, return undefined.\n *\n * @param names An array of strings representing names to search for within the directory\n * @param cwd A string representing the current working directory\n * @param multiple A boolean flag indicating whether to search for multiple levels. Useful for finding node_modules directories...\n * @param An array of strings representing the accumulated results used in multiple results\n * @returns Path(s) to a specific file or folder within the directory or parent directories\n */\nexport function findUp<Multiple extends boolean = false>(\n names: string[],\n cwd: string = process.cwd(),\n multiple: Multiple = false as Multiple,\n result: string[] = [],\n): FindUpResult<Multiple> {\n if (!names.some((name) => !!name)) return undefined;\n const target = names.find((name) => fs.existsSync(path.join(cwd, name)));\n if (multiple === false && target) return path.join(cwd, target) as FindUpResult<Multiple>;\n if (target) result.push(path.join(cwd, target));\n const up = path.resolve(cwd, '..');\n if (up === cwd) return (multiple && result.length > 0 ? result : undefined) as FindUpResult<Multiple>; // it'll fail anyway\n return findUp(names, up, multiple, result);\n}\n","function isObject(o: unknown) {\n return Object.prototype.toString.call(o) === '[object Object]';\n}\n\nexport function isPlainObject(o: unknown) {\n if (isObject(o) === false) return false;\n\n // If has modified constructor\n const ctor = (o as { constructor: unknown }).constructor;\n if (ctor === undefined) return true;\n\n // If has modified prototype\n const prot = (ctor as { prototype: unknown }).prototype;\n if (isObject(prot) === false) return false;\n\n // If constructor does not have an Object-specific method\n if (Object.prototype.hasOwnProperty.call(prot, 'isPrototypeOf') === false) {\n return false;\n }\n\n // Most likely a plain Object\n return true;\n}\n","export function lowerCaseFirst(input: string) {\n return input.charAt(0).toLowerCase() + input.slice(1);\n}\n","const DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;\nconst DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;\nconst DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;\n\nexport function paramCase(input: string) {\n const result = input\n .replace(DEFAULT_SPLIT_REGEXP_1, '$1\\0$2')\n .replace(DEFAULT_SPLIT_REGEXP_2, '$1\\0$2')\n .replace(DEFAULT_STRIP_REGEXP, '\\0');\n\n let start = 0;\n let end = result.length;\n\n while (result.charAt(start) === '\\0') start++;\n while (result.charAt(end - 1) === '\\0') end--;\n\n return result\n .slice(start, end)\n .split('\\0')\n .map((str) => str.toLowerCase())\n .join('-');\n}\n","export function sleep(timeout: number) {\n return new Promise<void>((resolve) => {\n setTimeout(() => resolve(), timeout);\n });\n}\n","const isProduction = process.env['NODE_ENV'] === 'production';\nconst prefix = 'Invariant failed';\n\nexport function invariant(condition: unknown, message?: string): asserts condition {\n if (condition) {\n return;\n }\n\n if (isProduction) {\n throw new Error(prefix);\n }\n\n throw new Error(message ? `${prefix}: ${message}` : prefix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ACxC,SAAOA,MAAMC,OAAO,CAAA,EAAGC,YAAW,IAAKF,MAAMG,MAAM,CAAA;AACvD;AAFgBJ;;;ACAhB,IAAMK,yBAAyB;AAC/B,IAAMC,yBAAyB;AAC/B,IAAMC,uBAAuB;AAEtB,SAASC,UAAUC,OAAa;AACnC,QAAMC,SAASD,MACVE,QAAQN,wBAAwB,QAAA,EAChCM,QAAQL,wBAAwB,QAAA,EAChCK,QAAQJ,sBAAsB,IAAA;AAEnC,MAAIK,QAAQ;AACZ,MAAIC,MAAMH,OAAOI;AAEjB,SAAOJ,OAAOK,OAAOH,KAAAA,MAAW,KAAMA;AACtC,SAAOF,OAAOK,OAAOF,MAAM,CAAA,MAAO,KAAMA;AAExC,SAAOH,OACFM,MAAMJ,OAAOC,GAAAA,EACbI,MAAM,IAAA,EACNC,IAAI,CAACC,QAAQA,IAAIC,YAAW,CAAA,EAC5BC,KAAK,GAAA;AACd;AAjBgBb;;;ACJT,SAASc,MAAMC,SAAe;AACjC,SAAO,IAAIC,QAAc,CAACC,YAAAA;AACtBC,eAAW,MAAMD,QAAAA,GAAWF,OAAAA;EAChC,CAAA;AACJ;AAJgBD;;;ACAhB,IAAMK,eAAeC,QAAQC,IAAI,UAAA,MAAgB;AACjD,IAAMC,SAAS;AAER,SAASC,UAAUC,WAAoBC,SAAgB;AAC1D,MAAID,WAAW;AACX;EACJ;AAEA,MAAIL,cAAc;AACd,UAAM,IAAIO,MAAMJ,MAAAA;EACpB;AAEA,QAAM,IAAII,MAAMD,UAAU,GAAGH,MAAAA,KAAWG,OAAAA,KAAYH,MAAAA;AACxD;AAVgBC;;;ACHT,SAASI,eAAeC,OAAa;AACxC,SAAOA,MAAMC,OAAO,CAAA,EAAGC,YAAW,IAAKF,MAAMG,MAAM,CAAA;AACvD;AAFgBJ;","names":["findUp","names","cwd","process","multiple","result","some","name","undefined","target","find","fs","existsSync","path","join","push","up","resolve","length","isObject","o","Object","prototype","toString","call","isPlainObject","ctor","undefined","prot","hasOwnProperty","lowerCaseFirst","input","charAt","toLowerCase","slice","DEFAULT_SPLIT_REGEXP_1","DEFAULT_SPLIT_REGEXP_2","DEFAULT_STRIP_REGEXP","paramCase","input","result","replace","start","end","length","charAt","slice","split","map","str","toLowerCase","join","sleep","timeout","Promise","resolve","setTimeout","isProduction","process","env","prefix","invariant","condition","message","Error","upperCaseFirst","input","charAt","toUpperCase","slice"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A type named FindUp that takes a type parameter e which extends boolean.
|
|
3
|
+
*/
|
|
4
|
+
type FindUpResult<Multiple extends boolean> = Multiple extends true ? string[] | undefined : string | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Find and return file paths by searching parent directories based on the given names list and current working directory (cwd) path.
|
|
7
|
+
* Optionally return a single path or multiple paths.
|
|
8
|
+
* If multiple allowed, return all paths found.
|
|
9
|
+
* If no paths are found, return undefined.
|
|
10
|
+
*
|
|
11
|
+
* @param names An array of strings representing names to search for within the directory
|
|
12
|
+
* @param cwd A string representing the current working directory
|
|
13
|
+
* @param multiple A boolean flag indicating whether to search for multiple levels. Useful for finding node_modules directories...
|
|
14
|
+
* @param An array of strings representing the accumulated results used in multiple results
|
|
15
|
+
* @returns Path(s) to a specific file or folder within the directory or parent directories
|
|
16
|
+
*/
|
|
17
|
+
declare function findUp<Multiple extends boolean = false>(names: string[], cwd?: string, multiple?: Multiple, result?: string[]): FindUpResult<Multiple>;
|
|
18
|
+
|
|
19
|
+
declare function isPlainObject(o: unknown): boolean;
|
|
20
|
+
|
|
21
|
+
declare function lowerCaseFirst(input: string): string;
|
|
22
|
+
|
|
23
|
+
declare function paramCase(input: string): string;
|
|
24
|
+
|
|
25
|
+
declare function sleep(timeout: number): Promise<void>;
|
|
26
|
+
|
|
27
|
+
declare function invariant(condition: unknown, message?: string): asserts condition;
|
|
28
|
+
|
|
29
|
+
declare function upperCaseFirst(input: string): string;
|
|
30
|
+
|
|
31
|
+
export { type FindUpResult, findUp, invariant, isPlainObject, lowerCaseFirst, paramCase, sleep, upperCaseFirst };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A type named FindUp that takes a type parameter e which extends boolean.
|
|
3
|
+
*/
|
|
4
|
+
type FindUpResult<Multiple extends boolean> = Multiple extends true ? string[] | undefined : string | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Find and return file paths by searching parent directories based on the given names list and current working directory (cwd) path.
|
|
7
|
+
* Optionally return a single path or multiple paths.
|
|
8
|
+
* If multiple allowed, return all paths found.
|
|
9
|
+
* If no paths are found, return undefined.
|
|
10
|
+
*
|
|
11
|
+
* @param names An array of strings representing names to search for within the directory
|
|
12
|
+
* @param cwd A string representing the current working directory
|
|
13
|
+
* @param multiple A boolean flag indicating whether to search for multiple levels. Useful for finding node_modules directories...
|
|
14
|
+
* @param An array of strings representing the accumulated results used in multiple results
|
|
15
|
+
* @returns Path(s) to a specific file or folder within the directory or parent directories
|
|
16
|
+
*/
|
|
17
|
+
declare function findUp<Multiple extends boolean = false>(names: string[], cwd?: string, multiple?: Multiple, result?: string[]): FindUpResult<Multiple>;
|
|
18
|
+
|
|
19
|
+
declare function isPlainObject(o: unknown): boolean;
|
|
20
|
+
|
|
21
|
+
declare function lowerCaseFirst(input: string): string;
|
|
22
|
+
|
|
23
|
+
declare function paramCase(input: string): string;
|
|
24
|
+
|
|
25
|
+
declare function sleep(timeout: number): Promise<void>;
|
|
26
|
+
|
|
27
|
+
declare function invariant(condition: unknown, message?: string): asserts condition;
|
|
28
|
+
|
|
29
|
+
declare function upperCaseFirst(input: string): string;
|
|
30
|
+
|
|
31
|
+
export { type FindUpResult, findUp, invariant, isPlainObject, lowerCaseFirst, paramCase, sleep, upperCaseFirst };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/find-up.ts
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
|
|
8
|
+
if (!names.some((name) => !!name)) return void 0;
|
|
9
|
+
const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
|
|
10
|
+
if (multiple === false && target) return path.join(cwd, target);
|
|
11
|
+
if (target) result.push(path.join(cwd, target));
|
|
12
|
+
const up = path.resolve(cwd, "..");
|
|
13
|
+
if (up === cwd) return multiple && result.length > 0 ? result : void 0;
|
|
14
|
+
return findUp(names, up, multiple, result);
|
|
15
|
+
}
|
|
16
|
+
__name(findUp, "findUp");
|
|
17
|
+
|
|
18
|
+
// src/is-plain-object.ts
|
|
19
|
+
function isObject(o) {
|
|
20
|
+
return Object.prototype.toString.call(o) === "[object Object]";
|
|
21
|
+
}
|
|
22
|
+
__name(isObject, "isObject");
|
|
23
|
+
function isPlainObject(o) {
|
|
24
|
+
if (isObject(o) === false) return false;
|
|
25
|
+
const ctor = o.constructor;
|
|
26
|
+
if (ctor === void 0) return true;
|
|
27
|
+
const prot = ctor.prototype;
|
|
28
|
+
if (isObject(prot) === false) return false;
|
|
29
|
+
if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
__name(isPlainObject, "isPlainObject");
|
|
35
|
+
|
|
36
|
+
// src/lower-case-first.ts
|
|
37
|
+
function lowerCaseFirst(input) {
|
|
38
|
+
return input.charAt(0).toLowerCase() + input.slice(1);
|
|
39
|
+
}
|
|
40
|
+
__name(lowerCaseFirst, "lowerCaseFirst");
|
|
41
|
+
|
|
42
|
+
// src/param-case.ts
|
|
43
|
+
var DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;
|
|
44
|
+
var DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;
|
|
45
|
+
var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
|
|
46
|
+
function paramCase(input) {
|
|
47
|
+
const result = input.replace(DEFAULT_SPLIT_REGEXP_1, "$1\0$2").replace(DEFAULT_SPLIT_REGEXP_2, "$1\0$2").replace(DEFAULT_STRIP_REGEXP, "\0");
|
|
48
|
+
let start = 0;
|
|
49
|
+
let end = result.length;
|
|
50
|
+
while (result.charAt(start) === "\0") start++;
|
|
51
|
+
while (result.charAt(end - 1) === "\0") end--;
|
|
52
|
+
return result.slice(start, end).split("\0").map((str) => str.toLowerCase()).join("-");
|
|
53
|
+
}
|
|
54
|
+
__name(paramCase, "paramCase");
|
|
55
|
+
|
|
56
|
+
// src/sleep.ts
|
|
57
|
+
function sleep(timeout) {
|
|
58
|
+
return new Promise((resolve) => {
|
|
59
|
+
setTimeout(() => resolve(), timeout);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
__name(sleep, "sleep");
|
|
63
|
+
|
|
64
|
+
// src/tiny-invariant.ts
|
|
65
|
+
var isProduction = process.env["NODE_ENV"] === "production";
|
|
66
|
+
var prefix = "Invariant failed";
|
|
67
|
+
function invariant(condition, message) {
|
|
68
|
+
if (condition) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (isProduction) {
|
|
72
|
+
throw new Error(prefix);
|
|
73
|
+
}
|
|
74
|
+
throw new Error(message ? `${prefix}: ${message}` : prefix);
|
|
75
|
+
}
|
|
76
|
+
__name(invariant, "invariant");
|
|
77
|
+
|
|
78
|
+
// src/upper-case-first.ts
|
|
79
|
+
function upperCaseFirst(input) {
|
|
80
|
+
return input.charAt(0).toUpperCase() + input.slice(1);
|
|
81
|
+
}
|
|
82
|
+
__name(upperCaseFirst, "upperCaseFirst");
|
|
83
|
+
export {
|
|
84
|
+
findUp,
|
|
85
|
+
invariant,
|
|
86
|
+
isPlainObject,
|
|
87
|
+
lowerCaseFirst,
|
|
88
|
+
paramCase,
|
|
89
|
+
sleep,
|
|
90
|
+
upperCaseFirst
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/find-up.ts","../src/is-plain-object.ts","../src/lower-case-first.ts","../src/param-case.ts","../src/sleep.ts","../src/tiny-invariant.ts","../src/upper-case-first.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\n\n/**\n * A type named FindUp that takes a type parameter e which extends boolean.\n */\nexport type FindUpResult<Multiple extends boolean> = Multiple extends true ? string[] | undefined : string | undefined;\n\n/**\n * Find and return file paths by searching parent directories based on the given names list and current working directory (cwd) path.\n * Optionally return a single path or multiple paths.\n * If multiple allowed, return all paths found.\n * If no paths are found, return undefined.\n *\n * @param names An array of strings representing names to search for within the directory\n * @param cwd A string representing the current working directory\n * @param multiple A boolean flag indicating whether to search for multiple levels. Useful for finding node_modules directories...\n * @param An array of strings representing the accumulated results used in multiple results\n * @returns Path(s) to a specific file or folder within the directory or parent directories\n */\nexport function findUp<Multiple extends boolean = false>(\n names: string[],\n cwd: string = process.cwd(),\n multiple: Multiple = false as Multiple,\n result: string[] = [],\n): FindUpResult<Multiple> {\n if (!names.some((name) => !!name)) return undefined;\n const target = names.find((name) => fs.existsSync(path.join(cwd, name)));\n if (multiple === false && target) return path.join(cwd, target) as FindUpResult<Multiple>;\n if (target) result.push(path.join(cwd, target));\n const up = path.resolve(cwd, '..');\n if (up === cwd) return (multiple && result.length > 0 ? result : undefined) as FindUpResult<Multiple>; // it'll fail anyway\n return findUp(names, up, multiple, result);\n}\n","function isObject(o: unknown) {\n return Object.prototype.toString.call(o) === '[object Object]';\n}\n\nexport function isPlainObject(o: unknown) {\n if (isObject(o) === false) return false;\n\n // If has modified constructor\n const ctor = (o as { constructor: unknown }).constructor;\n if (ctor === undefined) return true;\n\n // If has modified prototype\n const prot = (ctor as { prototype: unknown }).prototype;\n if (isObject(prot) === false) return false;\n\n // If constructor does not have an Object-specific method\n if (Object.prototype.hasOwnProperty.call(prot, 'isPrototypeOf') === false) {\n return false;\n }\n\n // Most likely a plain Object\n return true;\n}\n","export function lowerCaseFirst(input: string) {\n return input.charAt(0).toLowerCase() + input.slice(1);\n}\n","const DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;\nconst DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;\nconst DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;\n\nexport function paramCase(input: string) {\n const result = input\n .replace(DEFAULT_SPLIT_REGEXP_1, '$1\\0$2')\n .replace(DEFAULT_SPLIT_REGEXP_2, '$1\\0$2')\n .replace(DEFAULT_STRIP_REGEXP, '\\0');\n\n let start = 0;\n let end = result.length;\n\n while (result.charAt(start) === '\\0') start++;\n while (result.charAt(end - 1) === '\\0') end--;\n\n return result\n .slice(start, end)\n .split('\\0')\n .map((str) => str.toLowerCase())\n .join('-');\n}\n","export function sleep(timeout: number) {\n return new Promise<void>((resolve) => {\n setTimeout(() => resolve(), timeout);\n });\n}\n","const isProduction = process.env['NODE_ENV'] === 'production';\nconst prefix = 'Invariant failed';\n\nexport function invariant(condition: unknown, message?: string): asserts condition {\n if (condition) {\n return;\n }\n\n if (isProduction) {\n throw new Error(prefix);\n }\n\n throw new Error(message ? `${prefix}: ${message}` : prefix);\n}\n","export function upperCaseFirst(input: string) {\n return input.charAt(0).toUpperCase() + input.slice(1);\n}\n"],"mappings":";;;;AAAA,OAAOA,QAAQ;AACf,OAAOC,UAAU;AAmBV,SAASC,OACZC,OACAC,MAAcC,QAAQD,IAAG,GACzBE,WAAqB,OACrBC,SAAmB,CAAA,GAAE;AAErB,MAAI,CAACJ,MAAMK,KAAK,CAACC,SAAS,CAAC,CAACA,IAAAA,EAAO,QAAOC;AAC1C,QAAMC,SAASR,MAAMS,KAAK,CAACH,SAASI,GAAGC,WAAWC,KAAKC,KAAKZ,KAAKK,IAAAA,CAAAA,CAAA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M,MAAMJ,OAAOC,GAAAA,EACbI,MAAM,IAAA,EACNC,IAAI,CAACC,QAAQA,IAAIC,YAAW,CAAA,EAC5BC,KAAK,GAAA;AACd;AAjBgBb;;;ACJT,SAASc,MAAMC,SAAe;AACjC,SAAO,IAAIC,QAAc,CAACC,YAAAA;AACtBC,eAAW,MAAMD,QAAAA,GAAWF,OAAAA;EAChC,CAAA;AACJ;AAJgBD;;;ACAhB,IAAMK,eAAeC,QAAQC,IAAI,UAAA,MAAgB;AACjD,IAAMC,SAAS;AAER,SAASC,UAAUC,WAAoBC,SAAgB;AAC1D,MAAID,WAAW;AACX;EACJ;AAEA,MAAIL,cAAc;AACd,UAAM,IAAIO,MAAMJ,MAAAA;EACpB;AAEA,QAAM,IAAII,MAAMD,UAAU,GAAGH,MAAAA,KAAWG,OAAAA,KAAYH,MAAAA;AACxD;AAVgBC;;;ACHT,SAASI,eAAeC,OAAa;AACxC,SAAOA,MAAMC,OAAO,CAAA,EAAGC,YAAW,IAAKF,MAAMG,MAAM,CAAA;AACvD;AAFgBJ;","names":["fs","path","findUp","names","cwd","process","multiple","result","some","name","undefined","target","find","fs","existsSync","path","join","push","up","resolve","length","isObject","o","Object","prototype","toString","call","isPlainObject","ctor","undefined","prot","hasOwnProperty","lowerCaseFirst","input","charAt","toLowerCase","slice","DEFAULT_SPLIT_REGEXP_1","DEFAULT_SPLIT_REGEXP_2","DEFAULT_STRIP_REGEXP","paramCase","input","result","replace","start","end","length","charAt","slice","split","map","str","toLowerCase","join","sleep","timeout","Promise","resolve","setTimeout","isProduction","process","env","prefix","invariant","condition","message","Error","upperCaseFirst","input","charAt","toUpperCase","slice"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zenstackhq/common-helpers",
|
|
3
|
+
"version": "3.0.0-alpha.10",
|
|
4
|
+
"description": "ZenStack Common Helpers",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [],
|
|
7
|
+
"author": "ZenStack Team",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/index.d.cts",
|
|
20
|
+
"default": "./dist/index.cjs"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@zenstackhq/eslint-config": "3.0.0-alpha.10",
|
|
26
|
+
"@zenstackhq/typescript-config": "3.0.0-alpha.10"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup-node",
|
|
30
|
+
"watch": "tsup-node --watch",
|
|
31
|
+
"lint": "eslint src --ext ts",
|
|
32
|
+
"pack": "pnpm pack"
|
|
33
|
+
}
|
|
34
|
+
}
|