-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
250 lines (207 loc) · 8.11 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
'use strict';
module.exports = Delver;
function Delver( obj ) {
this._object = obj;
return this;
}
Delver.prototype.get = function( key, _default ) {
if ( !key ) {
return this._object;
}
return Delver.get( this._object, key, _default );
};
Delver.prototype.set = function( key, value ) {
return Delver.set( this._object, key, value );
};
const isArray = Array.isArray || function( obj ) {
return Object.prototype.toString.call( obj ) === '[object Array]';
};
const arraymatcher = /^(.*?)\[\s*(\d+)?\s*\]$/;
function getSubkey( parts, pos ) {
return parts.slice( 0, pos ).join( '.' );
}
/*******************************************************
* we need a lightweight 'accessor' because Javascript
* sadly does not support pointers to primitives. It would
* be much nicer if the traverser could return a pointer to
* the value you're trying to get/set.
*******************************************************/
function Accessor( obj, key ) {
this._object = obj;
this._key = key;
}
Accessor.prototype.get = function() {
return this._object[ this._key ];
};
Accessor.prototype.set = function( value ) {
this._object[ this._key ] = value;
return this._object[ this._key ];
};
function delve( options ) {
if ( typeof options.object !== 'object' ) {
throw new TypeError( 'Object to access must be an object.' );
}
if ( typeof options.key !== 'string' ) {
throw new TypeError( 'Key to access must be a string.' );
}
let obj = options.object;
const parts = options.key.split( '.' );
for ( let i = 0; i < parts.length; ++i ) {
const isKey = i === parts.length - 1;
const part = parts[ i ];
if ( typeof part !== 'string' || part.length === 0 ) {
throw new Error( 'Key is invalid: ' + options.key );
}
let exists = !!obj && ( options.strict && typeof obj.hasOwnProperty === 'function' ? obj.hasOwnProperty( part ) : typeof obj[ part ] !== 'undefined' );
// if we are not strict and this key part does not exist, stop and return undefined
// if ( !exists && !options.strict ) {
// return undefined;
// }
if ( /\[/.test( part ) ) {
const found = part.match( arraymatcher );
if ( !isArray( found ) || found.length !== 3 ) {
throw new Error( 'Subkey \'' + getSubkey( parts, i ) + '\' is not a valid array accessor. (' + options.key + ')' );
}
const name = found[ 1 ];
let index = found[ 2 ];
if ( name.length === 0 ) {
throw new Error( 'Subkey \'' + getSubkey( parts, i ) + '\' is not a valid key, it must have a name in addition to an array subscript. (' + options.key + ')' );
}
exists = options.strict ? obj.hasOwnProperty( name ) : typeof obj[ name ] !== 'undefined';
if ( index === undefined ) {
if ( options.read ) {
throw new Error( 'Subkey \'' + getSubkey( parts, i ) + '\' is not a valid key, it must have a valid array index when reading. (' + options.key + ')' );
}
index = exists && isArray( obj[ name ] ) ? obj[ name ].length : 0;
}
else {
index = parseInt( index );
}
if ( exists ) {
if ( !isArray( obj[ name ] ) ) {
throw new Error( 'Subkey \'' + getSubkey( parts, i ) + '\' is not valid because \'' + name + '\' is not an array. (' + options.key + ')' );
}
else if ( index > obj[ name ].length - 1 ) {
if ( !options.create ) {
if ( options.read ) {
return undefined;
}
throw new Error( 'Subkey \'' + getSubkey( parts, i ) + '\' is not valid because index \'' + index + '\' is out of bounds and create is not enabled. (' + options.key + ')' );
}
const sizeNeeded = index - obj[ name ].length + 1;
// special case for pushing one item since some libraries override push
if ( sizeNeeded === 1 ) {
// if this is the key, we push the value; however, the value will get overwritten
// by the accessor after the callback. we push the value because some libraries
// expect the value to be of a certain type and pushing null may cause issues
obj[ name ].push( isKey ? options.value : null );
}
else {
obj[ name ] = obj[ name ].concat( new Array( sizeNeeded ) );
}
if ( isKey ) {
return new Accessor( obj[ name ], index );
}
else {
obj = obj[ name ][ index ] = {};
}
}
else {
if ( isKey ) {
return new Accessor( obj[ name ], index );
}
else {
obj = obj[ name ][ index ];
}
}
}
else if ( options.create ) {
obj[ name ] = new Array( index + 1 );
if ( isKey ) {
return new Accessor( obj[ name ], index );
}
else {
obj = obj[ name ][ index ] = {};
}
}
else {
if ( options.read ) {
return undefined;
}
else {
throw new Error( 'Subkey \'' + getSubkey( parts, i ) + '\' is not valid because it does not exist and create is not enabled. (' + options.key + ')' );
}
}
continue;
}
else if ( typeof obj !== 'object' ) {
throw new Error( 'Part \'' + getSubkey( parts, i ) + '.\' of the given key is not an object.' );
}
else if ( isArray( obj ) ) {
throw new Error( 'Part \'' + getSubkey( parts, i ) + '.\' of the given key is an array, but has no index accessor ([]).' );
}
else if ( !exists ) {
if ( isKey ) {
if ( options.read ) {
return undefined;
}
return new Accessor( obj, part );
}
if ( options.read ) {
return undefined;
}
if ( !options.create ) {
throw new Error( 'Subkey \'' + getSubkey( parts, i ) + '\' is not valid because it does not exist and create is not enabled. (' + options.key + ')' );
}
obj = obj[ part ] = {};
}
else {
if ( isKey ) {
return new Accessor( obj, part );
}
else {
obj = obj[ part ];
}
}
}
throw Error( 'Failed to resolve: ' + options.key );
}
Delver.get = function( obj, key, _default ) {
let strict = true;
if ( typeof key === 'undefined' ) {
key = obj.key;
strict = typeof obj.strict !== 'undefined' ? obj.strict : strict;
_default = obj._default;
obj = obj.object;
}
let accessor = delve( {
read: true,
strict: strict,
object: obj,
key: key
} );
if ( accessor === undefined ) {
return _default;
}
return accessor.get();
};
Delver.set = function( obj, key, val, create ) {
let strict = true;
let _constructor = null;
if ( typeof key === 'undefined' ) {
key = obj.key;
val = obj.value;
create = obj.create;
strict = typeof obj.strict !== 'undefined' ? obj.strict : strict;
_constructor = obj._constructor;
obj = obj.object;
}
let accessor = delve( {
strict: strict,
create: typeof create !== 'undefined' ? create : true,
object: obj,
key: key,
value: val
} );
return accessor.set( _constructor ? _constructor( val ) : val );
};