@webalternatif/js-core 1.0.0 → 1.1.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.
- package/package.json +14 -3
- package/.babelrc +0 -12
- package/.idea/inspectionProfiles/Project_Default.xml +0 -12
- package/.idea/js-core.iml +0 -8
- package/.idea/modules.xml +0 -8
- package/.idea/php.xml +0 -20
- package/.idea/vcs.xml +0 -6
- package/i18n/agenda/en.js +0 -73
- package/i18n/agenda/fr.js +0 -73
- package/i18n/agenda/index.js +0 -2
- package/i18n/ajaxform/en.js +0 -5
- package/i18n/ajaxform/fr.js +0 -5
- package/i18n/ajaxform/index.js +0 -2
- package/i18n/ajaxupload/en.js +0 -12
- package/i18n/ajaxupload/fr.js +0 -12
- package/i18n/ajaxupload/index.js +0 -2
- package/i18n/autocomplete/en.js +0 -3
- package/i18n/autocomplete/fr.js +0 -3
- package/i18n/autocomplete/index.js +0 -2
- package/i18n/confirm/en.js +0 -5
- package/i18n/confirm/fr.js +0 -5
- package/i18n/confirm/index.js +0 -2
- package/i18n/core/en.js +0 -4
- package/i18n/core/fr.js +0 -4
- package/i18n/core/index.js +0 -2
- package/i18n/datagrid/en.js +0 -8
- package/i18n/datagrid/fr.js +0 -8
- package/i18n/datagrid/index.js +0 -2
- package/i18n/date/en.js +0 -51
- package/i18n/date/fr.js +0 -51
- package/i18n/date/index.js +0 -2
- package/i18n/datetimepicker/en.js +0 -30
- package/i18n/datetimepicker/fr.js +0 -30
- package/i18n/datetimepicker/index.js +0 -2
- package/i18n/dialog/en.js +0 -3
- package/i18n/dialog/fr.js +0 -3
- package/i18n/dialog/index.js +0 -2
- package/i18n/fulldayeventagenda/en.js +0 -73
- package/i18n/fulldayeventagenda/fr.js +0 -73
- package/i18n/fulldayeventagenda/index.js +0 -2
- package/i18n/index.d.ts +0 -4
- package/i18n/index.js +0 -15
- package/i18n/richtexteditor/en.js +0 -58
- package/i18n/richtexteditor/fr.js +0 -58
- package/i18n/richtexteditor/index.js +0 -2
- package/i18n/select/en.js +0 -3
- package/i18n/select/fr.js +0 -3
- package/i18n/select/index.js +0 -2
- package/i18n/timepicker/en.js +0 -3
- package/i18n/timepicker/fr.js +0 -3
- package/i18n/timepicker/index.js +0 -2
- package/i18n/useragenda/en.js +0 -74
- package/i18n/useragenda/fr.js +0 -73
- package/i18n/useragenda/index.js +0 -2
- package/jest.config.js +0 -14
- package/src/array.js +0 -124
- package/src/dom.js +0 -569
- package/src/eventDispatcher.js +0 -118
- package/src/i18n.js +0 -55
- package/src/index.js +0 -33
- package/src/is.js +0 -89
- package/src/math.js +0 -109
- package/src/random.js +0 -40
- package/src/string.js +0 -576
- package/src/stringPrototype.js +0 -15
- package/src/traversal.js +0 -134
- package/src/utils.js +0 -130
- package/tests/array.test.js +0 -326
- package/tests/dom.test.js +0 -239
- package/tests/eventdispatcher.test.js +0 -177
- package/tests/i18n.test.js +0 -132
- package/tests/index.test.js +0 -29
- package/tests/is.test.js +0 -354
- package/tests/math.test.js +0 -221
- package/tests/random.test.js +0 -72
- package/tests/string.test.js +0 -1106
- package/tests/traversal.test.js +0 -517
- package/tests/utils.test.js +0 -371
- package/tsconfig.json +0 -16
- package/webpack.config.cjs +0 -31
package/tests/is.test.js
DELETED
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
import {each} from "../src/traversal.js";
|
|
2
|
-
import {
|
|
3
|
-
isObject,
|
|
4
|
-
isPlainObject,
|
|
5
|
-
isFunction,
|
|
6
|
-
isDate,
|
|
7
|
-
isBoolean,
|
|
8
|
-
isBool,
|
|
9
|
-
isEvent,
|
|
10
|
-
isFloat, isScalar, isEventSupported, isTouchDevice
|
|
11
|
-
} from "../src/is.js";
|
|
12
|
-
|
|
13
|
-
describe('is methods', () => {
|
|
14
|
-
describe('isObject()', () => {
|
|
15
|
-
it('should return true for a plain object', () => {
|
|
16
|
-
const obj = {key: 'value'};
|
|
17
|
-
expect(isObject(obj)).toBe(true);
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
it('should return false for an array', () => {
|
|
21
|
-
const arr = [1, 2, 3];
|
|
22
|
-
expect(isObject(arr)).toBe(false);
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('should return true for an object created with Object.create', () => {
|
|
26
|
-
const obj = Object.create(null);
|
|
27
|
-
expect(isObject(obj)).toBe(true);
|
|
28
|
-
})
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
describe('isArray()', () => {
|
|
32
|
-
it('should return true for an empty array', () => {
|
|
33
|
-
const arr = [];
|
|
34
|
-
expect(isArray(arr)).toBe(true);
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it('should return true for an array with elements', () => {
|
|
38
|
-
const arr = [1, 2, 3];
|
|
39
|
-
expect(isArray(arr)).toBe(true);
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
it('should return false for a plain object', () => {
|
|
43
|
-
const obj = {key: 'value'};
|
|
44
|
-
expect(isArray(obj)).toBe(false);
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
const isArray = Array.isArray;
|
|
48
|
-
Array.isArray = undefined;
|
|
49
|
-
|
|
50
|
-
it('should return true id Array.isArray is undefined', () => {
|
|
51
|
-
const arr = [1, 2, 3];
|
|
52
|
-
expect(isArray(arr)).toBe(true);
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
Array.isArray = isArray;
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
describe('isPlainObject()', () => {
|
|
59
|
-
it('should return true for a plain object', () => {
|
|
60
|
-
const obj = { key: 'value' };
|
|
61
|
-
expect(isPlainObject(obj)).toBe(true);
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
it('should return false for null, undefined, array', () => {
|
|
65
|
-
expect(isPlainObject(null)).toBe(false);
|
|
66
|
-
expect(isPlainObject(undefined)).toBe(false);
|
|
67
|
-
expect(isPlainObject([1, 2, 3])).toBe(false);
|
|
68
|
-
expect(isPlainObject(() => {})).toBe(false);
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
it('should return true for an object created with Object.create(null)', () => {
|
|
72
|
-
const obj = Object.create(null);
|
|
73
|
-
expect(isPlainObject(obj)).toBe(true);
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
it('should return false for an object with a custom constructor', () => {
|
|
77
|
-
function CustomConstructor() {}
|
|
78
|
-
const obj = new CustomConstructor();
|
|
79
|
-
expect(isPlainObject(obj)).toBe(false);
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('should return false for non-object values', () => {
|
|
83
|
-
each([2, 'string', true, Symbol('sym')], (i, value) => {
|
|
84
|
-
expect(isPlainObject(value)).toBe(false);
|
|
85
|
-
})
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
it('should return false if isObject returns false for o.constructor.prototype', () => {
|
|
89
|
-
const obj = {};
|
|
90
|
-
obj.constructor = function CustomConstructor() {};
|
|
91
|
-
expect(isPlainObject(obj)).toBe(false);
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
it('should return false if isFunction returns true', () => {
|
|
95
|
-
const obj = function () {};
|
|
96
|
-
expect(isPlainObject(obj)).toBe(false);
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('should return false if o.constructor.prototype is not an object', () => {
|
|
100
|
-
const nonObjectPrototype = Object.create(null);
|
|
101
|
-
nonObjectPrototype.constructor = {
|
|
102
|
-
prototype: null,
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const result = isPlainObject(nonObjectPrototype);
|
|
106
|
-
|
|
107
|
-
expect(result).toBe(false);
|
|
108
|
-
})
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
describe('isFunction()', () => {
|
|
112
|
-
it('should return true for a regular and arrow functions', () => {
|
|
113
|
-
expect(isFunction(function () {})).toBe(true);
|
|
114
|
-
expect(isFunction(() => {})).toBe(true);
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
it('should return false for null, undefined, string, number, boolean, object, array, symbol', () => {
|
|
118
|
-
expect(isFunction(null)).toBe(false);
|
|
119
|
-
expect(isFunction(undefined)).toBe(false);
|
|
120
|
-
expect(isFunction('Hello world')).toBe(false);
|
|
121
|
-
expect(isFunction(2)).toBe(false);
|
|
122
|
-
expect(isFunction(true)).toBe(false);
|
|
123
|
-
expect(isFunction(false)).toBe(false);
|
|
124
|
-
expect(isFunction({ key: 'value' })).toBe(false);
|
|
125
|
-
expect(isFunction([1, 2, 3])).toBe(false);
|
|
126
|
-
expect(isFunction(Symbol('f'))).toBe(false);
|
|
127
|
-
expect(isFunction(class MyClass {})).toBe(true); // Les classes sont des fonctions
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
it('should return false for a function-like object', () => {
|
|
131
|
-
const funcLike = { call: () => {} };
|
|
132
|
-
expect(isFunction(funcLike)).toBe(false);
|
|
133
|
-
})
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
describe('isDate()', () => {
|
|
137
|
-
it('should return true for a valid Date object', () => {
|
|
138
|
-
const date = new Date();
|
|
139
|
-
expect(isDate(date)).toBe(true);
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
it('should return false for a string that looks like a date', () => {
|
|
143
|
-
const dateString = '2023-01-01';
|
|
144
|
-
expect(isDate(dateString)).toBe(false);
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
it('should return false for a number representing a timestamp', () => {
|
|
148
|
-
const timestamp = Date.now();
|
|
149
|
-
expect(isDate(timestamp)).toBe(false);
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
it('should return false for invalid Date objects', () => {
|
|
153
|
-
const invalidDate = new Date('invalid-date');
|
|
154
|
-
expect(isDate(invalidDate)).toBe(true);
|
|
155
|
-
})
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
describe('isBoolean(), isBool()', () => {
|
|
159
|
-
it('should return true for true and false', () => {
|
|
160
|
-
expect(isBoolean(true)).toBe(true);
|
|
161
|
-
expect(isBoolean(false)).toBe(true);
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
it('should return false for a string, a number, null and undefined', () => {
|
|
165
|
-
expect(isBoolean('true')).toBe(false);
|
|
166
|
-
expect(isBoolean('false')).toBe(false);
|
|
167
|
-
expect(isBoolean(1)).toBe(false);
|
|
168
|
-
expect(isBoolean(0)).toBe(false);
|
|
169
|
-
expect(isBoolean(null)).toBe(false);
|
|
170
|
-
expect(isBoolean(undefined)).toBe(false);
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
it('should be an alias of isBoolean', () => {
|
|
174
|
-
expect(isBool === isBoolean).toBe(true);
|
|
175
|
-
})
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
describe('isEvent()', () => {
|
|
179
|
-
it('should return true for a valid DOM Event object', () => {
|
|
180
|
-
const mockEvent = new Event('click');
|
|
181
|
-
expect(isEvent(mockEvent)).toBe(true);
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
it('should return true for an object with a preventDefault method', () => {
|
|
185
|
-
const mockEvent = {
|
|
186
|
-
preventDefault: () => {},
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
expect(isEvent(mockEvent)).toBe(true);
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
it('should return true if constructor matches [object Event]', () => {
|
|
193
|
-
const mockEvent = {
|
|
194
|
-
constructor: { toString: () => '[object Event]' },
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
expect(isEvent(mockEvent)).toBe(true);
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
it('should return false for non-object inputs', () => {
|
|
201
|
-
expect(isEvent(null)).toBe(false);
|
|
202
|
-
expect(isEvent(undefined)).toBe(false);
|
|
203
|
-
expect(isEvent(42)).toBe(false);
|
|
204
|
-
expect(isEvent('string')).toBe(false);
|
|
205
|
-
})
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
describe('isFloat() function()', () => {
|
|
209
|
-
it('should return true for valid float strings', () => {
|
|
210
|
-
expect(isFloat('3.14')).toBe(true);
|
|
211
|
-
expect(isFloat('-3.14')).toBe(true);
|
|
212
|
-
expect(isFloat('0.123')).toBe(true);
|
|
213
|
-
expect(isFloat('-0.123')).toBe(true);
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
it('should return true for valid integers (as they are also floats)', () => {
|
|
217
|
-
expect(isFloat('42')).toBe(true);
|
|
218
|
-
expect(isFloat('-42')).toBe(true);
|
|
219
|
-
expect(isFloat('0')).toBe(true);
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
it('should return true for valid float numbers', () => {
|
|
223
|
-
expect(isFloat(3.14)).toBe(true);
|
|
224
|
-
expect(isFloat(-3.14)).toBe(true);
|
|
225
|
-
expect(isFloat(0.123)).toBe(true);
|
|
226
|
-
expect(isFloat(-0.123)).toBe(true);
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
it('should return true for valid integer numbers', () => {
|
|
230
|
-
expect(isFloat(42)).toBe(true);
|
|
231
|
-
expect(isFloat(-42)).toBe(true);
|
|
232
|
-
expect(isFloat(0)).toBe(true);
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
it('should return false for invalid float strings', () => {
|
|
236
|
-
expect(isFloat('3.14.15')).toBe(false);
|
|
237
|
-
expect(isFloat('abc')).toBe(false);
|
|
238
|
-
expect(isFloat('-3.')).toBe(false);
|
|
239
|
-
expect(isFloat('-.')).toBe(false);
|
|
240
|
-
expect(isFloat('3-3')).toBe(false);
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
it('should return false for non-numeric values', () => {
|
|
244
|
-
expect(isFloat(null)).toBe(false);
|
|
245
|
-
expect(isFloat(undefined)).toBe(false);
|
|
246
|
-
expect(isFloat({})).toBe(false);
|
|
247
|
-
expect(isFloat([])).toBe(false);
|
|
248
|
-
expect(isFloat(() => {})).toBe(false);
|
|
249
|
-
})
|
|
250
|
-
|
|
251
|
-
it('should return true for floats in exponential notation', () => {
|
|
252
|
-
expect(isFloat('1e3')).toBe(false);
|
|
253
|
-
expect(isFloat('1.23e3')).toBe(false);
|
|
254
|
-
})
|
|
255
|
-
|
|
256
|
-
it('should return true for numbers passed as strings', () => {
|
|
257
|
-
expect(isFloat('123')).toBe(true);
|
|
258
|
-
expect(isFloat('123.456')).toBe(true);
|
|
259
|
-
expect(isFloat('-123.456')).toBe(true);
|
|
260
|
-
})
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
describe('isScalar', () => {
|
|
264
|
-
it('should return true for null', () => {
|
|
265
|
-
expect(isScalar(null)).toBe(true);
|
|
266
|
-
})
|
|
267
|
-
|
|
268
|
-
it('should return true for strings', () => {
|
|
269
|
-
expect(isScalar('hello')).toBe(true);
|
|
270
|
-
expect(isScalar('')).toBe(true);
|
|
271
|
-
})
|
|
272
|
-
|
|
273
|
-
it('should return true for numbers', () => {
|
|
274
|
-
expect(isScalar(42)).toBe(true);
|
|
275
|
-
expect(isScalar(0)).toBe(true);
|
|
276
|
-
expect(isScalar(-123.45)).toBe(true);
|
|
277
|
-
})
|
|
278
|
-
|
|
279
|
-
it('should return true for bigints', () => {
|
|
280
|
-
expect(isScalar(10n)).toBe(true);
|
|
281
|
-
})
|
|
282
|
-
|
|
283
|
-
it('should return true for booleans', () => {
|
|
284
|
-
expect(isScalar(true)).toBe(true);
|
|
285
|
-
expect(isScalar(false)).toBe(true);
|
|
286
|
-
})
|
|
287
|
-
|
|
288
|
-
it('should return true for symbols', () => {
|
|
289
|
-
expect(isScalar(Symbol('test'))).toBe(true);
|
|
290
|
-
})
|
|
291
|
-
|
|
292
|
-
it('should return false for objects', () => {
|
|
293
|
-
expect(isScalar({})).toBe(false);
|
|
294
|
-
expect(isScalar([])).toBe(false);
|
|
295
|
-
expect(isScalar(() => {})).toBe(false);
|
|
296
|
-
})
|
|
297
|
-
|
|
298
|
-
it('should return false for undefined', () => {
|
|
299
|
-
expect(isScalar(undefined)).toBe(false);
|
|
300
|
-
})
|
|
301
|
-
})
|
|
302
|
-
|
|
303
|
-
describe('isEventSupported', () => {
|
|
304
|
-
it('should return true for supported events on specific elements', () => {
|
|
305
|
-
expect(isEventSupported('select')).toBe(true);
|
|
306
|
-
expect(isEventSupported('change')).toBe(true);
|
|
307
|
-
expect(isEventSupported('submit')).toBe(true);
|
|
308
|
-
expect(isEventSupported('reset')).toBe(true);
|
|
309
|
-
expect(isEventSupported('error')).toBe(true);
|
|
310
|
-
expect(isEventSupported('load')).toBe(true);
|
|
311
|
-
expect(isEventSupported('abort')).toBe(true);
|
|
312
|
-
})
|
|
313
|
-
|
|
314
|
-
it('should return true for generic events on a div', () => {
|
|
315
|
-
expect(isEventSupported('click')).toBe(true);
|
|
316
|
-
expect(isEventSupported('mouseover')).toBe(true);
|
|
317
|
-
expect(isEventSupported('keydown')).toBe(true);
|
|
318
|
-
})
|
|
319
|
-
|
|
320
|
-
it('should return false for unsupported events', () => {
|
|
321
|
-
expect(isEventSupported('unsupportedEvent')).toBe(false);
|
|
322
|
-
expect(isEventSupported('customEvent')).toBe(false);
|
|
323
|
-
})
|
|
324
|
-
|
|
325
|
-
it('should handle empty event names gracefully', () => {
|
|
326
|
-
expect(isEventSupported('')).toBe(false);
|
|
327
|
-
})
|
|
328
|
-
|
|
329
|
-
it('should handle null and undefined inputs gracefully', () => {
|
|
330
|
-
expect(isEventSupported(null)).toBe(false);
|
|
331
|
-
expect(isEventSupported(undefined)).toBe(false);
|
|
332
|
-
})
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
describe('isTouchDevice()', () => {
|
|
336
|
-
it('should return true if the device supports touchstart events', () => {
|
|
337
|
-
jest.spyOn(document, 'createElement').mockImplementation(() => {
|
|
338
|
-
return { ontouchstart: jest.fn() };
|
|
339
|
-
})
|
|
340
|
-
|
|
341
|
-
expect(isTouchDevice()).toBe(true);
|
|
342
|
-
jest.restoreAllMocks();
|
|
343
|
-
})
|
|
344
|
-
|
|
345
|
-
it('should return false if the device does not support touchstart events', () => {
|
|
346
|
-
jest.spyOn(document, 'createElement').mockImplementation(() => {
|
|
347
|
-
return {};
|
|
348
|
-
})
|
|
349
|
-
|
|
350
|
-
expect(isTouchDevice()).toBe(false);
|
|
351
|
-
jest.restoreAllMocks();
|
|
352
|
-
})
|
|
353
|
-
})
|
|
354
|
-
})
|
package/tests/math.test.js
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
min,
|
|
3
|
-
max,
|
|
4
|
-
round,
|
|
5
|
-
hex2dec,
|
|
6
|
-
dec2hex,
|
|
7
|
-
floorTo,
|
|
8
|
-
plancher
|
|
9
|
-
} from "../src/math.js";
|
|
10
|
-
|
|
11
|
-
describe('math functions', () => {
|
|
12
|
-
describe('round()', () => {
|
|
13
|
-
it('should round to the nearest integer when no precision is given', () => {
|
|
14
|
-
expect(round(1.4)).toBe(1);
|
|
15
|
-
expect(round(1.6)).toBe(2);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('should round to the specified precision', () => {
|
|
19
|
-
expect(round(1.234, 1)).toBe(1.2);
|
|
20
|
-
expect(round(1.234, 2)).toBe(1.23);
|
|
21
|
-
expect(round(1.235, 2)).toBe(1.24);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('should handle negative precision', () => {
|
|
25
|
-
expect(round(1234.56, -1)).toBe(1230);
|
|
26
|
-
expect(round(1234.56, -2)).toBe(1200);
|
|
27
|
-
expect(round(1234.56, -3)).toBe(1000);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should handle edge cases with 0 precision', () => {
|
|
31
|
-
expect(round(0.5)).toBe(1);
|
|
32
|
-
expect(round(-0.5)).toBe(-0);
|
|
33
|
-
expect(round(0)).toBe(0);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should handle very large numbers', () => {
|
|
37
|
-
expect(round(123456789.98765, 3)).toBe(123456789.988);
|
|
38
|
-
expect(round(123456789.98765, -3)).toBe(123457000);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should handle negative numbers', () => {
|
|
42
|
-
expect(round(-1.234, 1)).toBe(-1.2);
|
|
43
|
-
expect(round(-1.234, 2)).toBe(-1.23);
|
|
44
|
-
expect(round(-1.235, 2)).toBe(-1.24);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('should handle edge cases with precision 0', () => {
|
|
48
|
-
expect(round(1.5, 0)).toBe(2);
|
|
49
|
-
expect(round(-1.5, 0)).toBe(-1);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('should return the same value for integers', () => {
|
|
53
|
-
expect(round(5)).toBe(5);
|
|
54
|
-
expect(round(-5)).toBe(-5);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe('floorTo()', () => {
|
|
59
|
-
it('should round down to the nearest multiple of the precision', () => {
|
|
60
|
-
expect(floorTo(123, 10)).toBe(120);
|
|
61
|
-
expect(floorTo(456, 100)).toBe(400);
|
|
62
|
-
expect(floorTo(789.25, 0.1)).toBe(789.2);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('should handle negative numbers correctly', () => {
|
|
66
|
-
expect(floorTo(-123, 10)).toBe(-130);
|
|
67
|
-
expect(floorTo(-456, 100)).toBe(-500);
|
|
68
|
-
expect(floorTo(-789.25, 0.1)).toBe(-789.3);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('should handle precision larger than the number', () => {
|
|
72
|
-
expect(floorTo(50, 100)).toBe(0);
|
|
73
|
-
expect(floorTo(-50, 100)).toBe(-100);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('should return the number itself when precision is 1', () => {
|
|
77
|
-
expect(floorTo(123, 1)).toBe(123);
|
|
78
|
-
expect(floorTo(-456, 1)).toBe(-456);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should return 0 when the number is 0', () => {
|
|
82
|
-
expect(floorTo(0, 10)).toBe(0);
|
|
83
|
-
expect(floorTo(0, 100)).toBe(0);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should handle edge cases with very small precision', () => {
|
|
87
|
-
expect(floorTo(123.456, 0.01)).toBe(123.45);
|
|
88
|
-
expect(floorTo(-123.456, 0.01)).toBe(-123.46);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('should throw an error if precision is 0', () => {
|
|
92
|
-
expect(() => floorTo(123, 0)).toThrow();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should throw an error if precision is negative', () => {
|
|
96
|
-
expect(() => floorTo(123, -10)).toThrow();
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
describe('min()', () => {
|
|
101
|
-
it('should return the minimum value from an array', () => {
|
|
102
|
-
const list = [5, 2, 9, 1, 7];
|
|
103
|
-
const result = min(list);
|
|
104
|
-
expect(result).toBe(1);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should handle an empty array and return Infinity', () => {
|
|
108
|
-
const list = [];
|
|
109
|
-
const result = min(list);
|
|
110
|
-
expect(result).toBe(Infinity);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('should handle an object with a custom comparison function', () => {
|
|
114
|
-
const list = { a: 5, b: 2, c: 9, d: 1, e: 7 };
|
|
115
|
-
const cmp_func = (a, b) => a - b;
|
|
116
|
-
const result = min(list, cmp_func);
|
|
117
|
-
expect(result).toBe(1);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should handle an object without a custom comparison function', () => {
|
|
121
|
-
const list = { a: 'z', b: 'b', c: 'm' };
|
|
122
|
-
const result = min(list);
|
|
123
|
-
expect(result).toBe('b');
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('should return undefined for an empty object', () => {
|
|
127
|
-
const list = {};
|
|
128
|
-
const result = min(list);
|
|
129
|
-
expect(result).toBeUndefined();
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it('should return undefined if neither array nor object is passed', () => {
|
|
133
|
-
const list = null;
|
|
134
|
-
const result = min(list);
|
|
135
|
-
expect(result).toBeUndefined();
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
describe('max()', () => {
|
|
140
|
-
it('should return the maximum value from an array', () => {
|
|
141
|
-
const list = [0, 1, 5, 2, 4, 9, 1, 7, 2];
|
|
142
|
-
const result = max(list);
|
|
143
|
-
expect(result).toBe(9);
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it('should handle an empty array and return -Infinity', () => {
|
|
147
|
-
const list = [];
|
|
148
|
-
const result = max(list);
|
|
149
|
-
expect(result).toBe(-Infinity);
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('should handle an object with a custom comparison function', () => {
|
|
153
|
-
const list = { a: 5, b: 2, c: 9, d: 1, e: 7 };
|
|
154
|
-
const cmp_func = (a, b) => b - a;
|
|
155
|
-
const result = max(list, cmp_func);
|
|
156
|
-
expect(result).toBe(9);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it('should handle an object without a custom comparison function', () => {
|
|
160
|
-
const list = { a: 'z', b: 'b', c: 'm' };
|
|
161
|
-
const result = max(list);
|
|
162
|
-
expect(result).toBe('z');
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it('should return undefined for an empty object', () => {
|
|
166
|
-
const list = {};
|
|
167
|
-
const result = max(list);
|
|
168
|
-
expect(result).toBeUndefined();
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('should return undefined if neither array nor object is passed', () => {
|
|
172
|
-
const list = null;
|
|
173
|
-
const result = max(list);
|
|
174
|
-
expect(result).toBeUndefined();
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
describe('hex2dec function', () => {
|
|
179
|
-
it('should convert a single-digit hexadecimal to decimal', () => {
|
|
180
|
-
expect(hex2dec('A')).toBe(10);
|
|
181
|
-
expect(hex2dec('F')).toBe(15);
|
|
182
|
-
expect(hex2dec('0')).toBe(0);
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
it('should convert a multi-digit hexadecimal to decimal', () => {
|
|
186
|
-
expect(hex2dec('1A')).toBe(26);
|
|
187
|
-
expect(hex2dec('FF')).toBe(255);
|
|
188
|
-
expect(hex2dec('100')).toBe(256);
|
|
189
|
-
expect(hex2dec('ABC')).toBe(2748);
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
it('should handle lowercase hexadecimal inputs', () => {
|
|
193
|
-
expect(hex2dec('a')).toBe(10);
|
|
194
|
-
expect(hex2dec('ff')).toBe(255);
|
|
195
|
-
expect(hex2dec('abc')).toBe(2748);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it('should handle mixed-case hexadecimal inputs', () => {
|
|
199
|
-
expect(hex2dec('aBc')).toBe(2748);
|
|
200
|
-
expect(hex2dec('Ff')).toBe(255);
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
it('should handle numeric strings as input', () => {
|
|
204
|
-
expect(hex2dec('123')).toBe(291);
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('should return 0 for an empty string', () => {
|
|
208
|
-
expect(hex2dec('')).toBe(0);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it('should handle non-string inputs by converting them to strings', () => {
|
|
212
|
-
expect(hex2dec(123)).toBe(291);
|
|
213
|
-
expect(hex2dec(0)).toBe(0);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('should return 0 for invalid hexadecimal characters', () => {
|
|
217
|
-
expect(hex2dec('G')).toBe(0);
|
|
218
|
-
expect(hex2dec('123G')).toBe(0);
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
});
|
package/tests/random.test.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import * as r from '../src/random.js';
|
|
2
|
-
|
|
3
|
-
describe('Random String Generation Functions', () => {
|
|
4
|
-
const isAlpha = /^[a-z]+$/;
|
|
5
|
-
const isAlphaCs = /^[a-zA-Z]+$/;
|
|
6
|
-
const isAlphaNum = /^[a-z0-9]+$/;
|
|
7
|
-
const isAlphaNumCs = /^[a-zA-Z0-9]+$/;
|
|
8
|
-
const isNum = /^[0-9]+$/;
|
|
9
|
-
|
|
10
|
-
it('should generate a unique ID of length 10 with uniqid', () => {
|
|
11
|
-
const id = r.uniqid();
|
|
12
|
-
expect(id).toHaveLength(10);
|
|
13
|
-
expect(isAlpha.test(id)).toBe(true);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('should generate a random lowercase alphabetic string with randAlpha', () => {
|
|
17
|
-
const str = r.randAlpha(15);
|
|
18
|
-
expect(str).toHaveLength(15);
|
|
19
|
-
expect(isAlpha.test(str)).toBe(true);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should generate a random alphabetic string with uppercase and lowercase with randAlphaCs', () => {
|
|
23
|
-
const str = r.randAlphaCs(12);
|
|
24
|
-
expect(str).toHaveLength(12);
|
|
25
|
-
expect(isAlphaCs.test(str)).toBe(true);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('should generate a random alphanumeric string with lowercase letters and numbers with randAlphaNum', () => {
|
|
29
|
-
const str = r.randAlphaNum(20);
|
|
30
|
-
expect(str).toHaveLength(20);
|
|
31
|
-
expect(isAlphaNum.test(str)).toBe(true);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('should generate a random alphanumeric string with uppercase, lowercase letters, and numbers with randAlphaNumCs', () => {
|
|
35
|
-
const str = r.randAlphaNumCs(25);
|
|
36
|
-
expect(str).toHaveLength(25);
|
|
37
|
-
expect(isAlphaNumCs.test(str)).toBe(true);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('should generate a random numeric string with randNum', () => {
|
|
41
|
-
const str = r.randNum(8);
|
|
42
|
-
expect(str).toHaveLength(8);
|
|
43
|
-
expect(isNum.test(str)).toBe(true);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should generate a random string from a custom range with rand', () => {
|
|
47
|
-
const range = ['A', 'B', 'C', '1', '2', '3'];
|
|
48
|
-
const str = r.rand(range, 6);
|
|
49
|
-
expect(str).toHaveLength(6);
|
|
50
|
-
[...str].forEach((char) => {
|
|
51
|
-
expect(range).toContain(char);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('should handle zero length input for all functions', () => {
|
|
56
|
-
expect(r.randAlpha(0)).toHaveLength(0);
|
|
57
|
-
expect(r.randAlphaCs(0)).toHaveLength(0);
|
|
58
|
-
expect(r.randAlphaNum(0)).toHaveLength(0);
|
|
59
|
-
expect(r.randAlphaNumCs(0)).toHaveLength(0);
|
|
60
|
-
expect(r.randNum(0)).toHaveLength(0);
|
|
61
|
-
expect(r.rand(['X', 'Y', 'Z'], 0)).toHaveLength(0);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should return an empty string if n is negative for all functions', () => {
|
|
65
|
-
expect(r.randAlpha(-5)).toHaveLength(0);
|
|
66
|
-
expect(r.randAlphaCs(-5)).toHaveLength(0);
|
|
67
|
-
expect(r.randAlphaNum(-5)).toHaveLength(0);
|
|
68
|
-
expect(r.randAlphaNumCs(-5)).toHaveLength(0);
|
|
69
|
-
expect(r.randNum(-5)).toHaveLength(0);
|
|
70
|
-
expect(r.rand(['A', 'B', 'C'], -5)).toHaveLength(0);
|
|
71
|
-
});
|
|
72
|
-
});
|