utilful 2.0.0 → 2.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/README.md +100 -10
- package/dist/array.d.ts +7 -5
- package/dist/array.js +11 -0
- package/dist/csv.d.ts +57 -57
- package/dist/csv.js +98 -0
- package/dist/defu.d.ts +16 -0
- package/dist/defu.js +34 -0
- package/dist/emitter.d.ts +20 -14
- package/dist/emitter.js +32 -0
- package/dist/index.d.ts +12 -10
- package/dist/index.js +12 -0
- package/dist/json.d.ts +13 -11
- package/dist/json.js +34 -0
- package/dist/module.d.ts +9 -7
- package/dist/module.js +14 -0
- package/dist/object.d.ts +23 -21
- package/dist/object.js +51 -0
- package/dist/path.d.ts +21 -19
- package/dist/path.js +105 -0
- package/dist/result.d.ts +13 -11
- package/dist/result.js +45 -0
- package/dist/string.d.ts +16 -14
- package/dist/string.js +32 -0
- package/dist/types.d.ts +4 -4
- package/dist/types.js +0 -0
- package/package.json +18 -13
- package/dist/array.d.mts +0 -10
- package/dist/array.mjs +0 -6
- package/dist/csv.d.mts +0 -67
- package/dist/csv.mjs +0 -77
- package/dist/emitter.d.mts +0 -24
- package/dist/emitter.mjs +0 -63
- package/dist/index.d.mts +0 -10
- package/dist/index.mjs +0 -9
- package/dist/json.d.mts +0 -16
- package/dist/json.mjs +0 -26
- package/dist/module.d.mts +0 -11
- package/dist/module.mjs +0 -6
- package/dist/object.d.mts +0 -31
- package/dist/object.mjs +0 -34
- package/dist/path.d.mts +0 -40
- package/dist/path.mjs +0 -113
- package/dist/result.d.mts +0 -32
- package/dist/result.mjs +0 -43
- package/dist/string.d.mts +0 -19
- package/dist/string.mjs +0 -16
- package/dist/types.d.mts +0 -8
- package/dist/types.mjs +0 -1
package/dist/module.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
//#region src/module.d.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
* Interop helper for default exports.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const mod = await interopDefault(import('./module.js'))
|
|
7
|
+
*/
|
|
7
8
|
declare function interopDefault<T>(m: T | Promise<T>): Promise<T extends {
|
|
8
|
-
|
|
9
|
+
default: infer U;
|
|
9
10
|
} ? U : T>;
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
//#endregion
|
|
13
|
+
export { interopDefault };
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/module.ts
|
|
2
|
+
/**
|
|
3
|
+
* Interop helper for default exports.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const mod = await interopDefault(import('./module.js'))
|
|
7
|
+
*/
|
|
8
|
+
async function interopDefault(m) {
|
|
9
|
+
const resolved = await m;
|
|
10
|
+
return resolved.default || resolved;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { interopDefault };
|
package/dist/object.d.ts
CHANGED
|
@@ -1,31 +1,33 @@
|
|
|
1
|
+
//#region src/object.d.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
* A simple general purpose memoizer utility.
|
|
4
|
+
* - Lazily computes a value when accessed
|
|
5
|
+
* - Auto-caches the result by overwriting the getter
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Useful for deferring initialization or expensive operations. Unlike a simple getter, there is no runtime overhead after the first invokation, since the getter itself is overwritten with the memoized value.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const myValue = lazy(() => 'Hello, World!')
|
|
12
|
+
* console.log(myValue.value) // Computes value, overwrites getter
|
|
13
|
+
* console.log(myValue.value) // Returns cached value
|
|
14
|
+
* console.log(myValue.value) // Returns cached value
|
|
15
|
+
*/
|
|
15
16
|
declare function memoize<T>(getter: () => T): {
|
|
16
|
-
|
|
17
|
+
value: T;
|
|
17
18
|
};
|
|
18
19
|
/**
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
* Strictly typed `Object.keys`.
|
|
21
|
+
*/
|
|
21
22
|
declare function objectKeys<T extends Record<any, any>>(obj: T): Array<`${keyof T & (string | number | boolean | null | undefined)}`>;
|
|
22
23
|
/**
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
* Strictly typed `Object.entries`.
|
|
25
|
+
*/
|
|
25
26
|
declare function objectEntries<T extends Record<any, any>>(obj: T): Array<[keyof T, T[keyof T]]>;
|
|
26
27
|
/**
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
* Deeply applies a callback to every key-value pair in the given object, as well as nested objects and arrays.
|
|
29
|
+
*/
|
|
29
30
|
declare function deepApply<T extends Record<any, any>>(data: T, callback: (item: T, key: keyof T, value: T[keyof T]) => void): void;
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
//#endregion
|
|
33
|
+
export { deepApply, memoize, objectEntries, objectKeys };
|
package/dist/object.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/object.ts
|
|
2
|
+
/**
|
|
3
|
+
* A simple general purpose memoizer utility.
|
|
4
|
+
* - Lazily computes a value when accessed
|
|
5
|
+
* - Auto-caches the result by overwriting the getter
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Useful for deferring initialization or expensive operations. Unlike a simple getter, there is no runtime overhead after the first invokation, since the getter itself is overwritten with the memoized value.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const myValue = lazy(() => 'Hello, World!')
|
|
12
|
+
* console.log(myValue.value) // Computes value, overwrites getter
|
|
13
|
+
* console.log(myValue.value) // Returns cached value
|
|
14
|
+
* console.log(myValue.value) // Returns cached value
|
|
15
|
+
*/
|
|
16
|
+
function memoize(getter) {
|
|
17
|
+
return { get value() {
|
|
18
|
+
const value = getter();
|
|
19
|
+
Object.defineProperty(this, "value", { value });
|
|
20
|
+
return value;
|
|
21
|
+
} };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Strictly typed `Object.keys`.
|
|
25
|
+
*/
|
|
26
|
+
function objectKeys(obj) {
|
|
27
|
+
return Object.keys(obj);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Strictly typed `Object.entries`.
|
|
31
|
+
*/
|
|
32
|
+
function objectEntries(obj) {
|
|
33
|
+
return Object.entries(obj);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Deeply applies a callback to every key-value pair in the given object, as well as nested objects and arrays.
|
|
37
|
+
*/
|
|
38
|
+
function deepApply(data, callback) {
|
|
39
|
+
for (const [key, value] of Object.entries(data)) {
|
|
40
|
+
callback(data, key, value);
|
|
41
|
+
if (Array.isArray(value)) {
|
|
42
|
+
for (const element of value) if (isObject(element)) deepApply(element, callback);
|
|
43
|
+
} else if (isObject(value)) deepApply(value, callback);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function isObject(value) {
|
|
47
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { deepApply, memoize, objectEntries, objectKeys };
|
package/dist/path.d.ts
CHANGED
|
@@ -1,40 +1,42 @@
|
|
|
1
|
+
//#region src/path.d.ts
|
|
1
2
|
type QueryValue = string | number | boolean | QueryValue[] | Record<string, any> | null | undefined;
|
|
2
3
|
type QueryObject = Record<string, QueryValue | QueryValue[]>;
|
|
3
4
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
* Removes the leading slash from the given path if it has one.
|
|
6
|
+
*/
|
|
6
7
|
declare function withoutLeadingSlash(path?: string): string;
|
|
7
8
|
/**
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
* Adds a leading slash to the given path if it does not already have one.
|
|
10
|
+
*/
|
|
10
11
|
declare function withLeadingSlash(path?: string): string;
|
|
11
12
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
* Removes the trailing slash from the given path if it has one.
|
|
14
|
+
*/
|
|
14
15
|
declare function withoutTrailingSlash(path?: string): string;
|
|
15
16
|
/**
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
* Adds a trailing slash to the given path if it does not already have one
|
|
18
|
+
*/
|
|
18
19
|
declare function withTrailingSlash(path?: string): string;
|
|
19
20
|
/**
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
* Joins the given URL path segments, ensuring that there is only one slash between them.
|
|
22
|
+
*/
|
|
22
23
|
declare function joinURL(...paths: (string | undefined)[]): string;
|
|
23
24
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
* Adds the base path to the input path, if it is not already present.
|
|
26
|
+
*/
|
|
26
27
|
declare function withBase(input?: string, base?: string): string;
|
|
27
28
|
/**
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
* Removes the base path from the input path, if it is present.
|
|
30
|
+
*/
|
|
30
31
|
declare function withoutBase(input?: string, base?: string): string;
|
|
31
32
|
/**
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
* Returns the pathname of the given path, which is the path without the query string.
|
|
34
|
+
*/
|
|
34
35
|
declare function getPathname(path?: string): string;
|
|
35
36
|
/**
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
* Returns the URL with the given query parameters. If a query parameter is undefined, it is omitted.
|
|
38
|
+
*/
|
|
38
39
|
declare function withQuery(input: string, query?: QueryObject): string;
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
//#endregion
|
|
42
|
+
export { QueryObject, QueryValue, getPathname, joinURL, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutLeadingSlash, withoutTrailingSlash };
|
package/dist/path.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
//#region src/path.ts
|
|
2
|
+
/**
|
|
3
|
+
* Removes the leading slash from the given path if it has one.
|
|
4
|
+
*/
|
|
5
|
+
function withoutLeadingSlash(path) {
|
|
6
|
+
if (!path || path === "/") return "/";
|
|
7
|
+
return path[0] === "/" ? path.slice(1) : path;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Adds a leading slash to the given path if it does not already have one.
|
|
11
|
+
*/
|
|
12
|
+
function withLeadingSlash(path) {
|
|
13
|
+
if (!path || path === "/") return "/";
|
|
14
|
+
return path[0] === "/" ? path : `/${path}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Removes the trailing slash from the given path if it has one.
|
|
18
|
+
*/
|
|
19
|
+
function withoutTrailingSlash(path) {
|
|
20
|
+
if (!path || path === "/") return "/";
|
|
21
|
+
return path[path.length - 1] === "/" ? path.slice(0, -1) : path;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Adds a trailing slash to the given path if it does not already have one
|
|
25
|
+
*/
|
|
26
|
+
function withTrailingSlash(path) {
|
|
27
|
+
if (!path || path === "/") return "/";
|
|
28
|
+
return path[path.length - 1] === "/" ? path : `${path}/`;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Joins the given URL path segments, ensuring that there is only one slash between them.
|
|
32
|
+
*/
|
|
33
|
+
function joinURL(...paths) {
|
|
34
|
+
let result = "";
|
|
35
|
+
for (let i = 0; i < paths.length; i++) {
|
|
36
|
+
const path = paths[i];
|
|
37
|
+
if (!result || result === "/") {
|
|
38
|
+
result = path || "/";
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (!path || path === "/") continue;
|
|
42
|
+
const resultHasTrailing = result[result.length - 1] === "/";
|
|
43
|
+
const pathHasLeading = path[0] === "/";
|
|
44
|
+
if (resultHasTrailing && pathHasLeading) result += path.slice(1);
|
|
45
|
+
else if (!resultHasTrailing && !pathHasLeading) result += `/${path}`;
|
|
46
|
+
else result += path;
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Adds the base path to the input path, if it is not already present.
|
|
52
|
+
*/
|
|
53
|
+
function withBase(input = "", base = "") {
|
|
54
|
+
if (!base || base === "/") return input;
|
|
55
|
+
const _base = withoutTrailingSlash(base);
|
|
56
|
+
if (input.startsWith(_base)) return input;
|
|
57
|
+
return joinURL(_base, input);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Removes the base path from the input path, if it is present.
|
|
61
|
+
*/
|
|
62
|
+
function withoutBase(input = "", base = "") {
|
|
63
|
+
if (!base || base === "/") return input;
|
|
64
|
+
const _base = withoutTrailingSlash(base);
|
|
65
|
+
if (!input.startsWith(_base)) return input;
|
|
66
|
+
const trimmed = input.slice(_base.length);
|
|
67
|
+
return trimmed[0] === "/" ? trimmed : `/${trimmed}`;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns the pathname of the given path, which is the path without the query string.
|
|
71
|
+
*/
|
|
72
|
+
function getPathname(path = "/") {
|
|
73
|
+
return path.startsWith("/") ? path.split("?")[0] : new URL(path, "http://localhost").pathname;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Returns the URL with the given query parameters. If a query parameter is undefined, it is omitted.
|
|
77
|
+
*/
|
|
78
|
+
function withQuery(input, query) {
|
|
79
|
+
if (!query || Object.keys(query).length === 0) return input;
|
|
80
|
+
const searchIndex = input.indexOf("?");
|
|
81
|
+
const hasExistingParams = searchIndex !== -1;
|
|
82
|
+
const base = hasExistingParams ? input.slice(0, searchIndex) : input;
|
|
83
|
+
const searchParams = hasExistingParams ? new URLSearchParams(input.slice(searchIndex + 1)) : new URLSearchParams();
|
|
84
|
+
for (const [key, value] of Object.entries(query)) {
|
|
85
|
+
if (value === void 0) {
|
|
86
|
+
searchParams.delete(key);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(value)) {
|
|
90
|
+
if (value.length === 0) continue;
|
|
91
|
+
for (const item of value) if (item !== void 0) searchParams.append(key, normalizeQueryValue(item));
|
|
92
|
+
} else searchParams.set(key, normalizeQueryValue(value));
|
|
93
|
+
}
|
|
94
|
+
const queryString = searchParams.toString();
|
|
95
|
+
return queryString ? `${base}?${queryString}` : base;
|
|
96
|
+
}
|
|
97
|
+
function normalizeQueryValue(value) {
|
|
98
|
+
if (value === null) return "";
|
|
99
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
100
|
+
if (typeof value === "object") return JSON.stringify(value);
|
|
101
|
+
return String(value);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
//#endregion
|
|
105
|
+
export { getPathname, joinURL, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutLeadingSlash, withoutTrailingSlash };
|
package/dist/result.d.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
+
//#region src/result.d.ts
|
|
1
2
|
type Result<T, E> = Ok<T> | Err<E>;
|
|
2
3
|
interface OkData<T> {
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
value: T;
|
|
5
|
+
error: undefined;
|
|
5
6
|
}
|
|
6
7
|
interface ErrData<E> {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
value: undefined;
|
|
9
|
+
error: E;
|
|
9
10
|
}
|
|
10
11
|
type ResultData<T, E> = OkData<T> | ErrData<E>;
|
|
11
12
|
declare class Ok<T> {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
readonly value: T;
|
|
14
|
+
readonly ok: true;
|
|
15
|
+
constructor(value: T);
|
|
15
16
|
}
|
|
16
17
|
declare class Err<E> {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
readonly error: E;
|
|
19
|
+
readonly ok: false;
|
|
20
|
+
constructor(error: E);
|
|
20
21
|
}
|
|
21
22
|
declare function ok<T>(value: T): Ok<T>;
|
|
22
23
|
declare function err<E extends string = string>(error: E): Err<E>;
|
|
@@ -29,4 +30,5 @@ declare function unwrapResult<T, E>(result: Result<T, E>): ResultData<T, E>;
|
|
|
29
30
|
declare function tryCatch<T, E = unknown>(fn: () => T): ResultData<T, E>;
|
|
30
31
|
declare function tryCatch<T, E = unknown>(promise: Promise<T>): Promise<ResultData<T, E>>;
|
|
31
32
|
|
|
32
|
-
|
|
33
|
+
//#endregion
|
|
34
|
+
export { Err, ErrData, Ok, OkData, Result, ResultData, err, ok, toResult, tryCatch, unwrapResult };
|
package/dist/result.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/result.ts
|
|
2
|
+
var Ok = class {
|
|
3
|
+
value;
|
|
4
|
+
ok = true;
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this.value = value;
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
var Err = class {
|
|
10
|
+
error;
|
|
11
|
+
ok = false;
|
|
12
|
+
constructor(error) {
|
|
13
|
+
this.error = error;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
function ok(value) {
|
|
17
|
+
return new Ok(value);
|
|
18
|
+
}
|
|
19
|
+
function err(error) {
|
|
20
|
+
return new Err(error);
|
|
21
|
+
}
|
|
22
|
+
function toResult(fnOrPromise) {
|
|
23
|
+
if (fnOrPromise instanceof Promise) return fnOrPromise.then(ok).catch(err);
|
|
24
|
+
try {
|
|
25
|
+
return ok(fnOrPromise());
|
|
26
|
+
} catch (error) {
|
|
27
|
+
return err(error);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function unwrapResult(result) {
|
|
31
|
+
return result.ok ? {
|
|
32
|
+
value: result.value,
|
|
33
|
+
error: void 0
|
|
34
|
+
} : {
|
|
35
|
+
value: void 0,
|
|
36
|
+
error: result.error
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function tryCatch(fnOrPromise) {
|
|
40
|
+
if (fnOrPromise instanceof Promise) return toResult(fnOrPromise).then((result) => unwrapResult(result));
|
|
41
|
+
return unwrapResult(toResult(fnOrPromise));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
export { Err, Ok, err, ok, toResult, tryCatch, unwrapResult };
|
package/dist/string.d.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
+
//#region src/string.d.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
* Simple template engine to replace variables in a string.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const str = 'Hello, {name}!'
|
|
7
|
+
* const variables = { name: 'world' }
|
|
8
|
+
*
|
|
9
|
+
* console.log(template(str, variables)) // Hello, world!
|
|
10
|
+
*/
|
|
10
11
|
declare function template(str: string, variables: Record<string | number, any>, fallback?: string | ((key: string) => string)): string;
|
|
11
12
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
* Generates a random string.
|
|
14
|
+
*
|
|
15
|
+
* @remarks Ported from `nanoid`.
|
|
16
|
+
* @see https://github.com/ai/nanoid
|
|
17
|
+
*/
|
|
17
18
|
declare function generateRandomId(size?: number, dict?: string): string;
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
//#endregion
|
|
21
|
+
export { generateRandomId, template };
|
package/dist/string.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/string.ts
|
|
2
|
+
const URL_ALPHABET = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
3
|
+
/**
|
|
4
|
+
* Simple template engine to replace variables in a string.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const str = 'Hello, {name}!'
|
|
8
|
+
* const variables = { name: 'world' }
|
|
9
|
+
*
|
|
10
|
+
* console.log(template(str, variables)) // Hello, world!
|
|
11
|
+
*/
|
|
12
|
+
function template(str, variables, fallback) {
|
|
13
|
+
return str.replace(/\{(\w+)\}/g, (_, key) => {
|
|
14
|
+
return variables[key] || ((typeof fallback === "function" ? fallback(key) : fallback) ?? key);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Generates a random string.
|
|
19
|
+
*
|
|
20
|
+
* @remarks Ported from `nanoid`.
|
|
21
|
+
* @see https://github.com/ai/nanoid
|
|
22
|
+
*/
|
|
23
|
+
function generateRandomId(size = 16, dict = URL_ALPHABET) {
|
|
24
|
+
let id = "";
|
|
25
|
+
let i = size;
|
|
26
|
+
const len = dict.length;
|
|
27
|
+
while (i--) id += dict[Math.random() * len | 0];
|
|
28
|
+
return id;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
export { generateRandomId, template };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
1
2
|
type AutocompletableString = string & {};
|
|
2
3
|
type LooseAutocomplete<T extends string> = T | AutocompletableString;
|
|
3
4
|
/** Also commonly referred to as `Prettify` */
|
|
4
|
-
type UnifyIntersection<T> = {
|
|
5
|
-
[K in keyof T]: T[K];
|
|
6
|
-
} & {};
|
|
5
|
+
type UnifyIntersection<T> = { [K in keyof T]: T[K] } & {};
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
//#endregion
|
|
8
|
+
export { AutocompletableString, LooseAutocomplete, UnifyIntersection };
|
package/dist/types.js
ADDED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "utilful",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
5
|
-
"packageManager": "pnpm@10.
|
|
4
|
+
"version": "2.1.0",
|
|
5
|
+
"packageManager": "pnpm@10.11.0",
|
|
6
6
|
"description": "A collection of TypeScript utilities",
|
|
7
7
|
"author": "Johann Schopplich <hello@johannschopplich.com>",
|
|
8
8
|
"license": "MIT",
|
|
@@ -28,9 +28,13 @@
|
|
|
28
28
|
"types": "./dist/csv.d.mts",
|
|
29
29
|
"default": "./dist/csv.mjs"
|
|
30
30
|
},
|
|
31
|
+
"./defu": {
|
|
32
|
+
"types": "./dist/defu.d.mts",
|
|
33
|
+
"default": "./dist/defu.mjs"
|
|
34
|
+
},
|
|
31
35
|
"./emitter": {
|
|
32
|
-
"types": "./dist/
|
|
33
|
-
"default": "./dist/
|
|
36
|
+
"types": "./dist/emitter.d.mts",
|
|
37
|
+
"default": "./dist/emitter.mjs"
|
|
34
38
|
},
|
|
35
39
|
"./json": {
|
|
36
40
|
"types": "./dist/json.d.mts",
|
|
@@ -66,7 +70,7 @@
|
|
|
66
70
|
"dist"
|
|
67
71
|
],
|
|
68
72
|
"scripts": {
|
|
69
|
-
"build": "
|
|
73
|
+
"build": "tsdown",
|
|
70
74
|
"lint": "eslint .",
|
|
71
75
|
"lint:fix": "eslint . --fix",
|
|
72
76
|
"test": "vitest",
|
|
@@ -74,17 +78,18 @@
|
|
|
74
78
|
"release": "bumpp"
|
|
75
79
|
},
|
|
76
80
|
"devDependencies": {
|
|
77
|
-
"@antfu/eslint-config": "^4.
|
|
78
|
-
"@types/node": "^22.
|
|
79
|
-
"bumpp": "^10.
|
|
80
|
-
"eslint": "^9.
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
"vitest": "^3.
|
|
81
|
+
"@antfu/eslint-config": "^4.13.2",
|
|
82
|
+
"@types/node": "^22.15.21",
|
|
83
|
+
"bumpp": "^10.1.1",
|
|
84
|
+
"eslint": "^9.27.0",
|
|
85
|
+
"tsdown": "^0.12.3",
|
|
86
|
+
"typescript": "^5.8.3",
|
|
87
|
+
"vitest": "^3.1.4"
|
|
84
88
|
},
|
|
85
89
|
"pnpm": {
|
|
86
90
|
"onlyBuiltDependencies": [
|
|
87
|
-
"esbuild"
|
|
91
|
+
"esbuild",
|
|
92
|
+
"unrs-resolver"
|
|
88
93
|
]
|
|
89
94
|
}
|
|
90
95
|
}
|
package/dist/array.d.mts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a value that can be either a single value or an array of values.
|
|
3
|
-
*/
|
|
4
|
-
type MaybeArray<T> = T | T[];
|
|
5
|
-
/**
|
|
6
|
-
* Converts `MaybeArray<T>` to `Array<T>`.
|
|
7
|
-
*/
|
|
8
|
-
declare function toArray<T>(array?: MaybeArray<T> | null | undefined): T[];
|
|
9
|
-
|
|
10
|
-
export { type MaybeArray, toArray };
|
package/dist/array.mjs
DELETED
package/dist/csv.d.mts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a row in a CSV file with column names of type T.
|
|
3
|
-
*/
|
|
4
|
-
type CSVRow<T extends string = string> = Record<T, string>;
|
|
5
|
-
/**
|
|
6
|
-
* Converts an array of objects to a comma-separated values (CSV) string
|
|
7
|
-
* that contains only the `columns` specified.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const data = [
|
|
11
|
-
* { name: 'John', age: '30', city: 'New York' },
|
|
12
|
-
* { name: 'Jane', age: '25', city: 'Boston' }
|
|
13
|
-
* ]
|
|
14
|
-
*
|
|
15
|
-
* const csv = createCSV(data, ['name', 'age'])
|
|
16
|
-
* // name,age
|
|
17
|
-
* // John,30
|
|
18
|
-
* // Jane,25
|
|
19
|
-
*/
|
|
20
|
-
declare function createCSV<T extends Record<string, unknown>>(data: T[], columns: (keyof T)[], options?: {
|
|
21
|
-
/** @default ',' */
|
|
22
|
-
delimiter?: string;
|
|
23
|
-
/** @default true */
|
|
24
|
-
addHeader?: boolean;
|
|
25
|
-
/** @default false */
|
|
26
|
-
quoteAll?: boolean;
|
|
27
|
-
}): string;
|
|
28
|
-
/**
|
|
29
|
-
* Escapes a value for a CSV string.
|
|
30
|
-
*
|
|
31
|
-
* @remarks
|
|
32
|
-
* Returns an empty string if the value is `null` or `undefined`.
|
|
33
|
-
* Values containing delimiters, quotes, or line breaks are quoted.
|
|
34
|
-
* Within quoted values, double quotes are escaped by doubling them.
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* escapeCSVValue('hello, world') // "hello, world"
|
|
38
|
-
* escapeCSVValue('contains "quotes"') // "contains ""quotes"""
|
|
39
|
-
*/
|
|
40
|
-
declare function escapeCSVValue(value: unknown, options?: {
|
|
41
|
-
/** @default ',' */
|
|
42
|
-
delimiter?: string;
|
|
43
|
-
/** @default false */
|
|
44
|
-
quoteAll?: boolean;
|
|
45
|
-
}): string;
|
|
46
|
-
/**
|
|
47
|
-
* Parses a comma-separated values (CSV) string into an array of objects.
|
|
48
|
-
*
|
|
49
|
-
* @remarks
|
|
50
|
-
* The first row of the CSV string is used as the header row.
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* const csv = `name,age
|
|
54
|
-
* John,30
|
|
55
|
-
* Jane,25`
|
|
56
|
-
*
|
|
57
|
-
* const data = parseCSV<'name' | 'age'>(csv)
|
|
58
|
-
* // [{ name: 'John', age: '30' }, { name: 'Jane', age: '25' }]
|
|
59
|
-
*/
|
|
60
|
-
declare function parseCSV<Header extends string>(csv?: string | null | undefined, options?: {
|
|
61
|
-
/** @default ',' */
|
|
62
|
-
delimiter?: string;
|
|
63
|
-
/** @default true */
|
|
64
|
-
trimValues?: boolean;
|
|
65
|
-
}): CSVRow<Header>[];
|
|
66
|
-
|
|
67
|
-
export { type CSVRow, createCSV, escapeCSVValue, parseCSV };
|