@tempots/dom 20.0.1 → 21.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tempots/dom",
3
- "version": "20.0.1",
3
+ "version": "21.0.0",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -1,4 +1,5 @@
1
- import { NValue, Renderable, Value } from '../types/domain';
1
+ import { NValue, Renderable } from '../types/domain';
2
+ import { Value } from '../std/value';
2
3
  /**
3
4
  * The `attr` object allows to create any HTML attribute. Either a literal value
4
5
  * or `Signal<?>` can be passed as a value. The type of the value is inferred
@@ -1,13 +1,14 @@
1
1
  import { TNode, Renderable } from '../types/domain';
2
2
  import { Signal } from '../std/signal';
3
+ import { Value } from '../std/value';
3
4
  /**
4
5
  * Represents a function that ensures a signal has a value before rendering a TNode.
5
6
  *
6
7
  * @typeParam T - The type of the signal value.
7
- * @param signal - The signal to ensure has a value.
8
+ * @param value - The signal or literal that may hold a value of type T or null or undefined.
8
9
  * @param then - The function that returns a TNode when the signal has a value. It takes a signal of the non-nullable type of T.
9
10
  * @param otherwise - The function that returns a TNode when the signal does not have a value.
10
11
  * @returns A renderable function that ensures the signal has a value before rendering a TNode.
11
12
  * @public
12
13
  */
13
- export declare const Ensure: <T>(signal: Signal<T | null> | Signal<T | undefined> | Signal<T | null | undefined>, then: (value: Signal<NonNullable<T>>) => TNode, otherwise?: () => TNode) => Renderable;
14
+ export declare const Ensure: <T>(value: Value<T | null> | Value<T | undefined> | Value<T | null | undefined>, then: (value: Signal<NonNullable<T>>) => TNode, otherwise?: () => TNode) => Renderable;
@@ -1,14 +1,15 @@
1
1
  import { TNode, Renderable } from '../types/domain';
2
2
  import { Signal } from '../std/signal';
3
3
  import { ElementPosition } from '../std/position';
4
+ import { Value } from '../std/value';
4
5
  /**
5
6
  * Renders a list of items based on a signal of arrays.
6
7
  *
7
8
  * @typeParam T - The type of items in the array.
8
- * @param signal - The signal of arrays to iterate over.
9
+ * @param value - The signal of arrays to iterate over.
9
10
  * @param item - The function that renders each item in the array.
10
11
  * @param separator - The function that renders the separator between items.
11
12
  * @returns - The renderable function that renders the list of items.
12
13
  * @public
13
14
  */
14
- export declare const ForEach: <T>(signal: Signal<T[]>, item: (value: Signal<T>, position: Signal<ElementPosition>) => TNode, separator?: (pos: Signal<ElementPosition>) => TNode) => Renderable;
15
+ export declare const ForEach: <T>(value: Value<T[]>, item: (value: Signal<T>, position: Signal<ElementPosition>) => TNode, separator?: (pos: Signal<ElementPosition>) => TNode) => Renderable;
@@ -1,5 +1,5 @@
1
1
  import { Renderable } from '../types/domain';
2
- import { Signal } from '../std/signal';
2
+ import { Value } from '../std/value';
3
3
  /**
4
4
  * Maps the values emitted by a signal to a renderable function and returns a new renderable function.
5
5
  *
@@ -10,9 +10,9 @@ import { Signal } from '../std/signal';
10
10
  * changes to a state that requires a different renderable function.
11
11
  *
12
12
  * @typeParam T - The type of values emitted by the signal.
13
- * @param signal - The signal to map.
13
+ * @param vlaue - The signal or value to map.
14
14
  * @param fn - The function to map the signal values to renderable functions.
15
15
  * @returns - A new renderable function that represents the mapped signal.
16
16
  * @public
17
17
  */
18
- export declare const MapSignal: <T>(signal: Signal<T>, fn: (value: T) => Renderable) => Renderable;
18
+ export declare const MapSignal: <T>(value: Value<T>, fn: (value: T) => Renderable) => Renderable;
@@ -1,4 +1,4 @@
1
- import { Signal } from '../std/signal';
1
+ import { Value } from '../std/value';
2
2
  import { Renderable } from '../types/domain';
3
3
  /**
4
4
  * Returns a renderable component that displays the given `display` component
@@ -6,10 +6,10 @@ import { Renderable } from '../types/domain';
6
6
  * otherwise.
7
7
  *
8
8
  * @typeParam T - The type of elements in the array.
9
- * @param signal - The signal containing the array.
9
+ * @param value - The signal or literal containing the array.
10
10
  * @param display - The component to display when the array is non-empty.
11
11
  * @param whenEmpty- The component to display when the array is empty.
12
12
  * @returns - The renderable component.
13
13
  * @public
14
14
  */
15
- export declare const NotEmpty: <T>(signal: Signal<T[]>, display: Renderable, whenEmpty?: Renderable) => Renderable;
15
+ export declare const NotEmpty: <T>(value: Value<T[]>, display: Renderable, whenEmpty?: Renderable) => Renderable;
@@ -1,4 +1,5 @@
1
1
  import { Signal } from '../std/signal';
2
+ import { Value } from '../std/value';
2
3
  import { Renderable, TNode } from '../types/domain';
3
4
  /**
4
5
  * Represents a set of options for a one-of type.
@@ -14,12 +15,12 @@ export type OneOfOptions<T extends Record<string, unknown>> = {
14
15
  * The signal value should be an object with a single key that matches one of the keys in the `cases` object.
15
16
  *
16
17
  * @typeParam T - The type of the signal value.
17
- * @param match - The signal to match against.
18
+ * @param match - The signal or value to match against.
18
19
  * @param cases - An object containing the different cases to render based on the signal value.
19
20
  * @returns A renderable function that renders the appropriate component based on the signal value.
20
21
  * @public
21
22
  */
22
- export declare const OneOf: <T extends Record<string, unknown>>(match: Signal<T>, cases: OneOfOptions<T>) => Renderable;
23
+ export declare const OneOf: <T extends Record<string, unknown>>(match: Value<T>, cases: OneOfOptions<T>) => Renderable;
23
24
  /**
24
25
  * Represents the options for a one-of field.
25
26
  *
@@ -39,13 +40,13 @@ export type OneOfFieldOptions<T extends {
39
40
  *
40
41
  * @typeParam T - The type containing the one-of field.
41
42
  * @typeParam K - The type of the one-of field key.
42
- * @param match - The signal that emits the object containing the one-of field.
43
+ * @param match - The signal or value that emits the object containing the one-of field.
43
44
  * @param field - The key of the one-of field.
44
45
  * @param cases - The options for the different cases of rendering based on the one-of field value.
45
46
  * @returns - The renderable field representing the one-of field.
46
47
  * @public
47
48
  */
48
- export declare const OneOfField: <T extends { [_ in K]: string; }, K extends string>(match: Signal<T>, field: K, cases: OneOfFieldOptions<T, K>) => Renderable;
49
+ export declare const OneOfField: <T extends { [_ in K]: string; }, K extends string>(match: Value<T>, field: K, cases: OneOfFieldOptions<T, K>) => Renderable;
49
50
  /**
50
51
  * The options for a one-of kind field.
51
52
  *
@@ -72,7 +73,7 @@ export type OneOfKindOptions<T extends {
72
73
  */
73
74
  export declare const OneOfKind: <T extends {
74
75
  kind: string;
75
- }>(match: Signal<T>, cases: OneOfKindOptions<T>) => Renderable;
76
+ }>(match: Value<T>, cases: OneOfKindOptions<T>) => Renderable;
76
77
  /**
77
78
  * Represents a mapping of keys to functions that accept a value of type `Signal<V>`
78
79
  * and return a `TNode`.
@@ -94,7 +95,7 @@ export type OneOfTupleOptions<T extends string, V> = {
94
95
  * @returns The result of matching the signal value with the cases.
95
96
  * @public
96
97
  */
97
- export declare const OneOfTuple: <T extends string, V>(match: Signal<[T, V]>, cases: OneOfTupleOptions<T, V>) => Renderable;
98
+ export declare const OneOfTuple: <T extends string, V>(match: Value<[T, V]>, cases: OneOfTupleOptions<T, V>) => Renderable;
98
99
  /**
99
100
  * Represents a mapping of types to rendering functions.
100
101
  * @typeParam T - The type that contains a `type` property.
@@ -120,7 +121,7 @@ export type OneOfTypeOptions<T extends {
120
121
  */
121
122
  export declare const OneOfType: <T extends {
122
123
  type: string;
123
- }>(match: Signal<T>, cases: OneOfTypeOptions<T>) => Renderable;
124
+ }>(match: Value<T>, cases: OneOfTypeOptions<T>) => Renderable;
124
125
  /**
125
126
  * Represents a set of options for a one-of value.
126
127
  * @typeParam T - The type of the one-of value.
@@ -140,4 +141,4 @@ export type OneOfValueOptions<T extends symbol | number | string> = {
140
141
  * @returns - The renderable value representing one of the cases.
141
142
  * @public
142
143
  */
143
- export declare const OneOfValue: <T extends symbol | number | string>(match: Signal<T>, cases: OneOfValueOptions<T>) => Renderable;
144
+ export declare const OneOfValue: <T extends symbol | number | string>(match: Value<T>, cases: OneOfValueOptions<T>) => Renderable;
@@ -1,5 +1,6 @@
1
1
  import { ElementPosition } from '../std/position';
2
2
  import { Signal } from '../std/signal';
3
+ import { Value } from '../std/value';
3
4
  import { TNode, Renderable } from '../types/domain';
4
5
  /**
5
6
  * Creates a renderable function that repeats a given element a specified number of times.
@@ -10,4 +11,4 @@ import { TNode, Renderable } from '../types/domain';
10
11
  * @returns A renderable function that renders the repeated elements.
11
12
  * @public
12
13
  */
13
- export declare const Repeat: (times: Signal<number>, element: (index: Signal<ElementPosition>) => TNode, separator?: (pos: Signal<ElementPosition>) => TNode) => Renderable;
14
+ export declare const Repeat: (times: Value<number>, element: (index: Signal<ElementPosition>) => TNode, separator?: (pos: Signal<ElementPosition>) => TNode) => Renderable;
@@ -1,5 +1,6 @@
1
- import { Renderable, Value } from '../types/domain';
1
+ import { Renderable } from '../types/domain';
2
2
  import { Signal } from '../std/signal';
3
+ import { Value } from '../std/value';
3
4
  /**
4
5
  * @internal
5
6
  */
@@ -1,5 +1,5 @@
1
1
  import { Renderable, TNode } from '../types/domain';
2
- import { Signal } from '../std/signal';
2
+ import { Value } from '../std/value';
3
3
  /**
4
4
  * Renders the `then` node if the `condition` is true, otherwise renders the `otherwise` node.
5
5
  *
@@ -9,7 +9,7 @@ import { Signal } from '../std/signal';
9
9
  * @returns The rendered node.
10
10
  * @public
11
11
  */
12
- export declare const When: (condition: Signal<boolean>, then: TNode, otherwise?: TNode) => Renderable;
12
+ export declare const When: (condition: Value<boolean>, then: TNode, otherwise?: TNode) => Renderable;
13
13
  /**
14
14
  * Executes the `then` TNode if the `condition` Signal evaluates to `false`, otherwise executes the `otherwise` TNode.
15
15
  *
@@ -19,4 +19,4 @@ export declare const When: (condition: Signal<boolean>, then: TNode, otherwise?:
19
19
  * @returns The result of executing the `then` or `otherwise` TNode based on the condition.
20
20
  * @public
21
21
  */
22
- export declare const Unless: (condition: Signal<boolean>, then: TNode, otherwise?: TNode) => Renderable;
22
+ export declare const Unless: (condition: Value<boolean>, then: TNode, otherwise?: TNode) => Renderable;
package/std/position.d.ts CHANGED
@@ -41,13 +41,13 @@ export declare class ElementPosition {
41
41
  */
42
42
  get isLast(): boolean;
43
43
  /**
44
- * Checks if the index of the element is even.
45
- * @returns `true` if the index is even, `false` otherwise.
44
+ * Checks if the counter of the element is even.
45
+ * @returns `true` if the counter is even, `false` otherwise.
46
46
  */
47
47
  get isEven(): boolean;
48
48
  /**
49
- * Checks if the index of the element is odd.
50
- * @returns `true` if the index is odd, `false` otherwise.
49
+ * Checks if the counter of the element is odd.
50
+ * @returns `true` if the counter is odd, `false` otherwise.
51
51
  */
52
52
  get isOdd(): boolean;
53
53
  }
@@ -1,5 +1,6 @@
1
- import { RemoveSignals, Value } from '../types/domain';
1
+ import { RemoveSignals } from '../types/domain';
2
2
  import { AnySignal, Computed, Prop, Signal } from './signal';
3
+ import { Value } from './value';
3
4
  /**
4
5
  * Represents a memory store that stores key-value pairs.
5
6
  *
package/std/signal.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { GetValueTypes, Value } from '../types/domain';
2
1
  /**
3
2
  * Represents any type of signal.
4
3
  * It can be a Signal, Prop, or Computed.
@@ -39,46 +38,7 @@ export declare class Signal<T> {
39
38
  * @param value - The value to check.
40
39
  * @returns `true` if the value is a Signal, `false` otherwise.
41
40
  */
42
- static readonly is: <O = unknown>(value: unknown) => value is Signal<O>;
43
- /**
44
- * Wraps a value or a Signal instance into a Signal.
45
- * If the value is already a Signal, it returns the value itself.
46
- * If the value is not a Signal, it creates a new Signal instance with the given value.
47
- *
48
- * @typeParam O - The type of the value.
49
- * @param value - The value or Signal instance to wrap.
50
- * @param equals - A function that determines if two values are equal. Defaults to strict equality (===).
51
- * @returns A Signal instance.
52
- */
53
- static readonly wrap: <O>(value: O | Signal<O>, equals?: (a: O, b: O) => boolean) => Signal<O>;
54
- /**
55
- * Wraps a value in a `Signal` if it is not already a `Signal`.
56
- * If the value is `null` or `undefined`, it returns `null` or `undefined` respectively.
57
- * @param value - The value to wrap or check.
58
- * @returns The wrapped value if it is not `null` or `undefined`, otherwise `null` or `undefined`.
59
- */
60
- static readonly maybeWrap: <O>(value: O | Signal<O> | null | undefined) => Signal<O> | undefined | null;
61
- /**
62
- * Unwraps a value from a `Signal` if it is a `Signal`, otherwise returns the value as is.
63
- *
64
- * @param value - The value to unwrap.
65
- * @returns The unwrapped value.
66
- */
67
- static readonly unwrap: <O>(value: Signal<O> | O) => O;
68
- /**
69
- * Maps the value of a `Signal` or a regular value using the provided mapping function.
70
- * If the input value is a `Signal`, the mapping function is applied to its value.
71
- * If the input value is not a `Signal`, the mapping function is directly applied to the value.
72
- *
73
- * @param value - The input value to be mapped.
74
- * @param fn - The mapping function to be applied to the value.
75
- * @returns A new `Signal` with the mapped value if the input value is a `Signal`,
76
- * otherwise, the result of applying the mapping function to the value.
77
- *
78
- * @typeParam N - The type of the input value.
79
- * @typeParam O - The type of the mapped value.
80
- */
81
- static readonly map: <N, O>(value: Value<N>, fn: (value: N) => O) => Value<O>;
41
+ static readonly is: <O>(value: O | Signal<O>) => value is Signal<O>;
82
42
  /**
83
43
  * @internal
84
44
  */
@@ -363,7 +323,7 @@ export declare class Prop<T> extends Signal<T> {
363
323
  * @param value - The value to check.
364
324
  * @returns `true` if the value is a Prop, `false` otherwise.
365
325
  */
366
- static is: <T_1 = unknown>(value: unknown) => value is Prop<T_1>;
326
+ static is: <T_1>(value: T_1 | Prop<T_1> | Signal<T_1> | Computed<T_1>) => value is Prop<T_1>;
367
327
  /**
368
328
  * @internal
369
329
  */
@@ -422,16 +382,6 @@ export declare class Prop<T> extends Signal<T> {
422
382
  * @public
423
383
  */
424
384
  export declare const makeComputed: <T>(fn: () => T, dependencies: Array<AnySignal>, equals?: (a: T, b: T) => boolean) => Computed<T>;
425
- /**
426
- * Creates a computed signal that depends on other signals or literal values and updates when any of the dependencies change.
427
- *
428
- * @typeParam T - The type of the argument values.
429
- * @param fn - The function that computes the value.
430
- * @param equals - The equality function used to compare the previous and current computed values.
431
- * @returns - The computed signal.
432
- * @public
433
- */
434
- export declare const makeComputedOf: <T extends Value<unknown>[]>(...args: T) => <O>(fn: (...args: GetValueTypes<T>) => O, equals?: (a: O, b: O) => boolean) => Computed<O>;
435
385
  /**
436
386
  * Executes the provided function `fn` whenever any of the signals in the `signals` array change.
437
387
  * Returns a disposable object that can be used to stop the effect.
@@ -442,14 +392,6 @@ export declare const makeComputedOf: <T extends Value<unknown>[]>(...args: T) =>
442
392
  * @public
443
393
  */
444
394
  export declare const makeEffect: (fn: () => void, signals: Array<AnySignal>) => () => void;
445
- /**
446
- * Creates an effect that depends on other signals or literal values and updates when any of the dependencies change.
447
- *
448
- * @param args - The array of signals or literal values that the effect depends on.
449
- * @returns A disposable object that can be used to stop the effect.
450
- * @public
451
- */
452
- export declare const makeEffectOf: <T extends Value<unknown>[]>(...args: T) => (fn: (...args: GetValueTypes<T>) => void) => void;
453
395
  /**
454
396
  * Creates a new Prop object with the specified value and equality function.
455
397
  *
package/std/value.d.ts ADDED
@@ -0,0 +1,71 @@
1
+ import { GetValueTypes } from '../types/domain';
2
+ import { Signal } from './signal';
3
+ /**
4
+ * Represents a value that can either be a `Signal<T>` or a generic type `T`.
5
+ *
6
+ * @public
7
+ */
8
+ export type Value<T> = Signal<T> | T;
9
+ export declare const Value: {
10
+ /**
11
+ * Maps a value or a Signal to a new value.
12
+ * If the value is a Signal, it returns a new Signal with the mapped value.
13
+ * If the value is not a Signal, it returns the mapped value.
14
+ *
15
+ * @typeParam T - The type of the value.
16
+ * @typeParam U - The type of the new value.
17
+ * @param value - The value or Signal to map.
18
+ * @param fn - The function to map the value.
19
+ * @returns The mapped value.
20
+ */
21
+ map: <T, U>(value: Value<T>, fn: (value: T) => U) => Value<U>;
22
+ /**
23
+ * Wraps a value or a Signal instance into a Signal.
24
+ * If the value is already a Signal, it returns the value itself.
25
+ * If the value is not a Signal, it creates a new Signal instance with the given value.
26
+ *
27
+ * @typeParam O - The type of the value.
28
+ * @param value - The value or Signal instance to wrap.
29
+ * @param equals - A function that determines if two values are equal. Defaults to strict equality (===).
30
+ * @returns A Signal instance.
31
+ */
32
+ toSignal: <T>(value: Value<T>, equals?: (a: T, b: T) => boolean) => Signal<T>;
33
+ /**
34
+ * Wraps a value in a `Signal` if it is not already a `Signal`.
35
+ * If the value is `null` or `undefined`, it returns `null` or `undefined` respectively.
36
+ * @param value - The value to wrap or check.
37
+ * @returns The wrapped value if it is not `null` or `undefined`, otherwise `null` or `undefined`.
38
+ */
39
+ maybeToSignal: <T>(value: Value<T> | undefined | null, equals?: (a: T, b: T) => boolean) => Signal<T> | undefined;
40
+ /**
41
+ * Gets the value from a `Signal` or the value itself if it is not a `Signal`.
42
+ * @param value - The value or Signal instance to get the value from.
43
+ * @returns The value.
44
+ */
45
+ get: <T>(value: Value<T>) => T;
46
+ /**
47
+ * Adds a listener to a `Signal` or calls the listener immediately if it is not a `Signal`.
48
+ * @param value - The value or Signal instance to add the listener to.
49
+ * @param listener - The listener to call when the value changes.
50
+ * @returns A function to remove the listener.
51
+ */
52
+ on: <T>(value: Value<T>, listener: (value: T) => void) => (() => void);
53
+ };
54
+ /**
55
+ * Creates a computed signal that depends on other signals or literal values and updates when any of the dependencies change.
56
+ *
57
+ * @typeParam T - The type of the argument values.
58
+ * @param fn - The function that computes the value.
59
+ * @param equals - The equality function used to compare the previous and current computed values.
60
+ * @returns - The computed signal.
61
+ * @public
62
+ */
63
+ export declare const makeComputedOf: <T extends Value<unknown>[]>(...args: T) => <O>(fn: (...args: GetValueTypes<T>) => O, equals?: (a: O, b: O) => boolean) => import('./signal').Computed<O>;
64
+ /**
65
+ * Creates an effect that depends on other signals or literal values and updates when any of the dependencies change.
66
+ *
67
+ * @param args - The array of signals or literal values that the effect depends on.
68
+ * @returns A disposable object that can be used to stop the effect.
69
+ * @public
70
+ */
71
+ export declare const makeEffectOf: <T extends Value<unknown>[]>(...args: T) => (fn: (...args: GetValueTypes<T>) => void) => void;
package/types/domain.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { DOMContext } from '../dom/dom-context';
2
2
  import { Signal } from '../std/signal';
3
+ import { Value } from '../std/value';
3
4
  /**
4
5
  * Represents a function that can be rendered in the DOM.
5
6
  * @param ctx - The DOM context for rendering.
@@ -47,12 +48,6 @@ export type Size = {
47
48
  */
48
49
  readonly height: number;
49
50
  };
50
- /**
51
- * Represents a value that can either be a `Signal<T>` or a generic type `T`.
52
- *
53
- * @public
54
- */
55
- export type Value<T> = Signal<T> | T;
56
51
  /**
57
52
  * Represents a nullable value or a signal of a nullable value.
58
53
  * @typeParam T - The type of the value.