@mgdis/core-ui-helpers 1.0.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.
- package/.turbo/turbo-build.log +52 -0
- package/CHANGELOG.md +12 -0
- package/LICENSE +29 -0
- package/README.md +95 -0
- package/dist/angular/index.cjs +27 -0
- package/dist/angular/index.cjs.map +1 -0
- package/dist/angular/index.d.ts +21 -0
- package/dist/angular/index.js +25 -0
- package/dist/angular/index.js.map +1 -0
- package/dist/angular/index.umd.js +33 -0
- package/dist/angular/index.umd.js.map +1 -0
- package/dist/locale/index.cjs +164 -0
- package/dist/locale/index.cjs.map +1 -0
- package/dist/locale/index.d.ts +103 -0
- package/dist/locale/index.js +156 -0
- package/dist/locale/index.js.map +1 -0
- package/dist/locale/index.umd.js +170 -0
- package/dist/locale/index.umd.js.map +1 -0
- package/dist/playwright/index.cjs +124 -0
- package/dist/playwright/index.cjs.map +1 -0
- package/dist/playwright/index.d.ts +35 -0
- package/dist/playwright/index.js +120 -0
- package/dist/playwright/index.js.map +1 -0
- package/dist/playwright/index.umd.js +130 -0
- package/dist/playwright/index.umd.js.map +1 -0
- package/dist/stencil/index.cjs +281 -0
- package/dist/stencil/index.cjs.map +1 -0
- package/dist/stencil/index.d.ts +111 -0
- package/dist/stencil/index.js +277 -0
- package/dist/stencil/index.js.map +1 -0
- package/dist/stencil/index.umd.js +287 -0
- package/dist/stencil/index.umd.js.map +1 -0
- package/dist/storybook/index.cjs +2109 -0
- package/dist/storybook/index.cjs.map +1 -0
- package/dist/storybook/index.d.ts +85 -0
- package/dist/storybook/index.js +2103 -0
- package/dist/storybook/index.js.map +1 -0
- package/dist/storybook/index.umd.js +2115 -0
- package/dist/storybook/index.umd.js.map +1 -0
- package/dist/tests/index.cjs +166 -0
- package/dist/tests/index.cjs.map +1 -0
- package/dist/tests/index.d.ts +91 -0
- package/dist/tests/index.js +161 -0
- package/dist/tests/index.js.map +1 -0
- package/dist/tests/index.umd.js +172 -0
- package/dist/tests/index.umd.js.map +1 -0
- package/dist/utils/index.cjs +433 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.ts +265 -0
- package/dist/utils/index.js +409 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.umd.js +439 -0
- package/dist/utils/index.umd.js.map +1 -0
- package/dist/vue/index.cjs +1934 -0
- package/dist/vue/index.cjs.map +1 -0
- package/dist/vue/index.d.ts +35 -0
- package/dist/vue/index.js +1932 -0
- package/dist/vue/index.js.map +1 -0
- package/dist/vue/index.umd.js +1940 -0
- package/dist/vue/index.umd.js.map +1 -0
- package/json-doc.test.json +253 -0
- package/package.json +80 -0
- package/rollup.config.mjs +61 -0
- package/src/angular/index.spec.ts +8 -0
- package/src/angular/index.ts +1 -0
- package/src/angular/mg-angular-logger/index.ts +1 -0
- package/src/angular/mg-angular-logger/mg-angular-logger.conf.ts +3 -0
- package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +40 -0
- package/src/angular/mg-angular-logger/mg-angular-logger.ts +23 -0
- package/src/locale/index.spec.ts +14 -0
- package/src/locale/index.ts +1 -0
- package/src/locale/locale.spec.ts +173 -0
- package/src/locale/locale.ts +166 -0
- package/src/playwright/config/index.ts +43 -0
- package/src/playwright/index.spec.ts +16 -0
- package/src/playwright/index.ts +4 -0
- package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +48 -0
- package/src/playwright/utils/index.ts +1 -0
- package/src/playwright/utils/utils.spec.ts +43 -0
- package/src/playwright/utils/utils.ts +64 -0
- package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +527 -0
- package/src/stencil/ide/ide.spec.ts +25 -0
- package/src/stencil/ide/ide.ts +214 -0
- package/src/stencil/ide/index.ts +1 -0
- package/src/stencil/index.spec.ts +12 -0
- package/src/stencil/index.ts +1 -0
- package/src/storybook/index.spec.ts +15 -0
- package/src/storybook/index.ts +1 -0
- package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +329 -0
- package/src/storybook/stencil/index.ts +1 -0
- package/src/storybook/stencil/stencil-shim.d.ts +1 -0
- package/src/storybook/stencil/stencil.conf.ts +4 -0
- package/src/storybook/stencil/stencil.spec.ts +142 -0
- package/src/storybook/stencil/stencil.ts +366 -0
- package/src/tests/index.spec.ts +13 -0
- package/src/tests/index.ts +1 -0
- package/src/tests/unit/__snapshots__/unit.spec.ts.snap +52 -0
- package/src/tests/unit/index.ts +1 -0
- package/src/tests/unit/unit.conf.ts +9 -0
- package/src/tests/unit/unit.spec.ts +44 -0
- package/src/tests/unit/unit.ts +171 -0
- package/src/utils/attributes/class-list/class-list.spec.ts +36 -0
- package/src/utils/attributes/class-list/class-list.ts +51 -0
- package/src/utils/attributes/class-list/index.ts +1 -0
- package/src/utils/attributes/id/id.spec.ts +51 -0
- package/src/utils/attributes/id/id.ts +46 -0
- package/src/utils/attributes/id/index.ts +1 -0
- package/src/utils/attributes/index.ts +3 -0
- package/src/utils/attributes/to-kebab-case/index.ts +1 -0
- package/src/utils/attributes/to-kebab-case/to-kebab-case.spec.ts +29 -0
- package/src/utils/attributes/to-kebab-case/to-kebab-case.ts +23 -0
- package/src/utils/components/__snapshots__/components.spec.ts.snap +313 -0
- package/src/utils/components/components.conf.ts +11 -0
- package/src/utils/components/components.spec.ts +192 -0
- package/src/utils/components/components.ts +138 -0
- package/src/utils/components/index.ts +2 -0
- package/src/utils/date/date.spec.ts +12 -0
- package/src/utils/date/date.ts +20 -0
- package/src/utils/date/index.ts +1 -0
- package/src/utils/elements/elements.spec.ts +17 -0
- package/src/utils/elements/elements.ts +16 -0
- package/src/utils/elements/index.ts +1 -0
- package/src/utils/index.spec.ts +84 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/number/index.ts +1 -0
- package/src/utils/number/number.spec.ts +14 -0
- package/src/utils/number/number.ts +6 -0
- package/src/utils/object/index.ts +2 -0
- package/src/utils/object/object.conf.ts +4 -0
- package/src/utils/object/object.spec.ts +98 -0
- package/src/utils/object/object.ts +26 -0
- package/src/utils/string/index.ts +1 -0
- package/src/utils/string/string.spec.ts +47 -0
- package/src/utils/string/string.ts +38 -0
- package/src/utils/windows/index.ts +1 -0
- package/src/utils/windows/windows.spec.ts +65 -0
- package/src/utils/windows/windows.ts +50 -0
- package/src/vue/index.spec.ts +8 -0
- package/src/vue/index.ts +1 -0
- package/src/vue/mg-model/index.ts +1 -0
- package/src/vue/mg-model/mg-model.conf.ts +6 -0
- package/src/vue/mg-model/mg-model.spec.ts +50 -0
- package/src/vue/mg-model/mg-model.ts +50 -0
- package/tsconfig.json +17 -0
- package/typedoc.json +14 -0
- package/vitest.config.mts +13 -0
|
@@ -0,0 +1,171 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './class-list';
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './id';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './to-kebab-case';
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|