-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
253 lines (225 loc) · 6.02 KB
/
Copy pathindex.js
File metadata and controls
253 lines (225 loc) · 6.02 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
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
"use strict";
var retrieveChild = require('retrieve-object-child')
, inherits = require('util').inherits
, EventEmitter = require('events').EventEmitter;
/**
* This points to a certain position inside an object and allows you to read and
* modify everything nested inside that position.
*
* @param {Object|Pointer} root - The root object of the Pointer
* @param {null|string|array} location - The location inside the root object
*/
var Pointer = function Pointer(root, location) {
this.setRoot(root, location);
};
inherits(Pointer, EventEmitter);
Pointer.prototype._parseLocation = function(key) {
if (key instanceof Array) {
var r = [];
for (var i = 0; i < key.length; i++) {
r[i] = key[i];
}
return r;
} else if (typeof key === "string") {
return [key];
} else if (!key) {
return [];
} else {
throw new TypeError("Key pointer must be either a string or array");
}
};
Pointer.prototype._refreshRoot = function() {
this._pointerRoot = undefined;
this._pointerRoot = this.getLocationObject();
};
Pointer.prototype._unhookPointer = function() {
if (this._masterPointer) {
this._root.removeListener('changed', this._pointerHook);
}
};
Pointer.prototype._hookPointer = function(pointer) {
this._pointerHook = function() {
this._refreshRoot();
}.bind(this);
pointer.on('changed', this._pointerHook);
};
/**
* This sets the root location of the Pointer
*
* @param {Object|Pointer} root - The root object of the Pointer
* @param {null|string|array} location - The location inside the root object
*/
Pointer.prototype.setRoot = function(obj, loc) {
if (!(obj instanceof Object)) {
throw new TypeError("Expecting an instance of object");
}
// unhook the previous pointer
if (obj instanceof Pointer) {
this._unhookPointer();
this._hookPointer(obj);
this._masterPointer = true;
} else {
this._masterPointer = false;
}
this._root = obj;
this.setLocation(loc, true);
this._refreshRoot();
this.emit('changed');
};
/**
* Returns the root object recursively
*/
Pointer.prototype.getRoot = function() {
if (this._masterPointer) {
return this._root.getRoot();
} else {
return this._root;
}
};
/**
* Sets the location inside the master object to which this pointer points.
* @param {null|string|array} location - The location inside the root object
*/
Pointer.prototype.setLocation = function(loc, suppress) {
if (loc)
this._loc = this._parseLocation(loc);
else
this._loc = [];
this._refreshRoot();
if (!suppress)
this.emit('changed');
};
/**
* This returns the root of the Pointer instance.
* @param {boolean} force - If root object doesn't exist, create it.
*/
Pointer.prototype.getLocationObject = function(force) {
if (this._pointerRoot) {
return this._pointerRoot;
} else {
if (this._masterPointer) {
if (force) {
// Must return the root object
var obj = this._root.getLocationObject(true);
var c = retrieveChild(obj, this._loc, force);
if (c instanceof Object) {
this._pointerRoot = c;
return c;
} else {
return undefined;
}
} else {
// Must return the root object or undefined
var obj = this._root.getLocationObject();
if (obj === undefined)
return obj;
var c = retrieveChild(obj, this._loc, force);
if (c === undefined)
return c;
if (c instanceof Object) {
this._pointerRoot = c;
return c;
} else {
return undefined;
}
}
} else {
var c = retrieveChild(this._root, this._loc, force);
if (c instanceof Object) {
this._pointerRoot = c;
return c;
} else {
return undefined;
}
}
}
};
/**
* This creates the nested objects up until the given location, and returns the
* object.
*
* @param {null|string|array} location
* @return {Object}
*/
Pointer.prototype.create = function(location) {
location = this._parseLocation(location);
return retrieveChild(this.getLocationObject(true), location, true);
};
/**
* Will set a value to the pointed object.
*
* @param {null|string|array} location - The location of the key, the final
* element is the actual key.
* @param {[type]} value
*/
Pointer.prototype.set = function(location, value) {
location = this._parseLocation(location);
if (location.length > 1) {
var k = location.pop();
var el = this.create(location);
el[k] = value;
} else if (location.length === 1) {
this.getLocationObject(true)[location[0]] = value;
} else {
throw new Error('Cannot set the root property');
}
};
/**
* Will return a value from the pointed object
*
* @param {null|string|array} location
* @param {mixed} defaultValue - The default value to return if value not
* found.
* @return {mixed}
*/
Pointer.prototype.get = function(location, defaultValue) {
location = this._parseLocation(location);
var obj = this.getLocationObject();
if (obj === undefined)
return defaultValue;
if (location.length > 0) {
var k = location.pop();
if (location.length > 0) {
obj = retrieveChild(obj, location);
if (obj === undefined)
return defaultValue;
}
if (obj[k] === undefined) {
return defaultValue;
} else {
return obj[k];
}
} else {
return obj;
}
};
/**
* Deletes the value at the given location.
*
* @param {string|array} location
*/
Pointer.prototype.clear= function(location) {
location = this._parseLocation(location);
if (location.length === 0) {
throw new Error("Clear must at least have one level of depth");
} else {
if (location.length === 1) {
var k = location[0];
var o = this.getLocationObject();
if (o && o[k]) {
delete o[k];
}
} else {
var k = location.pop();
var o = this.get(location);
if (o && o[k]) {
delete o[k];
for (var key in o) {
return;
}
this.clear(location);
}
}
}
};
module.exports = Pointer;