Created
April 25, 2012 09:22
-
-
Save eivindingebrigtsen/2488440 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var objectify = function () { | |
var glob = typeof arguments[0] == 'object' ? arguments[0] : window; | |
var a, b, i, l, n,v, ext = arguments; | |
for (a in ext) { | |
i = ext[a]; | |
l = null, n = null; | |
if (typeof i == 'string') { | |
if (i.match('.')) { | |
l = i.split('.'); | |
for (b in l) { | |
v = null; | |
v = l[b].split('='); | |
if(v.length>1){ | |
l[b] = v[0]; | |
} | |
if (n && l[b] in n) { | |
n = n[l[b]]; | |
continue; | |
} else if (b == 0 && l[b] in glob) { | |
n = glob[l[b]]; | |
continue; | |
} | |
if(v[1]){ | |
n[l[b]] = v[1]; | |
continue; | |
} | |
n = (n ? n[l[b]] = {} : glob[l[b]] = {}); | |
} | |
} | |
} | |
} | |
return glob; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var OBJ = {}; | |
objectify( OBJ, | |
'pension.plan.year1.total.payout=100000', | |
'insurance.private.house', | |
'bank.accounts.transactions.march.length=34', | |
'pension.payout.now=10' | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"pension": { | |
"plan": { | |
"year1": { | |
"total": { | |
"payout": "100000" | |
} | |
} | |
}, | |
"payout": { | |
"now": "10" | |
} | |
}, | |
"insurance": { | |
"private": { | |
"house": {} | |
} | |
}, | |
"bank": { | |
"accounts": { | |
"transactions": { | |
"march": { | |
"length": "34" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment