Skip to content

Instantly share code, notes, and snippets.

@theaccordance
Last active October 12, 2016 14:12
Show Gist options
  • Select an option

  • Save theaccordance/47e7a48ff40c36449ae671518a226c07 to your computer and use it in GitHub Desktop.

Select an option

Save theaccordance/47e7a48ff40c36449ae671518a226c07 to your computer and use it in GitHub Desktop.
var source = {
"alpha": "string alpha",
"bravo": {
"charlie": "string bravo.charlie",
"delta": "string bravo.delta",
"echo": {
"foxtrot": 'string bravo.echo.foxtrot',
"gamma": 'string bravo.echo.gamma',
"hotel": "string bravo.echo.hotel"
}
}
},
dictionary = {},
pointer = 'a';
function nextChar(a) {
return String.fromCharCode(a.charCodeAt() + 1);
}
function getPointer(string) {
pointer = nextChar(pointer);
dictionary[pointer] = string;
return ['@', pointer].join(':');
}
function minifyKeys(obj) {
var key,
has = Object.prototype.hasOwnProperty.bind(obj);
for (key in obj) {
if (has(key)) {
switch (typeof obj[key]) {
case 'object':
minifyKeys(obj[key]);
break;
case 'string':
obj[key] = getPointer(obj[key]);
}
}
}
}
minifyKeys(source);
setTimeout(function () {
console.log(source);
console.log(dictionary);
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment