Last active
January 7, 2019 23:21
-
-
Save AaronHarris/28173a18738b855b7bfebde07becb586 to your computer and use it in GitHub Desktop.
A module that shortens JSON to a number of bytes while keeping syntactically valid JSON. Useful for when apis can only accept a certain byte length for input. Only works for NODE but can be ported to browsers. WIP.
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 getBytes(string) { | |
return Buffer.byteLength(String(string), 'utf8'); | |
} | |
function trimToBytes(string, diff) { | |
return Buffer.alloc(diff, string, 'utf8').toString(); | |
} | |
// This is only intended atm for flat objects with string values | |
// Does not handle edge cases | |
// Push each item / level onto stack | |
// reverse stack anf append opposite | |
function limitReplacer(maxBytes) { | |
const size = 2; // {} or [] | |
const replacer = function (key, value) { | |
if (size >= maxBytes) { | |
return; | |
} | |
size += getBytes('"":,'); | |
if (!Array.isArray(this)) { | |
size += getBytes(key); | |
} | |
const valBytes = getBytes(value); | |
if (size + valBytes < maxBytes) { | |
size += valBytes; | |
return value; | |
} else { | |
const diff = maxBytes - size; | |
const truncatedValue = trimToBytes(string, diff); | |
size += diff; | |
return truncatedValue; | |
} | |
} | |
return replacer; | |
} | |
// problems to solve | |
// Object.keys() if value is object to determine if keys alone + 1 exceed limit | |
// Either raise if not all keys can be included even if every value is truncated, or start kicking off keys starting with the last | |
// test of different types | |
var foo = JSON.stringify({ a: 'foo', b: 5, c: 'hello', d: { e: 'f'}, g: [new String(1), Number(2), 3, Symbol(), new Boolean(0)] }, function(k, v) { | |
console.log( k, v, this, typeof v, Object.keys(v)); | |
return v; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment