-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
walkObject.js
168 lines (152 loc) · 5.69 KB
/
walkObject.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
export function isPlainObject(input) {
return input && !Array.isArray(input) && typeof input === 'object';
}
/*
* Helper function for _walkObject
*/
function doArrayWalk(key, value, walkArrays, walkArraysMatchingKeys) {
if (!Array.isArray(value)) {
return false;
}
if (walkArrays || (walkArraysMatchingKeys && walkArraysMatchingKeys.includes(key))) {
return true;
}
return false;
}
/*
* Helper utility for path accumulation in _walkObject. Supports array
* path accumulation and also Plotly.js nestedProperty style.
*/
function getPath(pathType = 'array', _path) {
return {
_path: _path || (pathType === 'array' ? [] : ''),
set(parent, key) {
let nextPath;
switch (pathType) {
case 'array':
nextPath = this._path.concat([key]);
break;
case 'nestedProperty':
if (this._path.length === 0) {
nextPath = key;
} else if (Array.isArray(parent)) {
nextPath = this._path + `[${key}]`;
} else {
nextPath = this._path + '.' + key;
}
break;
default:
throw new Error('unrecognized pathType ' + pathType);
}
return getPath(pathType, nextPath);
},
get(parent, key) {
// in array mode we do not return the leaf node key.
if (pathType === 'array') {
return this._path;
}
// in nestedProperty mode we return full path including final key
return this.set(parent, key)._path;
},
};
}
/*
* Helper function that transforms an array of path parts into a single path.
* For example:
*
* ['_fullData', 0, 'transforms', 3, 'type'] => 'transforms[3].type'
*
* Note that it strips out the _fullData part (and also _fullInput) since that's
* usually present in the attribute path but isn't necessary in the attribute
* string since it's usually implicitly applied through the userDataIndex.
*/
export function makeAttrSetterPath(parts) {
let path = '';
// Truncate the leading parts that aren't intersting when applying changes:
let i0 = 0;
if (parts[i0] === '_fullData') {
i0 += 2;
}
if (parts[i0] === '_fullInput') {
i0++;
}
if (parts[i0] === '_fullLayout') {
i0++;
}
for (let i = i0; i < parts.length; i++) {
if (typeof parts[i] === 'number' || Array.isArray(parts[i])) {
path += '[' + (Array.isArray(parts[i]) ? parts[i][0] : parts[i]) + ']';
} else {
path += (i > i0 ? '.' : '') + parts[i];
}
}
return path;
}
/**
* The function that walkObject calls at each node.
*
* @callback walkObjectCallback
* @param {string|number} key The current key, which may be nested.
* @param {object} parent The object which owns the 'key' as a prop.
* @param {Array} path The keys that lead to the 'parent' object.
* @returns {boolean} True if the value at 'key' should *not* be traversed into
* if it happens to be an object. I.e., you don't need to
* return anything if you want the default traversal of the
* whole object.
*/
/**
* Walks through object and recurses if necessary.
*
* @param {object} object The top-level or nested object we're walking through.
* @param {walkObjectCallback} callback Called at each object node.
* @param {Array} path The keys that lead from to top-level object to this one.
* @param {object} config configuration object
* @param {string} config.walkArrays flag allowing array walking
* @param {Array} config.walkArraysMatchingKeys An array of keys permitting
* array walking
* @param {string} config.pathType Either 'array' or 'nestedProperty'. Array
* based paths return string keys in an array up
* until the current key position.
* NestedProperty style returns a single
* concatenated "nestedProperty" style string.
* @returns {void}
* @private
*/
function _walkObject(object, callback, path, config) {
const {walkArrays, walkArraysMatchingKeys} = config;
Object.keys(object).forEach(key => {
// Callback can force traversal to stop by returning `true`.
if (callback(key, object, path.get(object, key))) {
return;
}
const value = object[key];
if (isPlainObject(value) || doArrayWalk(key, value, walkArrays, walkArraysMatchingKeys)) {
_walkObject(value, callback, path.set(object, key), config);
}
});
}
/**
* General function to walk object and call the given callback for each node.
*
* @param {Object|Array} input The object or array we want to walk.
* @param {walkObjectCallback} callback Called at each object node.
* @param {Object} [config] configuration object
* @param {Boolean} [config.walkArrays] flag allowing array walking
* @param {Array} [config.walkArraysMatchingKeys] An array of keys permitting
* array walking
* @param {String} [config.pathType] Either 'array' or 'nestedProperty'. Array
* based paths return string keys in an array
* up until the current key position.
* NestedProperty style returns a single
* concatenated "nestedProperty" style string
* with the current key included in the path.
* Defaults to "array"
* @returns {void}
*/
export default function walkObject(input, callback, config = {}) {
if (!isPlainObject(input) && !Array.isArray(input)) {
throw new Error('The input must be an object.');
}
var path = getPath(config.pathType);
_walkObject(input, callback, path, config);
}