@dvirus-js/utils 0.0.1
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/CHANGELOG.md +69 -0
- package/README.md +237 -0
- package/index.d.ts +11 -0
- package/index.js +837 -0
- package/lib/Result.d.ts +79 -0
- package/lib/brand-type/brand-factory.d.ts +39 -0
- package/lib/brand-type/index.d.ts +2 -0
- package/lib/brand-type/numeric-string.d.ts +15 -0
- package/lib/convert-cases.d.ts +15 -0
- package/lib/delay.d.ts +21 -0
- package/lib/getProp.d.ts +17 -0
- package/lib/group-by.d.ts +13 -0
- package/lib/html-text-parser/constant.d.ts +19 -0
- package/lib/html-text-parser/html-text-parser.d.ts +51 -0
- package/lib/html-text-parser/index.d.ts +2 -0
- package/lib/html-text-parser/methods.d.ts +28 -0
- package/lib/html-text-parser/types.d.ts +35 -0
- package/lib/html-text-parser___.d.ts +59 -0
- package/lib/http.d.ts +58 -0
- package/lib/signals.d.ts +72 -0
- package/lib/to-array.d.ts +1 -0
- package/lib/tryCatch.d.ts +22 -0
- package/package.json +11 -0
package/lib/Result.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class representing a result of an operation that can either be a success (ok) or a failure (err).
|
|
3
|
+
* @template T - The type of the success value.
|
|
4
|
+
* @template E - The type of the error value, extending Error.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Result<T, E extends Error = Error> {
|
|
7
|
+
#private;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a successful result.
|
|
10
|
+
* @param {T} val - The success value.
|
|
11
|
+
* @returns {Result<T, never>} A Result instance representing a success.
|
|
12
|
+
*/
|
|
13
|
+
static ok<T>(val: T): Result<T, never>;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a failed result.
|
|
16
|
+
* @param {E | string} err - The error value or message.
|
|
17
|
+
* @returns {Result<never, E>} A Result instance representing a failure.
|
|
18
|
+
*/
|
|
19
|
+
static err<E extends Error>(err: E | string): Result<never, E>;
|
|
20
|
+
/**
|
|
21
|
+
* Wraps a promise in a Result.
|
|
22
|
+
* @param {Promise<T>} promise - The promise to wrap.
|
|
23
|
+
* @returns {Promise<Result<T, E>>} A promise that resolves to a Result.
|
|
24
|
+
*/
|
|
25
|
+
static promise<T, E extends Error = Error>(promise: Promise<T>): Promise<Result<T, E>>;
|
|
26
|
+
/**
|
|
27
|
+
* Wraps a function call in a Result.
|
|
28
|
+
* @param {(...args: any[]) => T} func - The function to call.
|
|
29
|
+
* @param {...any[]} args - The arguments to pass to the function.
|
|
30
|
+
* @returns {Result<T, E>} A Result instance representing the function call result.
|
|
31
|
+
*/
|
|
32
|
+
static func<T, E extends Error = Error, TParam extends Array<unknown> = []>(func: (...args: TParam) => T, ...args: TParam): Result<T, E>;
|
|
33
|
+
/**
|
|
34
|
+
* @param {T | null} ok - The success value.
|
|
35
|
+
* @param {E | null} err - The error value.
|
|
36
|
+
* @throws {Error} If both ok and err are provided or neither is provided.
|
|
37
|
+
*/
|
|
38
|
+
constructor(ok: T | null, err: E | null);
|
|
39
|
+
/**
|
|
40
|
+
* Gets the success value, throwing an error if the result is a failure.
|
|
41
|
+
* @returns {T} The success value.
|
|
42
|
+
* @throws {Error} If the result is a failure.
|
|
43
|
+
*/
|
|
44
|
+
get value(): T;
|
|
45
|
+
/**
|
|
46
|
+
* Unwraps the result, returning the success value or throwing the error.
|
|
47
|
+
* @returns {T} The success value.
|
|
48
|
+
* @throws {E} If the result is a failure.
|
|
49
|
+
*/
|
|
50
|
+
unwrap(): T;
|
|
51
|
+
/**
|
|
52
|
+
* Unwraps the result, returning the success value or a default value if the result is a failure.
|
|
53
|
+
* @param {T} defaultValue - The default value to return if the result is a failure.
|
|
54
|
+
* @returns {T} The success value or the default value.
|
|
55
|
+
*/
|
|
56
|
+
unwrapOr(defaultValue: T): T;
|
|
57
|
+
/**
|
|
58
|
+
* Gets the success value, throwing a custom error message if the result is a failure.
|
|
59
|
+
* @param {string} msg - The custom error message.
|
|
60
|
+
* @returns {T} The success value.
|
|
61
|
+
* @throws {Error} If the result is a failure.
|
|
62
|
+
*/
|
|
63
|
+
expect(msg: string): T;
|
|
64
|
+
/**
|
|
65
|
+
* Checks if the result is a success.
|
|
66
|
+
* @returns {boolean} True if the result is a success, false otherwise.
|
|
67
|
+
*/
|
|
68
|
+
isOk(): this is Result<T, never>;
|
|
69
|
+
/**
|
|
70
|
+
* Checks if the result is a failure.
|
|
71
|
+
* @returns {boolean} True if the result is a failure, false otherwise.
|
|
72
|
+
*/
|
|
73
|
+
isErr(): this is Result<never, E>;
|
|
74
|
+
/**
|
|
75
|
+
* Gets the error value.
|
|
76
|
+
* @returns {E | null} The error value, or null if the result is a success.
|
|
77
|
+
*/
|
|
78
|
+
get error(): this extends Result<never, E> ? E : E | null;
|
|
79
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a nominally typed value by intersecting a base type with a hidden marker.
|
|
3
|
+
*
|
|
4
|
+
* @typeParam T - The underlying runtime type.
|
|
5
|
+
* @typeParam B - A unique compile-time brand marker.
|
|
6
|
+
*/
|
|
7
|
+
export type Brand<T, B> = T & {
|
|
8
|
+
readonly __brand: B;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Factory contract for creating and validating branded values.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam BrandType - The branded output type.
|
|
14
|
+
* @typeParam Param - The accepted input type.
|
|
15
|
+
*/
|
|
16
|
+
export interface BrandFactory<BrandType, Param> {
|
|
17
|
+
/**
|
|
18
|
+
* Creates a branded value or throws when the input is invalid.
|
|
19
|
+
* @throws Error if the input does not satisfy the brand's validation predicate.
|
|
20
|
+
*/
|
|
21
|
+
(value: Param): BrandType;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a branded value when valid, otherwise returns `undefined`.
|
|
24
|
+
*/
|
|
25
|
+
from(value: Param): BrandType | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Type guard that checks whether a value matches the branded type.
|
|
28
|
+
*/
|
|
29
|
+
is(value: unknown): value is BrandType;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Builds a branding factory from a runtime validation predicate.
|
|
33
|
+
*
|
|
34
|
+
* @param name - For Debugging: Human-readable brand name used in validation errors.
|
|
35
|
+
* @param testFn - Predicate that validates whether a value matches the brand.
|
|
36
|
+
* @param parseFn - Optional function to transform the input before validation (e.g. parsing).
|
|
37
|
+
* @returns A callable factory with helper methods for safe parsing and checks.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createBrand<BrandType, Param>(name: string, testFn: (value: unknown) => value is BrandType, parseFn?: (value: Param) => unknown): BrandFactory<BrandType, Param>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Brand } from './brand-factory';
|
|
2
|
+
/**
|
|
3
|
+
* A branded string type that only contains digit characters (`0-9`).
|
|
4
|
+
*
|
|
5
|
+
* At runtime this is just a plain `string` — no wrapper objects.
|
|
6
|
+
* The brand prevents accidental assignment of arbitrary strings at compile time.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const id: NumericString = NumericString('123'); // ok
|
|
11
|
+
* const id: NumericString = '123'; // compile error
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export type NumericString = Brand<string, 'NumericString'>;
|
|
15
|
+
export declare const NumericString: import('./brand-factory').BrandFactory<NumericString, string | number>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes any string to lowercase with spaces
|
|
3
|
+
* Handles various cases like special characters, extra spaces, and different formats
|
|
4
|
+
* @param input - The string to normalize
|
|
5
|
+
* @returns A normalized string in lowercase with single spaces
|
|
6
|
+
*/
|
|
7
|
+
export declare function normalizeString(input: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Converts a string to various cases
|
|
10
|
+
* @param input - The string to convert
|
|
11
|
+
* @param caseType - The target case type
|
|
12
|
+
* @returns The string converted to the specified case
|
|
13
|
+
*/
|
|
14
|
+
export type CaseType = 'lowercase' | 'UPPERCASE' | 'Title Case' | 'kebab-case' | 'snake_case' | 'camelCase' | 'PascalCase' | 'dot.case' | 'path/case' | 'Sentence case' | 'Header-Case' | 'reverse';
|
|
15
|
+
export declare function convertCase(input: string, caseType: CaseType): string;
|
package/lib/delay.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delay for a given time
|
|
3
|
+
* @param ms - Time in milliseconds
|
|
4
|
+
* @returns Promise
|
|
5
|
+
*/
|
|
6
|
+
export declare function delay(ms: number): Promise<unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* Clamp a value between a minimum and maximum value
|
|
9
|
+
* @param min - Minimum value
|
|
10
|
+
* @param value - Value
|
|
11
|
+
* @param max - Maximum value
|
|
12
|
+
* @returns Clamped value
|
|
13
|
+
*/
|
|
14
|
+
export declare function clamp(min: number, value: number, max: number): number;
|
|
15
|
+
/**
|
|
16
|
+
* Debounce a function
|
|
17
|
+
* @param func - Function to debounce
|
|
18
|
+
* @param delay - Delay in milliseconds
|
|
19
|
+
* @returns Debounced function
|
|
20
|
+
*/
|
|
21
|
+
export declare function debounce<T extends (...args: any[]) => void>(func: T, delay: number): (...args: Parameters<T>) => void;
|
package/lib/getProp.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A utility type to get the value type at a given path in an object.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the object.
|
|
5
|
+
* @template P - The path as a string.
|
|
6
|
+
*/
|
|
7
|
+
export type PathValue<T, P extends string> = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? PathValue<T[K], Rest> : undefined : P extends keyof T ? T[P] : undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Retrieves the value at a given path in an object.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of the object.
|
|
12
|
+
* @template P - The path as a string.
|
|
13
|
+
* @param {T} obj - The object to retrieve the value from.
|
|
14
|
+
* @param {P} path - The path to the value in the object.
|
|
15
|
+
* @returns {PathValue<T, P>} The value at the given path, or undefined if the path is invalid.
|
|
16
|
+
*/
|
|
17
|
+
export declare function getProp<T, P extends string>(obj: T, path: P): PathValue<T, P>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groups the elements of an array based on the provided keyGetter function.
|
|
3
|
+
*
|
|
4
|
+
* @param value - The array to group.
|
|
5
|
+
* @param {function(item T, number, array T[]): string | number} keyGetter - A function that takes an element, its
|
|
6
|
+
* index, and
|
|
7
|
+
* the array, and returns a key to group by.
|
|
8
|
+
* @returns {Record<string, array T>} - An object where the keys are the results of the keyGetter function and the
|
|
9
|
+
* values are arrays of elements that correspond to those keys.
|
|
10
|
+
*/
|
|
11
|
+
export declare function groupBy<T>(value: T[], keyGetter: (item: T, index: number, array: T[]) => string | number | {
|
|
12
|
+
toString: () => string;
|
|
13
|
+
}): Record<string, T[]>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps an HTML tag name to a CSS class applied to the segment.
|
|
3
|
+
* To add a new supported tag, add an entry here and a matching `.text--<style>` CSS class.
|
|
4
|
+
*/
|
|
5
|
+
export declare const RICH_TEXT_TAGS: Map<string, string>;
|
|
6
|
+
/**
|
|
7
|
+
* Set of tag names that act as block/container-level elements.
|
|
8
|
+
* Their styles are NOT inherited by nested text segments — only their tag name
|
|
9
|
+
* is tracked in `containerTagNames` so the renderer can wrap groups accordingly.
|
|
10
|
+
*/
|
|
11
|
+
export declare const BLOCK_TAGS: Set<string>;
|
|
12
|
+
export declare function getCssClassPrefix(): string;
|
|
13
|
+
export declare function setCssClassPrefix(prefix: string): void;
|
|
14
|
+
/**
|
|
15
|
+
* Maps self-closing HTML tag names to parser style keys.
|
|
16
|
+
* Example: `<br/>` creates an empty segment with the `br` style key.
|
|
17
|
+
*/
|
|
18
|
+
export declare const SELF_CLOSING_TAGS: Map<string, string>;
|
|
19
|
+
export declare const styleTagMap: Map<string, Map<string, string>>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { TextSegment } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Parses a string containing supported inline tags into typed segments.
|
|
4
|
+
* Supports nested tags. All other content is treated as plain text.
|
|
5
|
+
*
|
|
6
|
+
* Supported tags (add more in `RICH_TEXT_TAGS`):
|
|
7
|
+
* `<b>` / `<strong>`, `<i>` / `<em>`, `<u>`, `<s>`, `<mark>`, `<small>`,
|
|
8
|
+
* `<h1>` - `<h6>`, `<code>`, `<p>`, `<div>`
|
|
9
|
+
*
|
|
10
|
+
* Supported self-closing tags (add more in `SELF_CLOSING_TAGS`):
|
|
11
|
+
* `<br/>`
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* parseRichText('Hello <b>world</b>!')
|
|
15
|
+
* // [
|
|
16
|
+
* // { text: 'Hello ', styles: [], cssClass: '' },
|
|
17
|
+
* // { text: 'world', styles: ['bold'] },
|
|
18
|
+
* // { text: '!', styles: [] },
|
|
19
|
+
* // ]
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseRichText(input: string): TextSegment[];
|
|
22
|
+
export declare namespace parseRichText {
|
|
23
|
+
var setTag: typeof setRichTextTag;
|
|
24
|
+
var setSelfClosingTag: typeof import("./html-text-parser").setSelfClosingTag;
|
|
25
|
+
var setBlockTag: (tag: string) => Set<string>;
|
|
26
|
+
var removeBlockTag: (tag: string) => boolean;
|
|
27
|
+
var setCssClassPrefix: typeof import("./constant").setCssClassPrefix;
|
|
28
|
+
var generateStylesheet: typeof import("./methods").generateStylesheet;
|
|
29
|
+
var getStylesheet: typeof import("./methods").getStylesheet;
|
|
30
|
+
var groupByBlocks: typeof import("./methods").groupByBlocks;
|
|
31
|
+
}
|
|
32
|
+
export declare function setRichTextTag(tag: {
|
|
33
|
+
tag: string;
|
|
34
|
+
tagName?: string;
|
|
35
|
+
style?: Record<string, string>;
|
|
36
|
+
}): void;
|
|
37
|
+
export declare function setRichTextTag(tags: {
|
|
38
|
+
tag: string;
|
|
39
|
+
tagName?: string;
|
|
40
|
+
style?: Record<string, string>;
|
|
41
|
+
}[]): void;
|
|
42
|
+
export declare function setSelfClosingTag(tag: {
|
|
43
|
+
tag: string;
|
|
44
|
+
tagName?: string;
|
|
45
|
+
style?: Record<string, string>;
|
|
46
|
+
}): void;
|
|
47
|
+
export declare function setSelfClosingTag(tags: {
|
|
48
|
+
tag: string;
|
|
49
|
+
tagName?: string;
|
|
50
|
+
style?: Record<string, string>;
|
|
51
|
+
}[]): void;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { TextSegment, BlockGroup } from './types';
|
|
2
|
+
export declare const createTagSegment: (activeTagsKeys: string[], input: string, lastIndex: number, index: number, text?: string) => TextSegment;
|
|
3
|
+
export declare const getTagNames: () => string;
|
|
4
|
+
export declare const getSelfClosingTagNames: () => string;
|
|
5
|
+
export declare const getTagRegex: () => RegExp;
|
|
6
|
+
export declare function setStyleMap(key: string, style: Record<string, string>): void;
|
|
7
|
+
/**
|
|
8
|
+
* Generates a CSS stylesheet string from all registered tags and their styles.
|
|
9
|
+
* Each tag gets a class like `.rtp-b { font-weight: bold; }`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const css = generateStylesheet();
|
|
13
|
+
* // '.rtp-b { font-weight: bold; }\n.rtp-i { font-style: italic; }\n...'
|
|
14
|
+
*/
|
|
15
|
+
export declare function getStylesheet(): string;
|
|
16
|
+
export declare function generateStylesheet(): void;
|
|
17
|
+
/**
|
|
18
|
+
* Groups a flat array of segments by their block/container context.
|
|
19
|
+
* Consecutive segments with the same `containerTagNames` are merged into one `BlockGroup`.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const groups = groupByBlocks(parseRichText('<div>Hello <b>world</b></div> outside'));
|
|
23
|
+
* // [
|
|
24
|
+
* // { containerTagNames: ['div'], cssClass: 'rtp-div', segments: [...] },
|
|
25
|
+
* // { containerTagNames: [], cssClass: '', segments: [...] },
|
|
26
|
+
* // ]
|
|
27
|
+
*/
|
|
28
|
+
export declare function groupByBlocks(segments: TextSegment[]): BlockGroup[];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a parsed segment of rich text with active style classes.
|
|
3
|
+
*/
|
|
4
|
+
export interface TextSegment {
|
|
5
|
+
/** The text content of the segment (e.g. 'Hello world') */
|
|
6
|
+
text: string;
|
|
7
|
+
/** Comma-separated list of active tag names (e.g. 'b,i') */
|
|
8
|
+
tagNames: string;
|
|
9
|
+
/** Array of active tag names (e.g. ['b', 'i']) */
|
|
10
|
+
tagNamesList: string[];
|
|
11
|
+
/** CSS style string — only inline tag styles, excludes block/container styles */
|
|
12
|
+
style: string;
|
|
13
|
+
/** Style object — only inline tag styles, excludes block/container styles */
|
|
14
|
+
styleObject?: Record<string, string>;
|
|
15
|
+
/** Array of active block/container tag names (e.g. ['div', 'p']) */
|
|
16
|
+
containerTagNames: string[];
|
|
17
|
+
/** CSS class string for inline tags only (e.g. 'rtp-b rtp-i') */
|
|
18
|
+
cssClass: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A group of consecutive segments sharing the same block/container context.
|
|
22
|
+
* Use `groupByBlocks()` to produce these from a flat `TextSegment[]`.
|
|
23
|
+
*/
|
|
24
|
+
export interface BlockGroup {
|
|
25
|
+
/** Block/container tag names active for this group (e.g. ['div']) */
|
|
26
|
+
containerTagNames: string[];
|
|
27
|
+
/** CSS class string for block-level tags (e.g. 'rtp-div rtp-p') */
|
|
28
|
+
cssClass: string;
|
|
29
|
+
/** Inline CSS style string for block-level tags */
|
|
30
|
+
style: string;
|
|
31
|
+
/** Style object for block-level tags */
|
|
32
|
+
styleObject: Record<string, string>;
|
|
33
|
+
/** The text segments inside this block group */
|
|
34
|
+
segments: TextSegment[];
|
|
35
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a parsed segment of rich text with active style classes.
|
|
3
|
+
*/
|
|
4
|
+
export interface TextSegment {
|
|
5
|
+
/** The text content of the segment (e.g. 'Hello world') */
|
|
6
|
+
text: string;
|
|
7
|
+
/** Comma-separated list of active tag names (e.g. 'b,i') */
|
|
8
|
+
tagNames: string;
|
|
9
|
+
/** Array of active tag names (e.g. ['b', 'i']) */
|
|
10
|
+
tagNamesList: string[];
|
|
11
|
+
/** CSS style string (e.g. 'font-weight: bold; font-style: italic') */
|
|
12
|
+
style: string;
|
|
13
|
+
/** Optional style object for easier programmatic access (e.g. { 'font-weight': 'bold', 'font-style': 'italic' }) */
|
|
14
|
+
styleObject?: Record<string, string>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Parses a string containing supported inline tags into typed segments.
|
|
18
|
+
* Supports nested tags. All other content is treated as plain text.
|
|
19
|
+
*
|
|
20
|
+
* Supported tags (add more in `RICH_TEXT_TAGS`):
|
|
21
|
+
* `<b>` / `<strong>`, `<i>` / `<em>`, `<u>`, `<s>`, `<mark>`, `<small>`,
|
|
22
|
+
* `<h1>` - `<h6>`, `<code>`, `<p>`, `<div>`
|
|
23
|
+
*
|
|
24
|
+
* Supported self-closing tags (add more in `SELF_CLOSING_TAGS`):
|
|
25
|
+
* `<br/>`
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* parseRichText('Hello <b>world</b>!')
|
|
29
|
+
* // [
|
|
30
|
+
* // { text: 'Hello ', styles: [], cssClass: '' },
|
|
31
|
+
* // { text: 'world', styles: ['bold'] },
|
|
32
|
+
* // { text: '!', styles: [] },
|
|
33
|
+
* // ]
|
|
34
|
+
*/
|
|
35
|
+
export declare function parseRichText(input: string): TextSegment[];
|
|
36
|
+
export declare namespace parseRichText {
|
|
37
|
+
var addTag: typeof addRichTextTag;
|
|
38
|
+
var addSelfClosingTag: typeof import("./html-text-parser___").addSelfClosingTag;
|
|
39
|
+
}
|
|
40
|
+
export declare function addRichTextTag(tag: {
|
|
41
|
+
tag: string;
|
|
42
|
+
tagName?: string;
|
|
43
|
+
style?: Record<string, string>;
|
|
44
|
+
}): void;
|
|
45
|
+
export declare function addRichTextTag(tags: {
|
|
46
|
+
tag: string;
|
|
47
|
+
tagName?: string;
|
|
48
|
+
style?: Record<string, string>;
|
|
49
|
+
}[]): void;
|
|
50
|
+
export declare function addSelfClosingTag(tag: {
|
|
51
|
+
tag: string;
|
|
52
|
+
tagName?: string;
|
|
53
|
+
style?: Record<string, string>;
|
|
54
|
+
}): void;
|
|
55
|
+
export declare function addSelfClosingTag(tags: {
|
|
56
|
+
tag: string;
|
|
57
|
+
tagName?: string;
|
|
58
|
+
style?: Record<string, string>;
|
|
59
|
+
}[]): void;
|
package/lib/http.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A utility object for making HTTP requests.
|
|
3
|
+
*/
|
|
4
|
+
export declare const http: {
|
|
5
|
+
/**
|
|
6
|
+
* Makes a GET request to the specified URL.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} url - The URL to send the GET request to.
|
|
9
|
+
* @param {RequestInit} [options] - Optional request options.
|
|
10
|
+
* @returns {Promise<any>} The response data.
|
|
11
|
+
* @throws {Error} If the response is not ok.
|
|
12
|
+
*/
|
|
13
|
+
get: <R = any>(url: string, options?: RequestInit) => Promise<R>;
|
|
14
|
+
/**
|
|
15
|
+
* Makes a POST request to the specified URL with the given data.
|
|
16
|
+
*
|
|
17
|
+
* @template R
|
|
18
|
+
* @param {string} url - The URL to send the POST request to.
|
|
19
|
+
* @param {unknown} data - The data to send in the request body.
|
|
20
|
+
* @param {RequestInit} [options] - Optional request options.
|
|
21
|
+
* @returns {Promise<R>} The response data.
|
|
22
|
+
* @throws {Error} If the response is not ok.
|
|
23
|
+
*/
|
|
24
|
+
post: <R = any>(url: string, data: unknown, options?: RequestInit) => Promise<R>;
|
|
25
|
+
/**
|
|
26
|
+
* Makes a DELETE request to the specified URL with the given ID.
|
|
27
|
+
*
|
|
28
|
+
* @template R
|
|
29
|
+
* @param {string} url - The URL to send the DELETE request to.
|
|
30
|
+
* @param {string} id - The ID to send in the request body.
|
|
31
|
+
* @param {RequestInit} [options] - Optional request options.
|
|
32
|
+
* @returns {Promise<R>} The response data.
|
|
33
|
+
* @throws {Error} If the response is not ok.
|
|
34
|
+
*/
|
|
35
|
+
delete: <R = any>(url: string, id: string, options?: RequestInit) => Promise<R>;
|
|
36
|
+
/**
|
|
37
|
+
* Makes a PATCH request to the specified URL with the given data.
|
|
38
|
+
*
|
|
39
|
+
* @template R
|
|
40
|
+
* @param {string} url - The URL to send the PATCH request to.
|
|
41
|
+
* @param {unknown} data - The data to send in the request body.
|
|
42
|
+
* @param {RequestInit} [options] - Optional request options.
|
|
43
|
+
* @returns {Promise<R>} The response data.
|
|
44
|
+
* @throws {Error} If the response is not ok.
|
|
45
|
+
*/
|
|
46
|
+
patch: <R = any>(url: string, data: unknown, options?: RequestInit) => Promise<R>;
|
|
47
|
+
/**
|
|
48
|
+
* Makes a PUT request to the specified URL with the given data.
|
|
49
|
+
*
|
|
50
|
+
* @template R
|
|
51
|
+
* @param {string} url - The URL to send the PUT request to.
|
|
52
|
+
* @param {unknown} data - The data to send in the request body.
|
|
53
|
+
* @param {RequestInit} [options] - Optional request options.
|
|
54
|
+
* @returns {Promise<R>} The response data.
|
|
55
|
+
* @throws {Error} If the response is not ok.
|
|
56
|
+
*/
|
|
57
|
+
put: <R = any>(url: string, data: unknown, options?: RequestInit) => Promise<R>;
|
|
58
|
+
};
|
package/lib/signals.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
type Subscriber<T> = (value: T) => void;
|
|
2
|
+
type CleanupFn = () => void;
|
|
3
|
+
type UpdateFn<T> = (current: T) => T;
|
|
4
|
+
export declare const SIGNAL: unique symbol;
|
|
5
|
+
export declare const isSignal: (value: unknown) => value is Signal<unknown>;
|
|
6
|
+
export interface Signal<T> {
|
|
7
|
+
(): T;
|
|
8
|
+
[SIGNAL]: true;
|
|
9
|
+
subscribe: (fn: Subscriber<T>) => CleanupFn;
|
|
10
|
+
}
|
|
11
|
+
export interface WritableSignal<T> extends Signal<T> {
|
|
12
|
+
set: (value: T) => void;
|
|
13
|
+
update: (fn: UpdateFn<T>) => void;
|
|
14
|
+
asReadonly: () => Signal<T>;
|
|
15
|
+
}
|
|
16
|
+
export interface EffectRef {
|
|
17
|
+
destroy: CleanupFn;
|
|
18
|
+
}
|
|
19
|
+
type ResourceStatus = 'idle' | 'loading' | 'resolved' | 'error';
|
|
20
|
+
export type ResourceStreamItem<T> = {
|
|
21
|
+
value: T;
|
|
22
|
+
} | {
|
|
23
|
+
error: unknown;
|
|
24
|
+
};
|
|
25
|
+
export interface ResourceRef<T> {
|
|
26
|
+
readonly value: Signal<T | undefined>;
|
|
27
|
+
readonly status: Signal<ResourceStatus>;
|
|
28
|
+
readonly error: Signal<unknown>;
|
|
29
|
+
readonly isLoading: Signal<boolean>;
|
|
30
|
+
set: (value: T) => void;
|
|
31
|
+
update: (fn: UpdateFn<T | undefined>) => void;
|
|
32
|
+
reload: () => void;
|
|
33
|
+
destroy: CleanupFn;
|
|
34
|
+
}
|
|
35
|
+
export interface ResourceLoaderParamsNoParams {
|
|
36
|
+
abortSignal: AbortSignal;
|
|
37
|
+
}
|
|
38
|
+
export interface ResourceLoaderParams<R> extends ResourceLoaderParamsNoParams {
|
|
39
|
+
params?: NoInfer<R>;
|
|
40
|
+
}
|
|
41
|
+
interface ResourceLoaderWithParams<T, R> {
|
|
42
|
+
params: () => R;
|
|
43
|
+
loader: (params: ResourceLoaderParams<R>) => Promise<T>;
|
|
44
|
+
}
|
|
45
|
+
interface ResourceLoaderWithoutParams<T> {
|
|
46
|
+
loader: (params: ResourceLoaderParamsNoParams) => Promise<T>;
|
|
47
|
+
}
|
|
48
|
+
interface ResourceStreamWithParams<T, R> {
|
|
49
|
+
params: () => R;
|
|
50
|
+
stream: (params: ResourceLoaderParams<R>) => Signal<ResourceStreamItem<T> | undefined>;
|
|
51
|
+
}
|
|
52
|
+
interface ResourceStreamWithoutParams<T> {
|
|
53
|
+
stream: (params: ResourceLoaderParamsNoParams) => Signal<ResourceStreamItem<T> | undefined>;
|
|
54
|
+
}
|
|
55
|
+
export declare const untracked: <T>(fn: () => T) => T;
|
|
56
|
+
export declare const signal: <T>(initial: T) => WritableSignal<T>;
|
|
57
|
+
export declare const effect: (fn: (onCleanup: (cleanupFn: CleanupFn) => void) => void) => EffectRef;
|
|
58
|
+
export declare const computed: <T>(fn: () => T) => Signal<T>;
|
|
59
|
+
interface LinkedSignalOptions<S, T> {
|
|
60
|
+
source: () => S;
|
|
61
|
+
computation: (source: S, previous: {
|
|
62
|
+
source: S;
|
|
63
|
+
value: T;
|
|
64
|
+
} | undefined) => T;
|
|
65
|
+
}
|
|
66
|
+
export declare function linkedSignal<T>(computation: () => T): WritableSignal<T>;
|
|
67
|
+
export declare function linkedSignal<S, T>(options: LinkedSignalOptions<S, T>): WritableSignal<T>;
|
|
68
|
+
export declare function resource<T, R>(options: ResourceLoaderWithParams<T, R>): ResourceRef<T>;
|
|
69
|
+
export declare function resource<T>(options: ResourceLoaderWithoutParams<T>): ResourceRef<T>;
|
|
70
|
+
export declare function resource<T, R>(options: ResourceStreamWithParams<T, R>): ResourceRef<T>;
|
|
71
|
+
export declare function resource<T>(options: ResourceStreamWithoutParams<T>): ResourceRef<T>;
|
|
72
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function toArray<T>(obj: T | T[] | undefined | null, defaultValue?: T[]): T[];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for the result tuple with discriminated union
|
|
3
|
+
* @template T - Type of the successful result
|
|
4
|
+
* @template E - Type of the error, defaults to Error
|
|
5
|
+
*/
|
|
6
|
+
export type TryResult<T, E = Error> = [T, null] | [null, E];
|
|
7
|
+
/**
|
|
8
|
+
* Main wrapper function to handle promise with try-catch
|
|
9
|
+
* @template T - Type of the successful result
|
|
10
|
+
* @template E - Type of the error, defaults to Error
|
|
11
|
+
* @param {Promise<T>} promise - The promise to handle
|
|
12
|
+
* @returns {Promise<TryResult<T, E>>} - A promise that resolves to a tuple with either the result or the error
|
|
13
|
+
*/
|
|
14
|
+
export declare function tryCatchAsync<T, E = Error>(promise: Promise<T>): Promise<TryResult<T, E>>;
|
|
15
|
+
/**
|
|
16
|
+
* Main wrapper function to handle promise with try-catch
|
|
17
|
+
* @template T - Type of the successful result
|
|
18
|
+
* @template E - Type of the error, defaults to Error
|
|
19
|
+
* @param {()=> T} fn - The function to handle
|
|
20
|
+
* @returns {TryResult<T, E>} - A tuple with either the result or the error
|
|
21
|
+
*/
|
|
22
|
+
export declare function tryCatch<T, E = Error>(fn: () => T): TryResult<T, E>;
|