Last active
August 29, 2015 14:00
-
-
Save nporteschaikin/9b0203b79ed8bba28ba4 to your computer and use it in GitHub Desktop.
String of keys and values ---> Object
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
| /* | |
| * Returns an un-evaluated object from keys and values. | |
| * @param {String} str - a string of keys and values separated by commas, | |
| * | |
| * Example: | |
| * keyStr('firstname: "Noah", lastname: "Portes Chaikin", address: address_var'); | |
| * => Object {firstname: ""Noah"", lastname: ""PortesChaikin"", address: "address_var"} | |
| */ | |
| function keyStr(str) { | |
| var cur = function () { | |
| return str.charAt(0) | |
| } | |
| var advance = function () { | |
| str = str.substr(1).trim() | |
| } | |
| var object = {}; | |
| while (str.length) { | |
| var key = '' | |
| , match; | |
| while (cur() !== ':') { | |
| key += cur(); advance(); | |
| } | |
| advance(); | |
| object[key] = ''; | |
| if (match = /('|")/.exec(cur())) { | |
| object[key] += match[0]; | |
| advance(); | |
| while (cur() !== match[0] && cur()) { | |
| object[key] += cur(); | |
| advance(); | |
| } | |
| object[key] += match[0]; | |
| advance(); | |
| } else { | |
| while (cur() !== ',' && cur()) { | |
| object[key] += cur(); | |
| advance(); | |
| } | |
| } | |
| advance(); | |
| } | |
| return object; | |
| } | |
| /* | |
| * Minified... | |
| * | |
| */ | |
| function keyStr(e){var t=function(){return e.charAt(0)};var n=function(){e=e.substr(1).trim()};var r={};while(e.length){var i="",s;while(t()!==":"){i+=t();n()}n();r[i]="";if(s=/('|")/.exec(t())){r[i]+=s[0];n();while(t()!==s[0]&&t()){r[i]+=t();n()}r[i]+=s[0];n()}else{while(t()!==","&&t()){r[i]+=t();n()}}n()}return r} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment