utilful 1.0.0 → 1.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/LICENSE +21 -0
- package/README.md +378 -0
- package/dist/array.d.mts +10 -0
- package/dist/array.d.ts +10 -0
- package/dist/array.mjs +6 -0
- package/dist/csv.d.mts +65 -0
- package/dist/csv.d.ts +65 -0
- package/dist/csv.mjs +76 -0
- package/dist/emitter.d.mts +72 -0
- package/dist/emitter.d.ts +72 -0
- package/dist/emitter.mjs +18 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.mjs +9 -0
- package/dist/json.d.mts +16 -0
- package/dist/json.d.ts +16 -0
- package/dist/json.mjs +26 -0
- package/dist/lazy.d.mts +20 -0
- package/dist/lazy.d.ts +20 -0
- package/dist/lazy.mjs +11 -0
- package/dist/module.d.mts +11 -0
- package/dist/module.d.ts +11 -0
- package/dist/module.mjs +6 -0
- package/dist/object.d.mts +14 -0
- package/dist/object.d.ts +14 -0
- package/dist/object.mjs +25 -0
- package/dist/path.d.mts +40 -0
- package/dist/path.d.ts +40 -0
- package/dist/path.mjs +113 -0
- package/dist/string.d.mts +19 -0
- package/dist/string.d.ts +19 -0
- package/dist/string.mjs +16 -0
- package/dist/types.d.mts +8 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.mjs +1 -0
- package/package.json +85 -6
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
interface EventsMap {
|
|
2
|
+
[event: string]: any;
|
|
3
|
+
}
|
|
4
|
+
interface DefaultEvents extends EventsMap {
|
|
5
|
+
[event: string]: (...args: any) => void;
|
|
6
|
+
}
|
|
7
|
+
interface Unsubscribe {
|
|
8
|
+
(): void;
|
|
9
|
+
}
|
|
10
|
+
interface Emitter<Events extends EventsMap = DefaultEvents> {
|
|
11
|
+
/**
|
|
12
|
+
* Calls each of the listeners registered for a given event.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* emitter.emit('tick', tickType, tickDuration)
|
|
16
|
+
*
|
|
17
|
+
* @param event The event name.
|
|
18
|
+
* @param args The arguments for listeners.
|
|
19
|
+
*/
|
|
20
|
+
emit: <K extends keyof Events>(this: this, event: K, ...args: Parameters<Events[K]>) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Event names in keys and arrays with listeners in values.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* emitter1.events = emitter2.events
|
|
26
|
+
* emitter2.events = { }
|
|
27
|
+
*/
|
|
28
|
+
events: Partial<{
|
|
29
|
+
[E in keyof Events]: Events[E][];
|
|
30
|
+
}>;
|
|
31
|
+
/**
|
|
32
|
+
* Add a listener for a given event.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const unbind = emitter.on('tick', (tickType, tickDuration) => {
|
|
36
|
+
* count += 1
|
|
37
|
+
* })
|
|
38
|
+
*
|
|
39
|
+
* disable () {
|
|
40
|
+
* unbind()
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* @param event The event name.
|
|
44
|
+
* @param cb The listener function.
|
|
45
|
+
* @returns Unbind listener from event.
|
|
46
|
+
*/
|
|
47
|
+
on: <K extends keyof Events>(this: this, event: K, cb: Events[K]) => Unsubscribe;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create event emitter.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* import { createEmitter } from 'nanoevents'
|
|
54
|
+
*
|
|
55
|
+
* class Ticker {
|
|
56
|
+
* constructor() {
|
|
57
|
+
* this.emitter = createEmitter()
|
|
58
|
+
* }
|
|
59
|
+
* on(...args) {
|
|
60
|
+
* return this.emitter.on(...args)
|
|
61
|
+
* }
|
|
62
|
+
* tick() {
|
|
63
|
+
* this.emitter.emit('tick')
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* @remarks Ported from `nanoevents`.
|
|
68
|
+
* @see https://github.com/ai/nanoevents
|
|
69
|
+
*/
|
|
70
|
+
declare function createEmitter<Events extends EventsMap = DefaultEvents>(): Emitter<Events>;
|
|
71
|
+
|
|
72
|
+
export { type Emitter, createEmitter };
|
package/dist/emitter.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
function createEmitter() {
|
|
2
|
+
return {
|
|
3
|
+
emit(event, ...args) {
|
|
4
|
+
for (let callbacks = this.events[event] || [], i = 0, length = callbacks.length; i < length; i++) {
|
|
5
|
+
callbacks[i](...args);
|
|
6
|
+
}
|
|
7
|
+
},
|
|
8
|
+
events: {},
|
|
9
|
+
on(event, cb) {
|
|
10
|
+
(this.events[event] ||= []).push(cb);
|
|
11
|
+
return () => {
|
|
12
|
+
this.events[event] = this.events[event]?.filter((i) => cb !== i);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { createEmitter };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { MaybeArray, toArray } from './array.mjs';
|
|
2
|
+
export { CSVRow, createCSV, escapeCSVValue, parseCSV } from './csv.mjs';
|
|
3
|
+
export { Emitter, 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.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { MaybeArray, toArray } from './array.js';
|
|
2
|
+
export { CSVRow, createCSV, escapeCSVValue, parseCSV } from './csv.js';
|
|
3
|
+
export { Emitter, createEmitter } from './emitter.js';
|
|
4
|
+
export { cloneJSON, tryParseJSON } from './json.js';
|
|
5
|
+
export { lazy } from './lazy.js';
|
|
6
|
+
export { interopDefault } from './module.js';
|
|
7
|
+
export { deepApply, objectEntries, objectKeys } from './object.js';
|
|
8
|
+
export { QueryObject, QueryValue, getPathname, joinURL, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutLeadingSlash, withoutTrailingSlash } from './path.js';
|
|
9
|
+
export { generateRandomId, template } from './string.js';
|
|
10
|
+
export { AutocompletableString, LooseAutocomplete, UnifyIntersection } from './types.js';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
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 };
|
|
@@ -0,0 +1,19 @@
|
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|