Last active
August 29, 2015 14:25
-
-
Save nkt/42a2c7e0d9792535aaf7 to your computer and use it in GitHub Desktop.
REST response compressing
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
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 | |
}; | |
} |
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
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