-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
315 lines (260 loc) · 6.35 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* eslint-disable max-lines */
'use strict';
const extend = require('extend');
const expand = require('./lib/expand');
const Utils = require('./lib/utils');
const flatten = require('./lib/flatten');
const MODE_FLAT = 'flat';
const MODE_NESTED = 'nested';
const MODES = [MODE_FLAT, MODE_NESTED];
/**
* Object wrapping class.
*/
class Homefront {
/**
* @return {string}
*/
static get MODE_NESTED() {
return MODE_NESTED;
}
/**
* @return {string}
*/
static get MODE_FLAT() {
return MODE_FLAT;
}
/**
* Constructs a new instance of Homefront.
*
* @param {{}} [data] Defaults to empty object.
* @param {String} [mode] Defaults to nested
*/
constructor(data, mode) {
this.data = data || {};
this.setMode(mode);
}
/**
* Recursively merges given sources into data.
*
* @param {...Object} sources One or more, or array of, objects to merge into data (left to right).
*
* @return {Homefront}
*/
merge(sources) {
sources = Array.isArray(sources) ? sources : Array.prototype.slice.call(arguments); //eslint-disable-line prefer-rest-params
let mergeData = [];
sources.forEach(source => {
if (!source) {
return;
}
if (source instanceof Homefront) {
source = source.data;
}
mergeData.push(this.isModeFlat() ? flatten(source) : expand(source));
});
extend.apply(extend, [true, this.data].concat(mergeData));
return this;
}
/**
* Static version of merge, allowing you to merge objects together.
*
* @param {...Object} sources One or more, or array of, objects to merge (left to right).
*
* @return {{}}
*/
static merge(sources) {
sources = Array.isArray(sources) ? sources : Array.prototype.slice.call(arguments); //eslint-disable-line prefer-rest-params
return extend.apply(extend, [true].concat(sources));
}
/**
* Sets the mode.
*
* @param {String} [mode] Defaults to nested.
*
* @returns {Homefront} Fluent interface
*
* @throws {Error}
*/
setMode(mode) {
mode = mode || MODE_NESTED;
if (MODES.indexOf(mode) === -1) {
throw new Error(
`Invalid mode supplied. Must be one of "${MODES.join('" or "')}"`
);
}
this.mode = mode;
return this;
}
/**
* Gets the mode.
*
* @return {String}
*/
getMode() {
return this.mode;
}
/**
* Get data object.
*
* @return {Object}
*/
getData() {
return this.data;
}
/**
* Expands flat object to nested object.
*
* @return {{}}
*/
expand() {
return this.isModeNested() ? this.data : expand(this.data);
}
/**
* Flattens nested object (dot separated keys).
*
* @return {{}}
*/
flatten() {
return this.isModeFlat() ? this.data : flatten(this.data);
}
/**
* Returns whether or not mode is flat.
*
* @return {boolean}
*/
isModeFlat() {
return this.mode === MODE_FLAT;
}
/**
* Returns whether or not mode is nested.
*
* @return {boolean}
*/
isModeNested() {
return this.mode === MODE_NESTED;
}
/**
* Method allowing you to set missing keys (backwards-applied defaults) nested.
*
* @param {String|Array} key
* @param {*} defaults
*
* @returns {Homefront}
*/
applyDefaults(key, defaults) {
return this.put(key, Homefront.merge(defaults, this.fetch(key, {})));
}
/**
* Convenience method. Calls .fetch(), and on null result calls .put() using provided toPut.
*
* @param {String|Array} key
* @param {*} toPut
*
* @return {*}
*/
fetchOrPut(key, toPut) {
let wanted = this.fetch(key);
if (wanted === null) {
wanted = toPut;
this.put(key, toPut);
}
return wanted;
}
/**
* Fetches value of given key.
*
* @param {String|Array} key
* @param {*} [defaultValue] Value to return if key was not found
*
* @returns {*}
*/
fetch(key, defaultValue) {
defaultValue = typeof defaultValue === 'undefined' ? null : defaultValue;
if (typeof this.data[key] !== 'undefined') {
return this.data[key];
}
if (this.isModeFlat()) {
return defaultValue;
}
let keys = Utils.normalizeKey(key);
let lastKey = keys.pop();
let tmp = this.data;
for (let i = 0; i < keys.length; i += 1) {
if (typeof tmp[keys[i]] === 'undefined' || tmp[keys[i]] === null) {
return defaultValue;
}
tmp = tmp[keys[i]];
}
return typeof tmp[lastKey] === 'undefined' ? defaultValue : tmp[lastKey];
}
/**
* Sets value for a key (creates object in path when not found).
*
* @param {String|Array} key Array of key parts, or dot separated key.
* @param {*} value
*
* @returns {Homefront}
*/
put(key, value) {
if (this.isModeFlat() || key.indexOf('.') === -1) {
this.data[key] = value;
return this;
}
let keys = Utils.normalizeKey(key);
let lastKey = keys.pop();
let tmp = this.data;
keys.forEach(val => {
if (typeof tmp[val] === 'undefined') {
tmp[val] = {};
}
tmp = tmp[val];
});
tmp[lastKey] = value;
return this;
}
/**
* Removes value by key.
*
* @param {String} key
*
* @returns {Homefront}
*/
remove(key) {
if (this.isModeFlat() || key.indexOf('.') === -1) {
delete this.data[key];
return this;
}
let normalizedKey = Utils.normalizeKey(key);
let lastKey = normalizedKey.pop();
let source = this.fetch(normalizedKey);
if (typeof source === 'object' && source !== null) {
delete source[lastKey];
}
return this;
}
/**
* Search and return keys and values that match given string.
*
* @param {String|Number} phrase
*
* @returns {Array}
*/
search(phrase) {
let found = [];
let data = this.data;
if (this.isModeNested()) {
data = flatten(this.data);
}
Object.getOwnPropertyNames(data).forEach(key => {
let searchTarget = Array.isArray(data[key]) ? JSON.stringify(data[key]) : data[key];
if (searchTarget.search(phrase) > -1) {
found.push({key: key, value: data[key]});
}
});
return found;
}
}
module.exports.flatten = flatten;
module.exports.expand = expand;
module.exports.Utils = Utils;
module.exports.Homefront = Homefront;