@agnos-ui/core 0.0.1-alpha.0 → 0.0.1-alpha.10

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.
Files changed (126) hide show
  1. package/README.md +4 -4
  2. package/{dist/lib → components/accordion}/accordion.d.ts +29 -25
  3. package/{dist/lib → components/accordion}/accordion.js +81 -77
  4. package/components/accordion/index.d.ts +1 -0
  5. package/components/accordion/index.js +1 -0
  6. package/components/alert/alert.d.ts +31 -0
  7. package/components/alert/alert.js +22 -0
  8. package/{dist/lib/alert.d.ts → components/alert/common.d.ts} +17 -20
  9. package/{dist/lib/alert.js → components/alert/common.js} +22 -19
  10. package/components/alert/index.d.ts +2 -0
  11. package/components/alert/index.js +2 -0
  12. package/components/commonProps.d.ts +6 -0
  13. package/components/commonProps.js +1 -0
  14. package/components/modal/index.d.ts +1 -0
  15. package/components/modal/index.js +1 -0
  16. package/{dist/lib → components}/modal/modal.d.ts +30 -29
  17. package/{dist/lib → components}/modal/modal.js +59 -29
  18. package/components/pagination/index.d.ts +2 -0
  19. package/components/pagination/index.js +2 -0
  20. package/{dist/lib → components/pagination}/pagination.d.ts +5 -14
  21. package/{dist/lib → components/pagination}/pagination.js +6 -5
  22. package/components/progressbar/index.d.ts +1 -0
  23. package/components/progressbar/index.js +1 -0
  24. package/components/progressbar/progressbar.d.ts +86 -0
  25. package/components/progressbar/progressbar.js +78 -0
  26. package/components/rating/index.d.ts +1 -0
  27. package/components/rating/index.js +1 -0
  28. package/{dist/lib → components/rating}/rating.d.ts +6 -13
  29. package/{dist/lib → components/rating}/rating.js +6 -9
  30. package/components/select/index.d.ts +1 -0
  31. package/components/select/index.js +1 -0
  32. package/components/select/select.d.ts +337 -0
  33. package/components/select/select.js +266 -0
  34. package/components/slider/index.d.ts +1 -0
  35. package/components/slider/index.js +1 -0
  36. package/components/slider/slider.d.ts +245 -0
  37. package/components/slider/slider.js +413 -0
  38. package/{dist/lib/config.d.ts → config.d.ts} +17 -7
  39. package/{dist/lib/config.js → config.js} +3 -3
  40. package/index.d.ts +25 -0
  41. package/index.js +31 -0
  42. package/package.json +42 -28
  43. package/services/extendWidget.d.ts +23 -0
  44. package/services/extendWidget.js +35 -0
  45. package/services/floatingUI.d.ts +56 -0
  46. package/services/floatingUI.js +105 -0
  47. package/{dist/lib/services → services}/focustrack.js +5 -5
  48. package/services/intersection.d.ts +34 -0
  49. package/services/intersection.js +55 -0
  50. package/services/navManager.d.ts +93 -0
  51. package/services/navManager.js +172 -0
  52. package/{dist/lib/services → services}/portal.d.ts +7 -0
  53. package/services/portal.js +44 -0
  54. package/{dist/lib/services → services}/siblingsInert.d.ts +2 -1
  55. package/{dist/lib/services → services}/siblingsInert.js +2 -2
  56. package/{dist/lib → services}/transitions/baseTransitions.d.ts +15 -2
  57. package/{dist/lib → services}/transitions/baseTransitions.js +21 -10
  58. package/services/transitions/bootstrap/collapse.d.ts +2 -0
  59. package/services/transitions/bootstrap/fade.d.ts +1 -0
  60. package/services/transitions/bootstrap.d.ts +2 -0
  61. package/services/transitions/bootstrap.js +2 -0
  62. package/services/transitions/collapse.d.ts +43 -0
  63. package/{dist/lib → services}/transitions/collapse.js +15 -2
  64. package/{dist/lib → services}/transitions/cssTransitions.d.ts +6 -0
  65. package/{dist/lib → services}/transitions/cssTransitions.js +8 -4
  66. package/{dist/lib → services}/transitions/simpleClassTransition.d.ts +12 -1
  67. package/services/transitions/simpleClassTransition.js +42 -0
  68. package/{dist/lib/types.d.ts → types.d.ts} +43 -4
  69. package/types.js +14 -0
  70. package/{dist/lib/services/directiveUtils.js → utils/directive.js} +1 -1
  71. package/utils/internal/checks.d.ts +49 -0
  72. package/utils/internal/checks.js +60 -0
  73. package/utils/internal/dom.d.ts +25 -0
  74. package/utils/internal/dom.js +61 -0
  75. package/utils/internal/func.d.ts +11 -0
  76. package/utils/internal/func.js +11 -0
  77. package/utils/internal/isFocusable.d.ts +9 -0
  78. package/utils/internal/isFocusable.js +35 -0
  79. package/utils/internal/math.d.ts +5 -0
  80. package/utils/internal/math.js +13 -0
  81. package/utils/internal/promise.d.ts +87 -0
  82. package/utils/internal/promise.js +169 -0
  83. package/utils/internal/scrollbars.d.ts +8 -0
  84. package/{dist/lib/modal → utils/internal}/scrollbars.js +7 -1
  85. package/utils/internal/sort.d.ts +16 -0
  86. package/utils/internal/sort.js +28 -0
  87. package/utils/internal/textDirection.d.ts +7 -0
  88. package/utils/internal/textDirection.js +7 -0
  89. package/utils/internal/traversal.d.ts +54 -0
  90. package/utils/internal/traversal.js +105 -0
  91. package/{dist/lib/services → utils}/stores.d.ts +67 -33
  92. package/{dist/lib/services → utils}/stores.js +121 -59
  93. package/utils/writables.d.ts +32 -0
  94. package/utils/writables.js +72 -0
  95. package/dist/lib/index.d.ts +0 -11
  96. package/dist/lib/index.js +0 -11
  97. package/dist/lib/modal/scrollbars.d.ts +0 -2
  98. package/dist/lib/select.d.ts +0 -199
  99. package/dist/lib/select.js +0 -240
  100. package/dist/lib/services/checks.d.ts +0 -32
  101. package/dist/lib/services/checks.js +0 -43
  102. package/dist/lib/services/index.d.ts +0 -6
  103. package/dist/lib/services/index.js +0 -6
  104. package/dist/lib/services/portal.js +0 -33
  105. package/dist/lib/services/writables.d.ts +0 -7
  106. package/dist/lib/services/writables.js +0 -16
  107. package/dist/lib/transitions/bootstrap/collapse.d.ts +0 -2
  108. package/dist/lib/transitions/bootstrap/fade.d.ts +0 -1
  109. package/dist/lib/transitions/bootstrap/index.d.ts +0 -2
  110. package/dist/lib/transitions/bootstrap/index.js +0 -2
  111. package/dist/lib/transitions/collapse.d.ts +0 -29
  112. package/dist/lib/transitions/index.d.ts +0 -5
  113. package/dist/lib/transitions/index.js +0 -5
  114. package/dist/lib/transitions/simpleClassTransition.js +0 -28
  115. package/dist/lib/transitions/utils.d.ts +0 -20
  116. package/dist/lib/transitions/utils.js +0 -83
  117. package/dist/lib/tsdoc-metadata.json +0 -11
  118. package/dist/lib/types.js +0 -7
  119. package/dist/lib/utils.d.ts +0 -2
  120. package/dist/lib/utils.js +0 -2
  121. /package/{dist/lib/pagination.utils.d.ts → components/pagination/bootstrap.d.ts} +0 -0
  122. /package/{dist/lib/pagination.utils.js → components/pagination/bootstrap.js} +0 -0
  123. /package/{dist/lib/services → services}/focustrack.d.ts +0 -0
  124. /package/{dist/lib → services}/transitions/bootstrap/collapse.js +0 -0
  125. /package/{dist/lib → services}/transitions/bootstrap/fade.js +0 -0
  126. /package/{dist/lib/services/directiveUtils.d.ts → utils/directive.d.ts} +0 -0
@@ -0,0 +1,105 @@
1
+ const removeSymbol = Symbol('remove');
2
+ function _traversal(key, value, fn, index) {
3
+ const transformedValue = fn(key, value, { removeSymbol, index });
4
+ const type = Object.prototype.toString.call(transformedValue);
5
+ switch (type) {
6
+ case '[object Object]': {
7
+ const newJson = {};
8
+ const baseKey = key ? key + '.' : '';
9
+ for (const [name, objectValue] of Object.entries(transformedValue)) {
10
+ const newValue = _traversal(baseKey + name, objectValue, fn);
11
+ if (newValue !== removeSymbol) {
12
+ newJson[name] = newValue;
13
+ }
14
+ }
15
+ return newJson;
16
+ }
17
+ case '[object Map]': {
18
+ const oldMap = transformedValue;
19
+ const newMap = new Map();
20
+ const baseKey = key ? key + '.' : '';
21
+ for (const [mapKey, objectValue] of Object.entries(oldMap)) {
22
+ const newValue = _traversal(baseKey + name, objectValue, fn);
23
+ if (newValue !== removeSymbol) {
24
+ newMap.set(mapKey, newValue);
25
+ }
26
+ }
27
+ return newMap;
28
+ }
29
+ case '[object Array]': {
30
+ const ar = transformedValue;
31
+ const newArray = [];
32
+ const baseKey = (key ? key : '') + '[]';
33
+ for (let i = 0; i < ar.length; i++) {
34
+ const newValue = _traversal(baseKey, ar[i], fn, i);
35
+ if (newValue !== removeSymbol) {
36
+ newArray.push(newValue);
37
+ }
38
+ }
39
+ return newArray;
40
+ }
41
+ case '[object Set]': {
42
+ const oldSet = transformedValue;
43
+ const newSet = new Set();
44
+ const baseKey = (key ? key : '') + '[]';
45
+ const ar = [...oldSet];
46
+ for (let i = 0; i < ar.length; i++) {
47
+ const newValue = _traversal(baseKey, ar[i], fn, i);
48
+ if (newValue !== removeSymbol) {
49
+ newSet.add(newValue);
50
+ }
51
+ }
52
+ return newSet;
53
+ }
54
+ default:
55
+ break;
56
+ }
57
+ return transformedValue;
58
+ }
59
+ /**
60
+ * Creates a JSON walker function that can be used to traverse and transform
61
+ * the properties of a JSON object.
62
+ *
63
+ * @param fn - The callback function called for each property in the JSON object.
64
+ * @returns A function that takes a JSON object as input and applies the provided
65
+ * callback function to each property.
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * const json = {
70
+ * name: 'John',
71
+ * age: 30,
72
+ * address: {
73
+ * city: 'New York',
74
+ * country: 'USA',
75
+ * },
76
+ * useless: '',
77
+ * };
78
+ *
79
+ * const transform = createTraversal((key, value, {removeSymbol}) => {
80
+ * if (key === 'age') {
81
+ * return value * 2; // Double the age
82
+ * }
83
+ * if (key === 'useless') {
84
+ * return removeSymbol;
85
+ * }
86
+ * return value;
87
+ * });
88
+ *
89
+ * const transformedJson = transform(json);
90
+ * ```
91
+ */
92
+ export function createTraversal(fn) {
93
+ return (json) => _traversal('', json, fn);
94
+ }
95
+ /**
96
+ * Utility method to create a promise with resolve
97
+ * @returns a promise with resolve
98
+ */
99
+ export const promiseWithResolve = () => {
100
+ let resolve;
101
+ const promise = new Promise((r) => {
102
+ resolve = r;
103
+ });
104
+ return { promise, resolve: resolve };
105
+ };
@@ -1,9 +1,10 @@
1
- import type { ReadableSignal, WritableSignal, StoreOptions, StoreInput, StoresInputValues } from '@amadeus-it-group/tansu';
2
- export type ToWritableSignal<P, V = P> = {
3
- [K in keyof P & keyof V as `${K & string}$`]-?: WritableSignal<P[K], V[K] | undefined>;
1
+ import type { ReadableSignal, StoreInput, StoresInputValues, WritableSignal } from '@amadeus-it-group/tansu';
2
+ import type { ConfigValidator, PropsConfig, ValuesOrReadableSignals, WritableWithDefaultOptions } from '../types';
3
+ export type ToWritableSignal<P> = {
4
+ [K in keyof P as `${K & string}$`]-?: WritableSignal<P[K], P[K] | undefined>;
4
5
  };
5
- export type ValuesOrStores<T extends object> = {
6
- [K in keyof T]?: ReadableSignal<T[K]> | T[K];
6
+ export type ReadableSignals<T extends object> = {
7
+ [K in keyof T]?: ReadableSignal<T[K] | undefined>;
7
8
  };
8
9
  export type WithoutDollar<S extends `${string}$`> = S extends `${infer U}$` ? U : never;
9
10
  export type ValueOfStore<S extends ReadableSignal<any>> = S extends ReadableSignal<infer U> ? U : never;
@@ -32,7 +33,7 @@ export type ToState<S extends {
32
33
  * @param stores - object of stores
33
34
  * @returns the patch function
34
35
  */
35
- export declare function createPatch<T extends object, V extends object = T>(stores: ToWritableSignal<T, V>): <U extends Partial<T>>(storesValues?: void | U | undefined) => void;
36
+ export declare function createPatch<T extends object>(stores: ToWritableSignal<T>): <U extends Partial<T>>(storesValues?: void | U | undefined) => void;
36
37
  /**
37
38
  * This utility function is designed to compare the first level of two objects.
38
39
  *
@@ -45,18 +46,6 @@ export declare function createPatch<T extends object, V extends object = T>(stor
45
46
  * @returns the object with changed properties
46
47
  */
47
48
  export declare function findChangedProperties<T extends Record<string, any>>(obj1: Partial<T>, obj2: Partial<T>): Partial<T> | null;
48
- export declare const INVALID_VALUE: unique symbol;
49
- export type NormalizeValue<T, U = T> = (value: U) => T | typeof INVALID_VALUE;
50
- export interface WritableWithDefaultOptions<T, U = T> {
51
- /**
52
- * the normalize value function. should return the invalidValue symbol when the provided value is invalid
53
- */
54
- normalizeValue?: NormalizeValue<T, U>;
55
- /**
56
- * the equal function, allowing to compare two values. used to check if a previous and current values are equals.
57
- */
58
- equal?: StoreOptions<T>['equal'];
59
- }
60
49
  /**
61
50
  * Returns a writable store whose value is either its own value (when it is not undefined) or a default value
62
51
  * that comes either from the `config$` store (when it is not undefined) or from `defValue`.
@@ -66,14 +55,47 @@ export interface WritableWithDefaultOptions<T, U = T> {
66
55
  * `set` or `update` functions), or the `defValue` is used instead (if the invalid value comes from the `config$` store).
67
56
  *
68
57
  * @param defValue - Default value used when both the own value and the config$ value are undefined.
69
- * @param config$ - Default value used when the own value is undefined
58
+ * @param config$ - Store containing the default value used when the own value is undefined
70
59
  * @param options - Object which can contain the following optional functions: normalizeValue and equal
60
+ * @param own$ - Store containing the own value
71
61
  * @returns a writable store with the extra default value and normalization logic described above
72
62
  */
73
- export declare function writableWithDefault<T, U = T>(defValue: T, config$?: ReadableSignal<U | undefined>, { normalizeValue, equal }?: WritableWithDefaultOptions<T, U>): WritableSignal<T, U | undefined>;
74
- export type ConfigValidator<T extends object, U extends object = T> = {
75
- [K in keyof T & keyof U]?: WritableWithDefaultOptions<T[K], U[K]>;
76
- };
63
+ export declare function writableWithDefault<T>(defValue: T, config$?: ReadableSignal<T | undefined>, options?: WritableWithDefaultOptions<T>, own$?: WritableSignal<T | undefined>): WritableSignal<T, T | undefined>;
64
+ /**
65
+ * Returns true if the provided argument is a store (ReadableSignal).
66
+ * @param x - argument that is tested
67
+ * @returns true if the argument is a store (ReadableSignal)
68
+ */
69
+ export declare const isStore: (x: any) => x is ReadableSignal<any>;
70
+ /**
71
+ * If the provided argument is already a store, it is returned as is, otherwise, a readable store is created with the provided argument as its initial value.
72
+ * @param x - either a store or a simple value
73
+ * @returns either x if x is already a store, or readable(x) otherwise
74
+ */
75
+ export declare const toReadableStore: <T>(x: T | ReadableSignal<T>) => ReadableSignal<T>;
76
+ /**
77
+ * If the provided argument is already a store, it is returned as is, otherwise, a writable store is created with the provided argument as its initial value.
78
+ * @param x - either a writable store or a simple value
79
+ * @returns either x if x is already a store, or writable(x) otherwise
80
+ */
81
+ export declare const toWritableStore: <T>(x: T | WritableSignal<T, T>) => WritableSignal<T, T>;
82
+ /**
83
+ * Extract and normalize config stores.
84
+ *
85
+ * @param keys - the keys of the stores to extract / normalize
86
+ * @param config - the config stores
87
+ * @returns the normalized config stores
88
+ */
89
+ export declare const normalizeConfigStores: <T extends object>(keys: (keyof T)[], config?: ReadableSignal<Partial<T>> | ValuesOrReadableSignals<T> | undefined) => ReadableSignals<T>;
90
+ /**
91
+ * Merge two stores configs into one
92
+ *
93
+ * @param keys - the keys of the stores to extract and merge from the two provided configs
94
+ * @param config1 - the first config
95
+ * @param config2 - the second config
96
+ * @returns the merged config
97
+ */
98
+ export declare const mergeConfigStores: <T extends object>(keys: (keyof T)[], config1?: ReadableSignals<T> | undefined, config2?: ReadableSignals<T> | undefined) => ReadableSignals<T>;
77
99
  /**
78
100
  * Returns an object containing, for each property of `defConfig`, a corresponding writable with the normalization and default value logic
79
101
  * described in {@link writableWithDefault}. Keys in the returned object are the same as the ones present in `defConfig`,
@@ -81,8 +103,7 @@ export type ConfigValidator<T extends object, U extends object = T> = {
81
103
  *
82
104
  * @param defConfig - object containing, for each property, a default value to use in case `config$` does not provide the suitable default
83
105
  * value for that property
84
- * @param config - either a store of objects containing, for each property of `defConfig`, the default value or an object containing
85
- * for each property of `defConfig` either a store containing the default value or the default value itself
106
+ * @param propsConfig - object defining the config and props
86
107
  * @param options - object containing, for each property of `defConfig`, an optional object with the following optional functions: normalizeValue and equal
87
108
  * @returns an object containing writables
88
109
  *
@@ -90,8 +111,8 @@ export type ConfigValidator<T extends object, U extends object = T> = {
90
111
  * ```ts
91
112
  * const defConfig = {propA: 1};
92
113
  * const validation = {propA: {normalizeValue: value => +value}};
93
- * const config$ = writable({propA: 5});
94
- * const {propA$} = writablesWithDefault(defConfig, config$, validation);
114
+ * const config = writable({propA: 5});
115
+ * const {propA$} = writablesWithDefault(defConfig, {config}, validation);
95
116
  * ```
96
117
  *
97
118
  * @example With an object containing a value and a store
@@ -99,15 +120,15 @@ export type ConfigValidator<T extends object, U extends object = T> = {
99
120
  * const defConfig = {propA: 1, propB: 2};
100
121
  * const validation = {propA: {normalizeValue: value => +value}};
101
122
  * const config = {propA: 5, propB: writable(3)};
102
- * const {propA$, propB$} = writablesWithDefault(defConfig, config, validation);
123
+ * const {propA$, propB$} = writablesWithDefault(defConfig, {config}, validation);
103
124
  * ```
104
125
  */
105
- export declare const writablesWithDefault: <T extends object, U extends object = T>(defConfig: T, config?: PropsConfig<U> | undefined, options?: ConfigValidator<T, U> | undefined) => ToWritableSignal<T, U>;
126
+ export declare const writablesWithDefault: <T extends object>(defConfig: T, propsConfig?: PropsConfig<T> | undefined, options?: ConfigValidator<T> | undefined) => ToWritableSignal<T>;
106
127
  /**
107
128
  * Shortcut for calling both {@link writablesWithDefault} and {@link createPatch} in one call.
108
129
  * @param defConfig - object containing, for each property, a default value to use in case `config` does not provide the suitable default
109
130
  * value for that property
110
- * @param config - either a store of objects containing, for each property of `defConfig`, the default value or an object containing
131
+ * @param propsConfig - either a store of objects containing, for each property of `defConfig`, the default value or an object containing
111
132
  * for each property of `defConfig` either a store containing the default value or the default value itself
112
133
  * @param options - object containing, for each property of `defConfig`, an optional object with the following optional functions: normalizeValue and equal
113
134
  * @returns an array with two items: the first one containing the writables (returned by {@link writablesWithDefault}),
@@ -129,12 +150,25 @@ export declare const writablesWithDefault: <T extends object, U extends object =
129
150
  * const [{propA$, propB$}, patch] = writablesForProps(defConfig, config, validation);
130
151
  * ```
131
152
  */
132
- export declare const writablesForProps: <T extends object, U extends object = T>(defConfig: T, config?: PropsConfig<U> | undefined, options?: { [K in keyof T & keyof U]?: WritableWithDefaultOptions<T[K], U[K]> | undefined; } | undefined) => [ToWritableSignal<T, U>, <U_1 extends Partial<T>>(storesValues?: void | U_1 | undefined) => void];
133
- export type PropsConfig<T extends object> = ReadableSignal<Partial<T>> | ValuesOrStores<T>;
153
+ export declare const writablesForProps: <T extends object>(defConfig: T, propsConfig?: PropsConfig<T> | undefined, options?: { [K in keyof T]?: WritableWithDefaultOptions<T[K]> | undefined; } | undefined) => [ToWritableSignal<T>, <U extends Partial<T>>(storesValues?: void | U | undefined) => void];
154
+ /**
155
+ * Using input stores, this function builds an object containing the stores as readable and a global state.
156
+ *
157
+ * @param inputStores - the input stores
158
+ * @returns the object containing the stores as readable and the global state
159
+ */
134
160
  export declare const stateStores: <A extends {
135
161
  [x: `${string}$`]: ReadableSignal<any>;
136
162
  }>(inputStores: A) => {
137
163
  state$: ReadableSignal<ToState<A>>;
138
164
  stores: { [key in `${string}$` & keyof A]: ReadableSignal<ValueOfStore<A[key]>>; };
139
165
  };
140
- export declare const bindableDerived: <T, U extends [WritableSignal<T, T>, ...StoreInput<any>[]]>(onChange$: ReadableSignal<(value: T) => void>, stores: U, adjustValue: (arg: StoresInputValues<U>) => T) => ReadableSignal<T>;
166
+ /**
167
+ * Creates a computed store that binds to multiple stores and triggers a callback when the value changes.
168
+ * @param onChange$ - A readable signal callback function to execute when the value changes.
169
+ * @param stores - An array of Svelte stores, with the main store at index 0.
170
+ * @param adjustValue - A function to adjust the value of the main store. By default, the value of the main store is returned.
171
+ * @param equal - A function to determine if two values are equal. Used to compare the ajusted value with the current one.
172
+ * @returns The derived store that reflects the combined state of the input stores.
173
+ */
174
+ export declare const bindableDerived: <T, U extends [WritableSignal<T, T>, ...StoreInput<any>[]]>(onChange$: ReadableSignal<(value: T) => void>, stores: U, adjustValue?: (arg: StoresInputValues<U>) => T, equal?: (currentValue: T, newValue: T) => boolean) => ReadableSignal<T>;
@@ -1,5 +1,6 @@
1
- import { batch, computed, derived, get, readable, writable, asReadable } from '@amadeus-it-group/tansu';
2
- import { identity } from '../utils';
1
+ import { asReadable, asWritable, batch, computed, derived, get, readable, writable } from '@amadeus-it-group/tansu';
2
+ import { INVALID_VALUE } from '../types';
3
+ import { identity } from './internal/func';
3
4
  /**
4
5
  *
5
6
  * Utility function designed to create a `patch` function related to the provided stores.
@@ -56,11 +57,6 @@ export function findChangedProperties(obj1, obj2) {
56
57
  }
57
58
  return hasUpdate ? changedValues : null;
58
59
  }
59
- const update = function (updater) {
60
- this.set(updater(this()));
61
- };
62
- export const INVALID_VALUE = Symbol();
63
- /* eslint-disable jsdoc/check-param-names, jsdoc/require-param */
64
60
  /**
65
61
  * Returns a writable store whose value is either its own value (when it is not undefined) or a default value
66
62
  * that comes either from the `config$` store (when it is not undefined) or from `defValue`.
@@ -70,41 +66,93 @@ export const INVALID_VALUE = Symbol();
70
66
  * `set` or `update` functions), or the `defValue` is used instead (if the invalid value comes from the `config$` store).
71
67
  *
72
68
  * @param defValue - Default value used when both the own value and the config$ value are undefined.
73
- * @param config$ - Default value used when the own value is undefined
69
+ * @param config$ - Store containing the default value used when the own value is undefined
74
70
  * @param options - Object which can contain the following optional functions: normalizeValue and equal
71
+ * @param own$ - Store containing the own value
75
72
  * @returns a writable store with the extra default value and normalization logic described above
76
73
  */
77
- export function writableWithDefault(defValue, config$ = readable(undefined), { normalizeValue = identity, equal = Object.is } = {}) {
78
- const own$ = writable(undefined);
79
- const validatedDefConfig$ = computed(() => {
80
- const value = config$();
74
+ export function writableWithDefault(defValue, config$ = readable(undefined), options = {}, own$ = writable(undefined)) {
75
+ const { normalizeValue = identity, equal = Object.is } = options;
76
+ const getDefValue = () => defValue;
77
+ const callNormalizeValue = (value, defValue = getDefValue) => {
81
78
  const normalizedValue = value === undefined ? undefined : normalizeValue(value);
82
79
  if (normalizedValue === INVALID_VALUE) {
83
- console.error('Not using invalid value from default config', value);
84
- return defValue;
80
+ console.error('Not using invalid value', value);
81
+ return defValue();
85
82
  }
86
83
  if (normalizedValue === undefined) {
87
- return defValue;
84
+ return defValue();
88
85
  }
89
86
  return normalizedValue;
90
- }, { equal });
91
- return asReadable(computed(() => {
92
- const ownValue = own$();
93
- return ownValue !== undefined ? ownValue : validatedDefConfig$();
94
- }, { equal }), {
95
- set(value) {
96
- const normalizedValue = value === undefined ? undefined : normalizeValue(value);
87
+ };
88
+ const validatedDefConfig$ = computed(() => callNormalizeValue(config$()), { equal });
89
+ const validatedOwnValue$ = computed(() => callNormalizeValue(own$(), validatedDefConfig$), { equal });
90
+ return asWritable(validatedOwnValue$, (value) => {
91
+ if (value !== undefined) {
92
+ const normalizedValue = normalizeValue(value);
97
93
  if (normalizedValue === INVALID_VALUE) {
98
94
  console.error('Not setting invalid value', value);
95
+ return;
99
96
  }
100
- else {
101
- own$.set(normalizedValue);
102
- }
103
- },
104
- update,
97
+ value = normalizedValue;
98
+ }
99
+ own$.set(value);
105
100
  });
106
101
  }
107
- const isStore = (x) => !!(x && typeof x === 'function' && 'subscribe' in x);
102
+ /**
103
+ * Returns true if the provided argument is a store (ReadableSignal).
104
+ * @param x - argument that is tested
105
+ * @returns true if the argument is a store (ReadableSignal)
106
+ */
107
+ export const isStore = (x) => !!(x && typeof x === 'function' && 'subscribe' in x);
108
+ /**
109
+ * If the provided argument is already a store, it is returned as is, otherwise, a readable store is created with the provided argument as its initial value.
110
+ * @param x - either a store or a simple value
111
+ * @returns either x if x is already a store, or readable(x) otherwise
112
+ */
113
+ export const toReadableStore = (x) => (isStore(x) ? x : readable(x));
114
+ /**
115
+ * If the provided argument is already a store, it is returned as is, otherwise, a writable store is created with the provided argument as its initial value.
116
+ * @param x - either a writable store or a simple value
117
+ * @returns either x if x is already a store, or writable(x) otherwise
118
+ */
119
+ export const toWritableStore = (x) => (isStore(x) ? x : writable(x));
120
+ /**
121
+ * Extract and normalize config stores.
122
+ *
123
+ * @param keys - the keys of the stores to extract / normalize
124
+ * @param config - the config stores
125
+ * @returns the normalized config stores
126
+ */
127
+ export const normalizeConfigStores = (keys, config) => {
128
+ const res = {};
129
+ if (config) {
130
+ const configIsStore = isStore(config);
131
+ for (const key of keys) {
132
+ res[key] = configIsStore
133
+ ? computed(() => config()[key])
134
+ : toReadableStore(config[key]);
135
+ }
136
+ }
137
+ return res;
138
+ };
139
+ /**
140
+ * Merge two stores configs into one
141
+ *
142
+ * @param keys - the keys of the stores to extract and merge from the two provided configs
143
+ * @param config1 - the first config
144
+ * @param config2 - the second config
145
+ * @returns the merged config
146
+ */
147
+ export const mergeConfigStores = (keys, config1, config2) => {
148
+ const res = {};
149
+ for (const key of keys) {
150
+ const config1Store = config1?.[key];
151
+ const config2Store = config2?.[key];
152
+ res[key] = config1Store && config2Store ? computed(() => config1Store() ?? config2Store()) : config1Store || config2Store;
153
+ }
154
+ return res;
155
+ };
108
156
  /**
109
157
  * Returns an object containing, for each property of `defConfig`, a corresponding writable with the normalization and default value logic
110
158
  * described in {@link writableWithDefault}. Keys in the returned object are the same as the ones present in `defConfig`,
@@ -112,8 +160,7 @@ const isStore = (x) => !!(x && typeof x === 'function' && 'subscribe' in x);
112
160
  *
113
161
  * @param defConfig - object containing, for each property, a default value to use in case `config$` does not provide the suitable default
114
162
  * value for that property
115
- * @param config - either a store of objects containing, for each property of `defConfig`, the default value or an object containing
116
- * for each property of `defConfig` either a store containing the default value or the default value itself
163
+ * @param propsConfig - object defining the config and props
117
164
  * @param options - object containing, for each property of `defConfig`, an optional object with the following optional functions: normalizeValue and equal
118
165
  * @returns an object containing writables
119
166
  *
@@ -121,8 +168,8 @@ const isStore = (x) => !!(x && typeof x === 'function' && 'subscribe' in x);
121
168
  * ```ts
122
169
  * const defConfig = {propA: 1};
123
170
  * const validation = {propA: {normalizeValue: value => +value}};
124
- * const config$ = writable({propA: 5});
125
- * const {propA$} = writablesWithDefault(defConfig, config$, validation);
171
+ * const config = writable({propA: 5});
172
+ * const {propA$} = writablesWithDefault(defConfig, {config}, validation);
126
173
  * ```
127
174
  *
128
175
  * @example With an object containing a value and a store
@@ -130,19 +177,17 @@ const isStore = (x) => !!(x && typeof x === 'function' && 'subscribe' in x);
130
177
  * const defConfig = {propA: 1, propB: 2};
131
178
  * const validation = {propA: {normalizeValue: value => +value}};
132
179
  * const config = {propA: 5, propB: writable(3)};
133
- * const {propA$, propB$} = writablesWithDefault(defConfig, config, validation);
180
+ * const {propA$, propB$} = writablesWithDefault(defConfig, {config}, validation);
134
181
  * ```
135
182
  */
136
- export const writablesWithDefault = (defConfig, config, options) => {
183
+ export const writablesWithDefault = (defConfig, propsConfig, options) => {
137
184
  const res = {};
138
- const configIsStore = isStore(config);
139
- for (const key of Object.keys(defConfig)) {
140
- let store = configIsStore ? computed(() => config()[key]) : undefined;
141
- if (!configIsStore && config) {
142
- const value = config[key];
143
- store = isStore(value) ? value : readable(value);
144
- }
145
- res[`${key}$`] = writableWithDefault(defConfig[key], store, options?.[key]);
185
+ const keys = Object.keys(defConfig);
186
+ const configStores = normalizeConfigStores(keys, propsConfig?.config);
187
+ const props = propsConfig?.props;
188
+ for (const key of keys) {
189
+ const propValue = props?.[key];
190
+ res[`${key}$`] = writableWithDefault(defConfig[key], configStores[key], options?.[key], toWritableStore(propValue));
146
191
  }
147
192
  return res;
148
193
  };
@@ -150,7 +195,7 @@ export const writablesWithDefault = (defConfig, config, options) => {
150
195
  * Shortcut for calling both {@link writablesWithDefault} and {@link createPatch} in one call.
151
196
  * @param defConfig - object containing, for each property, a default value to use in case `config` does not provide the suitable default
152
197
  * value for that property
153
- * @param config - either a store of objects containing, for each property of `defConfig`, the default value or an object containing
198
+ * @param propsConfig - either a store of objects containing, for each property of `defConfig`, the default value or an object containing
154
199
  * for each property of `defConfig` either a store containing the default value or the default value itself
155
200
  * @param options - object containing, for each property of `defConfig`, an optional object with the following optional functions: normalizeValue and equal
156
201
  * @returns an array with two items: the first one containing the writables (returned by {@link writablesWithDefault}),
@@ -172,10 +217,16 @@ export const writablesWithDefault = (defConfig, config, options) => {
172
217
  * const [{propA$, propB$}, patch] = writablesForProps(defConfig, config, validation);
173
218
  * ```
174
219
  */
175
- export const writablesForProps = (defConfig, config, options) => {
176
- const stores = writablesWithDefault(defConfig, config, options);
220
+ export const writablesForProps = (defConfig, propsConfig, options) => {
221
+ const stores = writablesWithDefault(defConfig, propsConfig, options);
177
222
  return [stores, createPatch(stores)];
178
223
  };
224
+ /**
225
+ * Using input stores, this function builds an object containing the stores as readable and a global state.
226
+ *
227
+ * @param inputStores - the input stores
228
+ * @returns the object containing the stores as readable and the global state
229
+ */
179
230
  export const stateStores = (inputStores) => {
180
231
  const storesNames = [];
181
232
  const storesArray = [];
@@ -200,20 +251,31 @@ export const stateStores = (inputStores) => {
200
251
  }),
201
252
  };
202
253
  };
203
- export const bindableDerived = (onChange$, stores, adjustValue) => {
254
+ /**
255
+ * Creates a computed store that binds to multiple stores and triggers a callback when the value changes.
256
+ * @param onChange$ - A readable signal callback function to execute when the value changes.
257
+ * @param stores - An array of Svelte stores, with the main store at index 0.
258
+ * @param adjustValue - A function to adjust the value of the main store. By default, the value of the main store is returned.
259
+ * @param equal - A function to determine if two values are equal. Used to compare the ajusted value with the current one.
260
+ * @returns The derived store that reflects the combined state of the input stores.
261
+ */
262
+ export const bindableDerived = (onChange$, stores, adjustValue = (arg) => arg[0], equal = (currentValue, newValue) => newValue === currentValue) => {
204
263
  let currentValue = stores[0]();
205
- return derived(stores, (values) => {
206
- const newValue = adjustValue(values);
207
- const rectifiedValue = newValue !== values[0];
208
- if (rectifiedValue) {
209
- stores[0].set(newValue);
210
- }
211
- if (rectifiedValue || newValue !== currentValue) {
212
- currentValue = newValue;
213
- // TODO check if we should do this async to avoid issue
214
- // with angular and react only when rectifiedValue is true?
215
- onChange$()(newValue);
216
- }
217
- return newValue;
264
+ return derived(stores, {
265
+ derive(values) {
266
+ const newValue = adjustValue(values);
267
+ const rectifiedValue = !equal(values[0], newValue);
268
+ if (rectifiedValue) {
269
+ stores[0].set(newValue);
270
+ }
271
+ if (rectifiedValue || !equal(currentValue, newValue)) {
272
+ currentValue = newValue;
273
+ // TODO check if we should do this async to avoid issue
274
+ // with angular and react only when rectifiedValue is true?
275
+ onChange$()(newValue);
276
+ }
277
+ return newValue;
278
+ },
279
+ equal,
218
280
  });
219
281
  };
@@ -0,0 +1,32 @@
1
+ import type { WritableWithDefaultOptions } from '../types';
2
+ import { INVALID_VALUE } from '../types';
3
+ /**
4
+ * Check if a value respects a provided type guard.
5
+ *
6
+ * @param filter - the guard function
7
+ * @returns a function that takes a value as input, checks if it respects the type guard and returns `INVALID_VALUE` otherwise
8
+ */
9
+ export declare const testToNormalizeValue: <T>(filter: (value: any) => value is T) => (value: any) => typeof INVALID_VALUE | T;
10
+ export declare const typeNumber: WritableWithDefaultOptions<number>;
11
+ export interface TypeNumberInRangeOptions {
12
+ /** If `true`, the range checking will be strict, excluding the minimum and maximum values. Default is `false`. */
13
+ strict?: boolean;
14
+ /** If `true`, values outside the range will be clamped to the minimum or maximum. Default is `true`. */
15
+ useClamp?: boolean;
16
+ }
17
+ /**
18
+ * Factory function for creating a type constraint for numbers within a specified range.
19
+ *
20
+ * @param min - The minimum value.
21
+ * @param max - The maximum value.
22
+ * @param options - Additional options to customize the behavior.
23
+ *
24
+ * @returns A type guard function that returns the clamp value or INVALID_VALUE depending on the provided options.
25
+ */
26
+ export declare function typeNumberInRangeFactory(min: number, max: number, options?: TypeNumberInRangeOptions): WritableWithDefaultOptions<number>;
27
+ export declare const typeBoolean: WritableWithDefaultOptions<boolean>;
28
+ export declare const typeBooleanOrNull: WritableWithDefaultOptions<boolean | null>;
29
+ export declare const typeString: WritableWithDefaultOptions<string>;
30
+ export declare const typeFunction: WritableWithDefaultOptions<(...args: any[]) => any>;
31
+ export declare const typeHTMLElementOrNull: WritableWithDefaultOptions<HTMLElement | null>;
32
+ export declare const typeArray: WritableWithDefaultOptions<any[]>;
@@ -0,0 +1,72 @@
1
+ import { allowNull, clamp, isArray, isBoolean, isFunction, isHTMLElement, isNumber, isString } from './internal/checks';
2
+ import { INVALID_VALUE } from '../types';
3
+ /**
4
+ * Check if a value respects a provided type guard.
5
+ *
6
+ * @param filter - the guard function
7
+ * @returns a function that takes a value as input, checks if it respects the type guard and returns `INVALID_VALUE` otherwise
8
+ */
9
+ export const testToNormalizeValue = (filter) => (value) => filter(value) ? value : INVALID_VALUE;
10
+ const numberNormalizeFn = testToNormalizeValue(isNumber);
11
+ export const typeNumber = {
12
+ normalizeValue: numberNormalizeFn,
13
+ };
14
+ /**
15
+ * Factory function for creating a type constraint for numbers within a specified range.
16
+ *
17
+ * @param min - The minimum value.
18
+ * @param max - The maximum value.
19
+ * @param options - Additional options to customize the behavior.
20
+ *
21
+ * @returns A type guard function that returns the clamp value or INVALID_VALUE depending on the provided options.
22
+ */
23
+ export function typeNumberInRangeFactory(min, max, options = {}) {
24
+ const { strict = false, useClamp = true } = options;
25
+ return {
26
+ normalizeValue: (value) => {
27
+ let normalizedNumber = numberNormalizeFn(value);
28
+ if (normalizedNumber !== INVALID_VALUE) {
29
+ if (!strict && useClamp) {
30
+ normalizedNumber = clamp(normalizedNumber, max, min);
31
+ }
32
+ if (normalizedNumber >= min && normalizedNumber <= max) {
33
+ if (!strict || (normalizedNumber !== min && normalizedNumber !== max)) {
34
+ return normalizedNumber;
35
+ }
36
+ }
37
+ }
38
+ return INVALID_VALUE;
39
+ },
40
+ };
41
+ }
42
+ export const typeBoolean = {
43
+ normalizeValue: testToNormalizeValue(isBoolean),
44
+ };
45
+ export const typeBooleanOrNull = {
46
+ normalizeValue: testToNormalizeValue(allowNull(isBoolean)),
47
+ };
48
+ export const typeString = {
49
+ normalizeValue: testToNormalizeValue(isString),
50
+ };
51
+ export const typeFunction = {
52
+ normalizeValue: testToNormalizeValue(isFunction),
53
+ equal: Object.is,
54
+ };
55
+ export const typeHTMLElementOrNull = {
56
+ normalizeValue: testToNormalizeValue(allowNull(isHTMLElement)),
57
+ equal: Object.is,
58
+ };
59
+ export const typeArray = {
60
+ normalizeValue: testToNormalizeValue(isArray),
61
+ equal: (a, b) => {
62
+ if (a === b) {
63
+ return true;
64
+ }
65
+ else {
66
+ if (a?.length !== b?.length) {
67
+ return false;
68
+ }
69
+ return a.every((val, index) => val === b[index]);
70
+ }
71
+ },
72
+ };
@@ -1,11 +0,0 @@
1
- export * from './types';
2
- export * from './select';
3
- export * from './services';
4
- export * from './transitions';
5
- export * from './rating';
6
- export * from './pagination';
7
- export * from './pagination.utils';
8
- export * from './config';
9
- export * from './modal/modal';
10
- export * from './alert';
11
- export * from './accordion';
package/dist/lib/index.js DELETED
@@ -1,11 +0,0 @@
1
- export * from './types';
2
- export * from './select';
3
- export * from './services';
4
- export * from './transitions';
5
- export * from './rating';
6
- export * from './pagination';
7
- export * from './pagination.utils';
8
- export * from './config';
9
- export * from './modal/modal';
10
- export * from './alert';
11
- export * from './accordion';