@ditojs/utils 1.4.3 → 1.6.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ditojs/utils",
3
- "version": "1.4.3",
3
+ "version": "1.6.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": "f2a544aef8ab0a12c7019650cd86558f8365b007"
30
+ "gitHead": "a442b0e47237fef8aeb65793fa3f5bc6621442ce"
31
31
  }
@@ -1,7 +1,7 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
1
+ // Vitest Snapshot v1
2
2
 
3
- exports[`index exports all symbols 1`] = `
4
- Object {
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": Object {
15
- "date": Object {
14
+ "defaultFormats": {
15
+ "date": {
16
16
  "day": "numeric",
17
17
  "month": "long",
18
18
  "year": "numeric",
19
19
  },
20
- "number": Object {
20
+ "number": {
21
21
  "style": "decimal",
22
22
  },
23
- "time": Object {
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
  `;
@@ -7,7 +7,7 @@ describe('shuffle()', () => {
7
7
  expect(shuffle(array)).not.toBe(array)
8
8
  })
9
9
 
10
- it('should contain the same elements after a collection is shuffled', () => {
10
+ it(`should contain the same elements after a collection is shuffled and sorted again`, () => {
11
11
  expect(shuffle(array).sort()).toStrictEqual(array)
12
12
  })
13
13
 
@@ -1,11 +1,11 @@
1
1
  import { toPromiseCallback } from './toPromiseCallback.js'
2
- import { jest } from '@jest/globals'
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 = jest.fn()
8
- const reject = jest.fn()
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 = jest.fn()
18
- const reject = jest.fn()
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()
@@ -1,5 +1,5 @@
1
1
  import { clone } from './clone.js'
2
- import { jest } from '@jest/globals'
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: jest.fn(() => ({ b: 2 }))
50
+ clone: vi.fn(() => ({ b: 2 }))
51
51
  }
52
52
  const copy = clone(object)
53
53
  expect(object.clone).toBeCalledTimes(1)
@@ -11,10 +11,7 @@ export function merge(target, ...sources) {
11
11
  ? clone(target)
12
12
  : target
13
13
  for (const key of Object.keys(source)) {
14
- const value = _merge(target[key], source[key], true)
15
- if (value !== undefined || !(key in result)) {
16
- result[key] = value
17
- }
14
+ result[key] = _merge(target[key], source[key], true)
18
15
  }
19
16
  return result
20
17
  }
@@ -80,21 +80,20 @@ describe('merge()', () => {
80
80
  expect(actual2.a).toStrictEqual([[4, 5, 3]])
81
81
  })
82
82
 
83
- it('should not overwrite existing values with `undefined` in objects', () => {
83
+ it('should overwrite existing values with `undefined` in objects', () => {
84
84
  const actual = merge({ a: 1 }, { a: undefined, b: undefined })
85
- expect(actual).toStrictEqual({ a: 1, b: undefined })
85
+ expect(actual).toStrictEqual({ a: undefined, b: undefined })
86
86
  })
87
87
 
88
- it('should not overwrite existing values with `undefined` in arrays', () => {
88
+ it('should overwrite existing values with `undefined` in arrays', () => {
89
89
  const array1 = [1]
90
90
  array1[2] = 3
91
91
  const actual1 = merge([4, 5, 6], array1)
92
- const expected1 = [1, 5, 3]
93
- expect(actual1).toStrictEqual(expected1)
92
+ expect(actual1).toStrictEqual([1, 5, 3])
94
93
 
95
94
  const array2 = [1, undefined, 3]
96
95
  const actual2 = merge([4, 5, 6], array2)
97
- expect(actual2).toStrictEqual(expected1)
96
+ expect(actual2).toStrictEqual([1, undefined, 3])
98
97
  })
99
98
 
100
99
  it('should merge regexps', () => {
@@ -0,0 +1,3 @@
1
+ export function escapeRegexp(string) {
2
+ return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
3
+ }
@@ -2,6 +2,7 @@ export * from './camelize.js'
2
2
  export * from './capitalize.js'
3
3
  export * from './decamelize.js'
4
4
  export * from './deindent.js'
5
+ export * from './escapeRegexp.js'
5
6
  export * from './format.js'
6
7
  export * from './formatDate.js'
7
8
  export * from './getCommonPrefix.js'
@@ -1,4 +1,4 @@
1
- // eslint-disable-next-line node/no-deprecated-api
1
+ // eslint-disable-next-line n/no-deprecated-api
2
2
  import punycode from 'punycode'
3
3
 
4
4
  // Best effort approach, allowing Internationalized domain name (with punycode)
@@ -1,100 +1,100 @@
1
1
  import { debounce } from './debounce.js'
2
- import { jest } from '@jest/globals'
2
+ import { vi } from 'vitest'
3
3
 
4
- // Tell jest to mock all timeout functions:
5
- jest.useFakeTimers()
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 = jest.fn()
11
+ const func = vi.fn()
12
12
  const debounced = debounce(func, 1000)
13
13
  for (let i = 0; i < 10; i++) {
14
- jest.advanceTimersByTime(500)
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 = jest.fn()
21
+ const func = vi.fn()
22
22
  const debounced = debounce(func, 1000)
23
23
  for (let i = 0; i < 10; i++) {
24
- jest.advanceTimersByTime(500)
24
+ vi.advanceTimersByTime(500)
25
25
  debounced()
26
26
  }
27
- jest.advanceTimersByTime(1000)
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 = jest.fn(value => value)
33
+ const func = vi.fn(value => value)
34
34
  const debounced = debounce(func, 1000)
35
35
  expect(debounced(fixture)).toBeUndefined()
36
- jest.advanceTimersByTime(1000)
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 = jest.fn().mockReturnValue(fixture)
42
+ const func = vi.fn().mockReturnValue(fixture)
43
43
  const debounced = debounce(func, 1000)
44
44
  expect(debounced()).toBeUndefined()
45
- jest.advanceTimersByTime(1000)
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 = jest.fn().mockReturnThis()
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
- jest.advanceTimersByTime(1000)
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 = jest.fn()
60
+ const func = vi.fn()
61
61
  const debounced = debounce(func, 1000)
62
62
  debounced()
63
- jest.advanceTimersByTime(500)
63
+ vi.advanceTimersByTime(500)
64
64
  expect(debounced.cancel()).toBe(true)
65
65
  expect(debounced.cancel()).toBe(false)
66
- jest.advanceTimersByTime(500)
66
+ vi.advanceTimersByTime(500)
67
67
  expect(func).toBeCalledTimes(0)
68
- jest.advanceTimersByTime(1000)
68
+ vi.advanceTimersByTime(1000)
69
69
  expect(func).toBeCalledTimes(0)
70
70
  debounced()
71
- jest.advanceTimersByTime(1000)
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 = jest.fn()
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
- jest.advanceTimersByTime(500)
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 = jest.fn()
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
- jest.advanceTimersByTime(500)
93
+ vi.advanceTimersByTime(500)
94
94
  debounced()
95
95
  }
96
96
  expect(func).toBeCalledTimes(1)
97
- jest.advanceTimersByTime(1000)
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 { jest } from '@jest/globals'
2
+ import { vi } from 'vitest'
3
3
 
4
- // Tell jest to mock all timeout functions:
5
- jest.useFakeTimers()
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 = jest.fn()
11
+ const func = vi.fn()
12
12
  const debounced = debounceAsync(func, 1000)
13
13
  for (let i = 0; i < 10; i++) {
14
- jest.advanceTimersByTime(500)
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 = jest.fn()
21
+ const func = vi.fn()
22
22
  const debounced = debounceAsync(func, 1000)
23
23
  for (let i = 0; i < 10; i++) {
24
- jest.advanceTimersByTime(500)
24
+ vi.advanceTimersByTime(500)
25
25
  debounced()
26
26
  }
27
- jest.advanceTimersByTime(1000)
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 = jest.fn(async value => value)
34
+ const func = vi.fn(async value => value)
35
35
  const debounced = debounceAsync(func, 1000)
36
36
  const promise = debounced(fixture)
37
- jest.advanceTimersByTime(1000)
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 = jest.fn().mockResolvedValueOnce(fixture)
44
+ const func = vi.fn().mockResolvedValueOnce(fixture)
45
45
  const debounced = debounceAsync(func, 1000)
46
46
  const promises = []
47
47
  promises.push(debounced())
48
- jest.advanceTimersByTime(1000)
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 = jest.fn(async function() { return this })
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
- jest.advanceTimersByTime(1000)
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 = jest.fn()
69
+ const func = vi.fn()
70
70
  const debounced = debounceAsync(func, 1000)
71
71
  const promise = debounced()
72
- jest.advanceTimersByTime(500)
72
+ vi.advanceTimersByTime(500)
73
73
  expect(debounced.cancel()).toBe(true)
74
74
  expect(debounced.cancel()).toBe(false)
75
- jest.advanceTimersByTime(500)
75
+ vi.advanceTimersByTime(500)
76
76
  expect(func).toBeCalledTimes(0)
77
77
  expect(await promise).toBeUndefined()
78
- jest.advanceTimersByTime(1000)
78
+ vi.advanceTimersByTime(1000)
79
79
  expect(func).toBeCalledTimes(0)
80
80
  await debounced()
81
- jest.advanceTimersByTime(1000)
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 = jest.fn()
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
- jest.advanceTimersByTime(500)
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 = jest.fn()
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
- jest.advanceTimersByTime(500)
113
+ vi.advanceTimersByTime(500)
114
114
  promises.push(debounced())
115
115
  }
116
116
  expect(func).toBeCalledTimes(1)
117
- jest.advanceTimersByTime(1000)
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 = jest.fn(() => {
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
- jest.advanceTimersByTime(500)
139
+ vi.advanceTimersByTime(500)
140
140
  promises.push(debounced())
141
141
  }
142
142
  expect(func).toBeCalledTimes(1)
143
- jest.advanceTimersByTime(1000)
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))