@pivanov/utils 0.0.3 → 1.0.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 +95 -340
- package/dist/cjs/assertion/index.js +7 -0
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/object/index.js +7 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/promise/index.js +7 -0
- package/dist/cjs/string/index.js +7 -0
- package/dist/cjs/tools/index.js +7 -0
- package/dist/cjs/types/index.js +7 -0
- package/dist/esm/assertion/index.js +7 -0
- package/dist/esm/chunk-1rn730je.js +7 -0
- package/dist/esm/chunk-5nmphgya.js +8 -0
- package/dist/esm/chunk-bqewq152.js +8 -0
- package/dist/esm/chunk-f1nddzrj.js +6 -0
- package/dist/esm/chunk-hksj0qca.js +8 -0
- package/dist/esm/chunk-tdt3f0jc.js +8 -0
- package/dist/esm/index.js +2 -2
- package/dist/esm/object/index.js +7 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/promise/index.js +7 -0
- package/dist/esm/string/index.js +7 -0
- package/dist/esm/tools/index.js +7 -0
- package/dist/esm/types/index.js +7 -0
- package/dist/types/assertion/index.d.ts +145 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/object/index.d.ts +118 -0
- package/dist/types/promise/index.d.ts +81 -0
- package/dist/types/string/index.d.ts +127 -0
- package/dist/types/tools/cache-api/index.d.ts +75 -0
- package/dist/types/tools/deepClone.d.ts +2 -0
- package/dist/types/tools/dom.d.ts +82 -0
- package/dist/types/tools/eventBus/eventBus.d.ts +37 -0
- package/dist/types/tools/eventBus/index.d.ts +3 -0
- package/dist/types/tools/eventBus/types.d.ts +47 -0
- package/dist/types/tools/eventBus/useEventBus.d.ts +3 -0
- package/dist/types/tools/index.d.ts +5 -0
- package/dist/types/tools/isEqual.d.ts +21 -0
- package/dist/types/types/index.d.ts +61 -0
- package/package.json +65 -35
- package/dist/index.d.ts +0 -691
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a string to camelCase.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* camelCase('foo-bar'); // 'fooBar'
|
|
7
|
+
* camelCase('FOO_BAR'); // 'fooBar'
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare const camelCase: (str: string) => string;
|
|
11
|
+
/**
|
|
12
|
+
* Converts a string to PascalCase.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* pascalCase('foo-bar'); // 'FooBar'
|
|
17
|
+
* pascalCase('foo123bar'); // 'Foo123Bar'
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare const pascalCase: (str: string) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Capitalizes the first character of a string (runtime).
|
|
23
|
+
* For TypeScript literal-type preservation, use `capitalize` instead.
|
|
24
|
+
*/
|
|
25
|
+
export declare const capitalizeFirstLetter: (string: string) => string;
|
|
26
|
+
/**
|
|
27
|
+
* Converts a string to kebab-case.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* kebabCase('fooBar'); // 'foo-bar'
|
|
32
|
+
* kebabCase('XMLHttpRequest'); // 'xml-http-request'
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare const kebabCase: (str: string) => string;
|
|
36
|
+
/**
|
|
37
|
+
* Converts a string to snake_case.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* snakeCase('fooBar'); // 'foo_bar'
|
|
42
|
+
* snakeCase('XMLHttpRequest'); // 'xml_http_request'
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare const snakeCase: (str: string) => string;
|
|
46
|
+
/**
|
|
47
|
+
* Converts a string to Title Case - each word capitalized, separators
|
|
48
|
+
* normalized to single spaces.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* titleCase('hello world'); // 'Hello World'
|
|
53
|
+
* titleCase('foo-bar_baz'); // 'Foo Bar Baz'
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare const titleCase: (str: string) => string;
|
|
57
|
+
/**
|
|
58
|
+
* Converts a string into a URL-friendly slug. More aggressive than
|
|
59
|
+
* `kebabCase` - strips all non-ASCII-word characters.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* slugify('Hello World!'); // 'hello-world'
|
|
64
|
+
* slugify('Über Café'); // 'uber-cafe'
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare const slugify: (str: string) => string;
|
|
68
|
+
/**
|
|
69
|
+
* Capitalizes the first character; preserves TypeScript literal types.
|
|
70
|
+
*/
|
|
71
|
+
export declare const capitalize: <S extends string>(str: S) => Capitalize<S>;
|
|
72
|
+
/**
|
|
73
|
+
* Lower-cases the first character; preserves TypeScript literal types.
|
|
74
|
+
*/
|
|
75
|
+
export declare const uncapitalize: <S extends string>(str: S) => Uncapitalize<S>;
|
|
76
|
+
/**
|
|
77
|
+
* Truncates a string to `maxLength` characters, appending an ellipsis
|
|
78
|
+
* (default `…`) if truncation happened. The ellipsis is included in
|
|
79
|
+
* the final length.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```ts
|
|
83
|
+
* truncate('Hello, world!', 8); // 'Hello, …'
|
|
84
|
+
* truncate('Hello, world!', 8, '...'); // 'Hello...'
|
|
85
|
+
* truncate('Short', 20); // 'Short'
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare const truncate: (str: string, maxLength: number, ellipsis?: string) => string;
|
|
89
|
+
/**
|
|
90
|
+
* Escapes HTML special characters for safe interpolation into markup.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* escapeHtml('<script>alert(1)</script>');
|
|
95
|
+
* // '<script>alert(1)</script>'
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare const escapeHtml: (str: string) => string;
|
|
99
|
+
/**
|
|
100
|
+
* Escapes characters that have special meaning in a regular expression so the
|
|
101
|
+
* string can be safely embedded as a literal match.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* new RegExp(escapeRegExp('a.b*c')); // matches the literal "a.b*c"
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare const escapeRegExp: (str: string) => string;
|
|
109
|
+
/**
|
|
110
|
+
* Splits a string into words by whitespace, dashes, and underscores.
|
|
111
|
+
* Preserves case; filters out empty segments.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* words('hello_world-foo bar'); // ['hello', 'world', 'foo', 'bar']
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export declare const words: (str: string) => string[];
|
|
119
|
+
/**
|
|
120
|
+
* Splits a string by line breaks (`\r\n`, `\n`, or `\r`).
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* lines('a\nb\r\nc'); // ['a', 'b', 'c']
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export declare const lines: (str: string) => string[];
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `JSON.stringify` replacer that converts `BigInt` values to strings.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* JSON.stringify({ id: 9007199254740993n }, stringifyBigIntValues);
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
export declare const stringifyBigIntValues: (_key: string, value: unknown) => unknown;
|
|
10
|
+
/**
|
|
11
|
+
* Stores a JSON-serializable value in the browser Cache API.
|
|
12
|
+
*
|
|
13
|
+
* Note: values are serialized via `JSON.stringify`. `Date`, `Map`, `Set`,
|
|
14
|
+
* `undefined`, and `Symbol` values are lossy. `BigInt` is auto-stringified.
|
|
15
|
+
*/
|
|
16
|
+
export declare const storageSetItem: (cacheName: string, key: string, value: unknown) => Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves a value from the Cache API. Returns `null` if not found.
|
|
19
|
+
*/
|
|
20
|
+
export declare const storageGetItem: <T>(cacheName: string, key: string) => Promise<T | null>;
|
|
21
|
+
/**
|
|
22
|
+
* Stores a value with a TTL (time-to-live in milliseconds). After the TTL
|
|
23
|
+
* elapses, reads via `storageGetItemWithTTL` will return `null` and delete
|
|
24
|
+
* the expired entry.
|
|
25
|
+
*
|
|
26
|
+
* Wire format is a self-describing envelope: `{ __ttl: true, v, exp }`.
|
|
27
|
+
* Entries stored this way are only correctly read via the `WithTTL` variants.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* await storageSetItemWithTTL('my-cache', 'token', 'abc123', 60_000);
|
|
32
|
+
* const token = await storageGetItemWithTTL<string>('my-cache', 'token');
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare const storageSetItemWithTTL: (cacheName: string, key: string, value: unknown, ttlMs: number) => Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Reads a value previously stored with `storageSetItemWithTTL`. Returns
|
|
38
|
+
* `null` if absent or expired; expired entries are deleted.
|
|
39
|
+
*/
|
|
40
|
+
export declare const storageGetItemWithTTL: <T>(cacheName: string, key: string) => Promise<T | null>;
|
|
41
|
+
/**
|
|
42
|
+
* Removes a single key. Returns `true` if the key existed and was deleted.
|
|
43
|
+
*/
|
|
44
|
+
export declare const storageRemoveItem: (cacheName: string, key: string) => Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Clears every entry in the named cache.
|
|
47
|
+
*/
|
|
48
|
+
export declare const storageClear: (cacheName: string) => Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Clears every cache entry whose key matches `str` as prefix or suffix.
|
|
51
|
+
*
|
|
52
|
+
* @deprecated Prefer `storageClearByPrefix` / `storageClearBySuffix` for
|
|
53
|
+
* readability. This function will remain through v1.x.
|
|
54
|
+
*/
|
|
55
|
+
export declare const storageClearByPrefixOrSuffix: (cacheName: string, str: string, isPrefix?: boolean) => Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Clears every cache entry whose key starts with `prefix`.
|
|
58
|
+
*/
|
|
59
|
+
export declare const storageClearByPrefix: (cacheName: string, prefix: string) => Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Clears every cache entry whose key ends with `suffix`.
|
|
62
|
+
*/
|
|
63
|
+
export declare const storageClearBySuffix: (cacheName: string, suffix: string) => Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Checks whether a key exists in the cache.
|
|
66
|
+
*/
|
|
67
|
+
export declare const storageExists: (cacheName: string, key: string) => Promise<boolean>;
|
|
68
|
+
/**
|
|
69
|
+
* Returns every key currently stored in the cache.
|
|
70
|
+
*/
|
|
71
|
+
export declare const storageGetAllKeys: (cacheName: string) => Promise<string[]>;
|
|
72
|
+
/**
|
|
73
|
+
* Calculates the size in bytes of the cache, or of a single entry.
|
|
74
|
+
*/
|
|
75
|
+
export declare const storageCalculateSize: (cacheName: string, cacheKey?: string) => Promise<number>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns true when running in a browser-like environment.
|
|
3
|
+
*
|
|
4
|
+
* Checks for both `window` and `document` so service-worker and
|
|
5
|
+
* partially-mocked contexts are correctly reported as non-browser.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* if (isBrowser()) window.addEventListener('resize', onResize);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare const isBrowser: () => boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Sets CSS custom properties on an element. Safely no-ops when element is null.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* setStyleProperties(el, { '--primary': '#3b82f6', '--gap': '1rem' });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare const setStyleProperties: (el: HTMLElement | null, cssVars: Record<string, string>) => void;
|
|
22
|
+
interface CheckVisibilityOptions {
|
|
23
|
+
/** Require the element to intersect the viewport. Default: true. */
|
|
24
|
+
checkViewport?: boolean;
|
|
25
|
+
/** Require computed `display` to be non-"none". Default: true. */
|
|
26
|
+
checkDisplay?: boolean;
|
|
27
|
+
/** Require computed `visibility` to be "visible". Default: true. */
|
|
28
|
+
checkVisibility?: boolean;
|
|
29
|
+
/** Require computed `opacity` to be > 0. Default: true. */
|
|
30
|
+
checkOpacity?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether an element is visible to the user.
|
|
34
|
+
*
|
|
35
|
+
* By default verifies: attached to DOM, `display` not `none`,
|
|
36
|
+
* `visibility` is `visible`, `opacity > 0`, and intersects the viewport
|
|
37
|
+
* on both axes. Each check can be toggled via options.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* if (checkVisibility(el)) el.classList.add('seen');
|
|
42
|
+
* checkVisibility(el, { checkViewport: false }); // visible per CSS only
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare const checkVisibility: (element: HTMLElement, options?: CheckVisibilityOptions) => boolean;
|
|
46
|
+
/**
|
|
47
|
+
* @internal Resets the cached canvas - for tests only.
|
|
48
|
+
*/
|
|
49
|
+
export declare const __resetTextMeasurementCache: () => void;
|
|
50
|
+
interface IViewportOptions {
|
|
51
|
+
/** Require vertical intersection. Default: true. */
|
|
52
|
+
vertical?: boolean;
|
|
53
|
+
/** Require horizontal intersection. Default: true. */
|
|
54
|
+
horizontal?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns true when the element's bounding rect intersects the viewport.
|
|
58
|
+
* Pure geometry - ignores CSS visibility. Use `checkVisibility` for a full
|
|
59
|
+
* visibility check.
|
|
60
|
+
*
|
|
61
|
+
* Zero-sized rects (no layout yet) return true - we can't clip against
|
|
62
|
+
* nothing, and failing them would produce false negatives in test environments.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* if (isInViewport(el)) track();
|
|
67
|
+
* isInViewport(el, { horizontal: false }); // vertical only
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare const isInViewport: (element: HTMLElement, options?: IViewportOptions) => boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Measures the rendered width of text in pixels using a cached off-screen
|
|
73
|
+
* canvas. Returns `0` when 2D context is unavailable.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* calculateRenderedTextWidth('Hello World', 16);
|
|
78
|
+
* calculateRenderedTextWidth('Hi', 14, true, 'Arial');
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare const calculateRenderedTextWidth: (text: string, fontSize: number, isUppercase?: boolean, fontFamily?: string) => number;
|
|
82
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { IEventBus, IEventBusSubscribeOptions, TEventBusListener, TEventBusUnsubscribe } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Dispatches a message to every subscriber on `topic`.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* busDispatch('user-updated', { id: 1, name: 'John' });
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare const busDispatch: <T extends IEventBus>(topic: T["topic"], message: T["message"]) => void;
|
|
11
|
+
/**
|
|
12
|
+
* Subscribes to messages on a specific topic. Returns an unsubscribe function.
|
|
13
|
+
*
|
|
14
|
+
* Pass `options.onError` to handle listener exceptions (default: `console.error`).
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const unsubscribe = busSubscribe('user-updated', (msg) => {
|
|
19
|
+
* console.log(msg);
|
|
20
|
+
* });
|
|
21
|
+
* unsubscribe();
|
|
22
|
+
*
|
|
23
|
+
* // Custom error handler
|
|
24
|
+
* busSubscribe('x', handler, { onError: (e) => reportBug(e) });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare const busSubscribe: <T extends IEventBus>(topic: T["topic"], listener: TEventBusListener<T["message"]>, options?: IEventBusSubscribeOptions) => TEventBusUnsubscribe;
|
|
28
|
+
/**
|
|
29
|
+
* Subscribes to a topic and automatically unsubscribes after the first
|
|
30
|
+
* matching dispatch.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* busOnce('ready', () => startApp());
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare const busOnce: <T extends IEventBus>(topic: T["topic"], listener: TEventBusListener<T["message"]>, options?: IEventBusSubscribeOptions) => TEventBusUnsubscribe;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic event-bus shape. Extend this interface for typed dispatch/subscribe.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* interface UserLoggedIn extends IEventBus<{ id: number; name: string }> {
|
|
7
|
+
* topic: 'user:logged-in';
|
|
8
|
+
* }
|
|
9
|
+
* busDispatch<UserLoggedIn>('user:logged-in', { id: 1, name: 'John' });
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export interface IEventBus<T = unknown> {
|
|
13
|
+
topic: string;
|
|
14
|
+
message: T;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Listener callback shape.
|
|
18
|
+
*/
|
|
19
|
+
export type TEventBusListener<T = unknown> = (message: T) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Function returned by `busSubscribe` that removes the subscription.
|
|
22
|
+
*/
|
|
23
|
+
export type TEventBusUnsubscribe = () => void;
|
|
24
|
+
/**
|
|
25
|
+
* Optional behavior for a subscription.
|
|
26
|
+
*/
|
|
27
|
+
export interface IEventBusSubscribeOptions {
|
|
28
|
+
/** Called with the error when the listener throws. Defaults to `console.error`. */
|
|
29
|
+
onError?: (error: unknown) => void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Type helper: given an event map, extract the set of valid topic names.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* type Events = {
|
|
37
|
+
* 'user:login': { id: number };
|
|
38
|
+
* 'user:logout': void;
|
|
39
|
+
* };
|
|
40
|
+
* type Topic = TEventTopic<Events>; // 'user:login' | 'user:logout'
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export type TEventTopic<Map> = keyof Map & string;
|
|
44
|
+
/**
|
|
45
|
+
* Type helper: given an event map and a topic, extract the message payload.
|
|
46
|
+
*/
|
|
47
|
+
export type TEventMessage<Map, Topic extends TEventTopic<Map>> = Map[Topic];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deeply compares two values for structural equality.
|
|
3
|
+
*
|
|
4
|
+
* Supports: primitives (with NaN-equals-NaN), Arrays, Sets, Maps, Dates,
|
|
5
|
+
* RegExp (source + flags), Errors (name + message), TypedArrays,
|
|
6
|
+
* ArrayBuffer/DataView (byte-wise), and plain objects. Handles circular
|
|
7
|
+
* references via cycle tracking.
|
|
8
|
+
*
|
|
9
|
+
* Sets with non-primitive members use order-independent deep comparison
|
|
10
|
+
* (O(n²) worst case).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* isEqual({ a: 1, b: 2 }, { b: 2, a: 1 }); // true
|
|
15
|
+
* isEqual([1, 2, 3], [1, 2, 3]); // true
|
|
16
|
+
* isEqual(new Set([{ id: 1 }]), new Set([{ id: 1 }])); // true
|
|
17
|
+
* isEqual(/foo/gi, /foo/gi); // true
|
|
18
|
+
* isEqual(new Uint8Array([1, 2]), new Uint8Array([1, 2])); // true
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare const isEqual: <T, K>(obj: T | T[], objToCompare: K | K[]) => boolean;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A dictionary with string keys and values of type T.
|
|
3
|
+
*/
|
|
4
|
+
export type TDict<T = unknown> = Record<string, T>;
|
|
5
|
+
/**
|
|
6
|
+
* Object with string or number keys and values of type T.
|
|
7
|
+
*/
|
|
8
|
+
export type TObjType<T = unknown> = {
|
|
9
|
+
[key: string | number]: T;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Recursively applies `Partial` to every nested object property. Primitives
|
|
13
|
+
* and arrays pass through unchanged.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* type A = DeepPartial<{ a: { b: { c: number } } }>;
|
|
18
|
+
* // { a?: { b?: { c?: number } } }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export type DeepPartial<T> = T extends object ? {
|
|
22
|
+
[K in keyof T]?: DeepPartial<T[K]>;
|
|
23
|
+
} : T;
|
|
24
|
+
/**
|
|
25
|
+
* Recursively applies `Readonly` to every nested object property.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* type Config = DeepReadonly<{ flags: { debug: boolean } }>;
|
|
30
|
+
* // { readonly flags: { readonly debug: boolean } }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export type DeepReadonly<T> = T extends object ? {
|
|
34
|
+
readonly [K in keyof T]: DeepReadonly<T[K]>;
|
|
35
|
+
} : T;
|
|
36
|
+
/**
|
|
37
|
+
* Removes `readonly` modifiers from every property (top level only).
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const tuple = [1, 2, 3] as const;
|
|
42
|
+
* type T = Mutable<typeof tuple>; // number[]
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export type Mutable<T> = {
|
|
46
|
+
-readonly [K in keyof T]: T[K];
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Flattens an intersection or mapped type into a single object literal so it
|
|
50
|
+
* shows up clean in IDE tooltips. Pure type-level; no runtime impact.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* type A = { a: number };
|
|
55
|
+
* type B = { b: string };
|
|
56
|
+
* type C = Prettify<A & B>; // { a: number; b: string }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export type Prettify<T> = {
|
|
60
|
+
[K in keyof T]: T[K];
|
|
61
|
+
} & {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pivanov/utils",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "A collection of
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A focused collection of TypeScript utilities for modern web development",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -11,6 +11,20 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/pivanov/pivanov-utils/issues"
|
|
13
13
|
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "rm -rf dist && bun run scripts/build.ts && bun x tsc --emitDeclarationOnly",
|
|
16
|
+
"test": "bun test",
|
|
17
|
+
"test:coverage": "bun test --coverage",
|
|
18
|
+
"lint": "biome lint .",
|
|
19
|
+
"format": "biome format . --write",
|
|
20
|
+
"check": "biome check . --write",
|
|
21
|
+
"typecheck": "bun x tsc --noEmit",
|
|
22
|
+
"docs:dev": "bun x vitepress dev docs",
|
|
23
|
+
"docs:build": "bun x vitepress build docs",
|
|
24
|
+
"docs:preview": "bun x vitepress preview docs",
|
|
25
|
+
"prepublishOnly": "bun run build",
|
|
26
|
+
"publish": "npm publish --access public"
|
|
27
|
+
},
|
|
14
28
|
"author": {
|
|
15
29
|
"name": "Pavel Ivanov",
|
|
16
30
|
"email": "iweb.ivanov@gmail.com",
|
|
@@ -29,14 +43,45 @@
|
|
|
29
43
|
"promise"
|
|
30
44
|
],
|
|
31
45
|
"license": "MIT",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
46
|
+
"sideEffects": false,
|
|
47
|
+
"main": "./dist/cjs/index.js",
|
|
48
|
+
"module": "./dist/esm/index.js",
|
|
49
|
+
"types": "./dist/types/index.d.ts",
|
|
35
50
|
"exports": {
|
|
36
51
|
".": {
|
|
37
|
-
"types": "./dist/index.d.ts",
|
|
52
|
+
"types": "./dist/types/index.d.ts",
|
|
38
53
|
"import": "./dist/esm/index.js",
|
|
39
54
|
"require": "./dist/cjs/index.js"
|
|
55
|
+
},
|
|
56
|
+
"./assertion": {
|
|
57
|
+
"types": "./dist/types/assertion/index.d.ts",
|
|
58
|
+
"import": "./dist/esm/assertion/index.js",
|
|
59
|
+
"require": "./dist/cjs/assertion/index.js"
|
|
60
|
+
},
|
|
61
|
+
"./object": {
|
|
62
|
+
"types": "./dist/types/object/index.d.ts",
|
|
63
|
+
"import": "./dist/esm/object/index.js",
|
|
64
|
+
"require": "./dist/cjs/object/index.js"
|
|
65
|
+
},
|
|
66
|
+
"./promise": {
|
|
67
|
+
"types": "./dist/types/promise/index.d.ts",
|
|
68
|
+
"import": "./dist/esm/promise/index.js",
|
|
69
|
+
"require": "./dist/cjs/promise/index.js"
|
|
70
|
+
},
|
|
71
|
+
"./string": {
|
|
72
|
+
"types": "./dist/types/string/index.d.ts",
|
|
73
|
+
"import": "./dist/esm/string/index.js",
|
|
74
|
+
"require": "./dist/cjs/string/index.js"
|
|
75
|
+
},
|
|
76
|
+
"./tools": {
|
|
77
|
+
"types": "./dist/types/tools/index.d.ts",
|
|
78
|
+
"import": "./dist/esm/tools/index.js",
|
|
79
|
+
"require": "./dist/cjs/tools/index.js"
|
|
80
|
+
},
|
|
81
|
+
"./types": {
|
|
82
|
+
"types": "./dist/types/types/index.d.ts",
|
|
83
|
+
"import": "./dist/esm/types/index.js",
|
|
84
|
+
"require": "./dist/cjs/types/index.js"
|
|
40
85
|
}
|
|
41
86
|
},
|
|
42
87
|
"files": [
|
|
@@ -46,42 +91,27 @@
|
|
|
46
91
|
"react": ">=18",
|
|
47
92
|
"react-dom": ">=18"
|
|
48
93
|
},
|
|
94
|
+
"peerDependenciesMeta": {
|
|
95
|
+
"react": {
|
|
96
|
+
"optional": true
|
|
97
|
+
},
|
|
98
|
+
"react-dom": {
|
|
99
|
+
"optional": true
|
|
100
|
+
}
|
|
101
|
+
},
|
|
49
102
|
"devDependencies": {
|
|
50
|
-
"@biomejs/biome": "^
|
|
51
|
-
"@
|
|
52
|
-
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
53
|
-
"@rollup/plugin-typescript": "^12.1.1",
|
|
54
|
-
"@testing-library/dom": "^10.4.0",
|
|
103
|
+
"@biomejs/biome": "^2.4.11",
|
|
104
|
+
"@happy-dom/global-registrator": "^15.11.7",
|
|
55
105
|
"@testing-library/react": "^16.0.1",
|
|
56
|
-
"@
|
|
57
|
-
"@types/node": "^22.9.0",
|
|
106
|
+
"@types/bun": "^1.1.14",
|
|
58
107
|
"@types/react": "^18.3.12",
|
|
59
108
|
"@types/react-dom": "^18.3.1",
|
|
60
|
-
"@vercel/ncc": "0.38.2",
|
|
61
|
-
"@vitest/coverage-v8": "^2.1.4",
|
|
62
|
-
"evt": "^2.4.18",
|
|
63
|
-
"glob": "^11.0.0",
|
|
64
|
-
"husky": "^4.3.0",
|
|
65
|
-
"jsdom": "^25.0.1",
|
|
66
109
|
"react": "^18.3.1",
|
|
67
110
|
"react-dom": "^18.3.1",
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"rollup-plugin-terser": "^7.0.2",
|
|
71
|
-
"tslib": "^2.7.0",
|
|
72
|
-
"typescript": "5.7.2",
|
|
73
|
-
"vitest": "^2.1.4"
|
|
111
|
+
"typescript": "^5.7.2",
|
|
112
|
+
"vitepress": "~1.5.0"
|
|
74
113
|
},
|
|
75
114
|
"publishConfig": {
|
|
76
115
|
"access": "public"
|
|
77
|
-
},
|
|
78
|
-
"scripts": {
|
|
79
|
-
"build": "rm -rf dist && pnpm rollup -c",
|
|
80
|
-
"test": "pnpm vitest",
|
|
81
|
-
"test:coverage": "pnpm vitest --coverage",
|
|
82
|
-
"test:ui": "pnpm vitest --ui",
|
|
83
|
-
"lint": "biome lint .",
|
|
84
|
-
"format": "biome format . --write",
|
|
85
|
-
"check": "biome check . --write"
|
|
86
116
|
}
|
|
87
|
-
}
|
|
117
|
+
}
|