utilful 1.2.1 → 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 +240 -24
- 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 +26 -7
- package/dist/object.js +51 -0
- package/dist/path.d.ts +21 -19
- package/dist/path.js +105 -0
- package/dist/result.d.ts +34 -0
- 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 +23 -18
- 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/lazy.d.mts +0 -20
- package/dist/lazy.d.ts +0 -20
- package/dist/lazy.mjs +0 -11
- package/dist/module.d.mts +0 -11
- package/dist/module.mjs +0 -6
- package/dist/object.d.mts +0 -14
- package/dist/object.mjs +0 -25
- package/dist/path.d.mts +0 -40
- package/dist/path.mjs +0 -113
- 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/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 };
|
package/dist/csv.mjs
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
function createCSV(data, columns, options = {}) {
|
|
2
|
-
const {
|
|
3
|
-
delimiter = ",",
|
|
4
|
-
addHeader = true,
|
|
5
|
-
quoteAll = false
|
|
6
|
-
} = options;
|
|
7
|
-
const escapeAndQuote = (value) => escapeCSVValue(value, { delimiter, quoteAll });
|
|
8
|
-
const rows = data.map(
|
|
9
|
-
(obj) => columns.map((key) => escapeAndQuote(obj[key])).join(delimiter)
|
|
10
|
-
);
|
|
11
|
-
if (addHeader) {
|
|
12
|
-
rows.unshift(columns.map(escapeAndQuote).join(delimiter));
|
|
13
|
-
}
|
|
14
|
-
return rows.join("\n");
|
|
15
|
-
}
|
|
16
|
-
function escapeCSVValue(value, options = {}) {
|
|
17
|
-
const {
|
|
18
|
-
delimiter = ",",
|
|
19
|
-
quoteAll = false
|
|
20
|
-
} = options;
|
|
21
|
-
if (value == null) {
|
|
22
|
-
return "";
|
|
23
|
-
}
|
|
24
|
-
const stringValue = String(value);
|
|
25
|
-
const needsQuoting = quoteAll || stringValue.includes(delimiter) || stringValue.includes('"') || stringValue.includes("\n") || stringValue.includes("\r");
|
|
26
|
-
if (needsQuoting) {
|
|
27
|
-
return `"${stringValue.replaceAll('"', '""')}"`;
|
|
28
|
-
}
|
|
29
|
-
return stringValue;
|
|
30
|
-
}
|
|
31
|
-
function parseCSV(csv, options = {}) {
|
|
32
|
-
if (!csv?.trim())
|
|
33
|
-
return [];
|
|
34
|
-
const rows = [];
|
|
35
|
-
let currentRow = [];
|
|
36
|
-
let currentField = "";
|
|
37
|
-
let inQuotes = false;
|
|
38
|
-
const { delimiter = ",", trimValues = true } = options;
|
|
39
|
-
for (let i = 0; i < csv.length; i++) {
|
|
40
|
-
const char = csv[i];
|
|
41
|
-
const nextChar = i + 1 < csv.length ? csv[i + 1] : "";
|
|
42
|
-
if (char === '"') {
|
|
43
|
-
if (inQuotes && nextChar === '"') {
|
|
44
|
-
currentField += '"';
|
|
45
|
-
i++;
|
|
46
|
-
} else {
|
|
47
|
-
inQuotes = !inQuotes;
|
|
48
|
-
}
|
|
49
|
-
} else if (char === delimiter && !inQuotes) {
|
|
50
|
-
currentRow.push(trimValues ? currentField.trim() : currentField);
|
|
51
|
-
currentField = "";
|
|
52
|
-
} else if ((char === "\n" || char === "\r" && nextChar === "\n") && !inQuotes) {
|
|
53
|
-
if (char === "\r")
|
|
54
|
-
i++;
|
|
55
|
-
currentRow.push(trimValues ? currentField.trim() : currentField);
|
|
56
|
-
rows.push(currentRow);
|
|
57
|
-
currentRow = [];
|
|
58
|
-
currentField = "";
|
|
59
|
-
} else {
|
|
60
|
-
currentField += char;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (currentField || currentRow.length > 0) {
|
|
64
|
-
currentRow.push(trimValues ? currentField.trim() : currentField);
|
|
65
|
-
rows.push(currentRow);
|
|
66
|
-
}
|
|
67
|
-
if (rows.length <= 1)
|
|
68
|
-
return [];
|
|
69
|
-
const headers = rows[0];
|
|
70
|
-
return rows.slice(1).filter((row) => row.some((field) => field.trim().length > 0)).map((values) => {
|
|
71
|
-
return Object.fromEntries(
|
|
72
|
-
headers.map((header, index) => [header, index < values.length ? values[index] : ""])
|
|
73
|
-
);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export { createCSV, escapeCSVValue, parseCSV };
|
package/dist/emitter.d.mts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
type EventType = string | symbol;
|
|
2
|
-
type Handler<T = unknown> = (event: T) => void;
|
|
3
|
-
type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
|
|
4
|
-
type EventHandlerList<T = unknown> = Handler<T>[];
|
|
5
|
-
type WildCardEventHandlerList<T = Record<string, unknown>> = WildcardHandler<T>[];
|
|
6
|
-
type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | '*', EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
|
|
7
|
-
interface Emitter<Events extends Record<EventType, unknown>> {
|
|
8
|
-
events: EventHandlerMap<Events>;
|
|
9
|
-
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
|
|
10
|
-
on(type: '*', handler: WildcardHandler<Events>): void;
|
|
11
|
-
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void;
|
|
12
|
-
off(type: '*', handler: WildcardHandler<Events>): void;
|
|
13
|
-
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
|
|
14
|
-
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Simple functional event emitter / pubsub.
|
|
18
|
-
*
|
|
19
|
-
* @remarks Ported from `mitt`.
|
|
20
|
-
* @see https://github.com/developit/mitt
|
|
21
|
-
*/
|
|
22
|
-
declare function createEmitter<Events extends Record<EventType, unknown>>(events?: EventHandlerMap<Events>): Emitter<Events>;
|
|
23
|
-
|
|
24
|
-
export { type Emitter, type EventHandlerList, type EventHandlerMap, type EventType, type Handler, type WildCardEventHandlerList, type WildcardHandler, createEmitter };
|
package/dist/emitter.mjs
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
function createEmitter(events) {
|
|
2
|
-
events ||= /* @__PURE__ */ new Map();
|
|
3
|
-
return {
|
|
4
|
-
/**
|
|
5
|
-
* A Map of event names to registered handler functions.
|
|
6
|
-
*/
|
|
7
|
-
events,
|
|
8
|
-
/**
|
|
9
|
-
* Register an event handler for the given type.
|
|
10
|
-
*
|
|
11
|
-
* @memberOf createEmitter
|
|
12
|
-
*/
|
|
13
|
-
on(type, handler) {
|
|
14
|
-
const handlers = events.get(type);
|
|
15
|
-
if (handlers) {
|
|
16
|
-
handlers.push(handler);
|
|
17
|
-
} else {
|
|
18
|
-
events.set(type, [handler]);
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
/**
|
|
22
|
-
* Remove an event handler for the given type.
|
|
23
|
-
*
|
|
24
|
-
* @remarks
|
|
25
|
-
* If `handler` is omitted, all handlers of the given type are removed.
|
|
26
|
-
*
|
|
27
|
-
* @memberOf createEmitter
|
|
28
|
-
*/
|
|
29
|
-
off(type, handler) {
|
|
30
|
-
const handlers = events.get(type);
|
|
31
|
-
if (handlers) {
|
|
32
|
-
if (handler) {
|
|
33
|
-
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
|
|
34
|
-
} else {
|
|
35
|
-
events.set(type, []);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
/**
|
|
40
|
-
* Invoke all handlers for the given type.
|
|
41
|
-
*
|
|
42
|
-
* @remarks
|
|
43
|
-
* If present, `'*'` handlers are invoked after type-matched handlers.
|
|
44
|
-
* Manually firing '*' handlers is not supported.
|
|
45
|
-
*
|
|
46
|
-
* @memberOf createEmitter
|
|
47
|
-
*/
|
|
48
|
-
emit(type, evt) {
|
|
49
|
-
let handlers = events.get(type);
|
|
50
|
-
if (handlers) {
|
|
51
|
-
for (const handler of [...handlers])
|
|
52
|
-
handler(evt);
|
|
53
|
-
}
|
|
54
|
-
handlers = events.get("*");
|
|
55
|
-
if (handlers) {
|
|
56
|
-
for (const handler of [...handlers])
|
|
57
|
-
handler(type, evt);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export { createEmitter };
|
package/dist/index.d.mts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export { MaybeArray, toArray } from './array.mjs';
|
|
2
|
-
export { CSVRow, createCSV, escapeCSVValue, parseCSV } from './csv.mjs';
|
|
3
|
-
export { Emitter, EventHandlerList, EventHandlerMap, EventType, Handler, WildCardEventHandlerList, WildcardHandler, createEmitter } from './emitter.mjs';
|
|
4
|
-
export { cloneJSON, tryParseJSON } from './json.mjs';
|
|
5
|
-
export { lazy } from './lazy.mjs';
|
|
6
|
-
export { interopDefault } from './module.mjs';
|
|
7
|
-
export { deepApply, objectEntries, objectKeys } from './object.mjs';
|
|
8
|
-
export { QueryObject, QueryValue, getPathname, joinURL, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutLeadingSlash, withoutTrailingSlash } from './path.mjs';
|
|
9
|
-
export { generateRandomId, template } from './string.mjs';
|
|
10
|
-
export { AutocompletableString, LooseAutocomplete, UnifyIntersection } from './types.mjs';
|
package/dist/index.mjs
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export { toArray } from './array.mjs';
|
|
2
|
-
export { createCSV, escapeCSVValue, parseCSV } from './csv.mjs';
|
|
3
|
-
export { createEmitter } from './emitter.mjs';
|
|
4
|
-
export { cloneJSON, tryParseJSON } from './json.mjs';
|
|
5
|
-
export { lazy } from './lazy.mjs';
|
|
6
|
-
export { interopDefault } from './module.mjs';
|
|
7
|
-
export { deepApply, objectEntries, objectKeys } from './object.mjs';
|
|
8
|
-
export { getPathname, joinURL, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutLeadingSlash, withoutTrailingSlash } from './path.mjs';
|
|
9
|
-
export { generateRandomId, template } from './string.mjs';
|
package/dist/json.d.mts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Type-safe wrapper around `JSON.stringify`.
|
|
3
|
-
*
|
|
4
|
-
* @remarks
|
|
5
|
-
* Falls back to the original value if the JSON serialization fails or the value is not a string.
|
|
6
|
-
*/
|
|
7
|
-
declare function tryParseJSON<T = unknown>(value: unknown): T;
|
|
8
|
-
/**
|
|
9
|
-
* Clones the given JSON value.
|
|
10
|
-
*
|
|
11
|
-
* @remarks
|
|
12
|
-
* The value must not contain circular references as JSON does not support them. It also must contain JSON serializable values.
|
|
13
|
-
*/
|
|
14
|
-
declare function cloneJSON<T>(value: T): T;
|
|
15
|
-
|
|
16
|
-
export { cloneJSON, tryParseJSON };
|
package/dist/json.mjs
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
function tryParseJSON(value) {
|
|
2
|
-
if (typeof value !== "string") {
|
|
3
|
-
return value;
|
|
4
|
-
}
|
|
5
|
-
try {
|
|
6
|
-
return JSON.parse(value);
|
|
7
|
-
} catch {
|
|
8
|
-
return value;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
function cloneJSON(value) {
|
|
12
|
-
if (typeof value !== "object" || value === null) {
|
|
13
|
-
return value;
|
|
14
|
-
}
|
|
15
|
-
if (Array.isArray(value)) {
|
|
16
|
-
return value.map((e) => typeof e !== "object" || e === null ? e : cloneJSON(e));
|
|
17
|
-
}
|
|
18
|
-
const result = {};
|
|
19
|
-
for (const k in value) {
|
|
20
|
-
const v = value[k];
|
|
21
|
-
result[k] = typeof v !== "object" || v === null ? v : cloneJSON(v);
|
|
22
|
-
}
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export { cloneJSON, tryParseJSON };
|
package/dist/lazy.d.mts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A simple general purpose memoizer utility.
|
|
3
|
-
* - Lazily computes a value when accessed
|
|
4
|
-
* - Auto-caches the result by overwriting the getter
|
|
5
|
-
* - Typesafe
|
|
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
|
-
declare function lazy<T>(getter: () => T): {
|
|
17
|
-
value: T;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export { lazy };
|
package/dist/lazy.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A simple general purpose memoizer utility.
|
|
3
|
-
* - Lazily computes a value when accessed
|
|
4
|
-
* - Auto-caches the result by overwriting the getter
|
|
5
|
-
* - Typesafe
|
|
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
|
-
declare function lazy<T>(getter: () => T): {
|
|
17
|
-
value: T;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export { lazy };
|
package/dist/lazy.mjs
DELETED
package/dist/module.d.mts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Interop helper for default exports.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* const mod = await interopDefault(import('./module.js'))
|
|
6
|
-
*/
|
|
7
|
-
declare function interopDefault<T>(m: T | Promise<T>): Promise<T extends {
|
|
8
|
-
default: infer U;
|
|
9
|
-
} ? U : T>;
|
|
10
|
-
|
|
11
|
-
export { interopDefault };
|
package/dist/module.mjs
DELETED
package/dist/object.d.mts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Strictly typed `Object.keys`.
|
|
3
|
-
*/
|
|
4
|
-
declare function objectKeys<T extends Record<any, any>>(obj: T): Array<`${keyof T & (string | number | boolean | null | undefined)}`>;
|
|
5
|
-
/**
|
|
6
|
-
* Strictly typed `Object.entries`.
|
|
7
|
-
*/
|
|
8
|
-
declare function objectEntries<T extends Record<any, any>>(obj: T): Array<[keyof T, T[keyof T]]>;
|
|
9
|
-
/**
|
|
10
|
-
* Deeply applies a callback to every key-value pair in the given object, as well as nested objects and arrays.
|
|
11
|
-
*/
|
|
12
|
-
declare function deepApply<T extends Record<any, any>>(data: T, callback: (item: T, key: keyof T, value: T[keyof T]) => void): void;
|
|
13
|
-
|
|
14
|
-
export { deepApply, objectEntries, objectKeys };
|
package/dist/object.mjs
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
function objectKeys(obj) {
|
|
2
|
-
return Object.keys(obj);
|
|
3
|
-
}
|
|
4
|
-
function objectEntries(obj) {
|
|
5
|
-
return Object.entries(obj);
|
|
6
|
-
}
|
|
7
|
-
function deepApply(data, callback) {
|
|
8
|
-
for (const [key, value] of Object.entries(data)) {
|
|
9
|
-
callback(data, key, value);
|
|
10
|
-
if (Array.isArray(value)) {
|
|
11
|
-
for (const element of value) {
|
|
12
|
-
if (isObject(element)) {
|
|
13
|
-
deepApply(element, callback);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
} else if (isObject(value)) {
|
|
17
|
-
deepApply(value, callback);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
function isObject(value) {
|
|
22
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export { deepApply, objectEntries, objectKeys };
|
package/dist/path.d.mts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
type QueryValue = string | number | boolean | QueryValue[] | Record<string, any> | null | undefined;
|
|
2
|
-
type QueryObject = Record<string, QueryValue | QueryValue[]>;
|
|
3
|
-
/**
|
|
4
|
-
* Removes the leading slash from the given path if it has one.
|
|
5
|
-
*/
|
|
6
|
-
declare function withoutLeadingSlash(path?: string): string;
|
|
7
|
-
/**
|
|
8
|
-
* Adds a leading slash to the given path if it does not already have one.
|
|
9
|
-
*/
|
|
10
|
-
declare function withLeadingSlash(path?: string): string;
|
|
11
|
-
/**
|
|
12
|
-
* Removes the trailing slash from the given path if it has one.
|
|
13
|
-
*/
|
|
14
|
-
declare function withoutTrailingSlash(path?: string): string;
|
|
15
|
-
/**
|
|
16
|
-
* Adds a trailing slash to the given path if it does not already have one
|
|
17
|
-
*/
|
|
18
|
-
declare function withTrailingSlash(path?: string): string;
|
|
19
|
-
/**
|
|
20
|
-
* Joins the given URL path segments, ensuring that there is only one slash between them.
|
|
21
|
-
*/
|
|
22
|
-
declare function joinURL(...paths: (string | undefined)[]): string;
|
|
23
|
-
/**
|
|
24
|
-
* Adds the base path to the input path, if it is not already present.
|
|
25
|
-
*/
|
|
26
|
-
declare function withBase(input?: string, base?: string): string;
|
|
27
|
-
/**
|
|
28
|
-
* Removes the base path from the input path, if it is present.
|
|
29
|
-
*/
|
|
30
|
-
declare function withoutBase(input?: string, base?: string): string;
|
|
31
|
-
/**
|
|
32
|
-
* Returns the pathname of the given path, which is the path without the query string.
|
|
33
|
-
*/
|
|
34
|
-
declare function getPathname(path?: string): string;
|
|
35
|
-
/**
|
|
36
|
-
* Returns the URL with the given query parameters. If a query parameter is undefined, it is omitted.
|
|
37
|
-
*/
|
|
38
|
-
declare function withQuery(input: string, query?: QueryObject): string;
|
|
39
|
-
|
|
40
|
-
export { type QueryObject, type QueryValue, getPathname, joinURL, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutLeadingSlash, withoutTrailingSlash };
|
package/dist/path.mjs
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
function withoutLeadingSlash(path) {
|
|
2
|
-
if (!path || path === "/") {
|
|
3
|
-
return "/";
|
|
4
|
-
}
|
|
5
|
-
return path[0] === "/" ? path.slice(1) : path;
|
|
6
|
-
}
|
|
7
|
-
function withLeadingSlash(path) {
|
|
8
|
-
if (!path || path === "/") {
|
|
9
|
-
return "/";
|
|
10
|
-
}
|
|
11
|
-
return path[0] === "/" ? path : `/${path}`;
|
|
12
|
-
}
|
|
13
|
-
function withoutTrailingSlash(path) {
|
|
14
|
-
if (!path || path === "/") {
|
|
15
|
-
return "/";
|
|
16
|
-
}
|
|
17
|
-
return path[path.length - 1] === "/" ? path.slice(0, -1) : path;
|
|
18
|
-
}
|
|
19
|
-
function withTrailingSlash(path) {
|
|
20
|
-
if (!path || path === "/") {
|
|
21
|
-
return "/";
|
|
22
|
-
}
|
|
23
|
-
return path[path.length - 1] === "/" ? path : `${path}/`;
|
|
24
|
-
}
|
|
25
|
-
function joinURL(...paths) {
|
|
26
|
-
let result = "";
|
|
27
|
-
for (let i = 0; i < paths.length; i++) {
|
|
28
|
-
const path = paths[i];
|
|
29
|
-
if (!result || result === "/") {
|
|
30
|
-
result = path || "/";
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
if (!path || path === "/") {
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
const resultHasTrailing = result[result.length - 1] === "/";
|
|
37
|
-
const pathHasLeading = path[0] === "/";
|
|
38
|
-
if (resultHasTrailing && pathHasLeading) {
|
|
39
|
-
result += path.slice(1);
|
|
40
|
-
} else if (!resultHasTrailing && !pathHasLeading) {
|
|
41
|
-
result += `/${path}`;
|
|
42
|
-
} else {
|
|
43
|
-
result += path;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return result;
|
|
47
|
-
}
|
|
48
|
-
function withBase(input = "", base = "") {
|
|
49
|
-
if (!base || base === "/") {
|
|
50
|
-
return input;
|
|
51
|
-
}
|
|
52
|
-
const _base = withoutTrailingSlash(base);
|
|
53
|
-
if (input.startsWith(_base)) {
|
|
54
|
-
return input;
|
|
55
|
-
}
|
|
56
|
-
return joinURL(_base, input);
|
|
57
|
-
}
|
|
58
|
-
function withoutBase(input = "", base = "") {
|
|
59
|
-
if (!base || base === "/") {
|
|
60
|
-
return input;
|
|
61
|
-
}
|
|
62
|
-
const _base = withoutTrailingSlash(base);
|
|
63
|
-
if (!input.startsWith(_base)) {
|
|
64
|
-
return input;
|
|
65
|
-
}
|
|
66
|
-
const trimmed = input.slice(_base.length);
|
|
67
|
-
return trimmed[0] === "/" ? trimmed : `/${trimmed}`;
|
|
68
|
-
}
|
|
69
|
-
function getPathname(path = "/") {
|
|
70
|
-
return path.startsWith("/") ? path.split("?")[0] : new URL(path, "http://localhost").pathname;
|
|
71
|
-
}
|
|
72
|
-
function withQuery(input, query) {
|
|
73
|
-
if (!query || Object.keys(query).length === 0) {
|
|
74
|
-
return input;
|
|
75
|
-
}
|
|
76
|
-
const searchIndex = input.indexOf("?");
|
|
77
|
-
const hasExistingParams = searchIndex !== -1;
|
|
78
|
-
const base = hasExistingParams ? input.slice(0, searchIndex) : input;
|
|
79
|
-
const searchParams = hasExistingParams ? new URLSearchParams(input.slice(searchIndex + 1)) : new URLSearchParams();
|
|
80
|
-
for (const [key, value] of Object.entries(query)) {
|
|
81
|
-
if (value === void 0) {
|
|
82
|
-
searchParams.delete(key);
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
if (Array.isArray(value)) {
|
|
86
|
-
if (value.length === 0)
|
|
87
|
-
continue;
|
|
88
|
-
for (const item of value) {
|
|
89
|
-
if (item !== void 0) {
|
|
90
|
-
searchParams.append(key, normalizeQueryValue(item));
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
searchParams.set(key, normalizeQueryValue(value));
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
const queryString = searchParams.toString();
|
|
98
|
-
return queryString ? `${base}?${queryString}` : base;
|
|
99
|
-
}
|
|
100
|
-
function normalizeQueryValue(value) {
|
|
101
|
-
if (value === null) {
|
|
102
|
-
return "";
|
|
103
|
-
}
|
|
104
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
105
|
-
return String(value);
|
|
106
|
-
}
|
|
107
|
-
if (typeof value === "object") {
|
|
108
|
-
return JSON.stringify(value);
|
|
109
|
-
}
|
|
110
|
-
return String(value);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export { getPathname, joinURL, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutLeadingSlash, withoutTrailingSlash };
|
package/dist/string.d.mts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Simple template engine to replace variables in a string.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* const str = 'Hello, {name}!'
|
|
6
|
-
* const variables = { name: 'world' }
|
|
7
|
-
*
|
|
8
|
-
* console.log(template(str, variables)) // Hello, world!
|
|
9
|
-
*/
|
|
10
|
-
declare function template(str: string, variables: Record<string | number, any>, fallback?: string | ((key: string) => string)): string;
|
|
11
|
-
/**
|
|
12
|
-
* Generates a random string.
|
|
13
|
-
*
|
|
14
|
-
* @remarks Ported from `nanoid`.
|
|
15
|
-
* @see https://github.com/ai/nanoid
|
|
16
|
-
*/
|
|
17
|
-
declare function generateRandomId(size?: number, dict?: string): string;
|
|
18
|
-
|
|
19
|
-
export { generateRandomId, template };
|
package/dist/string.mjs
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
const URL_ALPHABET = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
2
|
-
function template(str, variables, fallback) {
|
|
3
|
-
return str.replace(/\{(\w+)\}/g, (_, key) => {
|
|
4
|
-
return variables[key] || ((typeof fallback === "function" ? fallback(key) : fallback) ?? key);
|
|
5
|
-
});
|
|
6
|
-
}
|
|
7
|
-
function generateRandomId(size = 16, dict = URL_ALPHABET) {
|
|
8
|
-
let id = "";
|
|
9
|
-
let i = size;
|
|
10
|
-
const len = dict.length;
|
|
11
|
-
while (i--)
|
|
12
|
-
id += dict[Math.random() * len | 0];
|
|
13
|
-
return id;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export { generateRandomId, template };
|
package/dist/types.d.mts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
type AutocompletableString = string & {};
|
|
2
|
-
type LooseAutocomplete<T extends string> = T | AutocompletableString;
|
|
3
|
-
/** Also commonly referred to as `Prettify` */
|
|
4
|
-
type UnifyIntersection<T> = {
|
|
5
|
-
[K in keyof T]: T[K];
|
|
6
|
-
} & {};
|
|
7
|
-
|
|
8
|
-
export type { AutocompletableString, LooseAutocomplete, UnifyIntersection };
|
package/dist/types.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|