Skip to content

Instantly share code, notes, and snippets.

@schovi
Last active December 11, 2015 01:39
Show Gist options
  • Save schovi/4525266 to your computer and use it in GitHub Desktop.
Save schovi/4525266 to your computer and use it in GitHub Desktop.
Helper for not so common case, where your nested object is flatten into lot of key -> val. In each pair key is created by join all keys in chain with dot. {'index.type':1} is {index: {type: 1}} and so. Works with arrays too. {'array.0':"a",'array.1':"b"} became {array:["a","b"]}. This is used in some parts of elasticsearch json http api.
// jsFiddle example
// http://jsfiddle.net/k7Fgt/1/
stringifiedObject = {
"index.analysis.analyzer.standard_lower_ascii.type" : "custom",
"index.refresh_interval" : "60s",
"index.analysis.analyzer.standard_lower_ascii.filter.0" : "standard",
"index.analysis.analyzer.remove_whitespaces.filter.1" : "remove_whitespaces",
"index.analysis.filter.remove_whitespaces.type" : "pattern_replace",
"index.number_of_shards" : "4",
"index.analysis.analyzer.standard_lower_ascii.filter.2" : "asciifolding",
"index.analysis.filter.remove_whitespaces.replacement" : "",
"index.analysis.analyzer.standard_lower_ascii.filter.1" : "lowercase",
"index.analysis.analyzer.remove_whitespaces.type" : "custom",
"index.analysis.filter.remove_whitespaces.pattern" : "\\s",
"index.analysis.analyzer.remove_whitespaces.filter.0" : "standard",
"index.analysis.analyzer.standard_lower_ascii.tokenizer" : "standard",
"index.analysis.analyzer.remove_whitespaces.tokenizer" : "keyword",
"index.number_of_replicas" : "2",
"index.action.auto_create_index" : "false",
"index.version.created" : "200051",
"index.array_with_object.0.a": "a",
"index.array_with_object.0.b": "b",
"index.array_with_object.1.a": "a",
"index.array_with_object.1.c": "c"
}
o = unstringifiedObject = unstringifyObject(stringifiedObject);
console.log(o)
console.log("simple value", o.index.refresh_interval == "60s")
console.log("deep nested value", o.index.analysis.analyzer.standard_lower_ascii.type == "custom")
console.log("deep nested array with value", o.index.analysis.analyzer.standard_lower_ascii.filter[1] == "lowercase")
console.log("array with object and value in it ", o.index.array_with_object[1].c == "c")
unstringifyObject = (function() {
var objToString = function(obj) {
return Object.prototype.toString.call(obj)
},
isArray = function(obj) {
return objToString(obj) === "[object Array]"
},
isPlainObject = function(obj) {
return objToString(obj) === "[object Object]"
},
processKeyNames = function(tmpResult, keyForAssign, keyNames, value) {
var currentKeyName = keyNames[0],
currentValue = tmpResult && tmpResult[keyForAssign],
nextResult = tmpResult;
if(!currentKeyName) {
tmpResult[keyForAssign] = value;
return;
}
if(parseInt(currentKeyName) >= 0) {
if(keyForAssign === undefined) {
if(tmpResult === undefined) {
nextResult = tmpResult = []
} else if(!isArray(tmpResult)) {
throw("Type mismatch in generating object. Expecting Array")
}
} else if(tmpResult[keyForAssign] === undefined) {
nextResult = tmpResult[keyForAssign] = []
} else if(currentValue !== undefined && !isArray(currentValue)) {
throw("Type mismatch in generating object. Expecting Array")
} else {
nextResult = tmpResult[keyForAssign]
}
} else {
if(keyForAssign === undefined) {
if(tmpResult === undefined) {
nextResult = tmpResult = {}
} else if(!isPlainObject(tmpResult)) {
throw("Type mismatch in generating object. Expecting Object")
}
} else if(tmpResult[keyForAssign] === undefined){
nextResult = tmpResult[keyForAssign] = {}
} else if(currentValue !== undefined && !isPlainObject(currentValue)) {
throw("Type mismatch in generating object. Expecting Object");
} else {
nextResult = tmpResult[keyForAssign]
}
}
processKeyNames(nextResult, currentKeyName, keyNames.slice(1), value)
return tmpResult;
},
main = function(obj) {
if(isPlainObject(obj)) {
var result = undefined,
key;
for(key in obj) {
if(Object.prototype.hasOwnProperty.call(obj, key)) {
var value = obj[key];
result = processKeyNames(result, undefined, key.split("."), value);
}
}
return result
} else {
throw "Can't unstringify non Plain object"
}
};
return main;
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment