@agnos-ui/core 0.2.0 → 0.3.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/README.md +3 -3
- package/accordion-BaWN0_n-.js +330 -0
- package/accordion-BuIgxZ0S.cjs +329 -0
- package/alert-C4jCg9Pl.cjs +8 -0
- package/alert-Dlf-BV98.js +9 -0
- package/common-DRdsw5m8.js +76 -0
- package/common-nJRMXbwj.cjs +75 -0
- package/components/accordion/accordion.d.ts +38 -49
- package/components/accordion/index.cjs +7 -0
- package/components/accordion/index.js +7 -1
- package/components/alert/alert.d.ts +3 -12
- package/components/alert/common.d.ts +4 -13
- package/components/alert/index.cjs +10 -0
- package/components/alert/index.js +10 -2
- package/components/modal/index.cjs +7 -0
- package/components/modal/index.js +7 -1
- package/components/modal/modal.d.ts +7 -49
- package/components/pagination/index.cjs +5 -0
- package/components/pagination/index.d.ts +0 -1
- package/components/pagination/index.js +5 -2
- package/components/pagination/pagination.d.ts +68 -139
- package/components/progressbar/index.cjs +5 -0
- package/components/progressbar/index.js +5 -1
- package/components/progressbar/progressbar.d.ts +6 -22
- package/components/rating/index.cjs +5 -0
- package/components/rating/index.js +5 -1
- package/components/rating/rating.d.ts +16 -11
- package/components/select/index.cjs +6 -0
- package/components/select/index.js +6 -1
- package/components/select/select.d.ts +15 -33
- package/components/slider/index.cjs +5 -0
- package/components/slider/index.js +5 -1
- package/components/slider/slider.d.ts +29 -35
- package/components/toast/index.cjs +5 -0
- package/components/toast/index.js +5 -1
- package/components/toast/toast.d.ts +11 -8
- package/config.cjs +38 -0
- package/config.d.ts +1 -1
- package/config.js +35 -50
- package/directive-BTSEYLF3.cjs +404 -0
- package/directive-DCYlDznf.js +405 -0
- package/func-DR0n-ShK.js +7 -0
- package/func-Qd3cD9a3.cjs +6 -0
- package/index.cjs +119 -0
- package/index.d.ts +1 -1
- package/index.js +119 -33
- package/modal-BI2qUu1M.js +251 -0
- package/modal-rzMpATf5.cjs +250 -0
- package/package.json +29 -19
- package/pagination--GkwduJn.js +263 -0
- package/pagination-EWSWQT1I.cjs +262 -0
- package/progressbar-DH7DHYMp.cjs +83 -0
- package/progressbar-DuRX7_my.js +84 -0
- package/promise-BMJ8qhA8.cjs +118 -0
- package/promise-CY2U8bTP.js +119 -0
- package/rating-BR5wD7y2.js +173 -0
- package/rating-CmuYUSxy.cjs +172 -0
- package/select-BCs6HQWn.js +358 -0
- package/select-CCIKn8WR.cjs +357 -0
- package/services/extendWidget.cjs +32 -0
- package/services/extendWidget.d.ts +2 -1
- package/services/extendWidget.js +31 -34
- package/services/floatingUI.cjs +131 -0
- package/services/floatingUI.d.ts +30 -14
- package/services/floatingUI.js +128 -102
- package/services/focustrack.cjs +47 -0
- package/services/focustrack.js +45 -44
- package/services/hash.cjs +15 -0
- package/services/hash.js +14 -12
- package/services/intersection.cjs +53 -0
- package/services/intersection.js +48 -50
- package/services/matchMedia.cjs +13 -0
- package/services/matchMedia.d.ts +7 -0
- package/services/matchMedia.js +13 -0
- package/services/navManager.cjs +196 -0
- package/services/navManager.d.ts +9 -9
- package/services/navManager.js +186 -168
- package/services/portal.cjs +43 -0
- package/services/portal.js +41 -42
- package/services/resizeObserver.cjs +32 -0
- package/services/resizeObserver.d.ts +1 -1
- package/services/resizeObserver.js +31 -28
- package/services/siblingsInert.cjs +40 -0
- package/services/siblingsInert.js +31 -31
- package/services/transitions/baseTransitions.cjs +171 -0
- package/services/transitions/baseTransitions.d.ts +16 -16
- package/services/transitions/baseTransitions.js +159 -170
- package/services/transitions/collapse.cjs +44 -0
- package/services/transitions/collapse.js +41 -49
- package/services/transitions/cssTransitions.cjs +32 -0
- package/services/transitions/cssTransitions.d.ts +2 -1
- package/services/transitions/cssTransitions.js +29 -39
- package/services/transitions/simpleClassTransition.cjs +31 -0
- package/services/transitions/simpleClassTransition.js +30 -41
- package/slider-CA_fszn7.js +536 -0
- package/slider-DsLvT87U.cjs +535 -0
- package/toast-8tWp6x89.js +63 -0
- package/toast-Aw8o0Iwe.cjs +62 -0
- package/types.cjs +12 -0
- package/types.d.ts +21 -1
- package/types.js +11 -13
- package/utils/directive.cjs +26 -0
- package/utils/directive.d.ts +148 -5
- package/utils/directive.js +25 -205
- package/utils/internal/dom.d.ts +43 -4
- package/utils/internal/promise.d.ts +2 -2
- package/utils/internal/ssrHTMLElement.d.ts +7 -0
- package/utils/stores.cjs +163 -0
- package/utils/stores.d.ts +9 -17
- package/utils/stores.js +149 -284
- package/utils/writables.cjs +13 -0
- package/utils/writables.js +12 -71
- package/writables-D46sFgGK.cjs +85 -0
- package/writables-DoU_XYTX.js +86 -0
- package/components/accordion/accordion.js +0 -264
- package/components/alert/alert.js +0 -22
- package/components/alert/common.js +0 -69
- package/components/commonProps.js +0 -1
- package/components/modal/modal.js +0 -186
- package/components/pagination/bootstrap.d.ts +0 -8
- package/components/pagination/bootstrap.js +0 -110
- package/components/pagination/pagination.js +0 -195
- package/components/progressbar/progressbar.js +0 -78
- package/components/rating/rating.js +0 -137
- package/components/select/select.js +0 -297
- package/components/slider/slider.js +0 -420
- package/components/toast/toast.js +0 -43
- package/services/transitions/bootstrap/collapse.d.ts +0 -2
- package/services/transitions/bootstrap/collapse.js +0 -15
- package/services/transitions/bootstrap/fade.d.ts +0 -1
- package/services/transitions/bootstrap/fade.js +0 -7
- package/services/transitions/bootstrap.d.ts +0 -2
- package/services/transitions/bootstrap.js +0 -2
- package/utils/internal/checks.js +0 -60
- package/utils/internal/dom.js +0 -82
- package/utils/internal/func.js +0 -11
- package/utils/internal/isFocusable.js +0 -37
- package/utils/internal/math.js +0 -13
- package/utils/internal/promise.js +0 -169
- package/utils/internal/scrollbars.js +0 -33
- package/utils/internal/sort.js +0 -28
- package/utils/internal/textDirection.js +0 -7
- package/utils/internal/traversal.js +0 -105
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
const isInertOrInvisible = (element) => {
|
|
2
|
-
let curElement = element;
|
|
3
|
-
while (curElement) {
|
|
4
|
-
const style = getComputedStyle(curElement);
|
|
5
|
-
if (curElement.inert || curElement.hidden || style.display === 'none' || style.visibility === 'hidden') {
|
|
6
|
-
return true;
|
|
7
|
-
}
|
|
8
|
-
curElement = curElement.parentElement;
|
|
9
|
-
}
|
|
10
|
-
return false;
|
|
11
|
-
};
|
|
12
|
-
const checkNotDisabled = (element) => {
|
|
13
|
-
if (element.disabled) {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
const parentFieldset = element.parentElement?.closest('fieldset');
|
|
17
|
-
return parentFieldset ? checkNotDisabled(parentFieldset) : true;
|
|
18
|
-
};
|
|
19
|
-
const isFocusableOtherTags = (element) => element.isContentEditable || !!element.hasAttribute('tabindex');
|
|
20
|
-
const isFocusableByTagName = {
|
|
21
|
-
INPUT: (element) => element.type !== 'hidden' && checkNotDisabled(element),
|
|
22
|
-
SELECT: checkNotDisabled,
|
|
23
|
-
TEXTAREA: checkNotDisabled,
|
|
24
|
-
BUTTON: checkNotDisabled,
|
|
25
|
-
A: (element) => !!element.href || isFocusableOtherTags(element),
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* Returns true if the given HTML element is programmatically focusable.
|
|
29
|
-
* Warning: this is a best-effort approximation of whether the element is really focusable.
|
|
30
|
-
* It may not handle all use cases accurately.
|
|
31
|
-
*
|
|
32
|
-
* @param element - element to test
|
|
33
|
-
* @returns true if the element is programmatically focusable.
|
|
34
|
-
*/
|
|
35
|
-
export const isFocusable = (element) => {
|
|
36
|
-
return document.contains(element) && !isInertOrInvisible(element) && (isFocusableByTagName[element.tagName] ?? isFocusableOtherTags)(element);
|
|
37
|
-
};
|
package/utils/internal/math.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const decimalRegExp = /(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/;
|
|
2
|
-
/**
|
|
3
|
-
* @param number - decimal number
|
|
4
|
-
* @returns the decimal precision of the number
|
|
5
|
-
*/
|
|
6
|
-
export function getDecimalPrecision(number) {
|
|
7
|
-
const matches = ('' + number).match(decimalRegExp);
|
|
8
|
-
return Math.max(0,
|
|
9
|
-
// Number of digits right of decimal point.
|
|
10
|
-
(matches[1]?.length ?? 0) -
|
|
11
|
-
// Adjust for exponential notation.
|
|
12
|
-
(+matches[2] || 0));
|
|
13
|
-
}
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import { asReadable, computed, derived, equal, readable, writable } from '@amadeus-it-group/tansu';
|
|
2
|
-
import { noop } from './func';
|
|
3
|
-
export const promisePending = { status: 'pending' };
|
|
4
|
-
const isThenable = (value) => {
|
|
5
|
-
// cf https://tc39.es/ecma262/#sec-promise-resolve-functions
|
|
6
|
-
const type = typeof value;
|
|
7
|
-
return (type === 'object' && value !== null) || type === 'function' ? typeof value.then === 'function' : false;
|
|
8
|
-
};
|
|
9
|
-
const createPromiseStateStore = (promise) => {
|
|
10
|
-
const store = writable(promisePending);
|
|
11
|
-
Promise.resolve(promise).then((value) => store.set({ status: 'fulfilled', value }), (reason) => store.set({ status: 'rejected', reason }));
|
|
12
|
-
return asReadable(store);
|
|
13
|
-
};
|
|
14
|
-
const promiseWeakMap = new WeakMap();
|
|
15
|
-
/**
|
|
16
|
-
* Create a readable promise state store from a promise.
|
|
17
|
-
*
|
|
18
|
-
* The state of the returned store tracks the state of the promise and the resolved value or rejection reason.
|
|
19
|
-
*
|
|
20
|
-
* @param value - the promise
|
|
21
|
-
* @returns the readable promise state store
|
|
22
|
-
*/
|
|
23
|
-
export const promiseStateStore = (value) => {
|
|
24
|
-
if (!isThenable(value)) {
|
|
25
|
-
return readable({ status: 'fulfilled', value: value });
|
|
26
|
-
}
|
|
27
|
-
let response = promiseWeakMap.get(value);
|
|
28
|
-
if (!response) {
|
|
29
|
-
response = createPromiseStateStore(value);
|
|
30
|
-
promiseWeakMap.set(value, response);
|
|
31
|
-
}
|
|
32
|
-
return response;
|
|
33
|
-
};
|
|
34
|
-
const promiseStateStoreEqual = (a, b) => Object.is(a, b) ||
|
|
35
|
-
(a.status === b.status &&
|
|
36
|
-
(a.status !== 'fulfilled' || equal(a.value, b.value)) &&
|
|
37
|
-
(a.status !== 'rejected' || equal(a.reason, b.reason)));
|
|
38
|
-
/**
|
|
39
|
-
* Create a readable promise state store from a promise store.
|
|
40
|
-
*
|
|
41
|
-
* @param promiseStore$ - the promise store
|
|
42
|
-
* @returns the readable promise state store
|
|
43
|
-
*/
|
|
44
|
-
export const promiseStoreToPromiseStateStore = (promiseStore$) => computed(() => promiseStateStore(promiseStore$())(), { equal: promiseStateStoreEqual });
|
|
45
|
-
/**
|
|
46
|
-
* Create a value store from a promise state store
|
|
47
|
-
*
|
|
48
|
-
* The returned value store is only updated if the promise is fulfilled.
|
|
49
|
-
*
|
|
50
|
-
* @param store$ - the promise state store
|
|
51
|
-
* @param initialValue - the initial value of the returned value store
|
|
52
|
-
* @param equal - an equal function to compare values
|
|
53
|
-
* @returns the value store
|
|
54
|
-
*/
|
|
55
|
-
export const promiseStateStoreToValueStore = (store$, initialValue, equal) => derived(store$, {
|
|
56
|
-
derive: (state, set) => {
|
|
57
|
-
if (state.status === 'fulfilled') {
|
|
58
|
-
set(state.value);
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
equal,
|
|
62
|
-
}, initialValue);
|
|
63
|
-
/**
|
|
64
|
-
* Create a value store from a promise store
|
|
65
|
-
*
|
|
66
|
-
* The returned value store is only updated if the promise is fulfilled.
|
|
67
|
-
*
|
|
68
|
-
* @param promiseStore$ - the promise store
|
|
69
|
-
* @param initialValue - the initial value of the returned value store
|
|
70
|
-
* @param equal - an equal function to compare values
|
|
71
|
-
* @returns the value store
|
|
72
|
-
*/
|
|
73
|
-
export const promiseStoreToValueStore = (promiseStore$, initialValue, equal) => promiseStateStoreToValueStore(promiseStoreToPromiseStateStore(promiseStore$), initialValue, equal);
|
|
74
|
-
const truthyValue = (value) => !!value;
|
|
75
|
-
/**
|
|
76
|
-
* Create a promise from a readable store and a fulfilled condition function.
|
|
77
|
-
*
|
|
78
|
-
* The promise is fulfilled when the state of the store respects the provided condition function.
|
|
79
|
-
*
|
|
80
|
-
* @param store - the readable store
|
|
81
|
-
* @param condition - the condition function
|
|
82
|
-
* @returns the promise and an unsubscribe function
|
|
83
|
-
*/
|
|
84
|
-
export const promiseFromStore = (store, condition = truthyValue) => {
|
|
85
|
-
let resolve;
|
|
86
|
-
const promise = new Promise((r) => (resolve = r));
|
|
87
|
-
let unsubscribe = () => {
|
|
88
|
-
storeUnsubscribe();
|
|
89
|
-
unsubscribe = noop;
|
|
90
|
-
};
|
|
91
|
-
let storeUnsubscribe = noop;
|
|
92
|
-
storeUnsubscribe = store.subscribe((value) => {
|
|
93
|
-
if (condition(value)) {
|
|
94
|
-
resolve(value);
|
|
95
|
-
unsubscribe();
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
if (unsubscribe === noop) {
|
|
99
|
-
storeUnsubscribe();
|
|
100
|
-
}
|
|
101
|
-
return {
|
|
102
|
-
promise,
|
|
103
|
-
unsubscribe() {
|
|
104
|
-
unsubscribe();
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
/**
|
|
109
|
-
* Create a promise from an HTML element event.
|
|
110
|
-
*
|
|
111
|
-
* @param element - the event target
|
|
112
|
-
* @param event - the event to listen to
|
|
113
|
-
* @returns the promise and an unsubscribe function
|
|
114
|
-
*/
|
|
115
|
-
export const promiseFromEvent = (element, event) => {
|
|
116
|
-
let resolve;
|
|
117
|
-
const promise = new Promise((r) => (resolve = r));
|
|
118
|
-
let unsubscribe = () => {
|
|
119
|
-
element.removeEventListener(event, eventListener);
|
|
120
|
-
unsubscribe = noop;
|
|
121
|
-
};
|
|
122
|
-
const eventListener = (event) => {
|
|
123
|
-
if (event.target === element) {
|
|
124
|
-
resolve(event);
|
|
125
|
-
unsubscribe();
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
element.addEventListener(event, eventListener);
|
|
129
|
-
return {
|
|
130
|
-
promise,
|
|
131
|
-
unsubscribe() {
|
|
132
|
-
unsubscribe();
|
|
133
|
-
},
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* Create a promise that resolves once a timeout has been reached.
|
|
138
|
-
*
|
|
139
|
-
* @param delay - the delay in milli seconds
|
|
140
|
-
* @returns a promise and an unsubscribe function
|
|
141
|
-
*/
|
|
142
|
-
export const promiseFromTimeout = (delay) => {
|
|
143
|
-
let timeout;
|
|
144
|
-
return {
|
|
145
|
-
promise: new Promise((r) => {
|
|
146
|
-
timeout = setTimeout(() => {
|
|
147
|
-
timeout = undefined;
|
|
148
|
-
r();
|
|
149
|
-
}, delay);
|
|
150
|
-
}),
|
|
151
|
-
unsubscribe() {
|
|
152
|
-
if (timeout) {
|
|
153
|
-
clearTimeout(timeout);
|
|
154
|
-
timeout = undefined;
|
|
155
|
-
}
|
|
156
|
-
},
|
|
157
|
-
};
|
|
158
|
-
};
|
|
159
|
-
/**
|
|
160
|
-
* Utility method to create a promise with resolve
|
|
161
|
-
* @returns a promise with resolve
|
|
162
|
-
*/
|
|
163
|
-
export const promiseWithResolve = () => {
|
|
164
|
-
let resolve;
|
|
165
|
-
const promise = new Promise((r) => {
|
|
166
|
-
resolve = r;
|
|
167
|
-
});
|
|
168
|
-
return { promise, resolve: resolve };
|
|
169
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { noop } from './func';
|
|
2
|
-
const internalRemoveScrollbars = () => {
|
|
3
|
-
const scrollbarWidth = Math.abs(window.innerWidth - document.documentElement.clientWidth);
|
|
4
|
-
const body = document.body;
|
|
5
|
-
const bodyStyle = body.style;
|
|
6
|
-
const { overflow, paddingRight } = bodyStyle;
|
|
7
|
-
if (scrollbarWidth > 0) {
|
|
8
|
-
const actualPadding = parseFloat(window.getComputedStyle(body).paddingRight);
|
|
9
|
-
bodyStyle.paddingRight = `${actualPadding + scrollbarWidth}px`;
|
|
10
|
-
}
|
|
11
|
-
bodyStyle.overflow = 'hidden';
|
|
12
|
-
return () => {
|
|
13
|
-
if (scrollbarWidth > 0) {
|
|
14
|
-
bodyStyle.paddingRight = paddingRight;
|
|
15
|
-
}
|
|
16
|
-
bodyStyle.overflow = overflow;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
let internalRevert = noop;
|
|
20
|
-
/**
|
|
21
|
-
* A function to remove the scrollbars on the body element. It can be reverted using the {@link revertScrollbars} function.
|
|
22
|
-
*/
|
|
23
|
-
export const removeScrollbars = () => {
|
|
24
|
-
internalRevert();
|
|
25
|
-
internalRevert = internalRemoveScrollbars();
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* A function to revert the removal of scrollbars performed by the {@link removeScrollbars} function.
|
|
29
|
-
*/
|
|
30
|
-
export const revertScrollbars = () => {
|
|
31
|
-
internalRevert();
|
|
32
|
-
internalRevert = noop;
|
|
33
|
-
};
|
package/utils/internal/sort.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The default comparision between two values, using the javascript < and > signs.
|
|
3
|
-
*
|
|
4
|
-
* @param a - the first input
|
|
5
|
-
* @param b - the second input
|
|
6
|
-
* @returns 1, 0 or -1 depending on the default compare
|
|
7
|
-
*/
|
|
8
|
-
export const compareDefault = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
9
|
-
/**
|
|
10
|
-
* A comparision function between DOM elements, based on [Node.compareDocumentPosition](https://developer.mozilla.org/fr/docs/Web/API/Node/compareDocumentPosition).
|
|
11
|
-
*
|
|
12
|
-
* @param element1 - the first node
|
|
13
|
-
* @param element2 - the second node
|
|
14
|
-
* @returns 1, 0 or -1
|
|
15
|
-
*/
|
|
16
|
-
export const compareDomOrder = (element1, element2) => {
|
|
17
|
-
if (element1 === element2) {
|
|
18
|
-
return 0;
|
|
19
|
-
}
|
|
20
|
-
const result = element1.compareDocumentPosition(element2);
|
|
21
|
-
if (result & Node.DOCUMENT_POSITION_FOLLOWING) {
|
|
22
|
-
return -1;
|
|
23
|
-
}
|
|
24
|
-
else if (result & Node.DOCUMENT_POSITION_PRECEDING) {
|
|
25
|
-
return 1;
|
|
26
|
-
}
|
|
27
|
-
throw new Error('failed to compare elements');
|
|
28
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the text direction of an element, using a call to `getComputedStyle`.
|
|
3
|
-
*
|
|
4
|
-
* @param element - the HTML element
|
|
5
|
-
* @returns the text direction of the element, 'ltr' or 'rtl'
|
|
6
|
-
*/
|
|
7
|
-
export const getTextDirection = (element) => getComputedStyle(element).direction;
|
|
@@ -1,105 +0,0 @@
|
|
|
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
|
-
};
|