Skip to content

Instantly share code, notes, and snippets.

@nkt
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkt/42a2c7e0d9792535aaf7 to your computer and use it in GitHub Desktop.
Save nkt/42a2c7e0d9792535aaf7 to your computer and use it in GitHub Desktop.
REST response compressing
function compress(objects) {
const fieldsSet = {};
for (let i = 0; i < objects.length; i++) {
const keys = Object.keys(objects[i]);
for (let j = 0; j < keys.length; j++) {
fieldsSet[keys[j]] = 1;
}
}
const fields = Object.keys(fieldsSet);
const entries = [];
for (let i = 0; i < objects.length; i++) {
const obj = objects[i];
const entry = [];
for (let j = 0; j < fields.length; j++) {
entry.push(obj[fields[j]]);
}
entries.push(entry);
}
return {
fields,
entries
};
}
function decompress({fields, entries}) {
const objects = [];
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
const obj = {};
for (let j = 0; j < fields.length; j++) {
obj[fields[j]] = entry[j];
}
objects.push(obj);
}
return objects;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment