@ditojs/utils 2.51.0 → 2.52.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": "2.51.0",
3
+ "version": "2.52.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",
@@ -39,5 +39,5 @@
39
39
  "devDependencies": {
40
40
  "typescript": "^5.8.3"
41
41
  },
42
- "gitHead": "0b8f114647510db71c50be9fdc331297b1e726ab"
42
+ "gitHead": "dbfde6b475d824a88fbbbc1e007fd264e80d46fb"
43
43
  }
@@ -9,7 +9,6 @@ exports[`index > exports all symbols 1`] = `
9
9
  "camelize": [Function],
10
10
  "capitalize": [Function],
11
11
  "clone": [Function],
12
- "convertToJson": [Function],
13
12
  "debounce": [Function],
14
13
  "debounceAsync": [Function],
15
14
  "decamelize": [Function],
@@ -59,6 +58,7 @@ exports[`index > exports all symbols 1`] = `
59
58
  "isModule": [Function],
60
59
  "isNumber": [Function],
61
60
  "isObject": [Function],
61
+ "isPlainArray": [Function],
62
62
  "isPlainObject": [Function],
63
63
  "isPromise": [Function],
64
64
  "isRegExp": [Function],
package/src/base/base.js CHANGED
@@ -13,17 +13,20 @@ export function isPlainObject(arg) {
13
13
  // We also need to check for ctor.name === 'Object', in case this is an object
14
14
  // from another global scope (e.g. another vm context in Node.js).
15
15
  // When an value has no constructor, it was created with `Object.create(null)`
16
- return (
17
- !!arg && (
18
- ctor && (
19
- ctor === Object ||
20
- ctor.name === 'Object'
21
- ) ||
16
+ return !!(
17
+ arg && (
18
+ ctor && (ctor === Object || ctor.name === 'Object') ||
22
19
  !ctor && !isModule(arg)
23
20
  )
24
21
  )
25
22
  }
26
23
 
24
+ export function isPlainArray(arg) {
25
+ const ctor = arg?.constructor
26
+ // See `isPlainObject()` for explanation.
27
+ return !!(arg && ctor && (ctor === Array || ctor.name === 'Array'))
28
+ }
29
+
27
30
  export function isObject(arg) {
28
31
  return !!arg && typeof arg === 'object' && !isArray(arg)
29
32
  }
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  isPlainObject,
3
+ isPlainArray,
3
4
  isObject,
4
5
  isArray,
5
6
  isModule,
@@ -23,6 +24,7 @@ import * as module from './index.js'
23
24
 
24
25
  const object = { a: 1 }
25
26
  const array = [1, 2, 3]
27
+ const arrayLike = { 0: 1, 1: 2, 2: 3, length: 3 }
26
28
  const string = 'a'
27
29
  const number = 1
28
30
  const boolean = true
@@ -42,6 +44,7 @@ describe('isPlainObject()', () => {
42
44
  [module, false],
43
45
  [object, true],
44
46
  [array, false],
47
+ [arrayLike, true],
45
48
  [string, false],
46
49
  [number, false],
47
50
  [boolean, false],
@@ -64,12 +67,42 @@ describe('isPlainObject()', () => {
64
67
  )
65
68
  })
66
69
 
70
+ describe('isPlainArray()', () => {
71
+ describe.each([
72
+ [null, false],
73
+ [module, false],
74
+ [object, false],
75
+ [array, true],
76
+ [arrayLike, false],
77
+ [string, false],
78
+ [number, false],
79
+ [boolean, false],
80
+ [date, false],
81
+ [regexp, false],
82
+ [symbol, false],
83
+ [instance, false],
84
+ [promise, false],
85
+ [func, false],
86
+ [lambda, false],
87
+ [asyncFunc, false],
88
+ [asyncLambda, false]
89
+ ])(
90
+ 'isPlainArray(%o)',
91
+ (value, expected) => {
92
+ it(`returns ${expected}`, () => {
93
+ expect(isPlainArray(value)).toBe(expected)
94
+ })
95
+ }
96
+ )
97
+ })
98
+
67
99
  describe('isObject()', () => {
68
100
  describe.each([
69
101
  [null, false],
70
102
  [module, true],
71
103
  [object, true],
72
104
  [array, false],
105
+ [arrayLike, true],
73
106
  [string, false],
74
107
  [number, false],
75
108
  [boolean, false],
@@ -101,6 +134,7 @@ describe('isArray()', () => {
101
134
  [module, false],
102
135
  [object, false],
103
136
  [array, true],
137
+ [arrayLike, false],
104
138
  [string, false],
105
139
  [number, false],
106
140
  [boolean, false],
@@ -129,6 +163,7 @@ describe('isModule()', () => {
129
163
  [module, true],
130
164
  [object, false],
131
165
  [array, false],
166
+ [arrayLike, false],
132
167
  [string, false],
133
168
  [number, false],
134
169
  [boolean, false],
@@ -157,6 +192,7 @@ describe('isFunction()', () => {
157
192
  [module, false],
158
193
  [object, false],
159
194
  [array, false],
195
+ [arrayLike, false],
160
196
  [string, false],
161
197
  [number, false],
162
198
  [boolean, false],
@@ -185,6 +221,7 @@ describe('isNumber()', () => {
185
221
  [module, false],
186
222
  [object, false],
187
223
  [array, false],
224
+ [arrayLike, false],
188
225
  [string, false],
189
226
  [number, true],
190
227
  [Object(number), true],
@@ -214,6 +251,7 @@ describe('isInteger()', () => {
214
251
  [module, false],
215
252
  [object, false],
216
253
  [array, false],
254
+ [arrayLike, false],
217
255
  [string, false],
218
256
  [number, true],
219
257
  [boolean, false],
@@ -270,6 +308,7 @@ describe('isString()', () => {
270
308
  [null, false],
271
309
  [object, false],
272
310
  [array, false],
311
+ [arrayLike, false],
273
312
  [string, true],
274
313
  [Object(string), true],
275
314
  [number, false],
@@ -298,6 +337,7 @@ describe('isBoolean()', () => {
298
337
  [null, false],
299
338
  [object, false],
300
339
  [array, false],
340
+ [arrayLike, false],
301
341
  [string, false],
302
342
  [number, false],
303
343
  [boolean, true],
@@ -326,6 +366,7 @@ describe('isDate()', () => {
326
366
  [null, false],
327
367
  [object, false],
328
368
  [array, false],
369
+ [arrayLike, false],
329
370
  [string, false],
330
371
  [number, false],
331
372
  [boolean, false],
@@ -353,6 +394,7 @@ describe('isRegExp()', () => {
353
394
  [null, false],
354
395
  [object, false],
355
396
  [array, false],
397
+ [arrayLike, false],
356
398
  [string, false],
357
399
  [number, false],
358
400
  [boolean, false],
@@ -388,6 +430,7 @@ describe('isPromise()', () => {
388
430
  [null, false],
389
431
  [object, false],
390
432
  [array, false],
433
+ [arrayLike, false],
391
434
  [string, false],
392
435
  [number, false],
393
436
  [boolean, false],
@@ -417,6 +460,7 @@ describe('isAsync()', () => {
417
460
  [null, false],
418
461
  [object, false],
419
462
  [array, false],
463
+ [arrayLike, false],
420
464
  [string, false],
421
465
  [number, false],
422
466
  [boolean, false],
@@ -444,6 +488,7 @@ describe('isArrayLike()', () => {
444
488
  [null, false],
445
489
  [object, false],
446
490
  [array, true],
491
+ [arrayLike, true],
447
492
  [string, true],
448
493
  [number, false],
449
494
  [boolean, false],
package/src/index.js CHANGED
@@ -8,4 +8,3 @@ export * from './promise/index.js'
8
8
  export * from './function/index.js'
9
9
  export * from './dataPath/index.js'
10
10
  export * from './html/index.js'
11
- export * from './json/index.js'
@@ -1,27 +0,0 @@
1
- import { isArray, isPlainObject } from '../base/index.js'
2
-
3
- export function convertToJson(arg) {
4
- return arg?.toJSON
5
- ? arg.toJSON()
6
- : isPlainObject(arg)
7
- ? convertToJsonObject(arg)
8
- : isArray(arg)
9
- ? convertToJsonArray(arg)
10
- : arg
11
- }
12
-
13
- function convertToJsonObject(arg) {
14
- const json = {}
15
- for (const key of Object.keys(arg)) {
16
- json[key] = convertToJson(arg[key])
17
- }
18
- return json
19
- }
20
-
21
- function convertToJsonArray(arg) {
22
- const ret = new Array(arg.length)
23
- for (let i = 0, l = arg.length; i < l; ++i) {
24
- ret[i] = convertToJson(arg[i])
25
- }
26
- return ret
27
- }
package/src/json/index.js DELETED
@@ -1 +0,0 @@
1
- export * from './convertToJson.js'