Skip to content

Commit 989a680

Browse files
author
Scott Switzer
committed
fix some tests - up to wankdanker#53
1 parent 92b3b42 commit 989a680

File tree

2 files changed

+2593
-2717
lines changed

2 files changed

+2593
-2717
lines changed

src/object-mapper.js

Lines changed: 45 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
'use strict';
22

33
var _undefined
4-
, performance = require('perf_hooks').performance
54

65
function ObjectMapper(from_object, to_object, property_map)
76
{
8-
global.elapsed['ObjectMapper'].n++ // performance monitoring
9-
let timer = performance.now()
10-
117
// There are a few different constructors - move around properties if needed
128
if (typeof property_map === 'undefined') {
139
property_map = to_object
@@ -16,189 +12,124 @@ function ObjectMapper(from_object, to_object, property_map)
1612

1713
// Loop through the map to process individual mapping instructions
1814
for (const [from_key_str, to_key_str] of Object.entries(property_map)) {
19-
20-
global.elapsed['ObjectMapper'].ms += (performance.now() - timer)
2115
const from_data = getKeyValue(from_object, from_key_str)
2216
to_object = setKeyValue(to_object, to_key_str, from_data)
23-
timer = performance.now()
2417
}
2518

26-
global.elapsed['ObjectMapper'].ms += (performance.now() - timer)
2719
return to_object
2820
}
2921

3022
// if the data is an array, walk down the obj path and build until there is an array key
3123
function update(obj, data, key_arr)
3224
{
33-
global.elapsed['update'].n++ // performance monitoring
34-
let timer = performance.now()
35-
36-
let x = 0 // index of the key array
37-
38-
var o
3925
// Get the object key and index that needs to be parsed
40-
global.elapsed['update'].ms += (performance.now() - timer)
41-
var [key, ix] = key_arr.shift()
42-
timer = performance.now()
26+
const [key, ix] = key_arr.shift()
4327

4428
// If there is a key, we need to traverse down to this part of the object
45-
if (key) {
46-
global.elapsed['update'].ms += (performance.now() - timer)
47-
o = update_obj(obj, key, data, key_arr)
48-
timer = performance.now()
49-
}
29+
if (key)
30+
return update_obj(obj, key, data, key_arr)
5031

51-
if (ix !== null) {
52-
global.elapsed['update'].ms += (performance.now() - timer)
53-
o = update_arr(obj, key, ix, data, key_arr)
54-
timer = performance.now()
55-
}
56-
57-
global.elapsed['update'].ms += (performance.now() - timer)
58-
return o
32+
// If there is an array index, we need to traverse through the array
33+
if (ix !== null)
34+
return update_arr(obj, ix, data, key_arr)
5935
}
6036

6137
function update_obj(obj, key, data, key_arr)
6238
{
63-
global.elapsed['update_obj'].n++ // performance monitoring
64-
let timer = performance.now()
65-
6639
// If the object is undefined, we need to create a new object
67-
if (obj == null)
68-
obj = {}
40+
obj = obj || {}
6941

7042
// Set the key of the object equal to the recursive, or if at the end, the data
7143
if (key_arr.length > 0) {
72-
global.elapsed['update_obj'].ms += (performance.now() - timer)
7344
obj[key] = update(obj[key], data, key_arr)
74-
timer = performance.now()
7545
} else {
7646
// This is a leaf.
7747
if (data) obj[key] = data
7848
}
79-
global.elapsed['update_obj'].ms += (performance.now() - timer)
49+
8050
return obj
8151
}
8252

83-
function update_arr(obj, key, ix, data, key_arr)
53+
function update_arr(arr, ix, data, key_arr)
8454
{
85-
global.elapsed['update_arr'].n++ // performance monitoring
86-
let timer = performance.now()
87-
8855
// If the top level object is undefined, we need to create a new array
89-
if (obj == null) obj = []
56+
if (arr == null) arr = []
57+
9058
// Make sure that there is an array item for each item in the data array
9159
if (Array.isArray(data)) {
92-
obj = data.reduce(function(o,d,i) {
60+
arr = data.reduce(function(o,d,i) {
9361
if (i == '' || i == i) {
94-
global.elapsed['update_arr'].ms += (performance.now() - timer)
9562
o[i] = update(o[i], d, key_arr.slice())
96-
timer = performance.now()
9763
return o
9864
}
99-
}, obj)
100-
global.elapsed['update_arr'].ms += (performance.now() - timer)
101-
return obj
65+
}, arr)
66+
67+
return arr
10268
}
10369
// If there is more work to be done, push an object onto the array
10470
else {
105-
var x = (ix) ? ix : 0
106-
if (key_arr.length > 0) {
107-
global.elapsed['update_arr'].ms += (performance.now() - timer)
108-
obj[x] = update(obj[x], data, key_arr)
109-
timer = performance.now()
110-
} else {
111-
global.elapsed['update_arr'].ms += (performance.now() - timer)
112-
obj[x] = data
113-
timer = performance.now()
114-
}
71+
const x = (ix) ? ix : 0
72+
if (key_arr.length > 0)
73+
arr[x] = update(arr[x], data, key_arr)
74+
else
75+
arr[x] = data
11576
}
11677

117-
global.elapsed['update_arr'].ms += (performance.now() - timer)
118-
return obj
78+
return arr
11979
}
12080

12181
function select(obj, key_arr)
12282
{
123-
global.elapsed['select'].n++ // performance monitoring
124-
let timer = performance.now()
125-
12683
// Get the object key or index that needs to be parsed
127-
global.elapsed['select'].ms += (performance.now() - timer)
128-
var [key, ix] = key_arr.shift()
129-
timer = performance.now()
84+
const [key, ix] = key_arr.shift()
13085

13186
// If there is an object key, grab the object property
13287
if (key) {
133-
global.elapsed['select'].ms += (performance.now() - timer)
13488
return select_obj(obj, key, key_arr)
13589
}
13690

13791
// If we need to deal with an array, then loop through and return recursive select for each
13892
if (Array.isArray(obj)) {
139-
global.elapsed['select'].ms += (performance.now() - timer)
140-
return select_arr(obj, key, ix, key_arr)
93+
return select_arr(obj, ix, key_arr)
14194
}
142-
global.elapsed['select'].ms += (performance.now() - timer)
14395
}
14496

14597
function select_obj(obj, key, key_arr)
14698
{
147-
global.elapsed['select_obj'].n++ // performance monitoring
148-
let timer = performance.now()
99+
// If the instruction is to get an object and there is an array, just select the first item in the array.
100+
// This should be in the map definition, but this is a solution for wonky APIs
101+
if (Array.isArray(obj)) obj = obj[0]
149102

150-
let o = null
151103
if (key in obj) {
152-
if (key_arr.length > 0) {
153-
global.elapsed['select_obj'].ms += (performance.now() - timer)
154-
o = select(obj[key], key_arr)
155-
timer = performance.now()
156-
} else
157-
o = obj[key]
104+
if (key_arr.length > 0)
105+
return select(obj[key], key_arr)
106+
return obj[key]
158107
}
159-
160-
// Nothing left to process - return the object
161-
global.elapsed['select_obj'].ms += (performance.now() - timer)
162-
return o
163108
}
164109

165-
function select_arr(obj, key, ix, key_arr)
110+
function select_arr(arr, ix, key_arr)
166111
{
167-
global.elapsed['select_arr'].n++ // performance monitoring
168-
let timer = performance.now()
169-
170112
// Recursively loop through the array and grab the data
171-
obj = obj.map( function(o) {
172-
global.elapsed['select_arr'].ms += (performance.now() - timer)
173-
let _o = select(o, key_arr.slice())
174-
timer = performance.now()
175-
return _o
113+
arr = arr.map( function(o) {
114+
return (key_arr.length > 0) ? select(o, key_arr.slice()) : o
176115
})
177-
// obj = obj.map( function(o) {
178-
// global.elapsed['select_arr'].ms += (performance.now() - timer)
179-
// return select(o, key_arr.slice())
180-
// timer = performance.now()
181-
// })
182-
global.elapsed['select_arr'].ms += (performance.now() - timer)
183116
// If we are expecting an array, just return what we did
184-
if (ix == '') return obj
117+
if (ix == '') return arr
185118
// If we are looking for a specific array element, just return that
186-
if (ix) return obj[ix]
119+
if (ix) return arr[ix]
187120
// Otherwise, return the results in the first array element
188-
return obj[0]
121+
return arr[0]
189122
}
190123

191124
// Turns a key string (like key1.key2[].key3 into ['key1','key2','[]','key3']...)
192125
//
193126
function parse(key_str, delimiter = '.')
194127
{
195-
global.elapsed['parse'].n++ // performance monitoring
196-
let timer = performance.now()
197128
let _default = null
198129
let _transpose = null
199130

200131
// If the value was passed in object notation, grab the default and transpose values, then rewrite as a string
201-
if (typeof(key) == 'object') {
132+
if (typeof(key_str) == 'object') {
202133
_default = key_str.default
203134
_transpose = key_str.transpose
204135
key_str = key_str.key
@@ -232,50 +163,27 @@ function parse(key_str, delimiter = '.')
232163
keys[n++] = [null, key.substring(begin+1,end)]
233164
}
234165
}
235-
global.elapsed['parse'].ms += (performance.now() - timer)
236-
return keys
166+
167+
return [keys, _default, _transpose]
237168
}
238169

239170
// A string of how to navigate through the incoming array is sent.
240171
// This is translated into an array of instructions for the recursive object
241172
function getKeyValue(obj, key_str)
242173
{
243-
global.elapsed['getKeyValue'].n++ // performance monitoring
244-
let timer = performance.now()
245-
246-
// Convert the mapping string into an array of instructions
247-
// e.g. 'foo.bar[].baz => foo, bar, [], baz
248-
global.elapsed['getKeyValue'].ms += (performance.now() - timer)
249-
let key_arr = parse(key_str)
250-
timer = performance.now()
251-
252-
// Return the selected object
253-
global.elapsed['getKeyValue'].ms += (performance.now() - timer)
254-
return select(obj, key_arr)
174+
return select(obj, parse(key_str) )
255175
}
256176

257177
function setKeyValue(obj, key_str, data)
258178
{
259-
global.elapsed['setKeyValue'].n++ // performance monitoring
260-
let timer = performance.now()
261-
262179
// Key_str is an array of values
263-
if (Array.isArray(key_str)) {
264-
for (let i=0, len=key_str.length; i<len; i++) {
265-
global.elapsed['setKeyValue'].ms += (performance.now() - timer)
266-
let key_arr = parse(to_key_str[i])
267-
obj = update(obj, data, key_arr)
268-
timer = performance.now()
269-
}
270-
} else {
271-
global.elapsed['setKeyValue'].ms += (performance.now() - timer)
272-
let key_arr = parse(key_str)
273-
obj = update(obj, data, key_arr)
274-
timer = performance.now()
275-
}
276-
global.elapsed['setKeyValue'].ms += (performance.now() - timer)
180+
if (Array.isArray(key_str))
181+
for (let i=0, len=key_str.length; i<len; i++)
182+
obj = update(obj, data, parse(key_str[i]) )
183+
else
184+
obj = update(obj, data, parse(key_str) )
185+
277186
return obj
278-
// Nothing else is valid
279187
}
280188

281189
module.exports = ObjectMapper

0 commit comments

Comments
 (0)