-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathiterate.js
More file actions
98 lines (69 loc) · 2.34 KB
/
iterate.js
File metadata and controls
98 lines (69 loc) · 2.34 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @function iterate
* Iterate over each (child) item and preserves object structure
*
* @param {*} data Anything you want to iterate over its items
* @param {function} cb function that is called for/on each item
*
* @returns input data (may be modified by callback function)
*
* @example
* ```js
* const _iterate = require(".../helper/iterate.js");
const input = JSON.stringify({
data: true,
timestamp: Date.now(),
array: [1, 2, 3],
obj: {
cool: "Nice ;)",
nested: [null, undefined, true, false, 0, 1]
},
buffer: Buffer.from("Hello World")
});
const output = JSON.parse(input);
console.log(output.buffer)
const modified = _iterate(output, (key, value, type, parent) => {
//console.log(`${key} = ${value}; ${type},; parent = ${parent}`)
// unit tests?
//console.log(value === parent[key]);
// Convert serialized buffer
if (type === "object", value.hasOwnProperty("type") && value.hasOwnProperty("data") && value.type === "Buffer") {
return Buffer.from(value.data);
}
return value;
});
console.log(modified)
* ```
*/
function iterate(data, cb) {
// cb(key, value, type, parent)
// https://stackoverflow.com/a/53106917/5781499
for (let key in data) {
if (data[key] instanceof Buffer) {
// handle buffer seperate
data[key] = cb(key, data[key], "buffer", data);
} else if (data[key] instanceof Array) {
// handle array before objects
// [] instanceof Object = true
data[key] = cb(key, data[key], "array", data);
// iterrate over array
// recurisv call with array entry/item
data[key] = data[key].map((entry) => {
return iterate(entry, cb);
});
} else if (data[key] instanceof Object) {
// call cb on before we iterate over each child
data[key] = cb(key, data[key], "object", data);
// iterrate over data object
// recursiv call with object
data[key] = iterate(data[key], cb);
} else {
// single property reached
// call callback with property type
// (key, value, type, parent) => {};
data[key] = cb(key, data[key], typeof data[key], data);
}
}
return data;
}
module.exports = iterate;