@agnos-ui/core 0.1.1 → 0.3.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.
Files changed (145) hide show
  1. package/README.md +3 -3
  2. package/accordion-BaWN0_n-.js +330 -0
  3. package/accordion-BuIgxZ0S.cjs +329 -0
  4. package/alert-C4jCg9Pl.cjs +8 -0
  5. package/alert-Dlf-BV98.js +9 -0
  6. package/common-DRdsw5m8.js +76 -0
  7. package/common-nJRMXbwj.cjs +75 -0
  8. package/components/accordion/accordion.d.ts +57 -64
  9. package/components/accordion/index.cjs +7 -0
  10. package/components/accordion/index.js +7 -1
  11. package/components/alert/alert.d.ts +3 -12
  12. package/components/alert/common.d.ts +4 -13
  13. package/components/alert/index.cjs +10 -0
  14. package/components/alert/index.js +10 -2
  15. package/components/modal/index.cjs +7 -0
  16. package/components/modal/index.js +7 -1
  17. package/components/modal/modal.d.ts +7 -49
  18. package/components/pagination/index.cjs +5 -0
  19. package/components/pagination/index.d.ts +0 -1
  20. package/components/pagination/index.js +5 -2
  21. package/components/pagination/pagination.d.ts +107 -128
  22. package/components/progressbar/index.cjs +5 -0
  23. package/components/progressbar/index.js +5 -1
  24. package/components/progressbar/progressbar.d.ts +6 -22
  25. package/components/rating/index.cjs +5 -0
  26. package/components/rating/index.js +5 -1
  27. package/components/rating/rating.d.ts +16 -11
  28. package/components/select/index.cjs +6 -0
  29. package/components/select/index.js +6 -1
  30. package/components/select/select.d.ts +45 -57
  31. package/components/slider/index.cjs +5 -0
  32. package/components/slider/index.js +5 -1
  33. package/components/slider/slider.d.ts +63 -5
  34. package/components/toast/index.cjs +5 -0
  35. package/components/toast/index.d.ts +1 -0
  36. package/components/toast/index.js +5 -0
  37. package/components/toast/toast.d.ts +47 -0
  38. package/config.cjs +38 -0
  39. package/config.d.ts +6 -1
  40. package/config.js +35 -50
  41. package/directive-BTSEYLF3.cjs +404 -0
  42. package/directive-DCYlDznf.js +405 -0
  43. package/func-DR0n-ShK.js +7 -0
  44. package/func-Qd3cD9a3.cjs +6 -0
  45. package/index.cjs +119 -0
  46. package/index.d.ts +3 -1
  47. package/index.js +119 -31
  48. package/modal-BI2qUu1M.js +251 -0
  49. package/modal-rzMpATf5.cjs +250 -0
  50. package/package.json +30 -22
  51. package/pagination--GkwduJn.js +263 -0
  52. package/pagination-EWSWQT1I.cjs +262 -0
  53. package/progressbar-DH7DHYMp.cjs +83 -0
  54. package/progressbar-DuRX7_my.js +84 -0
  55. package/promise-BMJ8qhA8.cjs +118 -0
  56. package/promise-CY2U8bTP.js +119 -0
  57. package/rating-BR5wD7y2.js +173 -0
  58. package/rating-CmuYUSxy.cjs +172 -0
  59. package/select-BCs6HQWn.js +358 -0
  60. package/select-CCIKn8WR.cjs +357 -0
  61. package/services/extendWidget.cjs +32 -0
  62. package/services/extendWidget.d.ts +10 -5
  63. package/services/extendWidget.js +31 -34
  64. package/services/floatingUI.cjs +131 -0
  65. package/services/floatingUI.d.ts +30 -14
  66. package/services/floatingUI.js +128 -102
  67. package/services/focustrack.cjs +47 -0
  68. package/services/focustrack.js +45 -44
  69. package/services/hash.cjs +15 -0
  70. package/services/hash.d.ts +2 -0
  71. package/services/hash.js +15 -0
  72. package/services/intersection.cjs +53 -0
  73. package/services/intersection.js +48 -50
  74. package/services/matchMedia.cjs +13 -0
  75. package/services/matchMedia.d.ts +7 -0
  76. package/services/matchMedia.js +13 -0
  77. package/services/navManager.cjs +196 -0
  78. package/services/navManager.d.ts +24 -17
  79. package/services/navManager.js +186 -162
  80. package/services/portal.cjs +43 -0
  81. package/services/portal.js +41 -42
  82. package/services/resizeObserver.cjs +32 -0
  83. package/services/resizeObserver.d.ts +14 -0
  84. package/services/resizeObserver.js +32 -0
  85. package/services/siblingsInert.cjs +40 -0
  86. package/services/siblingsInert.js +31 -31
  87. package/services/transitions/baseTransitions.cjs +171 -0
  88. package/services/transitions/baseTransitions.d.ts +16 -16
  89. package/services/transitions/baseTransitions.js +159 -170
  90. package/services/transitions/collapse.cjs +44 -0
  91. package/services/transitions/collapse.js +41 -49
  92. package/services/transitions/cssTransitions.cjs +32 -0
  93. package/services/transitions/cssTransitions.d.ts +2 -1
  94. package/services/transitions/cssTransitions.js +29 -39
  95. package/services/transitions/simpleClassTransition.cjs +31 -0
  96. package/services/transitions/simpleClassTransition.js +30 -41
  97. package/slider-CA_fszn7.js +536 -0
  98. package/slider-DsLvT87U.cjs +535 -0
  99. package/toast-8tWp6x89.js +63 -0
  100. package/toast-Aw8o0Iwe.cjs +62 -0
  101. package/types.cjs +12 -0
  102. package/types.d.ts +21 -1
  103. package/types.js +11 -13
  104. package/utils/directive.cjs +26 -0
  105. package/utils/directive.d.ts +155 -4
  106. package/utils/directive.js +25 -189
  107. package/utils/internal/dom.d.ts +57 -3
  108. package/utils/internal/promise.d.ts +2 -2
  109. package/utils/internal/ssrHTMLElement.d.ts +7 -0
  110. package/utils/internal/textDirection.d.ts +1 -1
  111. package/utils/stores.cjs +163 -0
  112. package/utils/stores.d.ts +23 -20
  113. package/utils/stores.js +151 -269
  114. package/utils/writables.cjs +13 -0
  115. package/utils/writables.js +12 -71
  116. package/writables-D46sFgGK.cjs +85 -0
  117. package/writables-DoU_XYTX.js +86 -0
  118. package/components/accordion/accordion.js +0 -267
  119. package/components/alert/alert.js +0 -22
  120. package/components/alert/common.js +0 -69
  121. package/components/commonProps.js +0 -1
  122. package/components/modal/modal.js +0 -186
  123. package/components/pagination/bootstrap.d.ts +0 -8
  124. package/components/pagination/bootstrap.js +0 -110
  125. package/components/pagination/pagination.js +0 -149
  126. package/components/progressbar/progressbar.js +0 -78
  127. package/components/rating/rating.js +0 -138
  128. package/components/select/select.js +0 -266
  129. package/components/slider/slider.js +0 -415
  130. package/services/transitions/bootstrap/collapse.d.ts +0 -2
  131. package/services/transitions/bootstrap/collapse.js +0 -15
  132. package/services/transitions/bootstrap/fade.d.ts +0 -1
  133. package/services/transitions/bootstrap/fade.js +0 -7
  134. package/services/transitions/bootstrap.d.ts +0 -2
  135. package/services/transitions/bootstrap.js +0 -2
  136. package/utils/internal/checks.js +0 -60
  137. package/utils/internal/dom.js +0 -61
  138. package/utils/internal/func.js +0 -11
  139. package/utils/internal/isFocusable.js +0 -35
  140. package/utils/internal/math.js +0 -13
  141. package/utils/internal/promise.js +0 -169
  142. package/utils/internal/scrollbars.js +0 -33
  143. package/utils/internal/sort.js +0 -28
  144. package/utils/internal/textDirection.js +0 -7
  145. package/utils/internal/traversal.js +0 -105
@@ -1,15 +0,0 @@
1
- import { createCollapseTransition } from '../collapse';
2
- const verticalConfig = {
3
- dimension: 'height',
4
- hideClasses: ['collapse'],
5
- showClasses: ['collapse', 'show'],
6
- animationPendingClasses: ['collapsing'],
7
- };
8
- const horizontalConfig = {
9
- dimension: 'width',
10
- hideClasses: ['collapse', 'collapse-horizontal'],
11
- showClasses: ['collapse', 'collapse-horizontal', 'show'],
12
- animationPendingClasses: ['collapsing', 'collapse-horizontal'],
13
- };
14
- export const collapseVerticalTransition = createCollapseTransition(verticalConfig);
15
- export const collapseHorizontalTransition = createCollapseTransition(horizontalConfig);
@@ -1 +0,0 @@
1
- export declare const fadeTransition: import("../baseTransitions").TransitionFn;
@@ -1,7 +0,0 @@
1
- import { createSimpleClassTransition } from '../simpleClassTransition';
2
- export const fadeTransition = createSimpleClassTransition({
3
- animationPendingClasses: ['fade'],
4
- animationPendingShowClasses: ['show'],
5
- showClasses: ['show'],
6
- hideClasses: ['d-none'],
7
- });
@@ -1,2 +0,0 @@
1
- export * from './bootstrap/collapse';
2
- export * from './bootstrap/fade';
@@ -1,2 +0,0 @@
1
- export * from './bootstrap/collapse';
2
- export * from './bootstrap/fade';
@@ -1,60 +0,0 @@
1
- /**
2
- * a number type guard
3
- * @param value - the value to check
4
- * @returns true if the value is a number
5
- */
6
- export function isNumber(value) {
7
- return typeof value === 'number' && !isNaN(value) && Number.isFinite(value);
8
- }
9
- /**
10
- * a boolean type guard
11
- * @param value - the value to check
12
- * @returns true if the value is a boolean
13
- */
14
- export function isBoolean(value) {
15
- return value === true || value === false;
16
- }
17
- /**
18
- * a function type guard
19
- * @param value - the value to check
20
- * @returns true if the value is a function
21
- */
22
- export function isFunction(value) {
23
- return typeof value === 'function';
24
- }
25
- /**
26
- * a string type guard
27
- * @param value - the value to check
28
- * @returns true if the value is a string
29
- */
30
- export function isString(value) {
31
- return typeof value === 'string';
32
- }
33
- /**
34
- * an array type guard
35
- * @returns true if the value is an array
36
- */
37
- export const isArray = Array.isArray;
38
- // TODO should we check that max > min?
39
- /**
40
- * Clamp the value based on a maximum and optional minimum
41
- * @param value - the value to check
42
- * @param max - the max to clamp to
43
- * @param [min] - the min to clamp to
44
- * @returns the clamped value
45
- */
46
- export function clamp(value, max, min = 0) {
47
- return Math.max(Math.min(value, max), min);
48
- }
49
- /**
50
- * an html element type guard
51
- * @param value - the value to check
52
- * @returns true if the value is an instance of HTMLElement
53
- */
54
- export const isHTMLElement = (value) => value instanceof HTMLElement;
55
- /**
56
- * Returns a new type guard that is based on the provided type guard and also returns true for null values.
57
- * @param isType - base type guard
58
- * @returns A type guard function that returns true for null values and calls the provided type guard for other values.
59
- */
60
- export const allowNull = (isType) => (value) => value === null || isType(value);
@@ -1,61 +0,0 @@
1
- /**
2
- * Returns the common ancestor of the provided DOM elements.
3
- * @param elements - array of DOM elements
4
- * @returns the common ancestor, or null if the array is empty or if there is no common ancestor (e.g.: if elements are detached)
5
- */
6
- export const computeCommonAncestor = (elements) => {
7
- const length = elements.length;
8
- if (length === 0)
9
- return null;
10
- let ancestor = elements[0];
11
- for (let i = 1; i < length && ancestor; i++) {
12
- const element = elements[i];
13
- while (ancestor) {
14
- if (ancestor === element) {
15
- break;
16
- }
17
- const comparison = ancestor.compareDocumentPosition(element);
18
- if (comparison & Node.DOCUMENT_POSITION_CONTAINED_BY) {
19
- break;
20
- }
21
- else if (comparison & Node.DOCUMENT_POSITION_CONTAINS) {
22
- ancestor = element;
23
- break;
24
- }
25
- else if (comparison & Node.DOCUMENT_POSITION_DISCONNECTED) {
26
- return null;
27
- }
28
- ancestor = ancestor.parentElement;
29
- }
30
- }
31
- return ancestor;
32
- };
33
- /**
34
- * Launch a reflow using a call to the provided html element getBoudingClientRect
35
- * @param element - the html element
36
- */
37
- export function reflow(element = document.body) {
38
- element.getBoundingClientRect();
39
- }
40
- /**
41
- * Attach the given css classes to the element
42
- *
43
- * @param element - the HTML element
44
- * @param classes - the css lcasses
45
- */
46
- export const addClasses = (element, classes) => {
47
- if (classes && classes.length > 0) {
48
- element.classList.add(...classes);
49
- }
50
- };
51
- /**
52
- * Remove the given css classes to the element
53
- *
54
- * @param element - the HTML element
55
- * @param classes - the css classes
56
- */
57
- export const removeClasses = (element, classes) => {
58
- if (classes && classes.length > 0) {
59
- element.classList.remove(...classes);
60
- }
61
- };
@@ -1,11 +0,0 @@
1
- /**
2
- * A noop function
3
- */
4
- export const noop = () => { };
5
- /**
6
- * The identity function
7
- *
8
- * @param x - the arg
9
- * @returns the arg
10
- */
11
- export const identity = (x) => x;
@@ -1,35 +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) => !isInertOrInvisible(element) && (isFocusableByTagName[element.tagName] ?? isFocusableOtherTags)(element);
@@ -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
- };
@@ -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
- };