@mgdis/core-ui-helpers 1.2.2 → 1.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.
Files changed (149) hide show
  1. package/dist/angular/index.cjs +27 -0
  2. package/dist/angular/index.cjs.map +1 -0
  3. package/dist/angular/index.d.ts +21 -0
  4. package/dist/angular/index.js +25 -0
  5. package/dist/angular/index.js.map +1 -0
  6. package/dist/angular/index.umd.js +33 -0
  7. package/dist/angular/index.umd.js.map +1 -0
  8. package/dist/locale/index.cjs +190 -0
  9. package/dist/locale/index.cjs.map +1 -0
  10. package/dist/locale/index.d.ts +110 -0
  11. package/dist/locale/index.js +181 -0
  12. package/dist/locale/index.js.map +1 -0
  13. package/dist/locale/index.umd.js +196 -0
  14. package/dist/locale/index.umd.js.map +1 -0
  15. package/dist/playwright/index.cjs +137 -0
  16. package/dist/playwright/index.cjs.map +1 -0
  17. package/dist/playwright/index.d.ts +39 -0
  18. package/dist/playwright/index.js +133 -0
  19. package/dist/playwright/index.js.map +1 -0
  20. package/dist/playwright/index.umd.js +143 -0
  21. package/dist/playwright/index.umd.js.map +1 -0
  22. package/dist/stencil/index.cjs +363 -0
  23. package/dist/stencil/index.cjs.map +1 -0
  24. package/dist/stencil/index.d.ts +222 -0
  25. package/dist/stencil/index.js +357 -0
  26. package/dist/stencil/index.js.map +1 -0
  27. package/dist/stencil/index.umd.js +369 -0
  28. package/dist/stencil/index.umd.js.map +1 -0
  29. package/dist/storybook/index.cjs +2335 -0
  30. package/dist/storybook/index.cjs.map +1 -0
  31. package/dist/storybook/index.d.ts +85 -0
  32. package/dist/storybook/index.js +2329 -0
  33. package/dist/storybook/index.js.map +1 -0
  34. package/dist/storybook/index.umd.js +2341 -0
  35. package/dist/storybook/index.umd.js.map +1 -0
  36. package/dist/tests/index.cjs +315 -0
  37. package/dist/tests/index.cjs.map +1 -0
  38. package/dist/tests/index.d.ts +100 -0
  39. package/dist/tests/index.js +309 -0
  40. package/dist/tests/index.js.map +1 -0
  41. package/dist/tests/index.umd.js +321 -0
  42. package/dist/tests/index.umd.js.map +1 -0
  43. package/dist/utils/index.cjs +441 -0
  44. package/dist/utils/index.cjs.map +1 -0
  45. package/dist/utils/index.d.ts +265 -0
  46. package/dist/utils/index.js +417 -0
  47. package/dist/utils/index.js.map +1 -0
  48. package/dist/utils/index.umd.js +447 -0
  49. package/dist/utils/index.umd.js.map +1 -0
  50. package/dist/vue/index.cjs +1935 -0
  51. package/dist/vue/index.cjs.map +1 -0
  52. package/dist/vue/index.d.ts +35 -0
  53. package/dist/vue/index.js +1933 -0
  54. package/dist/vue/index.js.map +1 -0
  55. package/dist/vue/index.umd.js +1941 -0
  56. package/dist/vue/index.umd.js.map +1 -0
  57. package/package.json +14 -11
  58. package/CHANGELOG.md +0 -101
  59. package/json-doc.test.json +0 -264
  60. package/rollup.config.mjs +0 -61
  61. package/src/angular/index.spec.ts +0 -8
  62. package/src/angular/index.ts +0 -1
  63. package/src/angular/mg-angular-logger/index.ts +0 -1
  64. package/src/angular/mg-angular-logger/mg-angular-logger.conf.ts +0 -3
  65. package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +0 -40
  66. package/src/angular/mg-angular-logger/mg-angular-logger.ts +0 -24
  67. package/src/locale/index.spec.ts +0 -24
  68. package/src/locale/index.ts +0 -1
  69. package/src/locale/locale.spec.ts +0 -291
  70. package/src/locale/locale.ts +0 -201
  71. package/src/playwright/config/index.ts +0 -43
  72. package/src/playwright/index.spec.ts +0 -16
  73. package/src/playwright/index.ts +0 -4
  74. package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +0 -48
  75. package/src/playwright/utils/index.ts +0 -1
  76. package/src/playwright/utils/utils.spec.ts +0 -50
  77. package/src/playwright/utils/utils.ts +0 -77
  78. package/src/stencil/ide/__snapshots__/cem.spec.ts.snap +0 -248
  79. package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +0 -414
  80. package/src/stencil/ide/cem.spec.ts +0 -35
  81. package/src/stencil/ide/cem.ts +0 -96
  82. package/src/stencil/ide/ide.spec.ts +0 -29
  83. package/src/stencil/ide/ide.ts +0 -221
  84. package/src/stencil/ide/index.ts +0 -2
  85. package/src/stencil/index.spec.ts +0 -16
  86. package/src/stencil/index.ts +0 -2
  87. package/src/stencil/normalize-boolean-attributes/index.ts +0 -1
  88. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.spec.ts +0 -63
  89. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.ts +0 -40
  90. package/src/storybook/index.spec.ts +0 -19
  91. package/src/storybook/index.ts +0 -1
  92. package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +0 -329
  93. package/src/storybook/stencil/index.ts +0 -1
  94. package/src/storybook/stencil/stencil-shim.d.ts +0 -1
  95. package/src/storybook/stencil/stencil.conf.ts +0 -4
  96. package/src/storybook/stencil/stencil.spec.ts +0 -170
  97. package/src/storybook/stencil/stencil.ts +0 -377
  98. package/src/tests/index.spec.ts +0 -20
  99. package/src/tests/index.ts +0 -1
  100. package/src/tests/unit/__snapshots__/unit.spec.ts.snap +0 -52
  101. package/src/tests/unit/index.ts +0 -1
  102. package/src/tests/unit/unit.conf.ts +0 -16
  103. package/src/tests/unit/unit.spec.ts +0 -206
  104. package/src/tests/unit/unit.ts +0 -333
  105. package/src/utils/attributes/class-list/class-list.spec.ts +0 -36
  106. package/src/utils/attributes/class-list/class-list.ts +0 -51
  107. package/src/utils/attributes/class-list/index.ts +0 -1
  108. package/src/utils/attributes/id/id.spec.ts +0 -54
  109. package/src/utils/attributes/id/id.ts +0 -47
  110. package/src/utils/attributes/id/index.ts +0 -1
  111. package/src/utils/attributes/index.ts +0 -3
  112. package/src/utils/attributes/to-kebab-case/index.ts +0 -1
  113. package/src/utils/attributes/to-kebab-case/to-kebab-case.spec.ts +0 -29
  114. package/src/utils/attributes/to-kebab-case/to-kebab-case.ts +0 -23
  115. package/src/utils/components/__snapshots__/components.spec.ts.snap +0 -313
  116. package/src/utils/components/components.conf.ts +0 -11
  117. package/src/utils/components/components.spec.ts +0 -192
  118. package/src/utils/components/components.ts +0 -152
  119. package/src/utils/components/index.ts +0 -2
  120. package/src/utils/date/date.spec.ts +0 -12
  121. package/src/utils/date/date.ts +0 -20
  122. package/src/utils/date/index.ts +0 -1
  123. package/src/utils/elements/elements.spec.ts +0 -17
  124. package/src/utils/elements/elements.ts +0 -18
  125. package/src/utils/elements/index.ts +0 -1
  126. package/src/utils/index.spec.ts +0 -84
  127. package/src/utils/index.ts +0 -8
  128. package/src/utils/number/index.ts +0 -1
  129. package/src/utils/number/number.spec.ts +0 -14
  130. package/src/utils/number/number.ts +0 -6
  131. package/src/utils/object/index.ts +0 -2
  132. package/src/utils/object/object.conf.ts +0 -4
  133. package/src/utils/object/object.spec.ts +0 -110
  134. package/src/utils/object/object.ts +0 -27
  135. package/src/utils/string/index.ts +0 -1
  136. package/src/utils/string/string.spec.ts +0 -54
  137. package/src/utils/string/string.ts +0 -39
  138. package/src/utils/windows/index.ts +0 -1
  139. package/src/utils/windows/windows.spec.ts +0 -69
  140. package/src/utils/windows/windows.ts +0 -50
  141. package/src/vue/index.spec.ts +0 -8
  142. package/src/vue/index.ts +0 -1
  143. package/src/vue/mg-model/index.ts +0 -1
  144. package/src/vue/mg-model/mg-model.conf.ts +0 -6
  145. package/src/vue/mg-model/mg-model.spec.ts +0 -50
  146. package/src/vue/mg-model/mg-model.ts +0 -55
  147. package/tsconfig.json +0 -17
  148. package/typedoc.json +0 -14
  149. package/vitest.config.mts +0 -13
@@ -1,333 +0,0 @@
1
- import type { SetupMutationObserverMockParams, setupResizeObserverMockParams } from './unit.conf';
2
-
3
- /**
4
- * Utility function that mocks the `MutationObserver` API. Recommended to execute inside `beforeEach`.
5
- * @param mutationObserverMock - Parameter that is sent to the `Object.defineProperty`
6
- * overwrite method. `jest.fn()` mock functions can be passed here if the goal is to not only
7
- * mock the mutation observer, but its methods.
8
- * You can manually fire an intersection entry:
9
- * @param mutationObserverMock - configuration object
10
- * @returns Mocked MutationObserver
11
- * @example
12
- * ```
13
- * let fireMo;
14
- * setupMutationObserverMock({
15
- * observe: function () {
16
- * fireMo = this.cb;
17
- * },
18
- * });
19
- * ...
20
- * fireMo([{ type: 'childList', addedNodes: [AMockElemenet, AnotherMockElemenet], target: yourMockElemenet }]);;
21
- * ```
22
- */
23
- export const setupMutationObserverMock = ({
24
- disconnect,
25
- observe,
26
- takeRecords,
27
- }: SetupMutationObserverMockParams): typeof MutationObserver => {
28
- class MockMutationObserver implements MutationObserver {
29
- /**
30
- *
31
- */
32
- disconnect: () => void = disconnect;
33
- /**
34
- *
35
- */
36
- observe: (target: Node, options?: MutationObserverInit) => void = observe;
37
- /**
38
- *
39
- */
40
- takeRecords: () => MutationRecord[] = takeRecords;
41
- /**
42
- *
43
- */
44
- cb: MutationCallback;
45
- constructor(fn: MutationCallback) {
46
- this.cb = fn;
47
- }
48
- }
49
-
50
- [window, global].forEach((element) => {
51
- Object.defineProperty(element, 'MutationObserver', {
52
- writable: true,
53
- configurable: true,
54
- value: MockMutationObserver,
55
- });
56
- });
57
-
58
- return MockMutationObserver;
59
- };
60
-
61
- /**
62
- * Utility function that mocks the `ResizeObserver` API. Recommended to execute inside `beforeEach`.
63
- * @param resizeObserverMock - Parameter that is sent to the `Object.defineProperty`
64
- * overwrite method. `jest.fn()` mock functions can be passed here if the goal is to not only
65
- * mock the resize observer, but its methods.
66
- * You can manually fire an intersection entry:
67
- * @param resizeObserverMock - configuration object
68
- * @returns Mocked ResizeObserver
69
- * @example
70
- * ```
71
- * let fireRo;
72
- * setupResizeObserverMock({
73
- * observe: function () {
74
- * fireRo = this.cb;
75
- * },
76
- * });
77
- * ...
78
- * fireRo([{
79
- * borderBoxSize: ResizeObserverSize[],
80
- * contentBoxSize: ResizeObserverSize[],
81
- * contentRect: DOMRectReadOnly,
82
- * devicePixelContentBoxSize: ResizeObserverSize[],
83
- * target: yourMockElemenet
84
- * }]);;
85
- * ```
86
- */
87
- export const setupResizeObserverMock = ({
88
- disconnect,
89
- observe,
90
- }: setupResizeObserverMockParams): typeof ResizeObserver => {
91
- class MockResizeObserver implements ResizeObserver {
92
- /**
93
- *
94
- */
95
- disconnect: () => void = disconnect;
96
- /**
97
- *
98
- */
99
- observe: (target: Element, options?: ResizeObserverOptions) => void = observe;
100
- /**
101
- *
102
- */
103
- unobserve!: () => void;
104
- /**
105
- *
106
- */
107
- cb: ResizeObserverCallback;
108
- constructor(fn: ResizeObserverCallback) {
109
- this.cb = fn;
110
- }
111
- }
112
-
113
- [window, global].forEach((element) => {
114
- Object.defineProperty(element, 'ResizeObserver', {
115
- writable: true,
116
- configurable: true,
117
- value: MockResizeObserver,
118
- });
119
- });
120
-
121
- return MockResizeObserver;
122
- };
123
-
124
- class MockCustomEvent extends Event {
125
- /**
126
- *
127
- */
128
- detail: any; // eslint-disable-line @typescript-eslint/no-explicit-any
129
- }
130
-
131
- /**
132
- * Utility function that mocks the `SubmitEvent` API. Recommended to execute inside `beforeEach`.
133
- * @example
134
- * ```
135
- * setupSubmitEventMock();
136
- * ```
137
- * @returns custom event mock
138
- */
139
- export const setupSubmitEventMock = (): typeof MockCustomEvent => {
140
- class SubmitEvent extends MockCustomEvent {}
141
-
142
- [window, global].forEach((element) => {
143
- Object.defineProperty(element, 'SubmitEvent', {
144
- writable: true,
145
- configurable: true,
146
- value: SubmitEvent,
147
- });
148
- });
149
-
150
- return SubmitEvent;
151
- };
152
-
153
- /**
154
- * Utility function that mocks the `requestAnimationFrame` API. Recommended to execute inside `test`.
155
- * @example
156
- * ```
157
- * setUpRequestAnimationFrameMock(jest.runOnlyPendingTimers);
158
- * ```
159
- * @param faketimer - recommended to use jest.runOnlyPendingTimers()
160
- * @returns custom setUpRequestAnimationFrameMock mock
161
- */
162
- export const setUpRequestAnimationFrameMock = (faketimer: () => void): typeof requestAnimationFrame => {
163
- const requestAnimationFrame = (callback: FrameRequestCallback) => {
164
- setTimeout(callback, 1);
165
- faketimer();
166
- return 0;
167
- };
168
-
169
- [window, global].forEach((element) => {
170
- Object.defineProperty(element, 'requestAnimationFrame', {
171
- writable: true,
172
- configurable: true,
173
- value: requestAnimationFrame,
174
- });
175
- });
176
-
177
- return requestAnimationFrame;
178
- };
179
-
180
- /**
181
- * Convert string to given type
182
- * @param value - string value to format
183
- * @param type - new value output type
184
- * @returns string value converted to given type
185
- */
186
- const convertString = (value: string, type: string): number | Date | string => {
187
- switch (type) {
188
- case 'date':
189
- return new Date(value);
190
- case 'number':
191
- return Number(value);
192
- default:
193
- return value;
194
- }
195
- };
196
-
197
- /**
198
- * Get range underflow value from input['min]
199
- * @param input - HTMLInputElement
200
- * @returns truthy iv value is underflow
201
- */
202
- const getRangeUnderflow = (input: HTMLInputElement): boolean => {
203
- const value = convertString(input.value, input.type);
204
- if (['date', 'number'].includes(input.type) && input.hasAttribute('min') && input.value.length > 0) {
205
- const min = convertString(input.min, input.type);
206
- return value < min;
207
- } else {
208
- return false;
209
- }
210
- };
211
-
212
- /**
213
- * Get range overflow value from input['max]
214
- * @param input - HTMLInputElement
215
- * @returns truthy iv value is overflow
216
- */
217
- const getRangeOverflow = (input: HTMLInputElement): boolean => {
218
- const value = convertString(input.value, input.type);
219
- if (['date', 'number'].includes(input.type) && input.hasAttribute('max') && input.value.length > 0) {
220
- const max = convertString(input.max, input.type);
221
- return value > max;
222
- } else {
223
- return false;
224
- }
225
- };
226
-
227
- /**
228
- * HTMLSelectElement type guard
229
- * @param input - HTMLElement to test
230
- * @returns truthy if element is HTMLSelectElement
231
- */
232
- const isHTMLSelectElement = (input: HTMLElement): input is HTMLSelectElement =>
233
- (input as HTMLSelectElement).options !== undefined;
234
- /**
235
- * Is value missing from input
236
- * @param input - input to test
237
- * @returns truthy if value is required
238
- */
239
- const isValueMissing = (
240
- input: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | HTMLFieldSetElement
241
- ): boolean => {
242
- if (input.hasAttribute('required') && (input as HTMLInputElement).required === true) {
243
- if (['checkbox', 'radio'].includes(input.type)) {
244
- return (input as HTMLInputElement).checked === false;
245
- } else if (isHTMLSelectElement(input)) {
246
- return input.options.item(input.options.selectedIndex)?.value === '';
247
- } else {
248
- return (input as HTMLInputElement).value.length === 0;
249
- }
250
- } else {
251
- return false;
252
- }
253
- };
254
-
255
- /**
256
- * Get input validity
257
- * @param input - input prototype
258
- * @returns input validity state
259
- */
260
- const getValidity = (input: HTMLInputElement): ValidityState => {
261
- // required field without a value
262
- const valueMissing = isValueMissing(input);
263
- // value of a number field is not a number | value of a date field is not a date
264
- const badInput =
265
- ['number', 'date'].includes(input.type) &&
266
- isNaN(input.type === 'date' ? Date.parse(input.value) : (input.value as unknown as number));
267
- // value does not conform to the pattern
268
- const patternMismatch = input.hasAttribute('pattern') && new RegExp(input.pattern).test(input.value) === false;
269
- // value of a number|date field is higher than the max attribute
270
- const rangeOverflow = getRangeOverflow(input);
271
- // value of a number|date field is lower than the min attribute
272
- const rangeUnderflow = getRangeUnderflow(input);
273
- // value of a number field does not conform to the stepattribute
274
- const stepMismatch =
275
- input.type === 'number' &&
276
- input.hasAttribute('step') &&
277
- input.step !== 'any' &&
278
- Number(input.value) % parseFloat(input.step) !== 0;
279
- // the user has edited a too-long value in a field with maxlength
280
- const tooLong = input.hasAttribute('maxLength') && input.value?.length > Number(input.maxLength);
281
- // the user has edited a too-short value in a field with minlength
282
- const tooShort = input.hasAttribute('minLength') && input.value?.length < Number(input.minLength);
283
- // value of a email or URL field is not an email address or URL
284
- const typeMismatch = input.type === 'url' && !URL.canParse(input.value);
285
- // value of validationMessage is not an empty string
286
- const customError = false;
287
-
288
- const valid = ![
289
- valueMissing,
290
- badInput,
291
- patternMismatch,
292
- rangeOverflow,
293
- rangeUnderflow,
294
- stepMismatch,
295
- tooLong,
296
- tooShort,
297
- typeMismatch,
298
- customError,
299
- ].some((invalid) => invalid);
300
- return {
301
- badInput,
302
- customError,
303
- patternMismatch,
304
- rangeOverflow,
305
- rangeUnderflow,
306
- stepMismatch,
307
- tooLong,
308
- tooShort,
309
- typeMismatch,
310
- valid,
311
- valueMissing,
312
- };
313
- };
314
-
315
- /**
316
- * Utility function that mocks the HTMLInputElement[`vality`] state API and the HTMLInputElement.checkvalidty() methode.
317
- * Recommended to execute inside `test`.
318
- * @example
319
- * ```
320
- * Array.from(document.querySelectorInputs('input')).forEach(setUpHTMLInputElementValidity);
321
- * ```
322
- */
323
- export const setUpHTMLInputElementValidity = (input: HTMLInputElement) => {
324
- input.checkValidity = () => {
325
- const { valid } = getValidity(input);
326
- input.dispatchEvent(new CustomEvent('invalid', { detail: !valid }));
327
- return valid;
328
- };
329
- Object.defineProperty(input, 'validity', {
330
- get: () => getValidity(input),
331
- configurable: true,
332
- });
333
- };
@@ -1,36 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { ClassList } from './class-list';
3
-
4
- describe('ClassList', () => {
5
- test('Should add classes to list', () => {
6
- const classCollection = new ClassList();
7
- expect(classCollection.classes).toEqual([]);
8
- classCollection.add('blu');
9
- expect(classCollection.classes).toEqual(['blu']);
10
- classCollection.add('bli');
11
- expect(classCollection.classes).toEqual(['blu', 'bli']);
12
- // Should not add classes if already in list
13
- classCollection.add('blu');
14
- expect(classCollection.classes).toEqual(['blu', 'bli']);
15
- });
16
-
17
- test('Should delete classes from list', () => {
18
- const classCollection = new ClassList(['blu', 'bli']);
19
- classCollection.delete('blu');
20
- expect(classCollection.classes).toEqual(['bli']);
21
- // List doesn't change if class name doesn't existe in list
22
- classCollection.delete('bla');
23
- expect(classCollection.classes).toEqual(['bli']);
24
- });
25
-
26
- test('Should check if class already in list', () => {
27
- const classCollection = new ClassList(['blu']);
28
- expect(classCollection.has('blu')).toEqual(true);
29
- expect(classCollection.has('bli')).toEqual(false);
30
- });
31
-
32
- test('Should return seperated space classes list', () => {
33
- const classCollection = new ClassList(['blu', 'bli']);
34
- expect(classCollection.join()).toEqual('blu bli');
35
- });
36
- });
@@ -1,51 +0,0 @@
1
- /**
2
- * Class to manage component classlist
3
- */
4
- export class ClassList {
5
- /**
6
- * Available classes
7
- */
8
- classes: string[];
9
-
10
- constructor(classlist: string[] = []) {
11
- this.classes = classlist;
12
- }
13
-
14
- /**
15
- * Add class
16
- * @param className - class name to add
17
- */
18
- add = (className: string): void => {
19
- if (!this.has(className)) {
20
- this.classes.push(className);
21
- }
22
- };
23
-
24
- /**
25
- * Delete class
26
- * @param className - class name to delete
27
- */
28
- delete = (className: string): void => {
29
- const index = this.classes.indexOf(className);
30
- if (index > -1) {
31
- this.classes.splice(index, 1);
32
- }
33
- };
34
-
35
- /**
36
- * Check if class exist in list
37
- * @param className - class name to check
38
- * @returns class name is in the list
39
- */
40
- has = (className: string): boolean => {
41
- return this.classes.includes(className);
42
- };
43
-
44
- /**
45
- * Join classes seperated by spaces
46
- * @returns joined values
47
- */
48
- join = (): string => {
49
- return this.classes.join(' ');
50
- };
51
- }
@@ -1 +0,0 @@
1
- export * from './class-list';
@@ -1,54 +0,0 @@
1
- import { describe, test, expect } from 'vitest';
2
- import { createID, formatID, isValideID } from './id';
3
-
4
- describe('id', () => {
5
- describe('createID', () => {
6
- test.each([undefined, '', 'blu'])('Should generate a prefixed ID if defined : %s', (prefix) => {
7
- const id = createID(prefix);
8
- let regexp = /^[a-z0-9]{10}$/;
9
- if (prefix !== undefined && prefix !== '') {
10
- regexp = new RegExp(`^${prefix}-[a-z0-9]{10}$`);
11
- }
12
- expect(id).toMatch(regexp);
13
- });
14
- test('Should generate a unique ID when both are created at the same time', () => {
15
- const id1 = createID();
16
- const id2 = createID();
17
- expect(id1).not.toEqual(id2);
18
- });
19
- });
20
-
21
- describe('isValideID', () => {
22
- test.each([undefined, '', { id: 'hello' }, '!batman', '{batman}', '123', '1-batman'])(
23
- 'Should NOT validate ID : %s',
24
- (id) => {
25
- const valide = isValideID(id);
26
- expect(valide).toEqual(false);
27
- }
28
- );
29
-
30
- test.each(['hello', 'hello-batman', 'hello-batman-in-gotham', 'hello-1'])('Should validate ID : %s', (id) => {
31
- const valide = isValideID(id);
32
- expect(valide).toEqual(true);
33
- });
34
- });
35
-
36
- describe('formatID', () => {
37
- test.each([
38
- ['UPPERCASE', 'uppercase'],
39
- ['camelCase', 'camel-case'],
40
- ['PascalCase', 'pascal-case'],
41
- ['text/is/href', 'text-is-href'],
42
- [1, '1'],
43
- [{ href: 'text/is/href' }, 'href-text-is-href'],
44
- [[{ href: 'text/is/href' }], 'href-text-is-href'],
45
- [null, undefined],
46
- [undefined, undefined],
47
- [true, undefined],
48
- [false, undefined],
49
- ])('Should format value as valid ID : %s', (input, expected) => {
50
- const id = formatID(input);
51
- expect(id).toEqual(expected);
52
- });
53
- });
54
- });
@@ -1,47 +0,0 @@
1
- import { isObject } from '../../object';
2
- import { isValidString } from '../../string';
3
- import { toKebabCase } from '../to-kebab-case';
4
-
5
- /**
6
- * Create random ID
7
- * @param prefix - add prefix to created ID
8
- * @param length - ID length
9
- * @returns ID
10
- */
11
- export const createID = (prefix = '', length = 10): string => {
12
- const randomBytes = new Uint8Array(length);
13
-
14
- crypto.getRandomValues(randomBytes);
15
-
16
- const hexString = Array.from(randomBytes)
17
- .map((byte) => byte.toString(16).padStart(2, '0'))
18
- .join('')
19
- .slice(0, length);
20
-
21
- return prefix !== '' ? `${prefix}-${hexString}` : hexString;
22
- };
23
-
24
- /**
25
- * Validate html `id` format
26
- * @param newValue - id value to validate
27
- * @returns true if `id` is valid
28
- */
29
- export const isValideID = (newValue: unknown): boolean =>
30
- isValidString(newValue) && /^([a-z][a-z0-9]*)(-[a-z0-9]+)*$/.exec(newValue) !== null;
31
-
32
- /**
33
- * Format id from value
34
- * @param value - id to transforme
35
- * @returns valid id
36
- */
37
- export const formatID = (value: unknown): string | undefined => {
38
- let id;
39
- if (typeof value === 'string') {
40
- id = value;
41
- } else if (Boolean(value) && typeof value === 'object' && (isObject(value) || Array.isArray(value))) {
42
- id = JSON.stringify(value);
43
- } else if (value !== null && value !== undefined && typeof value !== 'boolean' && typeof value !== 'object') {
44
- id = String(value);
45
- }
46
- return id ? toKebabCase(id) : id;
47
- };
@@ -1 +0,0 @@
1
- export * from './id';
@@ -1,3 +0,0 @@
1
- export * from './class-list';
2
- export * from './id';
3
- export * from './to-kebab-case';
@@ -1 +0,0 @@
1
- export * from './to-kebab-case';
@@ -1,29 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { toKebabCase } from '.';
3
-
4
- describe('toKebabCase', () => {
5
- test.each([
6
- ['UPPERCASE', 'uppercase'],
7
- ['camelCase', 'camel-case'],
8
- ['PascalCase', 'pascal-case'],
9
- ['XMLHttpRequest', 'xml-http-request'],
10
- ['lowercase', 'lowercase'],
11
- ['', ''],
12
- ['GEEKS__FOR__GEEKS', 'geeks-for-geeks'],
13
- ['GEEKS-----FOR_____Geeks', 'geeks-for-geeks'],
14
- ['geeks--FOR--geeks', 'geeks-for-geeks'],
15
- ['special@Case', 'special-case'],
16
- ['multiple---dashes', 'multiple-dashes'],
17
- ['case123Test', 'case123-test'],
18
- ['trailing--', 'trailing'],
19
- ['--leading', 'leading'],
20
- ['mixOfUPPERAndLower', 'mix-of-upper-and-lower'],
21
- ['special#characters!', 'special-characters'],
22
- ['12345', '12345'],
23
- ['textWith123Numbers', 'text-with123-numbers'],
24
- ['text/is/href', 'text-is-href'],
25
- ['field-enumeration-list-D_PROC_POLE_REMU_NATIONALITE', 'field-enumeration-list-d-proc-pole-remu-nationalite'],
26
- ])('should convert %s to %s', (input, expected) => {
27
- expect(toKebabCase(input)).toBe(expected);
28
- });
29
- });
@@ -1,23 +0,0 @@
1
- /**
2
- * Convert a string to kebab-case.
3
- *
4
- * This function ensures:
5
- * - All characters are converted to lowercase. Based on : https://stackoverflow.com/questions/63116039/camelcase-to-kebab-case
6
- * - Non-alphabetic characters (except numbers and hyphens) are replaced with hyphens.
7
- * - Consecutive hyphens are replaced with a single hyphen.
8
- * - Leading and trailing hyphens are removed.
9
- *
10
- * @param str - The input string to convert.
11
- * @returns The kebab-case formatted string.
12
- *
13
- * @example
14
- * ```typescript
15
- * toKebabCase('XMLHttpRequest'); // 'xml-http-request'
16
- * ```
17
- */
18
- export const toKebabCase = (str: string): string =>
19
- str
20
- .replace(/[A-Z]+(?![a-z])|[A-Z]/g, (match, offset) => (offset > 0 ? '-' : '') + match.toLowerCase())
21
- .replace(/[^a-z0-9-]+/g, '-') // Replace non a-z, 0-9, or hyphen characters with a hyphen
22
- .replace(/--+/g, '-') // Replace multiple consecutive hyphens with a single hyphen
23
- .replace(/(?:^-)|(?:-$)/g, ''); // Remove leading hyphens or number or trailing hyphens