-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathobject.test.ts
More file actions
71 lines (63 loc) · 1.97 KB
/
Copy pathobject.test.ts
File metadata and controls
71 lines (63 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import { isPlainRecord, isRecordLike, sortObjectKeysDeep } from './object.js'
class Sample {
value = 1
}
describe('isRecordLike', () => {
it('returns true for plain objects, Date, and class instances', () => {
expect(isRecordLike({})).toBe(true)
expect(isRecordLike(new Date())).toBe(true)
expect(isRecordLike(new Sample())).toBe(true)
})
it('returns false for arrays, null, and primitives', () => {
expect(isRecordLike([])).toBe(false)
expect(isRecordLike(null)).toBe(false)
expect(isRecordLike('not-a-record')).toBe(false)
expect(isRecordLike(42)).toBe(false)
})
})
describe('isPlainRecord', () => {
it('returns true for plain objects', () => {
expect(isPlainRecord({})).toBe(true)
expect(isPlainRecord(Object.create(null))).toBe(true)
})
it('returns false for Date, class instances, arrays, and null', () => {
expect(isPlainRecord(new Date())).toBe(false)
expect(isPlainRecord(new Sample())).toBe(false)
expect(isPlainRecord([])).toBe(false)
expect(isPlainRecord(null)).toBe(false)
})
})
describe('sortObjectKeysDeep', () => {
it('sorts keys deeply and recurses into array elements', () => {
const input = {
b: 1,
a: { d: 4, c: 3 },
list: [{ z: 26, y: 25 }],
}
const sorted = sortObjectKeysDeep(input)
expect(JSON.stringify(sorted)).toBe(
JSON.stringify({ a: { c: 3, d: 4 }, b: 1, list: [{ y: 25, z: 26 }] })
)
})
it('returns primitives and null unchanged', () => {
expect(sortObjectKeysDeep(null)).toBe(null)
expect(sortObjectKeysDeep(42)).toBe(42)
expect(sortObjectKeysDeep('x')).toBe('x')
})
it('preserves array order while sorting element keys', () => {
const sorted = sortObjectKeysDeep([
{ b: 1, a: 2 },
{ d: 3, c: 4 },
])
expect(JSON.stringify(sorted)).toBe(
JSON.stringify([
{ a: 2, b: 1 },
{ c: 4, d: 3 },
])
)
})
})