@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.
Files changed (146) hide show
  1. package/.turbo/turbo-build.log +52 -0
  2. package/CHANGELOG.md +12 -0
  3. package/LICENSE +29 -0
  4. package/README.md +95 -0
  5. package/dist/angular/index.cjs +27 -0
  6. package/dist/angular/index.cjs.map +1 -0
  7. package/dist/angular/index.d.ts +21 -0
  8. package/dist/angular/index.js +25 -0
  9. package/dist/angular/index.js.map +1 -0
  10. package/dist/angular/index.umd.js +33 -0
  11. package/dist/angular/index.umd.js.map +1 -0
  12. package/dist/locale/index.cjs +164 -0
  13. package/dist/locale/index.cjs.map +1 -0
  14. package/dist/locale/index.d.ts +103 -0
  15. package/dist/locale/index.js +156 -0
  16. package/dist/locale/index.js.map +1 -0
  17. package/dist/locale/index.umd.js +170 -0
  18. package/dist/locale/index.umd.js.map +1 -0
  19. package/dist/playwright/index.cjs +124 -0
  20. package/dist/playwright/index.cjs.map +1 -0
  21. package/dist/playwright/index.d.ts +35 -0
  22. package/dist/playwright/index.js +120 -0
  23. package/dist/playwright/index.js.map +1 -0
  24. package/dist/playwright/index.umd.js +130 -0
  25. package/dist/playwright/index.umd.js.map +1 -0
  26. package/dist/stencil/index.cjs +281 -0
  27. package/dist/stencil/index.cjs.map +1 -0
  28. package/dist/stencil/index.d.ts +111 -0
  29. package/dist/stencil/index.js +277 -0
  30. package/dist/stencil/index.js.map +1 -0
  31. package/dist/stencil/index.umd.js +287 -0
  32. package/dist/stencil/index.umd.js.map +1 -0
  33. package/dist/storybook/index.cjs +2109 -0
  34. package/dist/storybook/index.cjs.map +1 -0
  35. package/dist/storybook/index.d.ts +85 -0
  36. package/dist/storybook/index.js +2103 -0
  37. package/dist/storybook/index.js.map +1 -0
  38. package/dist/storybook/index.umd.js +2115 -0
  39. package/dist/storybook/index.umd.js.map +1 -0
  40. package/dist/tests/index.cjs +166 -0
  41. package/dist/tests/index.cjs.map +1 -0
  42. package/dist/tests/index.d.ts +91 -0
  43. package/dist/tests/index.js +161 -0
  44. package/dist/tests/index.js.map +1 -0
  45. package/dist/tests/index.umd.js +172 -0
  46. package/dist/tests/index.umd.js.map +1 -0
  47. package/dist/utils/index.cjs +433 -0
  48. package/dist/utils/index.cjs.map +1 -0
  49. package/dist/utils/index.d.ts +265 -0
  50. package/dist/utils/index.js +409 -0
  51. package/dist/utils/index.js.map +1 -0
  52. package/dist/utils/index.umd.js +439 -0
  53. package/dist/utils/index.umd.js.map +1 -0
  54. package/dist/vue/index.cjs +1934 -0
  55. package/dist/vue/index.cjs.map +1 -0
  56. package/dist/vue/index.d.ts +35 -0
  57. package/dist/vue/index.js +1932 -0
  58. package/dist/vue/index.js.map +1 -0
  59. package/dist/vue/index.umd.js +1940 -0
  60. package/dist/vue/index.umd.js.map +1 -0
  61. package/json-doc.test.json +253 -0
  62. package/package.json +80 -0
  63. package/rollup.config.mjs +61 -0
  64. package/src/angular/index.spec.ts +8 -0
  65. package/src/angular/index.ts +1 -0
  66. package/src/angular/mg-angular-logger/index.ts +1 -0
  67. package/src/angular/mg-angular-logger/mg-angular-logger.conf.ts +3 -0
  68. package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +40 -0
  69. package/src/angular/mg-angular-logger/mg-angular-logger.ts +23 -0
  70. package/src/locale/index.spec.ts +14 -0
  71. package/src/locale/index.ts +1 -0
  72. package/src/locale/locale.spec.ts +173 -0
  73. package/src/locale/locale.ts +166 -0
  74. package/src/playwright/config/index.ts +43 -0
  75. package/src/playwright/index.spec.ts +16 -0
  76. package/src/playwright/index.ts +4 -0
  77. package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +48 -0
  78. package/src/playwright/utils/index.ts +1 -0
  79. package/src/playwright/utils/utils.spec.ts +43 -0
  80. package/src/playwright/utils/utils.ts +64 -0
  81. package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +527 -0
  82. package/src/stencil/ide/ide.spec.ts +25 -0
  83. package/src/stencil/ide/ide.ts +214 -0
  84. package/src/stencil/ide/index.ts +1 -0
  85. package/src/stencil/index.spec.ts +12 -0
  86. package/src/stencil/index.ts +1 -0
  87. package/src/storybook/index.spec.ts +15 -0
  88. package/src/storybook/index.ts +1 -0
  89. package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +329 -0
  90. package/src/storybook/stencil/index.ts +1 -0
  91. package/src/storybook/stencil/stencil-shim.d.ts +1 -0
  92. package/src/storybook/stencil/stencil.conf.ts +4 -0
  93. package/src/storybook/stencil/stencil.spec.ts +142 -0
  94. package/src/storybook/stencil/stencil.ts +366 -0
  95. package/src/tests/index.spec.ts +13 -0
  96. package/src/tests/index.ts +1 -0
  97. package/src/tests/unit/__snapshots__/unit.spec.ts.snap +52 -0
  98. package/src/tests/unit/index.ts +1 -0
  99. package/src/tests/unit/unit.conf.ts +9 -0
  100. package/src/tests/unit/unit.spec.ts +44 -0
  101. package/src/tests/unit/unit.ts +171 -0
  102. package/src/utils/attributes/class-list/class-list.spec.ts +36 -0
  103. package/src/utils/attributes/class-list/class-list.ts +51 -0
  104. package/src/utils/attributes/class-list/index.ts +1 -0
  105. package/src/utils/attributes/id/id.spec.ts +51 -0
  106. package/src/utils/attributes/id/id.ts +46 -0
  107. package/src/utils/attributes/id/index.ts +1 -0
  108. package/src/utils/attributes/index.ts +3 -0
  109. package/src/utils/attributes/to-kebab-case/index.ts +1 -0
  110. package/src/utils/attributes/to-kebab-case/to-kebab-case.spec.ts +29 -0
  111. package/src/utils/attributes/to-kebab-case/to-kebab-case.ts +23 -0
  112. package/src/utils/components/__snapshots__/components.spec.ts.snap +313 -0
  113. package/src/utils/components/components.conf.ts +11 -0
  114. package/src/utils/components/components.spec.ts +192 -0
  115. package/src/utils/components/components.ts +138 -0
  116. package/src/utils/components/index.ts +2 -0
  117. package/src/utils/date/date.spec.ts +12 -0
  118. package/src/utils/date/date.ts +20 -0
  119. package/src/utils/date/index.ts +1 -0
  120. package/src/utils/elements/elements.spec.ts +17 -0
  121. package/src/utils/elements/elements.ts +16 -0
  122. package/src/utils/elements/index.ts +1 -0
  123. package/src/utils/index.spec.ts +84 -0
  124. package/src/utils/index.ts +8 -0
  125. package/src/utils/number/index.ts +1 -0
  126. package/src/utils/number/number.spec.ts +14 -0
  127. package/src/utils/number/number.ts +6 -0
  128. package/src/utils/object/index.ts +2 -0
  129. package/src/utils/object/object.conf.ts +4 -0
  130. package/src/utils/object/object.spec.ts +98 -0
  131. package/src/utils/object/object.ts +26 -0
  132. package/src/utils/string/index.ts +1 -0
  133. package/src/utils/string/string.spec.ts +47 -0
  134. package/src/utils/string/string.ts +38 -0
  135. package/src/utils/windows/index.ts +1 -0
  136. package/src/utils/windows/windows.spec.ts +65 -0
  137. package/src/utils/windows/windows.ts +50 -0
  138. package/src/vue/index.spec.ts +8 -0
  139. package/src/vue/index.ts +1 -0
  140. package/src/vue/mg-model/index.ts +1 -0
  141. package/src/vue/mg-model/mg-model.conf.ts +6 -0
  142. package/src/vue/mg-model/mg-model.spec.ts +50 -0
  143. package/src/vue/mg-model/mg-model.ts +50 -0
  144. package/tsconfig.json +17 -0
  145. package/typedoc.json +14 -0
  146. package/vitest.config.mts +13 -0
@@ -0,0 +1,313 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`components > Page > Should create Page instance 1`] = `
4
+ Page {
5
+ "baseIndex": 1,
6
+ "getIndexFromCursor": [Function],
7
+ "items": [
8
+ "batman",
9
+ "robin",
10
+ "joker",
11
+ "bane",
12
+ "harley quinn",
13
+ "dante",
14
+ ],
15
+ "next": undefined,
16
+ "top": 10,
17
+ "total": 6,
18
+ }
19
+ `;
20
+
21
+ exports[`components > Page > Should create Page instance 2`] = `
22
+ Page {
23
+ "baseIndex": 1,
24
+ "getIndexFromCursor": [Function],
25
+ "items": [
26
+ {
27
+ "title": "batman",
28
+ "value": 0,
29
+ },
30
+ {
31
+ "title": "robin",
32
+ "value": 1,
33
+ },
34
+ {
35
+ "title": "joker",
36
+ "value": 2,
37
+ },
38
+ {
39
+ "title": "bane",
40
+ "value": 3,
41
+ },
42
+ {
43
+ "title": "harley quinn",
44
+ "value": 4,
45
+ },
46
+ {
47
+ "title": "dante",
48
+ "value": 5,
49
+ },
50
+ ],
51
+ "next": undefined,
52
+ "top": 10,
53
+ "total": 6,
54
+ }
55
+ `;
56
+
57
+ exports[`components > Page > Should create Page instance 3`] = `
58
+ Page {
59
+ "baseIndex": 1,
60
+ "getIndexFromCursor": [Function],
61
+ "items": [
62
+ "batman",
63
+ "robin",
64
+ "joker",
65
+ "bane",
66
+ "harley quinn",
67
+ "dante",
68
+ ],
69
+ "next": undefined,
70
+ "top": 10,
71
+ "total": 100,
72
+ }
73
+ `;
74
+
75
+ exports[`components > Page > Should create Page instance 4`] = `
76
+ Page {
77
+ "baseIndex": 1,
78
+ "getIndexFromCursor": [Function],
79
+ "items": [
80
+ "batman",
81
+ "robin",
82
+ "joker",
83
+ "bane",
84
+ "harley quinn",
85
+ "dante",
86
+ ],
87
+ "next": undefined,
88
+ "top": 10,
89
+ "total": 6,
90
+ }
91
+ `;
92
+
93
+ exports[`components > Page > Should create Page instance 5`] = `
94
+ Page {
95
+ "baseIndex": 1,
96
+ "getIndexFromCursor": [Function],
97
+ "items": [
98
+ "batman",
99
+ "robin",
100
+ "joker",
101
+ "bane",
102
+ "harley quinn",
103
+ "dante",
104
+ ],
105
+ "next": "/next/url",
106
+ "top": 10,
107
+ "total": 6,
108
+ }
109
+ `;
110
+
111
+ exports[`components > Paginate > getPage() > Shoud get paginated page from instance { items: [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34' ], options: { next: '/next/page' } } 1`] = `
112
+ Page {
113
+ "baseIndex": 1,
114
+ "getIndexFromCursor": [Function],
115
+ "items": [
116
+ "1",
117
+ "2",
118
+ "3",
119
+ "4",
120
+ "5",
121
+ "6",
122
+ "7",
123
+ "8",
124
+ "9",
125
+ "10",
126
+ ],
127
+ "next": "/next/page",
128
+ "top": 10,
129
+ "total": 10,
130
+ }
131
+ `;
132
+
133
+ exports[`components > Paginate > getPage() > Shoud get paginated page from instance { items: [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34' ], options: { next: URL{ [Symbol(impl)]: URLImpl{ _url: { scheme: 'http', username: '', password: '', host: 'url.fr', port: null, path: [ 'next', 'page' ], query: null, fragment: null }, _query: URLSearchParamsImpl{ _list: [], _url: URLImpl[Circular], [Symbol(wrapper)]: URLSearchParams{ [Symbol(impl)]: URLSearchParamsImpl[Circular] } }, [Symbol(wrapper)]: URL[Circular] } } } } 1`] = `
134
+ Page {
135
+ "baseIndex": 1,
136
+ "getIndexFromCursor": [Function],
137
+ "items": [
138
+ "1",
139
+ "2",
140
+ "3",
141
+ "4",
142
+ "5",
143
+ "6",
144
+ "7",
145
+ "8",
146
+ "9",
147
+ "10",
148
+ ],
149
+ "next": "http://url.fr/next/page",
150
+ "top": 10,
151
+ "total": 10,
152
+ }
153
+ `;
154
+
155
+ exports[`components > Paginate > getPage() > Shoud get paginated page from instance { items: [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34' ], options: { top: 5 } } 1`] = `
156
+ Page {
157
+ "baseIndex": 1,
158
+ "getIndexFromCursor": [Function],
159
+ "items": [
160
+ "1",
161
+ "2",
162
+ "3",
163
+ "4",
164
+ "5",
165
+ ],
166
+ "next": [Function],
167
+ "top": 5,
168
+ "total": 5,
169
+ }
170
+ `;
171
+
172
+ exports[`components > Paginate > getPage() > Shoud get paginated page from instance { items: [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34' ], options: { total: 105 } } 1`] = `
173
+ Page {
174
+ "baseIndex": 1,
175
+ "getIndexFromCursor": [Function],
176
+ "items": [
177
+ "1",
178
+ "2",
179
+ "3",
180
+ "4",
181
+ "5",
182
+ "6",
183
+ "7",
184
+ "8",
185
+ "9",
186
+ "10",
187
+ ],
188
+ "next": [Function],
189
+ "top": 10,
190
+ "total": 105,
191
+ }
192
+ `;
193
+
194
+ exports[`components > Paginate > getPage() > Shoud get paginated page from instance { items: [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34' ], options: undefined } 1`] = `
195
+ Page {
196
+ "baseIndex": 1,
197
+ "getIndexFromCursor": [Function],
198
+ "items": [
199
+ "1",
200
+ "2",
201
+ "3",
202
+ "4",
203
+ "5",
204
+ "6",
205
+ "7",
206
+ "8",
207
+ "9",
208
+ "10",
209
+ ],
210
+ "next": [Function],
211
+ "top": 10,
212
+ "total": 10,
213
+ }
214
+ `;
215
+
216
+ exports[`components > Paginate > getPage() > Shoud get paginated page from instance { items: [ 'batman', 'robin', 'joker', 'bane', 'harley quinn', 'dante' ], options: undefined } 1`] = `
217
+ Page {
218
+ "baseIndex": 1,
219
+ "getIndexFromCursor": [Function],
220
+ "items": [
221
+ "batman",
222
+ "robin",
223
+ "joker",
224
+ "bane",
225
+ "harley quinn",
226
+ "dante",
227
+ ],
228
+ "next": undefined,
229
+ "top": 10,
230
+ "total": 6,
231
+ }
232
+ `;
233
+
234
+ exports[`components > Paginate > getPage() > Shoud get paginated page from instance { items: [], options: undefined } 1`] = `
235
+ Page {
236
+ "baseIndex": 1,
237
+ "getIndexFromCursor": [Function],
238
+ "items": [],
239
+ "next": undefined,
240
+ "top": 10,
241
+ "total": 0,
242
+ }
243
+ `;
244
+
245
+ exports[`components > Paginate > getPage() > Shoud get paginated page from instance { items: {}, options: undefined } 1`] = `
246
+ Page {
247
+ "baseIndex": 1,
248
+ "getIndexFromCursor": [Function],
249
+ "items": [],
250
+ "next": undefined,
251
+ "top": 10,
252
+ "total": 0,
253
+ }
254
+ `;
255
+
256
+ exports[`components > Paginate > getPage() > Should filter paginated page from instance 1`] = `
257
+ Page {
258
+ "baseIndex": 1,
259
+ "getIndexFromCursor": [Function],
260
+ "items": [
261
+ "batman",
262
+ "bane",
263
+ "dante",
264
+ ],
265
+ "next": undefined,
266
+ "top": 10,
267
+ "total": 3,
268
+ }
269
+ `;
270
+
271
+ exports[`components > Paginate > getPage() > Should get next() page from instance 1`] = `
272
+ Page {
273
+ "baseIndex": 1,
274
+ "getIndexFromCursor": [Function],
275
+ "items": [
276
+ "1",
277
+ "2",
278
+ "3",
279
+ "4",
280
+ "5",
281
+ "6",
282
+ "7",
283
+ "8",
284
+ "9",
285
+ "10",
286
+ ],
287
+ "next": [Function],
288
+ "top": 10,
289
+ "total": 10,
290
+ }
291
+ `;
292
+
293
+ exports[`components > Paginate > getPage() > Should get next() page from instance 2`] = `
294
+ Page {
295
+ "baseIndex": 1,
296
+ "getIndexFromCursor": [Function],
297
+ "items": [
298
+ "11",
299
+ "12",
300
+ "13",
301
+ "14",
302
+ "15",
303
+ "16",
304
+ "17",
305
+ "18",
306
+ "19",
307
+ "20",
308
+ ],
309
+ "next": undefined,
310
+ "top": 10,
311
+ "total": 10,
312
+ }
313
+ `;
@@ -0,0 +1,11 @@
1
+ import type { Page } from './components';
2
+
3
+ /**
4
+ * Define getPage type
5
+ */
6
+ export type IGetPage<T> = (offset?: number, filter?: Parameters<Array<T>['filter']>[0]) => Page<T>;
7
+
8
+ /**
9
+ * Cursor type
10
+ */
11
+ export type CursorType = 'first' | 'next' | 'previous' | 'last';
@@ -0,0 +1,192 @@
1
+ import { describe, expect, test, vi } from 'vitest';
2
+ import { Cursor, nextTick, Page, Paginate } from './components';
3
+ import { CursorType } from './components.conf';
4
+
5
+ const initArray = length => new Array(length).fill('').map((_, key) => (key + 1).toString());
6
+
7
+ describe('components', () => {
8
+ describe('nextTick', () => {
9
+ test('Should get a promise', async () => {
10
+ const job = nextTick();
11
+ expect(job.then).toBeDefined();
12
+
13
+ const res = await job;
14
+ expect(res).toBeUndefined();
15
+ });
16
+ test('Should wrapp and execute a callback in a promise', async () => {
17
+ const fn = vi.fn();
18
+ const job = nextTick(fn);
19
+ expect(job.then).toBeDefined();
20
+
21
+ const res = await job;
22
+ expect(fn).toHaveBeenCalled();
23
+ expect(res).toBeUndefined();
24
+ });
25
+ });
26
+ describe('Paginate', () => {
27
+ test('Should create Paginate instance', () => {
28
+ const res = new Paginate([]);
29
+ expect(res.items.length).toEqual(0);
30
+ expect(res).toHaveProperty('getPage');
31
+ });
32
+
33
+ describe('getPage()', () => {
34
+ test.each([
35
+ {
36
+ items: {},
37
+ options: undefined,
38
+ },
39
+ {
40
+ items: [],
41
+ options: undefined,
42
+ },
43
+ {
44
+ items: ['batman', 'robin', 'joker', 'bane', 'harley quinn', 'dante'],
45
+ options: undefined,
46
+ },
47
+ {
48
+ items: initArray(34),
49
+ options: undefined,
50
+ },
51
+ {
52
+ items: initArray(34),
53
+ options: { top: 5 },
54
+ },
55
+ {
56
+ items: initArray(34),
57
+ options: { total: 105 },
58
+ },
59
+ {
60
+ items: initArray(34),
61
+ options: { next: '/next/page' },
62
+ },
63
+ {
64
+ items: initArray(34),
65
+ options: { next: new URL('http://url.fr/next/page') },
66
+ },
67
+ ])('Shoud get paginated page from instance %s', ({ items, options }) => {
68
+ const res = new Paginate(items as unknown[], options);
69
+ const page = res.getPage();
70
+ expect(page).toMatchSnapshot();
71
+ });
72
+
73
+ test('Should filter paginated page from instance', () => {
74
+ const res = new Paginate(['batman', 'robin', 'joker', 'bane', 'harley quinn', 'dante']);
75
+ const page = res.getPage(0, val => val.includes('an'));
76
+ expect(page).toMatchSnapshot();
77
+ });
78
+
79
+ test('Should get next() page from instance', () => {
80
+ const res = new Paginate(initArray(20));
81
+ // get first page
82
+ const page = res.getPage();
83
+ expect(page).toMatchSnapshot();
84
+ // get next page
85
+ const next = (page.next as unknown as () => Page<unknown>)();
86
+ expect(next).toMatchSnapshot();
87
+ });
88
+ });
89
+ });
90
+
91
+ describe('Page', () => {
92
+ const items = ['batman', 'robin', 'joker', 'bane', 'harley quinn', 'dante'];
93
+ test.each([
94
+ {
95
+ items,
96
+ },
97
+ {
98
+ items: items.map((item, index) => ({ title: item, value: index })) as unknown[],
99
+ },
100
+ {
101
+ items,
102
+ total: 100,
103
+ },
104
+ {
105
+ items,
106
+ top: 10,
107
+ },
108
+ {
109
+ items,
110
+ next: '/next/url',
111
+ },
112
+ ])('Should create Page instance', init => {
113
+ const page = new Page(init);
114
+ expect(page).toMatchSnapshot();
115
+ });
116
+
117
+ test('Should thrown error with invalid Page init', () => {
118
+ expect.assertions(1);
119
+ try {
120
+ new Page('batman' as unknown as Record<string, string>);
121
+ } catch (err) {
122
+ expect(err.message).toEqual('Page - init must match IPage type.');
123
+ }
124
+ });
125
+
126
+ describe('getIndexFromCursor()', () => {
127
+ test.each([undefined, 'hello'])('Should get first index by default, cursor %s', cursor => {
128
+ const page = new Page({ items });
129
+ page.items.forEach(() => {
130
+ const index = page.getIndexFromCursor(cursor as CursorType);
131
+ expect(index).toEqual(0);
132
+ });
133
+ });
134
+ describe.each(Object.values(Cursor))('cursor %s', cursor => {
135
+ let result = 0;
136
+ if (cursor === 'first') result = 0;
137
+ if (cursor === 'last') result = items.length - 1;
138
+ if (cursor === 'next') result = 1;
139
+ if (cursor === 'previous') result = items.length - 1;
140
+ test.each([
141
+ {
142
+ items: 'batman',
143
+ result: null,
144
+ },
145
+ {
146
+ items: {},
147
+ result: null,
148
+ },
149
+ {
150
+ items: [],
151
+ result: null,
152
+ },
153
+ {
154
+ items,
155
+ result,
156
+ },
157
+ {
158
+ items: items.map((item, index) => ({ title: item, value: index })) as unknown[],
159
+ result,
160
+ },
161
+ ])('Sould get index from curor %s', ({ items, result }) => {
162
+ const page = new Page({ items: items as unknown[] });
163
+ const index = page.getIndexFromCursor(cursor);
164
+ expect(index).toEqual(result);
165
+ });
166
+ });
167
+ test('Should go to next value witdh oldValue', () => {
168
+ const page = new Page({ items });
169
+ for (const oldIndex in page.items) {
170
+ const index = page.getIndexFromCursor('next', items[Number(oldIndex)]);
171
+ const nextIndex = Number(oldIndex) + page.baseIndex;
172
+ expect(index).toEqual(nextIndex >= page.items.length ? 0 : nextIndex);
173
+ }
174
+ });
175
+ test('Should go to previous value witdh oldValue', () => {
176
+ const page = new Page({ items });
177
+ for (const oldIndex in page.items) {
178
+ const index = page.getIndexFromCursor('previous', items[Number(oldIndex)]);
179
+ const nextIndex = Number(oldIndex) - page.baseIndex;
180
+ expect(index).toEqual(Number(oldIndex) === 0 ? page.items.length - page.baseIndex : nextIndex);
181
+ }
182
+ });
183
+ test.each(['previous', 'next'])('Should get first index with unavailable item, cursor %s', cursor => {
184
+ const page = new Page({ items });
185
+ page.items.forEach(() => {
186
+ const index = page.getIndexFromCursor(cursor as CursorType, 'hello');
187
+ expect(index).toEqual(0);
188
+ });
189
+ });
190
+ });
191
+ });
192
+ });
@@ -0,0 +1,138 @@
1
+ import { isObject } from '../object';
2
+ import { CursorType, IGetPage } from './components.conf';
3
+
4
+ /**
5
+ * Use to process code next tick in the event loop
6
+ * @param callback - code to excute on next tick
7
+ * @returns differed code excution
8
+ */
9
+ export const nextTick = async (callback?: () => void): Promise<void> => {
10
+ if (callback) return callback();
11
+ };
12
+
13
+ /**
14
+ * Cursor possible values
15
+ */
16
+ export const Cursor: Record<string, CursorType> = {
17
+ FIRST: 'first',
18
+ NEXT: 'next',
19
+ PREVIOUS: 'previous',
20
+ LAST: 'last',
21
+ } as const;
22
+
23
+ const DEFAULT_TOP = 10;
24
+
25
+ /**
26
+ * Define a valid Page object and navigate throw page items with cursor.
27
+ * Page object entries follow the REST API page practices.
28
+ */
29
+ export class Page<T> {
30
+ /**
31
+ * Define items
32
+ */
33
+ public items: T[] = [];
34
+ /**
35
+ * Define total
36
+ */
37
+ public total: number;
38
+ /**
39
+ * Define top
40
+ */
41
+ public top: number = DEFAULT_TOP;
42
+ /**
43
+ * Define next
44
+ */
45
+ public next?: IGetPage<T> | string | URL;
46
+ /**
47
+ * Define base index
48
+ */
49
+ public readonly baseIndex = 1;
50
+
51
+ constructor(init: Partial<Pick<Page<T>, 'items' | 'top' | 'total' | 'next'>>) {
52
+ if (!isObject(init)) {
53
+ throw new Error('Page - init must match IPage type.');
54
+ } else {
55
+ if (Array.isArray(init.items)) this.items = init.items;
56
+ if (typeof init.top === 'number') this.top = init.top;
57
+ this.total = typeof init.total === 'number' ? init.total : this.items.length;
58
+ this.next = init.next;
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Get index of items from cursor
64
+ * @param cursor - cursor to find
65
+ * @param oldItem - previous item
66
+ * @returns item index
67
+ */
68
+ public getIndexFromCursor = (cursor: CursorType = 'first', oldItem?: T): number | null => {
69
+ const startIndex = 0;
70
+ if (!Array.isArray(this.items) || !this.items.length) return null;
71
+ const lastIndex = this.items.length - this.baseIndex;
72
+
73
+ let newIndex;
74
+ let oldIndex = startIndex;
75
+ if (['previous', 'next'].includes(cursor) && oldItem) {
76
+ const findedIndex = this.items.findIndex(item => JSON.stringify(item) === JSON.stringify(oldItem));
77
+ if (findedIndex === -1) return startIndex;
78
+ oldIndex = findedIndex;
79
+ }
80
+ // Update index from cursor
81
+ if (cursor === 'first') {
82
+ newIndex = startIndex;
83
+ } else if (cursor === 'last') {
84
+ newIndex = lastIndex;
85
+ } else if (cursor === 'previous') {
86
+ newIndex = JSON.stringify(this.items[oldIndex]) === JSON.stringify(this.items[startIndex]) ? lastIndex : oldIndex - this.baseIndex;
87
+ } else if (cursor === 'next') {
88
+ newIndex = JSON.stringify(this.items[oldIndex]) === JSON.stringify(this.items[lastIndex]) ? startIndex : oldIndex + this.baseIndex;
89
+ } else {
90
+ newIndex = startIndex;
91
+ }
92
+
93
+ return newIndex;
94
+ };
95
+ }
96
+
97
+ /**
98
+ * Paginate an items array to navigate into with pages.
99
+ * It follow REST standard and allow to navigate in items array with a similar format.
100
+ */
101
+ export class Paginate<T> {
102
+ /**
103
+ * Define paginated items
104
+ */
105
+ public items: Page<T>['items'] = [];
106
+
107
+ /* Privates */
108
+ #top: Page<T>['top'] = DEFAULT_TOP;
109
+ #next?: Page<T>['next'];
110
+ #total?: Page<T>['total'];
111
+
112
+ constructor(items: Page<T>['items'], options?: { step?: number; top?: Page<T>['top']; total?: Page<T>['total']; next?: Page<T>['next'] }) {
113
+ if (Array.isArray(items)) this.items = items;
114
+ if (options && (['string', 'function'].includes(typeof options.next) || (isObject<URL>(options.next) && URL.canParse(options.next)))) this.#next = options.next;
115
+ if (typeof options?.top === 'number') this.#top = options.top;
116
+ if (typeof options?.total === 'number') this.#total = options.total;
117
+ }
118
+
119
+ /**
120
+ * Get page
121
+ * @param offset - pagiantion offset
122
+ * @param filter - filter methode
123
+ * @returns formated page
124
+ */
125
+ public getPage: IGetPage<T> = (offset = 0, filter?) => {
126
+ const items = typeof filter === 'function' ? this.items.filter(filter) : this.items;
127
+ let next;
128
+ if (this.#next) next = this.#next;
129
+ else if (items.length > offset + this.#top) next = () => this.getPage(offset + this.#top, filter);
130
+
131
+ return new Page({
132
+ items: items.slice(offset, offset + this.#top),
133
+ total: this.#total,
134
+ top: this.#top,
135
+ next,
136
+ });
137
+ };
138
+ }
@@ -0,0 +1,2 @@
1
+ export * from './components';
2
+ export type { CursorType } from './components.conf';
@@ -0,0 +1,12 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { dateToString } from './date';
3
+
4
+ describe('date', () => {
5
+ describe('dateToString', () => {
6
+ test('Should Format date to string pattern', () => {
7
+ const stringDate = '2023-12-24';
8
+ const formatedtext = dateToString(new Date(stringDate));
9
+ expect(formatedtext).toEqual(stringDate);
10
+ });
11
+ });
12
+ });
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Date RegExp, usefull to test if string is a follow the date pattern
3
+ * @example
4
+ * ```ts
5
+ * dateRegExp.test('mystring') // false
6
+ * dateRegExp.test('2020-12-31') // true
7
+ * ```
8
+ */
9
+ export const dateRegExp = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
10
+
11
+ /**
12
+ * Formats a date object to a string with the pattern 'YYYY-MM-DD'.
13
+ * @param date - date to parse
14
+ * @returns string date with pattern 'YYYY-MM-DD'
15
+ * @example
16
+ * ```ts
17
+ * dateToString(new Date('2023-12-24')) // '2023-12-24'
18
+ * ```
19
+ */
20
+ export const dateToString = (date: Date): string | undefined => date.toISOString().split('T')[0];
@@ -0,0 +1 @@
1
+ export * from './date';