@mgdis/core-ui-helpers 1.2.1 → 1.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 (143) hide show
  1. package/dist/angular/index.cjs +1 -1
  2. package/dist/angular/index.cjs.map +1 -1
  3. package/dist/angular/index.js +1 -1
  4. package/dist/angular/index.js.map +1 -1
  5. package/dist/angular/index.umd.js +1 -1
  6. package/dist/angular/index.umd.js.map +1 -1
  7. package/dist/locale/index.cjs +3 -1
  8. package/dist/locale/index.cjs.map +1 -1
  9. package/dist/locale/index.js +3 -1
  10. package/dist/locale/index.js.map +1 -1
  11. package/dist/locale/index.umd.js +3 -1
  12. package/dist/locale/index.umd.js.map +1 -1
  13. package/dist/playwright/index.cjs +12 -5
  14. package/dist/playwright/index.cjs.map +1 -1
  15. package/dist/playwright/index.js +12 -5
  16. package/dist/playwright/index.js.map +1 -1
  17. package/dist/playwright/index.umd.js +12 -5
  18. package/dist/playwright/index.umd.js.map +1 -1
  19. package/dist/stencil/index.cjs +33 -33
  20. package/dist/stencil/index.cjs.map +1 -1
  21. package/dist/stencil/index.js +33 -33
  22. package/dist/stencil/index.js.map +1 -1
  23. package/dist/stencil/index.umd.js +33 -33
  24. package/dist/stencil/index.umd.js.map +1 -1
  25. package/dist/storybook/index.cjs +9 -7
  26. package/dist/storybook/index.cjs.map +1 -1
  27. package/dist/storybook/index.js +9 -7
  28. package/dist/storybook/index.js.map +1 -1
  29. package/dist/storybook/index.umd.js +9 -7
  30. package/dist/storybook/index.umd.js.map +1 -1
  31. package/dist/tests/index.cjs +24 -9
  32. package/dist/tests/index.cjs.map +1 -1
  33. package/dist/tests/index.d.ts +2 -2
  34. package/dist/tests/index.js +24 -9
  35. package/dist/tests/index.js.map +1 -1
  36. package/dist/tests/index.umd.js +24 -9
  37. package/dist/tests/index.umd.js.map +1 -1
  38. package/dist/utils/index.cjs +14 -6
  39. package/dist/utils/index.cjs.map +1 -1
  40. package/dist/utils/index.js +14 -6
  41. package/dist/utils/index.js.map +1 -1
  42. package/dist/utils/index.umd.js +14 -6
  43. package/dist/utils/index.umd.js.map +1 -1
  44. package/dist/vue/index.cjs +2 -1
  45. package/dist/vue/index.cjs.map +1 -1
  46. package/dist/vue/index.js +2 -1
  47. package/dist/vue/index.js.map +1 -1
  48. package/dist/vue/index.umd.js +2 -1
  49. package/dist/vue/index.umd.js.map +1 -1
  50. package/package.json +11 -8
  51. package/.turbo/turbo-build.log +0 -49
  52. package/CHANGELOG.md +0 -95
  53. package/json-doc.test.json +0 -264
  54. package/rollup.config.mjs +0 -61
  55. package/src/angular/index.spec.ts +0 -8
  56. package/src/angular/index.ts +0 -1
  57. package/src/angular/mg-angular-logger/index.ts +0 -1
  58. package/src/angular/mg-angular-logger/mg-angular-logger.conf.ts +0 -3
  59. package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +0 -40
  60. package/src/angular/mg-angular-logger/mg-angular-logger.ts +0 -23
  61. package/src/locale/index.spec.ts +0 -15
  62. package/src/locale/index.ts +0 -1
  63. package/src/locale/locale.spec.ts +0 -266
  64. package/src/locale/locale.ts +0 -193
  65. package/src/playwright/config/index.ts +0 -43
  66. package/src/playwright/index.spec.ts +0 -16
  67. package/src/playwright/index.ts +0 -4
  68. package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +0 -48
  69. package/src/playwright/utils/index.ts +0 -1
  70. package/src/playwright/utils/utils.spec.ts +0 -43
  71. package/src/playwright/utils/utils.ts +0 -69
  72. package/src/stencil/ide/__snapshots__/cem.spec.ts.snap +0 -248
  73. package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +0 -414
  74. package/src/stencil/ide/cem.spec.ts +0 -35
  75. package/src/stencil/ide/cem.ts +0 -96
  76. package/src/stencil/ide/ide.spec.ts +0 -29
  77. package/src/stencil/ide/ide.ts +0 -221
  78. package/src/stencil/ide/index.ts +0 -2
  79. package/src/stencil/index.spec.ts +0 -16
  80. package/src/stencil/index.ts +0 -2
  81. package/src/stencil/normalize-boolean-attributes/index.ts +0 -1
  82. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.spec.ts +0 -63
  83. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.ts +0 -40
  84. package/src/storybook/index.spec.ts +0 -19
  85. package/src/storybook/index.ts +0 -1
  86. package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +0 -329
  87. package/src/storybook/stencil/index.ts +0 -1
  88. package/src/storybook/stencil/stencil-shim.d.ts +0 -1
  89. package/src/storybook/stencil/stencil.conf.ts +0 -4
  90. package/src/storybook/stencil/stencil.spec.ts +0 -146
  91. package/src/storybook/stencil/stencil.ts +0 -366
  92. package/src/tests/index.spec.ts +0 -14
  93. package/src/tests/index.ts +0 -1
  94. package/src/tests/unit/__snapshots__/unit.spec.ts.snap +0 -52
  95. package/src/tests/unit/index.ts +0 -1
  96. package/src/tests/unit/unit.conf.ts +0 -9
  97. package/src/tests/unit/unit.spec.ts +0 -164
  98. package/src/tests/unit/unit.ts +0 -306
  99. package/src/utils/attributes/class-list/class-list.spec.ts +0 -36
  100. package/src/utils/attributes/class-list/class-list.ts +0 -51
  101. package/src/utils/attributes/class-list/index.ts +0 -1
  102. package/src/utils/attributes/id/id.spec.ts +0 -51
  103. package/src/utils/attributes/id/id.ts +0 -46
  104. package/src/utils/attributes/id/index.ts +0 -1
  105. package/src/utils/attributes/index.ts +0 -3
  106. package/src/utils/attributes/to-kebab-case/index.ts +0 -1
  107. package/src/utils/attributes/to-kebab-case/to-kebab-case.spec.ts +0 -29
  108. package/src/utils/attributes/to-kebab-case/to-kebab-case.ts +0 -23
  109. package/src/utils/components/__snapshots__/components.spec.ts.snap +0 -313
  110. package/src/utils/components/components.conf.ts +0 -11
  111. package/src/utils/components/components.spec.ts +0 -192
  112. package/src/utils/components/components.ts +0 -138
  113. package/src/utils/components/index.ts +0 -2
  114. package/src/utils/date/date.spec.ts +0 -12
  115. package/src/utils/date/date.ts +0 -20
  116. package/src/utils/date/index.ts +0 -1
  117. package/src/utils/elements/elements.spec.ts +0 -17
  118. package/src/utils/elements/elements.ts +0 -16
  119. package/src/utils/elements/index.ts +0 -1
  120. package/src/utils/index.spec.ts +0 -84
  121. package/src/utils/index.ts +0 -8
  122. package/src/utils/number/index.ts +0 -1
  123. package/src/utils/number/number.spec.ts +0 -14
  124. package/src/utils/number/number.ts +0 -6
  125. package/src/utils/object/index.ts +0 -2
  126. package/src/utils/object/object.conf.ts +0 -4
  127. package/src/utils/object/object.spec.ts +0 -110
  128. package/src/utils/object/object.ts +0 -26
  129. package/src/utils/string/index.ts +0 -1
  130. package/src/utils/string/string.spec.ts +0 -47
  131. package/src/utils/string/string.ts +0 -38
  132. package/src/utils/windows/index.ts +0 -1
  133. package/src/utils/windows/windows.spec.ts +0 -65
  134. package/src/utils/windows/windows.ts +0 -50
  135. package/src/vue/index.spec.ts +0 -8
  136. package/src/vue/index.ts +0 -1
  137. package/src/vue/mg-model/index.ts +0 -1
  138. package/src/vue/mg-model/mg-model.conf.ts +0 -6
  139. package/src/vue/mg-model/mg-model.spec.ts +0 -50
  140. package/src/vue/mg-model/mg-model.ts +0 -50
  141. package/tsconfig.json +0 -17
  142. package/typedoc.json +0 -14
  143. package/vitest.config.mts +0 -13
@@ -1,306 +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 = ({ disconnect, observe, takeRecords }: SetupMutationObserverMockParams): typeof MutationObserver => {
24
- class MockMutationObserver implements MutationObserver {
25
- /**
26
- *
27
- */
28
- disconnect: () => void = disconnect;
29
- /**
30
- *
31
- */
32
- observe: (target: Node, options?: MutationObserverInit) => void = observe;
33
- /**
34
- *
35
- */
36
- takeRecords: () => MutationRecord[] = takeRecords;
37
- /**
38
- *
39
- */
40
- cb: MutationCallback;
41
- constructor(fn: MutationCallback) {
42
- this.cb = fn;
43
- }
44
- }
45
-
46
- [window, global].forEach(element => {
47
- Object.defineProperty(element, 'MutationObserver', {
48
- writable: true,
49
- configurable: true,
50
- value: MockMutationObserver,
51
- });
52
- });
53
-
54
- return MockMutationObserver;
55
- };
56
-
57
- /**
58
- * Utility function that mocks the `ResizeObserver` API. Recommended to execute inside `beforeEach`.
59
- * @param resizeObserverMock - Parameter that is sent to the `Object.defineProperty`
60
- * overwrite method. `jest.fn()` mock functions can be passed here if the goal is to not only
61
- * mock the resize observer, but its methods.
62
- * You can manually fire an intersection entry:
63
- * @param resizeObserverMock - configuration object
64
- * @returns Mocked ResizeObserver
65
- * @example
66
- * ```
67
- * let fireRo;
68
- * setupResizeObserverMock({
69
- * observe: function () {
70
- * fireRo = this.cb;
71
- * },
72
- * });
73
- * ...
74
- * fireRo([{
75
- * borderBoxSize: ResizeObserverSize[],
76
- * contentBoxSize: ResizeObserverSize[],
77
- * contentRect: DOMRectReadOnly,
78
- * devicePixelContentBoxSize: ResizeObserverSize[],
79
- * target: yourMockElemenet
80
- * }]);;
81
- * ```
82
- */
83
- export const setupResizeObserverMock = ({ disconnect, observe }: setupResizeObserverMockParams): typeof ResizeObserver => {
84
- class MockResizeObserver implements ResizeObserver {
85
- /**
86
- *
87
- */
88
- disconnect: () => void = disconnect;
89
- /**
90
- *
91
- */
92
- observe: (target: Element, options?: ResizeObserverOptions) => void = observe;
93
- /**
94
- *
95
- */
96
- unobserve!: () => void;
97
- /**
98
- *
99
- */
100
- cb: ResizeObserverCallback;
101
- constructor(fn: ResizeObserverCallback) {
102
- this.cb = fn;
103
- }
104
- }
105
-
106
- [window, global].forEach(element => {
107
- Object.defineProperty(element, 'ResizeObserver', {
108
- writable: true,
109
- configurable: true,
110
- value: MockResizeObserver,
111
- });
112
- });
113
-
114
- return MockResizeObserver;
115
- };
116
-
117
- class MockCustomEvent extends Event {
118
- /**
119
- *
120
- */
121
- detail: any; // eslint-disable-line @typescript-eslint/no-explicit-any
122
- }
123
-
124
- /**
125
- * Utility function that mocks the `SubmitEvent` API. Recommended to execute inside `beforeEach`.
126
- * @example
127
- * ```
128
- * setupSubmitEventMock();
129
- * ```
130
- * @returns custom event mock
131
- */
132
- export const setupSubmitEventMock = (): typeof MockCustomEvent => {
133
- class SubmitEvent extends MockCustomEvent {}
134
-
135
- [window, global].forEach(element => {
136
- Object.defineProperty(element, 'SubmitEvent', {
137
- writable: true,
138
- configurable: true,
139
- value: SubmitEvent,
140
- });
141
- });
142
-
143
- return SubmitEvent;
144
- };
145
-
146
- /**
147
- * Utility function that mocks the `requestAnimationFrame` API. Recommended to execute inside `test`.
148
- * @example
149
- * ```
150
- * setUpRequestAnimationFrameMock(jest.runOnlyPendingTimers);
151
- * ```
152
- * @param faketimer - recommended to use jest.runOnlyPendingTimers()
153
- * @returns custom setUpRequestAnimationFrameMock mock
154
- */
155
- export const setUpRequestAnimationFrameMock = (faketimer: () => void): typeof requestAnimationFrame => {
156
- const requestAnimationFrame = (callback: FrameRequestCallback) => {
157
- setTimeout(callback, 1);
158
- faketimer();
159
- return 0;
160
- };
161
-
162
- [window, global].forEach(element => {
163
- Object.defineProperty(element, 'requestAnimationFrame', {
164
- writable: true,
165
- configurable: true,
166
- value: requestAnimationFrame,
167
- });
168
- });
169
-
170
- return requestAnimationFrame;
171
- };
172
-
173
- /**
174
- * Convert string to given type
175
- * @param value - string value to format
176
- * @param type - new value output type
177
- * @returns string value converted to given type
178
- */
179
- const convertString = (value: string, type: string): number | Date | string => {
180
- switch (type) {
181
- case 'date':
182
- return new Date(value);
183
- case 'number':
184
- return Number(value);
185
- default:
186
- return value;
187
- }
188
- };
189
-
190
- /**
191
- * Get range underflow value from input['min]
192
- * @param input - HTMLInputElement
193
- * @returns truthy iv value is underflow
194
- */
195
- const getRangeUnderflow = (input: HTMLInputElement): boolean => {
196
- const value = convertString(input.value, input.type);
197
- if (['date', 'number'].includes(input.type) && input.hasAttribute('min') && input.value.length > 0) {
198
- const min = convertString(input.min, input.type);
199
- return value < min;
200
- } else {
201
- return false;
202
- }
203
- };
204
-
205
- /**
206
- * Get range overflow value from input['max]
207
- * @param input - HTMLInputElement
208
- * @returns truthy iv value is overflow
209
- */
210
- const getRangeOverflow = (input: HTMLInputElement): boolean => {
211
- const value = convertString(input.value, input.type);
212
- if (['date', 'number'].includes(input.type) && input.hasAttribute('max') && input.value.length > 0) {
213
- const max = convertString(input.max, input.type);
214
- return value > max;
215
- } else {
216
- return false;
217
- }
218
- };
219
-
220
- /**
221
- * HTMLSelectElement type guard
222
- * @param input - HTMLElement to test
223
- * @returns truthy if element is HTMLSelectElement
224
- */
225
- const isHTMLSelectElement = (input: HTMLElement): input is HTMLSelectElement => (input as HTMLSelectElement).options !== undefined;
226
- /**
227
- * Is value missing from input
228
- * @param input - input to test
229
- * @returns truthy if value is required
230
- */
231
- const isValueMissing = (input: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | HTMLFieldSetElement): boolean => {
232
- if (input.hasAttribute('required') && (input as HTMLInputElement).required === true) {
233
- if (['checkbox', 'radio'].includes(input.type)) {
234
- return (input as HTMLInputElement).checked === false;
235
- } else if (isHTMLSelectElement(input)) {
236
- return input.options.item(input.options.selectedIndex)?.value === '';
237
- } else {
238
- return (input as HTMLInputElement).value.length === 0;
239
- }
240
- } else {
241
- return false;
242
- }
243
- };
244
-
245
- /**
246
- * Get input validity
247
- * @param input - input prototype
248
- * @returns input validity state
249
- */
250
- const getValidity = (input: HTMLInputElement): ValidityState => {
251
- // required field without a value
252
- const valueMissing = isValueMissing(input);
253
- // value of a number field is not a number | value of a date field is not a date
254
- const badInput = ['number', 'date'].includes(input.type) && isNaN(input.type === 'date' ? Date.parse(input.value) : (input.value as unknown as number));
255
- // value does not conform to the pattern
256
- const patternMismatch = input.hasAttribute('pattern') && new RegExp(input.pattern).test(input.value) === false;
257
- // value of a number|date field is higher than the max attribute
258
- const rangeOverflow = getRangeOverflow(input);
259
- // value of a number|date field is lower than the min attribute
260
- const rangeUnderflow = getRangeUnderflow(input);
261
- // value of a number field does not conform to the stepattribute
262
- const stepMismatch = input.type === 'number' && input.hasAttribute('step') && input.step !== 'any' && Number(input.value) % parseFloat(input.step) !== 0;
263
- // the user has edited a too-long value in a field with maxlength
264
- const tooLong = input.hasAttribute('maxLength') && input.value?.length > Number(input.maxLength);
265
- // the user has edited a too-short value in a field with minlength
266
- const tooShort = input.hasAttribute('minLength') && input.value?.length < Number(input.minLength);
267
- // value of a email or URL field is not an email address or URL
268
- const typeMismatch = input.type === 'url' && !URL.canParse(input.value);
269
- // value of validationMessage is not an empty string
270
- const customError = false;
271
-
272
- const valid = ![valueMissing, badInput, patternMismatch, rangeOverflow, rangeUnderflow, stepMismatch, tooLong, tooShort, typeMismatch, customError].some(invalid => invalid);
273
- return {
274
- badInput,
275
- customError,
276
- patternMismatch,
277
- rangeOverflow,
278
- rangeUnderflow,
279
- stepMismatch,
280
- tooLong,
281
- tooShort,
282
- typeMismatch,
283
- valid,
284
- valueMissing,
285
- };
286
- };
287
-
288
- /**
289
- * Utility function that mocks the HTMLInputElement[`vality`] state API and the HTMLInputElement.checkvalidty() methode.
290
- * Recommended to execute inside `test`.
291
- * @example
292
- * ```
293
- * Array.from(document.querySelectorInputs('input')).forEach(setUpHTMLInputElementValidity);
294
- * ```
295
- */
296
- export const setUpHTMLInputElementValidity = (input: HTMLInputElement) => {
297
- input.checkValidity = () => {
298
- const { valid } = getValidity(input);
299
- input.dispatchEvent(new CustomEvent('invalid', { detail: !valid }));
300
- return valid;
301
- };
302
- Object.defineProperty(input, 'validity', {
303
- get: () => getValidity(input),
304
- configurable: true,
305
- });
306
- };
@@ -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,51 +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'])('Should NOT validate ID : %s', id => {
23
- const valide = isValideID(id);
24
- expect(valide).toEqual(false);
25
- });
26
-
27
- test.each(['hello', 'hello-batman', 'hello-batman-in-gotham', 'hello-1'])('Should validate ID : %s', id => {
28
- const valide = isValideID(id);
29
- expect(valide).toEqual(true);
30
- });
31
- });
32
-
33
- describe('formatID', () => {
34
- test.each([
35
- ['UPPERCASE', 'uppercase'],
36
- ['camelCase', 'camel-case'],
37
- ['PascalCase', 'pascal-case'],
38
- ['text/is/href', 'text-is-href'],
39
- [1, '1'],
40
- [{ href: 'text/is/href' }, 'href-text-is-href'],
41
- [[{ href: 'text/is/href' }], 'href-text-is-href'],
42
- [null, undefined],
43
- [undefined, undefined],
44
- [true, undefined],
45
- [false, undefined],
46
- ])('Should format value as valid ID : %s', (input, expected) => {
47
- const id = formatID(input);
48
- expect(id).toEqual(expected);
49
- });
50
- });
51
- });
@@ -1,46 +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 => isValidString(newValue) && /^([a-z][a-z0-9]*)(-[a-z0-9]+)*$/.exec(newValue) !== null;
30
-
31
- /**
32
- * Format id from value
33
- * @param value - id to transforme
34
- * @returns valid id
35
- */
36
- export const formatID = (value: unknown): string | undefined => {
37
- let id;
38
- if (typeof value === 'string') {
39
- id = value;
40
- } else if (Boolean(value) && typeof value === 'object' && (isObject(value) || Array.isArray(value))) {
41
- id = JSON.stringify(value);
42
- } else if (value !== null && value !== undefined && typeof value !== 'boolean' && typeof value !== 'object') {
43
- id = String(value);
44
- }
45
- return id ? toKebabCase(id) : id;
46
- };
@@ -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