-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
89 lines (82 loc) · 1.96 KB
/
test.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
let dr = require('./')
let assert = require('assert')
let reduced = dr({
a: 'a',
b: { c: 'b.c' },
c: [
'c.0',
{
d: 'c.1.d',
e: [
'c.1.e.0'
]
}
]
}, (reduced, value, path) => {
if (typeof value === 'string') {
reduced[path] = value
}
return reduced
})
assert.deepEqual(reduced, {
a: 'a',
'b.c': 'b.c',
'c.0': 'c.0',
'c.1.d': 'c.1.d',
'c.1.e.0': 'c.1.e.0'
})
reduced = dr({
id: 'A8811',
packages: [
{
id: 'P100',
contents: [
{
id: 'R88',
name: 'resistor'
},
{
id: 'C99',
name: 'capacitor'
}
]
}, {
id: 'P101',
contents: [
{
id: 'C96',
name: 'coil'
}
]
}
]
}, (reduced, value, path) => {
if (path.match(/packages\.\d+\.contents\.\d+$/)) {
reduced.push(value)
}
return reduced
}, [])
assert.deepEqual(reduced, [
{ id: 'R88', name: 'resistor' },
{ id: 'C99', name: 'capacitor' },
{ id: 'C96', name: 'coil' }])
// test performance
let obj = require('./MAT1-04.json')
let start = Date.now()
dr(obj, reduced => reduced) // noop
let end = Date.now()
console.log(`Traversed MAT1-04.json (149kb) in ${end - start} milliseconds.`)
start = Date.now()
reduced = dr(obj, (reduced, value, path) =>
path.match(/kompetansemaal\.\d+$/) // get all items of kompetansemaal arrays
? reduced.push(value) && reduced
: reduced, [])
end = Date.now()
console.log(`Traversed MAT1-04.json (149kb) and got ${reduced.length} objects in ${end - start} milliseconds.`)
start = Date.now()
reduced = dr(obj, (reduced, value, path) =>
path.match(/kompetansemaal\.\d+$/) // get all items of kompetansemaal arrays
? reduced.push(value) && reduced
: reduced, [], 'kompetansemaal-kapittel.kompetansemaalsett')
end = Date.now()
console.log(`Traversed kompetansemaal-kapittel.kompetansemaalsett in MAT1-04.json and got ${reduced.length} objects in ${end - start} milliseconds.`)