@ditojs/utils 1.4.3 → 1.5.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/package.json +2 -2
- package/src/__snapshots__/index.test.js.snap +9 -7
- package/src/function/toPromiseCallback.test.js +5 -5
- package/src/object/clone.test.js +2 -2
- package/src/string/escapeRegexp.js +3 -0
- package/src/string/index.js +1 -0
- package/src/timer/debounce.test.js +24 -24
- package/src/timer/debounceAsync.test.js +27 -27
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ditojs/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Dito.js Utility Functions – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
|
|
6
6
|
"repository": "https://github.com/ditojs/dito/tree/master/packages/utils",
|
|
@@ -27,5 +27,5 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"punycode": "^2.1.1"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "36a57512228cc233211bf6e4038123a9e775f5c5"
|
|
31
31
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
//
|
|
1
|
+
// Vitest Snapshot v1
|
|
2
2
|
|
|
3
|
-
exports[`index exports all symbols 1`] = `
|
|
4
|
-
|
|
3
|
+
exports[`index > exports all symbols 1`] = `
|
|
4
|
+
{
|
|
5
5
|
"asArray": [Function],
|
|
6
6
|
"asFunction": [Function],
|
|
7
7
|
"asObject": [Function],
|
|
@@ -11,16 +11,16 @@ Object {
|
|
|
11
11
|
"debounce": [Function],
|
|
12
12
|
"debounceAsync": [Function],
|
|
13
13
|
"decamelize": [Function],
|
|
14
|
-
"defaultFormats":
|
|
15
|
-
"date":
|
|
14
|
+
"defaultFormats": {
|
|
15
|
+
"date": {
|
|
16
16
|
"day": "numeric",
|
|
17
17
|
"month": "long",
|
|
18
18
|
"year": "numeric",
|
|
19
19
|
},
|
|
20
|
-
"number":
|
|
20
|
+
"number": {
|
|
21
21
|
"style": "decimal",
|
|
22
22
|
},
|
|
23
|
-
"time":
|
|
23
|
+
"time": {
|
|
24
24
|
"hour": "2-digit",
|
|
25
25
|
"minute": "2-digit",
|
|
26
26
|
"second": "2-digit",
|
|
@@ -29,6 +29,7 @@ Object {
|
|
|
29
29
|
"deindent": [Function],
|
|
30
30
|
"equals": [Function],
|
|
31
31
|
"escapeHtml": [Function],
|
|
32
|
+
"escapeRegexp": [Function],
|
|
32
33
|
"flatten": [Function],
|
|
33
34
|
"format": [Function],
|
|
34
35
|
"formatDate": [Function],
|
|
@@ -77,5 +78,6 @@ Object {
|
|
|
77
78
|
"toCallback": [Function],
|
|
78
79
|
"toPromiseCallback": [Function],
|
|
79
80
|
"underscore": [Function],
|
|
81
|
+
Symbol(Symbol.toStringTag): "Module",
|
|
80
82
|
}
|
|
81
83
|
`;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { toPromiseCallback } from './toPromiseCallback.js'
|
|
2
|
-
import {
|
|
2
|
+
import { vi } from 'vitest'
|
|
3
3
|
|
|
4
4
|
describe('toPromiseCallback()', () => {
|
|
5
5
|
it('should call reject() when called with an error', () => {
|
|
6
6
|
const error = new Error('My Error')
|
|
7
|
-
const resolve =
|
|
8
|
-
const reject =
|
|
7
|
+
const resolve = vi.fn()
|
|
8
|
+
const reject = vi.fn()
|
|
9
9
|
const callback = toPromiseCallback(resolve, reject)
|
|
10
10
|
callback(error)
|
|
11
11
|
expect(resolve).not.toBeCalled()
|
|
@@ -14,8 +14,8 @@ describe('toPromiseCallback()', () => {
|
|
|
14
14
|
|
|
15
15
|
it('should call resolve() when called with a result', () => {
|
|
16
16
|
const result = 42
|
|
17
|
-
const resolve =
|
|
18
|
-
const reject =
|
|
17
|
+
const resolve = vi.fn()
|
|
18
|
+
const reject = vi.fn()
|
|
19
19
|
const callback = toPromiseCallback(resolve, reject)
|
|
20
20
|
callback(null, result)
|
|
21
21
|
expect(reject).not.toBeCalled()
|
package/src/object/clone.test.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { clone } from './clone.js'
|
|
2
|
-
import {
|
|
2
|
+
import { vi } from 'vitest'
|
|
3
3
|
|
|
4
4
|
describe('clone()', () => {
|
|
5
5
|
it('should clone objects', () => {
|
|
@@ -47,7 +47,7 @@ describe('clone()', () => {
|
|
|
47
47
|
it('should use clone() methods if available', () => {
|
|
48
48
|
const object = {
|
|
49
49
|
a: 1,
|
|
50
|
-
clone:
|
|
50
|
+
clone: vi.fn(() => ({ b: 2 }))
|
|
51
51
|
}
|
|
52
52
|
const copy = clone(object)
|
|
53
53
|
expect(object.clone).toBeCalledTimes(1)
|
package/src/string/index.js
CHANGED
|
@@ -1,100 +1,100 @@
|
|
|
1
1
|
import { debounce } from './debounce.js'
|
|
2
|
-
import {
|
|
2
|
+
import { vi } from 'vitest'
|
|
3
3
|
|
|
4
|
-
// Tell
|
|
5
|
-
|
|
4
|
+
// Tell vitest to mock all timeout functions:
|
|
5
|
+
vi.useFakeTimers()
|
|
6
6
|
|
|
7
7
|
describe('debounce()', () => {
|
|
8
8
|
const fixture = Symbol('fixture')
|
|
9
9
|
|
|
10
10
|
it('should never execute if intervals are less than wait', () => {
|
|
11
|
-
const func =
|
|
11
|
+
const func = vi.fn()
|
|
12
12
|
const debounced = debounce(func, 1000)
|
|
13
13
|
for (let i = 0; i < 10; i++) {
|
|
14
|
-
|
|
14
|
+
vi.advanceTimersByTime(500)
|
|
15
15
|
debounced()
|
|
16
16
|
}
|
|
17
17
|
expect(func).toBeCalledTimes(0)
|
|
18
18
|
})
|
|
19
19
|
|
|
20
20
|
it('should execute just once if an interval is big enough', () => {
|
|
21
|
-
const func =
|
|
21
|
+
const func = vi.fn()
|
|
22
22
|
const debounced = debounce(func, 1000)
|
|
23
23
|
for (let i = 0; i < 10; i++) {
|
|
24
|
-
|
|
24
|
+
vi.advanceTimersByTime(500)
|
|
25
25
|
debounced()
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
vi.advanceTimersByTime(1000)
|
|
28
28
|
debounced()
|
|
29
29
|
expect(func).toBeCalledTimes(1)
|
|
30
30
|
})
|
|
31
31
|
|
|
32
32
|
it('should pass through argument', async () => {
|
|
33
|
-
const func =
|
|
33
|
+
const func = vi.fn(value => value)
|
|
34
34
|
const debounced = debounce(func, 1000)
|
|
35
35
|
expect(debounced(fixture)).toBeUndefined()
|
|
36
|
-
|
|
36
|
+
vi.advanceTimersByTime(1000)
|
|
37
37
|
expect(debounced()).toBe(fixture)
|
|
38
38
|
expect(func).toBeCalledTimes(1)
|
|
39
39
|
})
|
|
40
40
|
|
|
41
41
|
it('should pass through return value', () => {
|
|
42
|
-
const func =
|
|
42
|
+
const func = vi.fn().mockReturnValue(fixture)
|
|
43
43
|
const debounced = debounce(func, 1000)
|
|
44
44
|
expect(debounced()).toBeUndefined()
|
|
45
|
-
|
|
45
|
+
vi.advanceTimersByTime(1000)
|
|
46
46
|
expect(debounced()).toBe(fixture)
|
|
47
47
|
expect(func).toBeCalledTimes(1)
|
|
48
48
|
})
|
|
49
49
|
|
|
50
50
|
it('should pass through `this`', () => {
|
|
51
|
-
const func =
|
|
51
|
+
const func = vi.fn().mockReturnThis()
|
|
52
52
|
const debounced = debounce(func, 0)
|
|
53
53
|
const obj = {}
|
|
54
54
|
expect(debounced.call(obj)).toBeUndefined()
|
|
55
|
-
|
|
55
|
+
vi.advanceTimersByTime(1000)
|
|
56
56
|
expect(debounced.call(obj)).toBe(obj)
|
|
57
57
|
})
|
|
58
58
|
|
|
59
59
|
it('should allow to cancel', () => {
|
|
60
|
-
const func =
|
|
60
|
+
const func = vi.fn()
|
|
61
61
|
const debounced = debounce(func, 1000)
|
|
62
62
|
debounced()
|
|
63
|
-
|
|
63
|
+
vi.advanceTimersByTime(500)
|
|
64
64
|
expect(debounced.cancel()).toBe(true)
|
|
65
65
|
expect(debounced.cancel()).toBe(false)
|
|
66
|
-
|
|
66
|
+
vi.advanceTimersByTime(500)
|
|
67
67
|
expect(func).toBeCalledTimes(0)
|
|
68
|
-
|
|
68
|
+
vi.advanceTimersByTime(1000)
|
|
69
69
|
expect(func).toBeCalledTimes(0)
|
|
70
70
|
debounced()
|
|
71
|
-
|
|
71
|
+
vi.advanceTimersByTime(1000)
|
|
72
72
|
expect(func).toBeCalledTimes(1)
|
|
73
73
|
})
|
|
74
74
|
|
|
75
75
|
it('should execute once immediately if intervals are less than wait', () => {
|
|
76
|
-
const func =
|
|
76
|
+
const func = vi.fn()
|
|
77
77
|
const debounced = debounce(func, { delay: 1000, immediate: true })
|
|
78
78
|
debounced()
|
|
79
79
|
expect(func).toBeCalledTimes(1)
|
|
80
80
|
for (let i = 0; i < 10; i++) {
|
|
81
|
-
|
|
81
|
+
vi.advanceTimersByTime(500)
|
|
82
82
|
debounced()
|
|
83
83
|
}
|
|
84
84
|
expect(func).toBeCalledTimes(1)
|
|
85
85
|
})
|
|
86
86
|
|
|
87
87
|
it(`should execute once immediately and once at the end with long enough intervals`, () => {
|
|
88
|
-
const func =
|
|
88
|
+
const func = vi.fn()
|
|
89
89
|
const debounced = debounce(func, { delay: 1000, immediate: true })
|
|
90
90
|
debounced()
|
|
91
91
|
expect(func).toBeCalledTimes(1)
|
|
92
92
|
for (let i = 0; i < 10; i++) {
|
|
93
|
-
|
|
93
|
+
vi.advanceTimersByTime(500)
|
|
94
94
|
debounced()
|
|
95
95
|
}
|
|
96
96
|
expect(func).toBeCalledTimes(1)
|
|
97
|
-
|
|
97
|
+
vi.advanceTimersByTime(1000)
|
|
98
98
|
debounced()
|
|
99
99
|
expect(func).toBeCalledTimes(2)
|
|
100
100
|
})
|
|
@@ -1,51 +1,51 @@
|
|
|
1
1
|
import { debounceAsync } from './debounceAsync.js'
|
|
2
|
-
import {
|
|
2
|
+
import { vi } from 'vitest'
|
|
3
3
|
|
|
4
|
-
// Tell
|
|
5
|
-
|
|
4
|
+
// Tell vitest to mock all timeout functions:
|
|
5
|
+
vi.useFakeTimers()
|
|
6
6
|
|
|
7
7
|
describe('debounceAsync()', () => {
|
|
8
8
|
const fixture = Symbol('fixture')
|
|
9
9
|
|
|
10
10
|
it('should never execute if intervals are less than wait', () => {
|
|
11
|
-
const func =
|
|
11
|
+
const func = vi.fn()
|
|
12
12
|
const debounced = debounceAsync(func, 1000)
|
|
13
13
|
for (let i = 0; i < 10; i++) {
|
|
14
|
-
|
|
14
|
+
vi.advanceTimersByTime(500)
|
|
15
15
|
debounced()
|
|
16
16
|
}
|
|
17
17
|
expect(func).toBeCalledTimes(0)
|
|
18
18
|
})
|
|
19
19
|
|
|
20
20
|
it('should execute just once if an interval is big enough', () => {
|
|
21
|
-
const func =
|
|
21
|
+
const func = vi.fn()
|
|
22
22
|
const debounced = debounceAsync(func, 1000)
|
|
23
23
|
for (let i = 0; i < 10; i++) {
|
|
24
|
-
|
|
24
|
+
vi.advanceTimersByTime(500)
|
|
25
25
|
debounced()
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
vi.advanceTimersByTime(1000)
|
|
28
28
|
debounced()
|
|
29
29
|
expect(func).toBeCalledTimes(1)
|
|
30
30
|
})
|
|
31
31
|
|
|
32
32
|
it('should pass through argument', async () => {
|
|
33
33
|
expect.assertions(2)
|
|
34
|
-
const func =
|
|
34
|
+
const func = vi.fn(async value => value)
|
|
35
35
|
const debounced = debounceAsync(func, 1000)
|
|
36
36
|
const promise = debounced(fixture)
|
|
37
|
-
|
|
37
|
+
vi.advanceTimersByTime(1000)
|
|
38
38
|
expect(await promise).toBe(fixture)
|
|
39
39
|
expect(func).toBeCalledTimes(1)
|
|
40
40
|
})
|
|
41
41
|
|
|
42
42
|
it('should pass through return value', async () => {
|
|
43
43
|
expect.assertions(2)
|
|
44
|
-
const func =
|
|
44
|
+
const func = vi.fn().mockResolvedValueOnce(fixture)
|
|
45
45
|
const debounced = debounceAsync(func, 1000)
|
|
46
46
|
const promises = []
|
|
47
47
|
promises.push(debounced())
|
|
48
|
-
|
|
48
|
+
vi.advanceTimersByTime(1000)
|
|
49
49
|
promises.push(debounced())
|
|
50
50
|
expect(await Promise.all(promises)).toStrictEqual([fixture, fixture])
|
|
51
51
|
expect(func).toBeCalledTimes(1)
|
|
@@ -53,12 +53,12 @@ describe('debounceAsync()', () => {
|
|
|
53
53
|
|
|
54
54
|
it('should pass through `this`', async () => {
|
|
55
55
|
expect.assertions(2)
|
|
56
|
-
const func =
|
|
56
|
+
const func = vi.fn(async function() { return this })
|
|
57
57
|
const debounced = debounceAsync(func, 1000)
|
|
58
58
|
const obj = {}
|
|
59
59
|
const promises = []
|
|
60
60
|
promises.push(debounced.call(obj))
|
|
61
|
-
|
|
61
|
+
vi.advanceTimersByTime(1000)
|
|
62
62
|
promises.push(debounced.call(obj))
|
|
63
63
|
expect(await Promise.all(promises)).toStrictEqual([obj, obj])
|
|
64
64
|
expect(func).toBeCalledTimes(1)
|
|
@@ -66,19 +66,19 @@ describe('debounceAsync()', () => {
|
|
|
66
66
|
|
|
67
67
|
it('should allow to cancel', async () => {
|
|
68
68
|
expect.assertions(6)
|
|
69
|
-
const func =
|
|
69
|
+
const func = vi.fn()
|
|
70
70
|
const debounced = debounceAsync(func, 1000)
|
|
71
71
|
const promise = debounced()
|
|
72
|
-
|
|
72
|
+
vi.advanceTimersByTime(500)
|
|
73
73
|
expect(debounced.cancel()).toBe(true)
|
|
74
74
|
expect(debounced.cancel()).toBe(false)
|
|
75
|
-
|
|
75
|
+
vi.advanceTimersByTime(500)
|
|
76
76
|
expect(func).toBeCalledTimes(0)
|
|
77
77
|
expect(await promise).toBeUndefined()
|
|
78
|
-
|
|
78
|
+
vi.advanceTimersByTime(1000)
|
|
79
79
|
expect(func).toBeCalledTimes(0)
|
|
80
80
|
await debounced()
|
|
81
|
-
|
|
81
|
+
vi.advanceTimersByTime(1000)
|
|
82
82
|
expect(func).toBeCalledTimes(1)
|
|
83
83
|
})
|
|
84
84
|
|
|
@@ -86,13 +86,13 @@ describe('debounceAsync()', () => {
|
|
|
86
86
|
'should execute once immediately if intervals are less than wait',
|
|
87
87
|
async () => {
|
|
88
88
|
expect.assertions(2)
|
|
89
|
-
const func =
|
|
89
|
+
const func = vi.fn()
|
|
90
90
|
const debounced = debounceAsync(func, { delay: 1000, immediate: true })
|
|
91
91
|
const promises = []
|
|
92
92
|
promises.push(debounced())
|
|
93
93
|
expect(func).toBeCalledTimes(1)
|
|
94
94
|
for (let i = 0; i < 10; i++) {
|
|
95
|
-
|
|
95
|
+
vi.advanceTimersByTime(500)
|
|
96
96
|
promises.push(debounced())
|
|
97
97
|
}
|
|
98
98
|
await Promise.all(promises)
|
|
@@ -104,17 +104,17 @@ describe('debounceAsync()', () => {
|
|
|
104
104
|
'should execute twice immediately with long enough intervals',
|
|
105
105
|
async () => {
|
|
106
106
|
expect.assertions(3)
|
|
107
|
-
const func =
|
|
107
|
+
const func = vi.fn()
|
|
108
108
|
const debounced = debounceAsync(func, { delay: 1000, immediate: true })
|
|
109
109
|
const promises = []
|
|
110
110
|
promises.push(debounced())
|
|
111
111
|
expect(func).toBeCalledTimes(1)
|
|
112
112
|
for (let i = 0; i < 10; i++) {
|
|
113
|
-
|
|
113
|
+
vi.advanceTimersByTime(500)
|
|
114
114
|
promises.push(debounced())
|
|
115
115
|
}
|
|
116
116
|
expect(func).toBeCalledTimes(1)
|
|
117
|
-
|
|
117
|
+
vi.advanceTimersByTime(1000)
|
|
118
118
|
promises.push(debounced())
|
|
119
119
|
await Promise.all(promises)
|
|
120
120
|
expect(func).toBeCalledTimes(2)
|
|
@@ -126,7 +126,7 @@ describe('debounceAsync()', () => {
|
|
|
126
126
|
async () => {
|
|
127
127
|
expect.assertions(5)
|
|
128
128
|
let count = 0
|
|
129
|
-
const func =
|
|
129
|
+
const func = vi.fn(() => {
|
|
130
130
|
if (++count > 1) {
|
|
131
131
|
throw new Error('boom')
|
|
132
132
|
}
|
|
@@ -136,11 +136,11 @@ describe('debounceAsync()', () => {
|
|
|
136
136
|
promises.push(debounced())
|
|
137
137
|
expect(func).toBeCalledTimes(1)
|
|
138
138
|
for (let i = 0; i < 10; i++) {
|
|
139
|
-
|
|
139
|
+
vi.advanceTimersByTime(500)
|
|
140
140
|
promises.push(debounced())
|
|
141
141
|
}
|
|
142
142
|
expect(func).toBeCalledTimes(1)
|
|
143
|
-
|
|
143
|
+
vi.advanceTimersByTime(1000)
|
|
144
144
|
await expect(debounced()).rejects.toThrow('boom')
|
|
145
145
|
const results = await Promise.all(promises)
|
|
146
146
|
expect(results).toStrictEqual(new Array(11).fill(undefined))
|