@ditojs/utils 3.0.0 → 3.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.
Files changed (44) hide show
  1. package/package.json +5 -3
  2. package/src/__snapshots__/index.test.js.snap +0 -90
  3. package/src/array/flattten.test.js +0 -29
  4. package/src/array/shuffle.test.js +0 -25
  5. package/src/base/base.test.js +0 -590
  6. package/src/class/mixin.test.js +0 -70
  7. package/src/dataPath/getEntriesAtDataPath.test.js +0 -204
  8. package/src/dataPath/getValueAtDataPath.test.js +0 -140
  9. package/src/dataPath/normalizeDataPath.test.js +0 -36
  10. package/src/dataPath/parseDataPath.test.js +0 -67
  11. package/src/dataPath/setDataPathEntries.test.js +0 -36
  12. package/src/dataPath/setValueAtDataPath.test.js +0 -34
  13. package/src/function/toAsync.test.js +0 -31
  14. package/src/function/toCallback.test.js +0 -29
  15. package/src/function/toPromiseCallback.test.js +0 -24
  16. package/src/html/escapeHtml.test.js +0 -19
  17. package/src/html/stripHtml.test.js +0 -37
  18. package/src/index.test.js +0 -7
  19. package/src/object/clone.test.js +0 -131
  20. package/src/object/equals.test.js +0 -278
  21. package/src/object/groupBy.test.js +0 -70
  22. package/src/object/mapKeys.test.js +0 -16
  23. package/src/object/mapValues.test.js +0 -38
  24. package/src/object/mergeDeeply.test.js +0 -152
  25. package/src/object/pick.test.js +0 -23
  26. package/src/object/pickBy.test.js +0 -48
  27. package/src/string/camelize.test.js +0 -37
  28. package/src/string/capitalize.test.js +0 -33
  29. package/src/string/decamelize.test.js +0 -83
  30. package/src/string/deindent.test.js +0 -181
  31. package/src/string/format.test.js +0 -196
  32. package/src/string/formatDate.test.js +0 -28
  33. package/src/string/formatPlainDate.test.js +0 -13
  34. package/src/string/getCommonPrefix.test.js +0 -23
  35. package/src/string/isAbsoluteUrl.test.js +0 -15
  36. package/src/string/isCreditCard.test.js +0 -50
  37. package/src/string/isDomain.test.js +0 -24
  38. package/src/string/isEmail.test.js +0 -37
  39. package/src/string/isHostname.test.js +0 -12
  40. package/src/string/isUrl.test.js +0 -1595
  41. package/src/string/labelize.test.js +0 -39
  42. package/src/string/parsePlainDate.test.js +0 -20
  43. package/src/timer/debounce.test.js +0 -101
  44. package/src/timer/debounceAsync.test.js +0 -143
@@ -1,39 +0,0 @@
1
- import { labelize } from './labelize.js'
2
-
3
- describe('labelize()', () => {
4
- it('should labelize hyphenated text', () => {
5
- expect(labelize('some-hyphenated-text')).toBe('Some Hyphenated Text')
6
- expect(labelize('one-&-two', true)).toBe('One & Two')
7
- expect(labelize('one-2-three', true)).toBe('One 2 Three')
8
- })
9
-
10
- it('should labelize underscored text', () => {
11
- expect(labelize('some_underscored_text')).toBe('Some Underscored Text')
12
- })
13
-
14
- it('should labelize camel-cased text', () => {
15
- expect(labelize('SomeCamelCasedText')).toBe('Some Camel Cased Text')
16
- })
17
-
18
- it('should labelize normal text', () => {
19
- expect(labelize('some normal text')).toBe('Some Normal Text')
20
- })
21
-
22
- it('should split numbers and words', () => {
23
- expect(labelize('one2')).toBe('One 2')
24
- })
25
-
26
- it('should split words and numbers both sides', () => {
27
- expect(labelize('one2three')).toBe('One 2 Three')
28
- })
29
-
30
- it('should keep multiple numbers together', () => {
31
- expect(labelize('one234five')).toBe('One 234 Five')
32
- })
33
-
34
- it('should return an empty string if nothing can be processed', () => {
35
- expect(labelize()).toBe('')
36
- expect(labelize(null)).toBe('')
37
- expect(labelize('')).toBe('')
38
- })
39
- })
@@ -1,20 +0,0 @@
1
- import { parsePlainDate } from './parsePlainDate.js'
2
-
3
- describe('parsePlainDate()', () => {
4
- it('should parse plain dates as local midnight', () => {
5
- expect(parsePlainDate('2026-05-14')).toEqual(new Date(2026, 4, 14))
6
- expect(parsePlainDate('1850-07-01')).toEqual(new Date(1850, 6, 1))
7
- })
8
-
9
- it('should return null for other values', () => {
10
- expect(parsePlainDate(null)).toBe(null)
11
- expect(parsePlainDate('')).toBe(null)
12
- expect(parsePlainDate('2026-05-14T00:00:00.000Z')).toBe(null)
13
- expect(parsePlainDate('14.05.2026')).toBe(null)
14
- })
15
-
16
- it('should return null for invalid dates', () => {
17
- expect(parsePlainDate('2026-02-30')).toBe(null)
18
- expect(parsePlainDate('2026-13-01')).toBe(null)
19
- })
20
- })
@@ -1,101 +0,0 @@
1
- import { debounce } from './debounce.js'
2
- import { vi } from 'vitest'
3
-
4
- // Tell vitest to mock all timeout functions:
5
- vi.useFakeTimers()
6
-
7
- describe('debounce()', () => {
8
- const fixture = Symbol('fixture')
9
-
10
- it('should never execute if intervals are less than wait', () => {
11
- const func = vi.fn()
12
- const debounced = debounce(func, 1000)
13
- for (let i = 0; i < 10; i++) {
14
- vi.advanceTimersByTime(500)
15
- debounced()
16
- }
17
- expect(func).toBeCalledTimes(0)
18
- })
19
-
20
- it('should execute just once if an interval is big enough', () => {
21
- const func = vi.fn()
22
- const debounced = debounce(func, 1000)
23
- for (let i = 0; i < 10; i++) {
24
- vi.advanceTimersByTime(500)
25
- debounced()
26
- }
27
- vi.advanceTimersByTime(1000)
28
- debounced()
29
- expect(func).toBeCalledTimes(1)
30
- })
31
-
32
- it('should pass through argument', async () => {
33
- const func = vi.fn(value => value)
34
- const debounced = debounce(func, 1000)
35
- expect(debounced(fixture)).toBeUndefined()
36
- vi.advanceTimersByTime(1000)
37
- expect(debounced()).toBe(fixture)
38
- expect(func).toBeCalledTimes(1)
39
- })
40
-
41
- it('should pass through return value', () => {
42
- const func = vi.fn().mockReturnValue(fixture)
43
- const debounced = debounce(func, 1000)
44
- expect(debounced()).toBeUndefined()
45
- vi.advanceTimersByTime(1000)
46
- expect(debounced()).toBe(fixture)
47
- expect(func).toBeCalledTimes(1)
48
- })
49
-
50
- it('should pass through `this`', () => {
51
- const func = vi.fn().mockReturnThis()
52
- const debounced = debounce(func, 0)
53
- const obj = {}
54
- expect(debounced.call(obj)).toBeUndefined()
55
- vi.advanceTimersByTime(1000)
56
- expect(debounced.call(obj)).toBe(obj)
57
- })
58
-
59
- it('should allow to cancel', () => {
60
- const func = vi.fn()
61
- const debounced = debounce(func, 1000)
62
- debounced()
63
- vi.advanceTimersByTime(500)
64
- expect(debounced.cancel()).toBe(true)
65
- expect(debounced.cancel()).toBe(false)
66
- vi.advanceTimersByTime(500)
67
- expect(func).toBeCalledTimes(0)
68
- vi.advanceTimersByTime(1000)
69
- expect(func).toBeCalledTimes(0)
70
- debounced()
71
- vi.advanceTimersByTime(1000)
72
- expect(func).toBeCalledTimes(1)
73
- })
74
-
75
- it('should execute once immediately if intervals are less than wait', () => {
76
- const func = vi.fn()
77
- const debounced = debounce(func, { delay: 1000, immediate: true })
78
- debounced()
79
- expect(func).toBeCalledTimes(1)
80
- for (let i = 0; i < 10; i++) {
81
- vi.advanceTimersByTime(500)
82
- debounced()
83
- }
84
- expect(func).toBeCalledTimes(1)
85
- })
86
-
87
- it(`should execute once immediately and once at the end with long enough intervals`, () => {
88
- const func = vi.fn()
89
- const debounced = debounce(func, { delay: 1000, immediate: true })
90
- debounced()
91
- expect(func).toBeCalledTimes(1)
92
- for (let i = 0; i < 10; i++) {
93
- vi.advanceTimersByTime(500)
94
- debounced()
95
- }
96
- expect(func).toBeCalledTimes(1)
97
- vi.advanceTimersByTime(1000)
98
- debounced()
99
- expect(func).toBeCalledTimes(2)
100
- })
101
- })
@@ -1,143 +0,0 @@
1
- import { debounceAsync } from './debounceAsync.js'
2
- import { vi } from 'vitest'
3
-
4
- // Tell vitest to mock all timeout functions:
5
- vi.useFakeTimers()
6
-
7
- describe('debounceAsync()', () => {
8
- const fixture = Symbol('fixture')
9
-
10
- it('should never execute if intervals are less than wait', () => {
11
- const func = vi.fn()
12
- const debounced = debounceAsync(func, 1000)
13
- for (let i = 0; i < 10; i++) {
14
- vi.advanceTimersByTime(500)
15
- debounced()
16
- }
17
- expect(func).toBeCalledTimes(0)
18
- })
19
-
20
- it('should execute just once if an interval is big enough', () => {
21
- const func = vi.fn()
22
- const debounced = debounceAsync(func, 1000)
23
- for (let i = 0; i < 10; i++) {
24
- vi.advanceTimersByTime(500)
25
- debounced()
26
- }
27
- vi.advanceTimersByTime(1000)
28
- debounced()
29
- expect(func).toBeCalledTimes(1)
30
- })
31
-
32
- it('should pass through argument', async () => {
33
- expect.assertions(2)
34
- const func = vi.fn(async value => value)
35
- const debounced = debounceAsync(func, 1000)
36
- const promise = debounced(fixture)
37
- vi.advanceTimersByTime(1000)
38
- expect(await promise).toBe(fixture)
39
- expect(func).toBeCalledTimes(1)
40
- })
41
-
42
- it('should pass through return value', async () => {
43
- expect.assertions(2)
44
- const func = vi.fn().mockResolvedValueOnce(fixture)
45
- const debounced = debounceAsync(func, 1000)
46
- const promises = []
47
- promises.push(debounced())
48
- vi.advanceTimersByTime(1000)
49
- promises.push(debounced())
50
- expect(await Promise.all(promises)).toStrictEqual([fixture, fixture])
51
- expect(func).toBeCalledTimes(1)
52
- })
53
-
54
- it('should pass through `this`', async () => {
55
- expect.assertions(2)
56
- const func = vi.fn(async function () {
57
- return this
58
- })
59
- const debounced = debounceAsync(func, 1000)
60
- const obj = {}
61
- const promises = []
62
- promises.push(debounced.call(obj))
63
- vi.advanceTimersByTime(1000)
64
- promises.push(debounced.call(obj))
65
- expect(await Promise.all(promises)).toStrictEqual([obj, obj])
66
- expect(func).toBeCalledTimes(1)
67
- })
68
-
69
- it('should allow to cancel', async () => {
70
- expect.assertions(6)
71
- const func = vi.fn()
72
- const debounced = debounceAsync(func, 1000)
73
- const promise = debounced()
74
- vi.advanceTimersByTime(500)
75
- expect(debounced.cancel()).toBe(true)
76
- expect(debounced.cancel()).toBe(false)
77
- vi.advanceTimersByTime(500)
78
- expect(func).toBeCalledTimes(0)
79
- expect(await promise).toBeUndefined()
80
- vi.advanceTimersByTime(1000)
81
- expect(func).toBeCalledTimes(0)
82
- await debounced()
83
- vi.advanceTimersByTime(1000)
84
- expect(func).toBeCalledTimes(1)
85
- })
86
-
87
- it('should execute once immediately if intervals are less than wait', async () => {
88
- expect.assertions(2)
89
- const func = vi.fn()
90
- const debounced = debounceAsync(func, { delay: 1000, immediate: true })
91
- const promises = []
92
- promises.push(debounced())
93
- expect(func).toBeCalledTimes(1)
94
- for (let i = 0; i < 10; i++) {
95
- vi.advanceTimersByTime(500)
96
- promises.push(debounced())
97
- }
98
- await Promise.all(promises)
99
- expect(func).toBeCalledTimes(1)
100
- })
101
-
102
- it('should execute twice immediately with long enough intervals', async () => {
103
- expect.assertions(3)
104
- const func = vi.fn()
105
- const debounced = debounceAsync(func, { delay: 1000, immediate: true })
106
- const promises = []
107
- promises.push(debounced())
108
- expect(func).toBeCalledTimes(1)
109
- for (let i = 0; i < 10; i++) {
110
- vi.advanceTimersByTime(500)
111
- promises.push(debounced())
112
- }
113
- expect(func).toBeCalledTimes(1)
114
- vi.advanceTimersByTime(1000)
115
- promises.push(debounced())
116
- await Promise.all(promises)
117
- expect(func).toBeCalledTimes(2)
118
- })
119
-
120
- it('should only reject the last waiting promise', async () => {
121
- expect.assertions(5)
122
- let count = 0
123
- const func = vi.fn(() => {
124
- if (++count > 1) {
125
- throw new Error('boom')
126
- }
127
- })
128
- const debounced = debounceAsync(func, { delay: 1000, immediate: true })
129
- const promises = []
130
- promises.push(debounced())
131
- expect(func).toBeCalledTimes(1)
132
- for (let i = 0; i < 10; i++) {
133
- vi.advanceTimersByTime(500)
134
- promises.push(debounced())
135
- }
136
- expect(func).toBeCalledTimes(1)
137
- vi.advanceTimersByTime(1000)
138
- await expect(debounced()).rejects.toThrow('boom')
139
- const results = await Promise.all(promises)
140
- expect(results).toStrictEqual(new Array(11).fill(undefined))
141
- expect(func).toBeCalledTimes(2)
142
- })
143
- })