superjs-core 0.1.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/async/index.d.ts +44 -0
- package/dist/async/index.js +88 -0
- package/dist/async/index.js.map +1 -0
- package/dist/collection/index.d.ts +75 -0
- package/dist/collection/index.js +140 -0
- package/dist/collection/index.js.map +1 -0
- package/dist/core/index.d.ts +95 -0
- package/dist/core/index.js +272 -0
- package/dist/core/index.js.map +1 -0
- package/dist/crypto/index.d.ts +78 -0
- package/dist/crypto/index.js +147 -0
- package/dist/crypto/index.js.map +1 -0
- package/dist/date/index.d.ts +203 -0
- package/dist/date/index.js +282 -0
- package/dist/date/index.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +1505 -0
- package/dist/index.js.map +1 -0
- package/dist/io/index.d.ts +39 -0
- package/dist/io/index.js +110 -0
- package/dist/io/index.js.map +1 -0
- package/dist/math/index.d.ts +117 -0
- package/dist/math/index.js +104 -0
- package/dist/math/index.js.map +1 -0
- package/dist/path/index.d.ts +81 -0
- package/dist/path/index.js +134 -0
- package/dist/path/index.js.map +1 -0
- package/dist/string/index.d.ts +91 -0
- package/dist/string/index.js +146 -0
- package/dist/string/index.js.map +1 -0
- package/dist/type/index.d.ts +87 -0
- package/dist/type/index.js +109 -0
- package/dist/type/index.js.map +1 -0
- package/package.json +103 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Joins path segments with the proper separator.
|
|
3
|
+
*
|
|
4
|
+
* @param segments - Path segments to join.
|
|
5
|
+
* @returns The joined path.
|
|
6
|
+
*/
|
|
7
|
+
declare function join(...segments: string[]): string;
|
|
8
|
+
/**
|
|
9
|
+
* Resolves path segments to an absolute path.
|
|
10
|
+
*
|
|
11
|
+
* @param segments - Path segments to resolve.
|
|
12
|
+
* @returns The resolved absolute path.
|
|
13
|
+
*/
|
|
14
|
+
declare function resolve(...segments: string[]): string;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the filename from a path.
|
|
17
|
+
*
|
|
18
|
+
* @param p - The path.
|
|
19
|
+
* @param ext - Optional extension to strip.
|
|
20
|
+
* @returns The filename.
|
|
21
|
+
*/
|
|
22
|
+
declare function basename(p: string, ext?: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the directory portion of a path.
|
|
25
|
+
*
|
|
26
|
+
* @param p - The path.
|
|
27
|
+
* @returns The directory path.
|
|
28
|
+
*/
|
|
29
|
+
declare function dirname(p: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Returns the file extension from a path.
|
|
32
|
+
*
|
|
33
|
+
* @param p - The path.
|
|
34
|
+
* @returns The extension including the dot, or empty string.
|
|
35
|
+
*/
|
|
36
|
+
declare function extname(p: string): string;
|
|
37
|
+
/**
|
|
38
|
+
* Normalizes a path, resolving '..' and '.' segments.
|
|
39
|
+
*
|
|
40
|
+
* @param p - The path to normalize.
|
|
41
|
+
* @returns The normalized path.
|
|
42
|
+
*/
|
|
43
|
+
declare function normalize(p: string): string;
|
|
44
|
+
/**
|
|
45
|
+
* Checks if a path is absolute.
|
|
46
|
+
*
|
|
47
|
+
* @param p - The path to check.
|
|
48
|
+
* @returns Whether the path is absolute.
|
|
49
|
+
*/
|
|
50
|
+
declare function isAbsolute(p: string): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Computes the relative path from `from` to `to`.
|
|
53
|
+
*
|
|
54
|
+
* @param from - The base path.
|
|
55
|
+
* @param to - The target path.
|
|
56
|
+
* @returns The relative path.
|
|
57
|
+
*/
|
|
58
|
+
declare function relative(from: string, to: string): string;
|
|
59
|
+
interface ParsedPath {
|
|
60
|
+
root: string;
|
|
61
|
+
dir: string;
|
|
62
|
+
base: string;
|
|
63
|
+
name: string;
|
|
64
|
+
ext: string;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Parses a path into its components.
|
|
68
|
+
*
|
|
69
|
+
* @param p - The path to parse.
|
|
70
|
+
* @returns An object with root, dir, base, name, and ext.
|
|
71
|
+
*/
|
|
72
|
+
declare function parse(p: string): ParsedPath;
|
|
73
|
+
/**
|
|
74
|
+
* Formats a parsed path object back into a path string.
|
|
75
|
+
*
|
|
76
|
+
* @param parsed - The parsed path components.
|
|
77
|
+
* @returns The formatted path string.
|
|
78
|
+
*/
|
|
79
|
+
declare function format(parsed: Partial<ParsedPath>): string;
|
|
80
|
+
|
|
81
|
+
export { type ParsedPath, basename, dirname, extname, format, isAbsolute, join, normalize, parse, relative, resolve };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// src/path/index.ts
|
|
2
|
+
var SEP = "/";
|
|
3
|
+
function join(...segments) {
|
|
4
|
+
const parts = [];
|
|
5
|
+
for (const seg of segments) {
|
|
6
|
+
if (seg === "") continue;
|
|
7
|
+
const split = seg.split(/[\\/]/);
|
|
8
|
+
for (const part of split) {
|
|
9
|
+
if (part === "" || part === ".") continue;
|
|
10
|
+
if (part === "..") {
|
|
11
|
+
if (parts.length > 0 && parts[parts.length - 1] !== "..") {
|
|
12
|
+
parts.pop();
|
|
13
|
+
} else {
|
|
14
|
+
parts.push("..");
|
|
15
|
+
}
|
|
16
|
+
} else {
|
|
17
|
+
parts.push(part);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (parts.length === 0) return ".";
|
|
22
|
+
const result = parts.join(SEP);
|
|
23
|
+
if (segments.length > 0 && segments[0].startsWith("/")) {
|
|
24
|
+
return SEP + result;
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
function resolve(...segments) {
|
|
29
|
+
let resolved = "";
|
|
30
|
+
let isAbs = false;
|
|
31
|
+
for (const seg of segments) {
|
|
32
|
+
if (seg.startsWith("/")) {
|
|
33
|
+
resolved = seg;
|
|
34
|
+
isAbs = true;
|
|
35
|
+
} else if (isAbs) {
|
|
36
|
+
resolved = join(resolved, seg);
|
|
37
|
+
} else {
|
|
38
|
+
resolved = resolved ? join(resolved, seg) : seg;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!isAbs) {
|
|
42
|
+
resolved = join(SEP, resolved);
|
|
43
|
+
}
|
|
44
|
+
return normalize(resolved);
|
|
45
|
+
}
|
|
46
|
+
function basename(p, ext) {
|
|
47
|
+
const normalized = p.replace(/[\\/]+$/, "");
|
|
48
|
+
const idx = normalized.lastIndexOf(SEP);
|
|
49
|
+
let base = idx === -1 ? normalized : normalized.slice(idx + 1);
|
|
50
|
+
if (ext && base.endsWith(ext)) {
|
|
51
|
+
base = base.slice(0, -ext.length);
|
|
52
|
+
}
|
|
53
|
+
return base;
|
|
54
|
+
}
|
|
55
|
+
function dirname(p) {
|
|
56
|
+
if (p === SEP) return SEP;
|
|
57
|
+
const normalized = p.replace(/[\\/]+$/, "");
|
|
58
|
+
if (normalized === "") return ".";
|
|
59
|
+
const idx = normalized.lastIndexOf(SEP);
|
|
60
|
+
if (idx === -1) return ".";
|
|
61
|
+
if (idx === 0) return SEP;
|
|
62
|
+
return normalized.slice(0, idx);
|
|
63
|
+
}
|
|
64
|
+
function extname(p) {
|
|
65
|
+
const base = basename(p);
|
|
66
|
+
const idx = base.lastIndexOf(".");
|
|
67
|
+
if (idx === -1 || idx === 0) return "";
|
|
68
|
+
return base.slice(idx);
|
|
69
|
+
}
|
|
70
|
+
function normalize(p) {
|
|
71
|
+
const parts = p.split(/[\\/]+/);
|
|
72
|
+
const stack = [];
|
|
73
|
+
let isAbs = p.startsWith("/");
|
|
74
|
+
for (const part of parts) {
|
|
75
|
+
if (part === "" || part === ".") continue;
|
|
76
|
+
if (part === "..") {
|
|
77
|
+
if (stack.length > 0 && stack[stack.length - 1] !== "..") {
|
|
78
|
+
stack.pop();
|
|
79
|
+
} else if (!isAbs) {
|
|
80
|
+
stack.push("..");
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
stack.push(part);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const result = stack.join(SEP);
|
|
87
|
+
if (isAbs) return SEP + result;
|
|
88
|
+
if (result === "") return ".";
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
function isAbsolute(p) {
|
|
92
|
+
return p.startsWith("/");
|
|
93
|
+
}
|
|
94
|
+
function relative(from, to) {
|
|
95
|
+
const fromParts = normalize(from).split(SEP);
|
|
96
|
+
const toParts = normalize(to).split(SEP);
|
|
97
|
+
let i = 0;
|
|
98
|
+
while (i < fromParts.length && i < toParts.length && fromParts[i] === toParts[i]) {
|
|
99
|
+
i++;
|
|
100
|
+
}
|
|
101
|
+
const up = fromParts.slice(i).map(() => "..");
|
|
102
|
+
const down = toParts.slice(i);
|
|
103
|
+
const result = [...up, ...down].join(SEP);
|
|
104
|
+
return result || ".";
|
|
105
|
+
}
|
|
106
|
+
function parse(p) {
|
|
107
|
+
const root = p.startsWith(SEP) ? SEP : "";
|
|
108
|
+
const base = basename(p);
|
|
109
|
+
const ext = extname(base);
|
|
110
|
+
const name = ext ? base.slice(0, -ext.length) : base;
|
|
111
|
+
const dir = root ? dirname(p) : dirname(p) === "." ? "" : dirname(p);
|
|
112
|
+
return { root, dir, base, name, ext };
|
|
113
|
+
}
|
|
114
|
+
function format(parsed) {
|
|
115
|
+
const { root = "", dir = "", base = "", name = "", ext = "" } = parsed;
|
|
116
|
+
const baseName = base || name + ext;
|
|
117
|
+
if (dir) {
|
|
118
|
+
return dir + SEP + baseName;
|
|
119
|
+
}
|
|
120
|
+
return root + baseName;
|
|
121
|
+
}
|
|
122
|
+
export {
|
|
123
|
+
basename,
|
|
124
|
+
dirname,
|
|
125
|
+
extname,
|
|
126
|
+
format,
|
|
127
|
+
isAbsolute,
|
|
128
|
+
join,
|
|
129
|
+
normalize,
|
|
130
|
+
parse,
|
|
131
|
+
relative,
|
|
132
|
+
resolve
|
|
133
|
+
};
|
|
134
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/path/index.ts"],"sourcesContent":["const SEP = '/'\n\n/**\n * Joins path segments with the proper separator.\n *\n * @param segments - Path segments to join.\n * @returns The joined path.\n */\nexport function join(...segments: string[]): string {\n const parts: string[] = []\n for (const seg of segments) {\n if (seg === '') continue\n const split = seg.split(/[\\\\/]/)\n for (const part of split) {\n if (part === '' || part === '.') continue\n if (part === '..') {\n if (parts.length > 0 && parts[parts.length - 1] !== '..') {\n parts.pop()\n } else {\n parts.push('..')\n }\n } else {\n parts.push(part)\n }\n }\n }\n if (parts.length === 0) return '.'\n const result = parts.join(SEP)\n if (segments.length > 0 && segments[0]!.startsWith('/')) {\n return SEP + result\n }\n return result\n}\n\n/**\n * Resolves path segments to an absolute path.\n *\n * @param segments - Path segments to resolve.\n * @returns The resolved absolute path.\n */\nexport function resolve(...segments: string[]): string {\n let resolved = ''\n let isAbs = false\n\n for (const seg of segments) {\n if (seg.startsWith('/')) {\n resolved = seg\n isAbs = true\n } else if (isAbs) {\n resolved = join(resolved, seg)\n } else {\n resolved = resolved ? join(resolved, seg) : seg\n }\n }\n\n if (!isAbs) {\n resolved = join(SEP, resolved)\n }\n\n return normalize(resolved)\n}\n\n/**\n * Returns the filename from a path.\n *\n * @param p - The path.\n * @param ext - Optional extension to strip.\n * @returns The filename.\n */\nexport function basename(p: string, ext?: string): string {\n const normalized = p.replace(/[\\\\/]+$/, '')\n const idx = normalized.lastIndexOf(SEP)\n let base = idx === -1 ? normalized : normalized.slice(idx + 1)\n if (ext && base.endsWith(ext)) {\n base = base.slice(0, -ext.length)\n }\n return base\n}\n\n/**\n * Returns the directory portion of a path.\n *\n * @param p - The path.\n * @returns The directory path.\n */\nexport function dirname(p: string): string {\n if (p === SEP) return SEP\n const normalized = p.replace(/[\\\\/]+$/, '')\n if (normalized === '') return '.'\n const idx = normalized.lastIndexOf(SEP)\n if (idx === -1) return '.'\n if (idx === 0) return SEP\n return normalized.slice(0, idx)\n}\n\n/**\n * Returns the file extension from a path.\n *\n * @param p - The path.\n * @returns The extension including the dot, or empty string.\n */\nexport function extname(p: string): string {\n const base = basename(p)\n const idx = base.lastIndexOf('.')\n if (idx === -1 || idx === 0) return ''\n return base.slice(idx)\n}\n\n/**\n * Normalizes a path, resolving '..' and '.' segments.\n *\n * @param p - The path to normalize.\n * @returns The normalized path.\n */\nexport function normalize(p: string): string {\n const parts = p.split(/[\\\\/]+/)\n const stack: string[] = []\n let isAbs = p.startsWith('/')\n\n for (const part of parts) {\n if (part === '' || part === '.') continue\n if (part === '..') {\n if (stack.length > 0 && stack[stack.length - 1] !== '..') {\n stack.pop()\n } else if (!isAbs) {\n stack.push('..')\n }\n } else {\n stack.push(part)\n }\n }\n\n const result = stack.join(SEP)\n if (isAbs) return SEP + result\n if (result === '') return '.'\n return result\n}\n\n/**\n * Checks if a path is absolute.\n *\n * @param p - The path to check.\n * @returns Whether the path is absolute.\n */\nexport function isAbsolute(p: string): boolean {\n return p.startsWith('/')\n}\n\n/**\n * Computes the relative path from `from` to `to`.\n *\n * @param from - The base path.\n * @param to - The target path.\n * @returns The relative path.\n */\nexport function relative(from: string, to: string): string {\n const fromParts = normalize(from).split(SEP)\n const toParts = normalize(to).split(SEP)\n\n let i = 0\n while (i < fromParts.length && i < toParts.length && fromParts[i] === toParts[i]) {\n i++\n }\n\n const up = fromParts.slice(i).map(() => '..')\n const down = toParts.slice(i)\n const result = [...up, ...down].join(SEP)\n\n return result || '.'\n}\n\nexport interface ParsedPath {\n root: string\n dir: string\n base: string\n name: string\n ext: string\n}\n\n/**\n * Parses a path into its components.\n *\n * @param p - The path to parse.\n * @returns An object with root, dir, base, name, and ext.\n */\nexport function parse(p: string): ParsedPath {\n const root = p.startsWith(SEP) ? SEP : ''\n const base = basename(p)\n const ext = extname(base)\n const name = ext ? base.slice(0, -ext.length) : base\n const dir = root ? dirname(p) : (dirname(p) === '.' ? '' : dirname(p))\n\n return { root, dir, base, name, ext }\n}\n\n/**\n * Formats a parsed path object back into a path string.\n *\n * @param parsed - The parsed path components.\n * @returns The formatted path string.\n */\nexport function format(parsed: Partial<ParsedPath>): string {\n const { root = '', dir = '', base = '', name = '', ext = '' } = parsed\n const baseName = base || name + ext\n if (dir) {\n return dir + SEP + baseName\n }\n return root + baseName\n}\n"],"mappings":";AAAA,IAAM,MAAM;AAQL,SAAS,QAAQ,UAA4B;AAClD,QAAM,QAAkB,CAAC;AACzB,aAAW,OAAO,UAAU;AAC1B,QAAI,QAAQ,GAAI;AAChB,UAAM,QAAQ,IAAI,MAAM,OAAO;AAC/B,eAAW,QAAQ,OAAO;AACxB,UAAI,SAAS,MAAM,SAAS,IAAK;AACjC,UAAI,SAAS,MAAM;AACjB,YAAI,MAAM,SAAS,KAAK,MAAM,MAAM,SAAS,CAAC,MAAM,MAAM;AACxD,gBAAM,IAAI;AAAA,QACZ,OAAO;AACL,gBAAM,KAAK,IAAI;AAAA,QACjB;AAAA,MACF,OAAO;AACL,cAAM,KAAK,IAAI;AAAA,MACjB;AAAA,IACF;AAAA,EACF;AACA,MAAI,MAAM,WAAW,EAAG,QAAO;AAC/B,QAAM,SAAS,MAAM,KAAK,GAAG;AAC7B,MAAI,SAAS,SAAS,KAAK,SAAS,CAAC,EAAG,WAAW,GAAG,GAAG;AACvD,WAAO,MAAM;AAAA,EACf;AACA,SAAO;AACT;AAQO,SAAS,WAAW,UAA4B;AACrD,MAAI,WAAW;AACf,MAAI,QAAQ;AAEZ,aAAW,OAAO,UAAU;AAC1B,QAAI,IAAI,WAAW,GAAG,GAAG;AACvB,iBAAW;AACX,cAAQ;AAAA,IACV,WAAW,OAAO;AAChB,iBAAW,KAAK,UAAU,GAAG;AAAA,IAC/B,OAAO;AACL,iBAAW,WAAW,KAAK,UAAU,GAAG,IAAI;AAAA,IAC9C;AAAA,EACF;AAEA,MAAI,CAAC,OAAO;AACV,eAAW,KAAK,KAAK,QAAQ;AAAA,EAC/B;AAEA,SAAO,UAAU,QAAQ;AAC3B;AASO,SAAS,SAAS,GAAW,KAAsB;AACxD,QAAM,aAAa,EAAE,QAAQ,WAAW,EAAE;AAC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AC7C,SAAO,EAAE,WAAW,GAAG;AACzB;AASO,SAAS,SAAS,MAAc,IAAoB;AACzD,QAAM,YAAY,UAAU,IAAI,EAAE,MAAM,GAAG;AAC3C,QAAM,UAAU,UAAU,EAAE,EAAE,MAAM,GAAG;AAEvC,MAAI,IAAI;AACR,SAAO,IAAI,UAAU,UAAU,IAAI,QAAQ,UAAU,UAAU,CAAC,MAAM,QAAQ,CAAC,GAAG;AAChF;AAAA,EACF;AAEA,QAAM,KAAK,UAAU,MAAM,CAAC,EAAE,IAAI,MAAM,IAAI;AAC5C,QAAM,OAAO,QAAQ,MAAM,CAAC;AAC5B,QAAM,SAAS,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,KAAK,GAAG;AAExC,SAAO,UAAU;AACnB;AAgBO,SAAS,MAAM,GAAuB;AAC3C,QAAM,OAAO,EAAE,WAAW,GAAG,IAAI,MAAM;AACvC,QAAM,OAAO,SAAS,CAAC;AACvB,QAAM,MAAM,QAAQ,IAAI;AACxB,QAAM,OAAO,MAAM,KAAK,MAAM,GAAG,CAAC,IAAI,MAAM,IAAI;AAChD,QAAM,MAAM,OAAO,QAAQ,CAAC,IAAK,QAAQ,CAAC,MAAM,MAAM,KAAK,QAAQ,CAAC;AAEpE,SAAO,EAAE,MAAM,KAAK,MAAM,MAAM,IAAI;AACtC;AAQO,SAAS,OAAO,QAAqC;AAC1D,QAAM,EAAE,OAAO,IAAI,MAAM,IAAI,OAAO,IAAI,OAAO,IAAI,MAAM,GAAG,IAAI;AAChE,QAAM,WAAW,QAAQ,OAAO;AAChC,MAAI,KAAK;AACP,WAAO,MAAM,MAAM;AAAA,EACrB;AACA,SAAO,OAAO;AAChB;","names":[]}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capitalizes the first character and lowercases the rest.
|
|
3
|
+
*/
|
|
4
|
+
declare function capitalize(str: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Converts a string to camelCase.
|
|
7
|
+
*/
|
|
8
|
+
declare function camelCase(str: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Converts a string to kebab-case.
|
|
11
|
+
*/
|
|
12
|
+
declare function kebabCase(str: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Converts a string to snake_case.
|
|
15
|
+
*/
|
|
16
|
+
declare function snakeCase(str: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Converts a string to PascalCase.
|
|
19
|
+
*/
|
|
20
|
+
declare function pascalCase(str: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Truncates a string to the specified length, appending a suffix (default "...").
|
|
23
|
+
*/
|
|
24
|
+
declare function truncate(str: string, maxLength: number, suffix?: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Simple string interpolation using {{key}} syntax.
|
|
27
|
+
*
|
|
28
|
+
* @example template("Hello {{name}}", { name: "world" }) // => "Hello world"
|
|
29
|
+
*/
|
|
30
|
+
declare function template(str: string, data: Record<string, string | number>): string;
|
|
31
|
+
/**
|
|
32
|
+
* Generates a UUID v4 string.
|
|
33
|
+
* Uses crypto.randomUUID when available, falls back to manual implementation.
|
|
34
|
+
*/
|
|
35
|
+
declare function uuid(): string;
|
|
36
|
+
/**
|
|
37
|
+
* Generates a short random ID with configurable length and alphabet.
|
|
38
|
+
*
|
|
39
|
+
* @default size = 21, alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-"
|
|
40
|
+
*/
|
|
41
|
+
declare function nanoid(size?: number, alphabet?: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Escapes HTML special characters (&, <, >, ", ').
|
|
44
|
+
*/
|
|
45
|
+
declare function escapeHtml(str: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Unescapes common HTML entities.
|
|
48
|
+
*/
|
|
49
|
+
declare function unescapeHtml(str: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* Removes whitespace from both ends of a string.
|
|
52
|
+
*/
|
|
53
|
+
declare function trim(str: string): string;
|
|
54
|
+
/**
|
|
55
|
+
* Removes whitespace from the start of a string.
|
|
56
|
+
*/
|
|
57
|
+
declare function trimStart(str: string): string;
|
|
58
|
+
/**
|
|
59
|
+
* Removes whitespace from the end of a string.
|
|
60
|
+
*/
|
|
61
|
+
declare function trimEnd(str: string): string;
|
|
62
|
+
/**
|
|
63
|
+
* Pads a string to the given length by adding characters to both sides.
|
|
64
|
+
*/
|
|
65
|
+
declare function pad(str: string, length: number, char?: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Pads the start of a string to the given length.
|
|
68
|
+
*/
|
|
69
|
+
declare function padStart(str: string, length: number, char?: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* Pads the end of a string to the given length.
|
|
72
|
+
*/
|
|
73
|
+
declare function padEnd(str: string, length: number, char?: string): string;
|
|
74
|
+
/**
|
|
75
|
+
* Reverses a string.
|
|
76
|
+
*/
|
|
77
|
+
declare function reverse(str: string): string;
|
|
78
|
+
/**
|
|
79
|
+
* Splits a string into words.
|
|
80
|
+
*/
|
|
81
|
+
declare function words(str: string): string[];
|
|
82
|
+
/**
|
|
83
|
+
* Converts a string to a URL-friendly slug.
|
|
84
|
+
*/
|
|
85
|
+
declare function slugify(str: string): string;
|
|
86
|
+
/**
|
|
87
|
+
* Counts occurrences of a substring in a string.
|
|
88
|
+
*/
|
|
89
|
+
declare function countOccurrences(str: string, substring: string): number;
|
|
90
|
+
|
|
91
|
+
export { camelCase, capitalize, countOccurrences, escapeHtml, kebabCase, nanoid, pad, padEnd, padStart, pascalCase, reverse, slugify, snakeCase, template, trim, trimEnd, trimStart, truncate, unescapeHtml, uuid, words };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// src/string/index.ts
|
|
2
|
+
var WORD_SPLIT_RE = /[A-Z]?[a-z]+|[A-Z]+(?=[A-Z][a-z]|\d|\b)|\d+/g;
|
|
3
|
+
function splitWords(str) {
|
|
4
|
+
return str.match(WORD_SPLIT_RE) ?? [];
|
|
5
|
+
}
|
|
6
|
+
function capitalize(str) {
|
|
7
|
+
if (str.length === 0) return str;
|
|
8
|
+
return str[0].toUpperCase() + str.slice(1).toLowerCase();
|
|
9
|
+
}
|
|
10
|
+
function camelCase(str) {
|
|
11
|
+
const words2 = splitWords(str);
|
|
12
|
+
if (words2.length === 0) return "";
|
|
13
|
+
const [firstWord, ...rest] = words2;
|
|
14
|
+
return firstWord.toLowerCase() + rest.map((w) => w[0].toUpperCase() + w.slice(1).toLowerCase()).join("");
|
|
15
|
+
}
|
|
16
|
+
function kebabCase(str) {
|
|
17
|
+
return splitWords(str).map((w) => w.toLowerCase()).join("-");
|
|
18
|
+
}
|
|
19
|
+
function snakeCase(str) {
|
|
20
|
+
return splitWords(str).map((w) => w.toLowerCase()).join("_");
|
|
21
|
+
}
|
|
22
|
+
function pascalCase(str) {
|
|
23
|
+
return splitWords(str).map((w) => w[0].toUpperCase() + w.slice(1).toLowerCase()).join("");
|
|
24
|
+
}
|
|
25
|
+
function truncate(str, maxLength, suffix = "...") {
|
|
26
|
+
if (str.length <= maxLength) return str;
|
|
27
|
+
return str.slice(0, Math.max(0, maxLength - suffix.length)) + suffix;
|
|
28
|
+
}
|
|
29
|
+
function template(str, data) {
|
|
30
|
+
return str.replace(/\{\{(\w+)\}\}/g, (_, key) => {
|
|
31
|
+
const value = data[key];
|
|
32
|
+
return value !== void 0 ? String(value) : `{{${key}}}`;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function uuid() {
|
|
36
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
37
|
+
return crypto.randomUUID();
|
|
38
|
+
}
|
|
39
|
+
const hex = "0123456789abcdef";
|
|
40
|
+
const chars = [];
|
|
41
|
+
for (let i = 0; i < 36; i++) {
|
|
42
|
+
if (i === 8 || i === 13 || i === 18 || i === 23) {
|
|
43
|
+
chars.push("-");
|
|
44
|
+
} else if (i === 14) {
|
|
45
|
+
chars.push("4");
|
|
46
|
+
} else if (i === 19) {
|
|
47
|
+
chars.push(hex[Math.floor(Math.random() * 4) + 8]);
|
|
48
|
+
} else {
|
|
49
|
+
chars.push(hex[Math.floor(Math.random() * 16)]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return chars.join("");
|
|
53
|
+
}
|
|
54
|
+
function nanoid(size = 21, alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-") {
|
|
55
|
+
const len = alphabet.length;
|
|
56
|
+
let result = "";
|
|
57
|
+
for (let i = 0; i < size; i++) {
|
|
58
|
+
result += alphabet[Math.floor(Math.random() * len)];
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
var HTML_ESCAPE_MAP = {
|
|
63
|
+
"&": "&",
|
|
64
|
+
"<": "<",
|
|
65
|
+
">": ">",
|
|
66
|
+
'"': """,
|
|
67
|
+
"'": "'"
|
|
68
|
+
};
|
|
69
|
+
var HTML_UNESCAPE_MAP = {
|
|
70
|
+
"&": "&",
|
|
71
|
+
"<": "<",
|
|
72
|
+
">": ">",
|
|
73
|
+
""": '"',
|
|
74
|
+
"'": "'",
|
|
75
|
+
"'": "'"
|
|
76
|
+
};
|
|
77
|
+
function escapeHtml(str) {
|
|
78
|
+
return str.replace(/[&<>"']/g, (ch) => HTML_ESCAPE_MAP[ch] ?? ch);
|
|
79
|
+
}
|
|
80
|
+
function unescapeHtml(str) {
|
|
81
|
+
return str.replace(/&(?:amp|lt|gt|quot|#39|#x27);/g, (entity) => HTML_UNESCAPE_MAP[entity] ?? entity);
|
|
82
|
+
}
|
|
83
|
+
function trim(str) {
|
|
84
|
+
return str.trim();
|
|
85
|
+
}
|
|
86
|
+
function trimStart(str) {
|
|
87
|
+
return str.trimStart();
|
|
88
|
+
}
|
|
89
|
+
function trimEnd(str) {
|
|
90
|
+
return str.trimEnd();
|
|
91
|
+
}
|
|
92
|
+
function pad(str, length, char = " ") {
|
|
93
|
+
const totalPadding = Math.max(0, length - str.length);
|
|
94
|
+
const leftPad = Math.floor(totalPadding / 2);
|
|
95
|
+
const rightPad = totalPadding - leftPad;
|
|
96
|
+
return char.repeat(leftPad) + str + char.repeat(rightPad);
|
|
97
|
+
}
|
|
98
|
+
function padStart(str, length, char = " ") {
|
|
99
|
+
return str.padStart(length, char);
|
|
100
|
+
}
|
|
101
|
+
function padEnd(str, length, char = " ") {
|
|
102
|
+
return str.padEnd(length, char);
|
|
103
|
+
}
|
|
104
|
+
function reverse(str) {
|
|
105
|
+
return str.split("").reverse().join("");
|
|
106
|
+
}
|
|
107
|
+
function words(str) {
|
|
108
|
+
return splitWords(str);
|
|
109
|
+
}
|
|
110
|
+
function slugify(str) {
|
|
111
|
+
return str.toLowerCase().replace(/[^\w\s-]/g, "").replace(/[\s_]+/g, "-").replace(/-+/g, "-").replace(/^-+/, "").replace(/-+$/, "");
|
|
112
|
+
}
|
|
113
|
+
function countOccurrences(str, substring) {
|
|
114
|
+
if (substring.length === 0 || str.length === 0) return 0;
|
|
115
|
+
let count = 0;
|
|
116
|
+
let pos = 0;
|
|
117
|
+
while ((pos = str.indexOf(substring, pos)) !== -1) {
|
|
118
|
+
count++;
|
|
119
|
+
pos += substring.length;
|
|
120
|
+
}
|
|
121
|
+
return count;
|
|
122
|
+
}
|
|
123
|
+
export {
|
|
124
|
+
camelCase,
|
|
125
|
+
capitalize,
|
|
126
|
+
countOccurrences,
|
|
127
|
+
escapeHtml,
|
|
128
|
+
kebabCase,
|
|
129
|
+
nanoid,
|
|
130
|
+
pad,
|
|
131
|
+
padEnd,
|
|
132
|
+
padStart,
|
|
133
|
+
pascalCase,
|
|
134
|
+
reverse,
|
|
135
|
+
slugify,
|
|
136
|
+
snakeCase,
|
|
137
|
+
template,
|
|
138
|
+
trim,
|
|
139
|
+
trimEnd,
|
|
140
|
+
trimStart,
|
|
141
|
+
truncate,
|
|
142
|
+
unescapeHtml,
|
|
143
|
+
uuid,
|
|
144
|
+
words
|
|
145
|
+
};
|
|
146
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/string/index.ts"],"sourcesContent":["const WORD_SPLIT_RE = /[A-Z]?[a-z]+|[A-Z]+(?=[A-Z][a-z]|\\d|\\b)|\\d+/g\n\nfunction splitWords(str: string): string[] {\n return str.match(WORD_SPLIT_RE) ?? []\n}\n\n/**\n * Capitalizes the first character and lowercases the rest.\n */\nexport function capitalize(str: string): string {\n if (str.length === 0) return str\n return str[0]!.toUpperCase() + str.slice(1).toLowerCase()\n}\n\n/**\n * Converts a string to camelCase.\n */\nexport function camelCase(str: string): string {\n const words = splitWords(str)\n if (words.length === 0) return ''\n const [firstWord, ...rest] = words\n return firstWord!.toLowerCase() + rest.map(w => w[0]!.toUpperCase() + w.slice(1).toLowerCase()).join('')\n}\n\n/**\n * Converts a string to kebab-case.\n */\nexport function kebabCase(str: string): string {\n return splitWords(str).map(w => w.toLowerCase()).join('-')\n}\n\n/**\n * Converts a string to snake_case.\n */\nexport function snakeCase(str: string): string {\n return splitWords(str).map(w => w.toLowerCase()).join('_')\n}\n\n/**\n * Converts a string to PascalCase.\n */\nexport function pascalCase(str: string): string {\n return splitWords(str).map(w => w[0]!.toUpperCase() + w.slice(1).toLowerCase()).join('')\n}\n\n/**\n * Truncates a string to the specified length, appending a suffix (default \"...\").\n */\nexport function truncate(str: string, maxLength: number, suffix = '...'): string {\n if (str.length <= maxLength) return str\n return str.slice(0, Math.max(0, maxLength - suffix.length)) + suffix\n}\n\n/**\n * Simple string interpolation using {{key}} syntax.\n *\n * @example template(\"Hello {{name}}\", { name: \"world\" }) // => \"Hello world\"\n */\nexport function template(str: string, data: Record<string, string | number>): string {\n return str.replace(/\\{\\{(\\w+)\\}\\}/g, (_, key: string) => {\n const value = data[key]\n return value !== undefined ? String(value) : `{{${key}}}`\n })\n}\n\n/**\n * Generates a UUID v4 string.\n * Uses crypto.randomUUID when available, falls back to manual implementation.\n */\nexport function uuid(): string {\n if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {\n return crypto.randomUUID()\n }\n const hex = '0123456789abcdef'\n const chars: string[] = []\n for (let i = 0; i < 36; i++) {\n if (i === 8 || i === 13 || i === 18 || i === 23) {\n chars.push('-')\n } else if (i === 14) {\n chars.push('4')\n } else if (i === 19) {\n chars.push(hex[Math.floor(Math.random() * 4) + 8]!)\n } else {\n chars.push(hex[Math.floor(Math.random() * 16)]!)\n }\n }\n return chars.join('')\n}\n\n/**\n * Generates a short random ID with configurable length and alphabet.\n *\n * @default size = 21, alphabet = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-\"\n */\nexport function nanoid(size = 21, alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-'): string {\n const len = alphabet.length\n let result = ''\n for (let i = 0; i < size; i++) {\n result += alphabet[Math.floor(Math.random() * len)]!\n }\n return result\n}\n\nconst HTML_ESCAPE_MAP: Record<string, string> = {\n '&': '&',\n '<': '<',\n '>': '>',\n '\"': '"',\n \"'\": ''',\n}\n\nconst HTML_UNESCAPE_MAP: Record<string, string> = {\n '&': '&',\n '<': '<',\n '>': '>',\n '"': '\"',\n ''': \"'\",\n ''': \"'\",\n}\n\n/**\n * Escapes HTML special characters (&, <, >, \", ').\n */\nexport function escapeHtml(str: string): string {\n return str.replace(/[&<>\"']/g, ch => HTML_ESCAPE_MAP[ch] ?? ch)\n}\n\n/**\n * Unescapes common HTML entities.\n */\nexport function unescapeHtml(str: string): string {\n return str.replace(/&(?:amp|lt|gt|quot|#39|#x27);/g, entity => HTML_UNESCAPE_MAP[entity] ?? entity)\n}\n\n/**\n * Removes whitespace from both ends of a string.\n */\nexport function trim(str: string): string {\n return str.trim()\n}\n\n/**\n * Removes whitespace from the start of a string.\n */\nexport function trimStart(str: string): string {\n return str.trimStart()\n}\n\n/**\n * Removes whitespace from the end of a string.\n */\nexport function trimEnd(str: string): string {\n return str.trimEnd()\n}\n\n/**\n * Pads a string to the given length by adding characters to both sides.\n */\nexport function pad(str: string, length: number, char = ' '): string {\n const totalPadding = Math.max(0, length - str.length)\n const leftPad = Math.floor(totalPadding / 2)\n const rightPad = totalPadding - leftPad\n return char.repeat(leftPad) + str + char.repeat(rightPad)\n}\n\n/**\n * Pads the start of a string to the given length.\n */\nexport function padStart(str: string, length: number, char = ' '): string {\n return str.padStart(length, char)\n}\n\n/**\n * Pads the end of a string to the given length.\n */\nexport function padEnd(str: string, length: number, char = ' '): string {\n return str.padEnd(length, char)\n}\n\n/**\n * Reverses a string.\n */\nexport function reverse(str: string): string {\n return str.split('').reverse().join('')\n}\n\n/**\n * Splits a string into words.\n */\nexport function words(str: string): string[] {\n return splitWords(str)\n}\n\n/**\n * Converts a string to a URL-friendly slug.\n */\nexport function slugify(str: string): string {\n return str\n .toLowerCase()\n .replace(/[^\\w\\s-]/g, '')\n .replace(/[\\s_]+/g, '-')\n .replace(/-+/g, '-')\n .replace(/^-+/, '')\n .replace(/-+$/, '')\n}\n\n/**\n * Counts occurrences of a substring in a string.\n */\nexport function countOccurrences(str: string, substring: string): number {\n if (substring.length === 0 || str.length === 0) return 0\n let count = 0\n let pos = 0\n while ((pos = str.indexOf(substring, pos)) !== -1) {\n count++\n pos += substring.length\n }\n return count\n}\n"],"mappings":";AAAA,IAAM,gBAAgB;AAEtB,SAAS,WAAW,KAAuB;AACzC,SAAO,IAAI,MAAM,aAAa,KAAK,CAAC;AACtC;AAKO,SAAS,WAAW,KAAqB;AAC9C,MAAI,IAAI,WAAW,EAAG,QAAO;AAC7B,SAAO,IAAI,CAAC,EAAG,YAAY,IAAI,IAAI,MAAM,CAAC,EAAE,YAAY;AAC1D;AAKO,SAAS,UAAU,KAAqB;AAC7C,QAAMA,SAAQ,WAAW,GAAG;AAC5B,MAAIA,OAAM,WAAW,EAAG,QAAO;AAC/B,QAAM,CAAC,WAAW,GAAG,IAAI,IAAIA;AAC7B,SAAO,UAAW,YAAY,IAAI,KAAK,IAA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AK,OAAO,QAAQ;AAC1D;AAKO,SAAS,SAAS,KAAa,QAAgB,OAAO,KAAa;AACxE,SAAO,IAAI,SAAS,QAAQ,IAAI;AAClC;AAKO,SAAS,OAAO,KAAa,QAAgB,OAAO,KAAa;AACtE,SAAO,IAAI,OAAO,QAAQ,IAAI;AAChC;AAKO,SAAS,QAAQ,KAAqB;AAC3C,SAAO,IAAI,MAAM,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE;AACxC;AAKO,SAAS,MAAM,KAAuB;AAC3C,SAAO,WAAW,GAAG;AACvB;AAKO,SAAS,QAAQ,KAAqB;AAC3C,SAAO,IACJ,YAAY,EACZ,QAAQ,aAAa,EAAE,EACvB,QAAQ,WAAW,GAAG,EACtB,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,EAAE,EACjB,QAAQ,OAAO,EAAE;AACtB;AAKO,SAAS,iBAAiB,KAAa,WAA2B;AACvE,MAAI,UAAU,WAAW,KAAK,IAAI,WAAW,EAAG,QAAO;AACvD,MAAI,QAAQ;AACZ,MAAI,MAAM;AACV,UAAQ,MAAM,IAAI,QAAQ,WAAW,GAAG,OAAO,IAAI;AACjD;AACA,WAAO,UAAU;AAAA,EACnB;AACA,SAAO;AACT;","names":["words"]}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a value is a string.
|
|
3
|
+
*/
|
|
4
|
+
declare function isString(value: unknown): value is string;
|
|
5
|
+
/**
|
|
6
|
+
* Checks if a value is a number (not NaN, not Infinity).
|
|
7
|
+
*/
|
|
8
|
+
declare function isNumber(value: unknown): value is number;
|
|
9
|
+
/**
|
|
10
|
+
* Checks if a value is a boolean.
|
|
11
|
+
*/
|
|
12
|
+
declare function isBoolean(value: unknown): value is boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Checks if a value is a plain object (not null, not array, typeof 'object').
|
|
15
|
+
*/
|
|
16
|
+
declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a value is an array.
|
|
19
|
+
*/
|
|
20
|
+
declare function isArray(value: unknown): value is unknown[];
|
|
21
|
+
/**
|
|
22
|
+
* Checks if a value is a function.
|
|
23
|
+
*/
|
|
24
|
+
declare function isFunction(value: unknown): value is (...args: unknown[]) => unknown;
|
|
25
|
+
/**
|
|
26
|
+
* Checks if a value is a Date.
|
|
27
|
+
*/
|
|
28
|
+
declare function isDate(value: unknown): value is Date;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a value is a RegExp.
|
|
31
|
+
*/
|
|
32
|
+
declare function isRegExp(value: unknown): value is RegExp;
|
|
33
|
+
/**
|
|
34
|
+
* Checks if a value is a Map.
|
|
35
|
+
*/
|
|
36
|
+
declare function isMap(value: unknown): value is Map<unknown, unknown>;
|
|
37
|
+
/**
|
|
38
|
+
* Checks if a value is a Set.
|
|
39
|
+
*/
|
|
40
|
+
declare function isSet(value: unknown): value is Set<unknown>;
|
|
41
|
+
/**
|
|
42
|
+
* Checks if a value is a Promise.
|
|
43
|
+
*/
|
|
44
|
+
declare function isPromise(value: unknown): value is Promise<unknown>;
|
|
45
|
+
/**
|
|
46
|
+
* Checks if a value is null.
|
|
47
|
+
*/
|
|
48
|
+
declare function isNull(value: unknown): value is null;
|
|
49
|
+
/**
|
|
50
|
+
* Checks if a value is undefined.
|
|
51
|
+
*/
|
|
52
|
+
declare function isUndefined(value: unknown): value is undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Checks if a value is null or undefined.
|
|
55
|
+
*/
|
|
56
|
+
declare function isNil(value: unknown): value is null | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Checks if a value is empty.
|
|
59
|
+
* Works for strings, arrays, objects, Map, and Set.
|
|
60
|
+
*/
|
|
61
|
+
declare function isEmpty(value: unknown): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Asserts that a value is defined (not null or undefined).
|
|
64
|
+
* Throws if the value is null or undefined.
|
|
65
|
+
*/
|
|
66
|
+
declare function assertDefined<T>(value: T, message?: string): asserts value is NonNullable<T>;
|
|
67
|
+
/**
|
|
68
|
+
* Asserts that a value matches a type guard. Throws if it doesn't.
|
|
69
|
+
*/
|
|
70
|
+
declare function assertType<T>(value: unknown, guard: (v: unknown) => v is T, message?: string): asserts value is T;
|
|
71
|
+
/**
|
|
72
|
+
* Wraps a value in an array if it is not already one.
|
|
73
|
+
*/
|
|
74
|
+
declare function ensureArray<T>(value: T | T[]): T[];
|
|
75
|
+
/**
|
|
76
|
+
* Alias for ensureArray.
|
|
77
|
+
*/
|
|
78
|
+
declare function castArray<T>(value: T | T[]): T[];
|
|
79
|
+
/**
|
|
80
|
+
* Returns a string representation of the value's type.
|
|
81
|
+
*
|
|
82
|
+
* Possible values: "string", "number", "boolean", "array", "object", "function",
|
|
83
|
+
* "date", "regexp", "map", "set", "promise", "null", "undefined", "nan", "infinity"
|
|
84
|
+
*/
|
|
85
|
+
declare function getType(value: unknown): string;
|
|
86
|
+
|
|
87
|
+
export { assertDefined, assertType, castArray, ensureArray, getType, isArray, isBoolean, isDate, isEmpty, isFunction, isMap, isNil, isNull, isNumber, isObject, isPromise, isRegExp, isSet, isString, isUndefined };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/type/index.ts
|
|
2
|
+
function isString(value) {
|
|
3
|
+
return typeof value === "string";
|
|
4
|
+
}
|
|
5
|
+
function isNumber(value) {
|
|
6
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
7
|
+
}
|
|
8
|
+
function isBoolean(value) {
|
|
9
|
+
return typeof value === "boolean";
|
|
10
|
+
}
|
|
11
|
+
function isObject(value) {
|
|
12
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
13
|
+
}
|
|
14
|
+
function isArray(value) {
|
|
15
|
+
return Array.isArray(value);
|
|
16
|
+
}
|
|
17
|
+
function isFunction(value) {
|
|
18
|
+
return typeof value === "function";
|
|
19
|
+
}
|
|
20
|
+
function isDate(value) {
|
|
21
|
+
return value instanceof Date && !Number.isNaN(value.getTime());
|
|
22
|
+
}
|
|
23
|
+
function isRegExp(value) {
|
|
24
|
+
return value instanceof RegExp;
|
|
25
|
+
}
|
|
26
|
+
function isMap(value) {
|
|
27
|
+
return value instanceof Map;
|
|
28
|
+
}
|
|
29
|
+
function isSet(value) {
|
|
30
|
+
return value instanceof Set;
|
|
31
|
+
}
|
|
32
|
+
function isPromise(value) {
|
|
33
|
+
return value instanceof Promise;
|
|
34
|
+
}
|
|
35
|
+
function isNull(value) {
|
|
36
|
+
return value === null;
|
|
37
|
+
}
|
|
38
|
+
function isUndefined(value) {
|
|
39
|
+
return value === void 0;
|
|
40
|
+
}
|
|
41
|
+
function isNil(value) {
|
|
42
|
+
return value === null || value === void 0;
|
|
43
|
+
}
|
|
44
|
+
function isEmpty(value) {
|
|
45
|
+
if (value === null || value === void 0) return true;
|
|
46
|
+
if (typeof value === "string") return value.length === 0;
|
|
47
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
48
|
+
if (value instanceof Map || value instanceof Set) return value.size === 0;
|
|
49
|
+
if (typeof value === "object") return Object.keys(value).length === 0;
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
function assertDefined(value, message) {
|
|
53
|
+
if (value === null || value === void 0) {
|
|
54
|
+
throw new Error(message ?? "Expected value to be defined");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function assertType(value, guard, message) {
|
|
58
|
+
if (!guard(value)) {
|
|
59
|
+
throw new Error(message ?? "Value does not match expected type");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function ensureArray(value) {
|
|
63
|
+
return Array.isArray(value) ? value : [value];
|
|
64
|
+
}
|
|
65
|
+
function castArray(value) {
|
|
66
|
+
return ensureArray(value);
|
|
67
|
+
}
|
|
68
|
+
function getType(value) {
|
|
69
|
+
if (value === null) return "null";
|
|
70
|
+
if (value === void 0) return "undefined";
|
|
71
|
+
if (typeof value === "string") return "string";
|
|
72
|
+
if (typeof value === "boolean") return "boolean";
|
|
73
|
+
if (typeof value === "function") return "function";
|
|
74
|
+
if (typeof value === "number") {
|
|
75
|
+
if (Number.isNaN(value)) return "nan";
|
|
76
|
+
if (!Number.isFinite(value)) return "infinity";
|
|
77
|
+
return "number";
|
|
78
|
+
}
|
|
79
|
+
if (Array.isArray(value)) return "array";
|
|
80
|
+
if (value instanceof Date) return "date";
|
|
81
|
+
if (value instanceof RegExp) return "regexp";
|
|
82
|
+
if (value instanceof Map) return "map";
|
|
83
|
+
if (value instanceof Set) return "set";
|
|
84
|
+
if (value instanceof Promise) return "promise";
|
|
85
|
+
return "object";
|
|
86
|
+
}
|
|
87
|
+
export {
|
|
88
|
+
assertDefined,
|
|
89
|
+
assertType,
|
|
90
|
+
castArray,
|
|
91
|
+
ensureArray,
|
|
92
|
+
getType,
|
|
93
|
+
isArray,
|
|
94
|
+
isBoolean,
|
|
95
|
+
isDate,
|
|
96
|
+
isEmpty,
|
|
97
|
+
isFunction,
|
|
98
|
+
isMap,
|
|
99
|
+
isNil,
|
|
100
|
+
isNull,
|
|
101
|
+
isNumber,
|
|
102
|
+
isObject,
|
|
103
|
+
isPromise,
|
|
104
|
+
isRegExp,
|
|
105
|
+
isSet,
|
|
106
|
+
isString,
|
|
107
|
+
isUndefined
|
|
108
|
+
};
|
|
109
|
+
//# sourceMappingURL=index.js.map
|