This repository was archived by the owner on Dec 5, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathly.js
66 lines (56 loc) · 1.84 KB
/
pathly.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
(function () {
'use strict';
function compress(a) {
var n = [], i;
for (i = 0; i <= a.length; i += 1) { if (a[i]) { n.push(a[i]); } }
return n;
}
function merge(a, b) {
if (a.substring) { return b; }
var p;
for (p in b) {
if (b.hasOwnProperty(p)) {
if (!a[p]) {
// Add if not found
a[p] = b[p];
} else {
// If found, merge again
a[p] = merge(a[p], b[p]);
}
}
}
return a.join ? compress(a) : a;
}
function main(obj, path, value, opts) {
var tempObj,
key,
delimiter = (opts && opts.delimiter) || '.',
emptyKey = (opts && opts.emptyKey) || 'undefined';
path = path.replace(/\[(\d)\]/g, '.$1').split(delimiter);
//replace empty keys with `emptyKey`
path = path.map(function (item) { return item || emptyKey; });
while (key = path.pop()) {
tempObj = isNaN(key) ? {} : [];
if ((opts || {}).toNative !== false) {
if (!isNaN(value)) { value = parseFloat(value, 10); }
if (value === 'true') { value = true; }
if (value === 'false') { value = false; }
}
if (!value) { break; }
tempObj[key] = value;
value = tempObj;
if (!path.length) { merge(obj, tempObj); }
}
}
function init() {
/*global define, module*/
if (typeof define === 'function' && define.amd) {
define(function () { return main; });
} else if (typeof module === 'object' && module.exports) {
module.exports = main;
} else {
this.pathly = main;
}
}
init();
}());