From 9d37074da3374b819d4ce2eff0fd1f983e1a2626 Mon Sep 17 00:00:00 2001 From: Joseph Chamochumbi Date: Fri, 29 Nov 2019 15:55:42 +0100 Subject: [PATCH] feat: use TS and output cjs, es, umd and minified/uglified version --- .gitignore | 2 + JSONCrush.js | 156 ------------------------------------------- index.html | 2 +- package.json | 31 +++++++++ rollup.config.js | 21 ++++++ src/JSONCrush.ts | 105 +++++++++++++++++++++++++++++ src/JSONCrushSwap.ts | 35 ++++++++++ src/JSONUncrush.ts | 23 +++++++ src/index.ts | 4 ++ tsconfig.json | 64 ++++++++++++++++++ yarn.lock | 137 +++++++++++++++++++++++++++++++++++++ 11 files changed, 423 insertions(+), 157 deletions(-) create mode 100644 .gitignore delete mode 100644 JSONCrush.js create mode 100644 package.json create mode 100644 rollup.config.js create mode 100644 src/JSONCrush.ts create mode 100644 src/JSONCrushSwap.ts create mode 100644 src/JSONUncrush.ts create mode 100644 src/index.ts create mode 100644 tsconfig.json create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..76add87 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +dist \ No newline at end of file diff --git a/JSONCrush.js b/JSONCrush.js deleted file mode 100644 index 7f3c765..0000000 --- a/JSONCrush.js +++ /dev/null @@ -1,156 +0,0 @@ -/////////////////////////////////////////////////////////////////////// -// JSONCrush by Frank Force [MIT] https://github.com/KilledByAPixel/JSONCrush -// Based on JSCrush - Javascript crusher by @aivopaas. [MIT] http://www.iteral.com/jscrush -/////////////////////////////////////////////////////////////////////// - -"use strict"; // strict mode - -function JSONCrush(string) -{ - const JSCrush=(string, characters)=> - { - // JSCrush Algorithm (remove repeated substrings) - const ByteLength=string=>encodeURI(string).replace(/%../g,'i').length; - let maxSubstringLength = 50; // speed it up by limiting max length - let X, B, O, m, i, c, e, N, M, o, t, j, x, R; - let Q = characters; - let s = string; - X=1; - m=''; - while(true) - { - for(M=N=e=c=0,i=Q.length;!c&&i--;)!~s.indexOf(Q[i])&&(c=Q[i]); - if(!c) break; - if(O) - { - o={}; - for(x in O) - for(j=s.indexOf(x),o[x]=0;~j;o[x]++)j=s.indexOf(x,j+x.length); - O=o; - } - else for(O=o={},t=1;X&&tM||j==M&&R>N)&&(M=j,N=R,e=x); - if(j<1) - delete O[x] - } - o={}; - for(let x in O) - o[x.split(e).join(c)]=1; - O=o; - if(!e) break; - s=s.split(e).join(c)+c+e - m=c+m; - } - - return {a:s, b:m}; - } - - // remove \u0001 if it is found in the string so it can be used as a delimiter - string = string.replace(/\u0001/g,''); - - // swap out common json characters - string = JSONCrushSwap(string); - - // create a string of characters that will not be escaped by encodeURIComponent - let characters=[]; - const unescapedCharacters = `-_.!~*'()`; - for (let i=127;--i;) - { - if ( - (i>=48&&i<=57) || // 0-9 - (i>=65&&i<=90) || // A-Z - (i>=97&&i<=122)|| // a-z - unescapedCharacters.includes(String.fromCharCode(i)) - ) - characters.push(String.fromCharCode(i)); - } - - // check if every character is used - let allUsed = true; - for(let i in characters) - { - let c = characters[i]; - if (!string.includes(c)) - { - allUsed = false; - break; - } - } - - if (allUsed) - { - // use extended set if all the unescaped ones are used - for (let i=2;i<255;++i) - { - let c = String.fromCharCode(i); - if (c!='\\' && !characters.includes(c)) - characters.unshift(c); - } - } - - // crush with JS crush - let crushed = JSCrush(string, characters); - - // use \u0001 as a delimiter between JSCrush parts - let crushedString = crushed.a + '\u0001' + crushed.b; - - // encode URI - return encodeURIComponent(crushedString); -} - -function JSONUncrush(string) -{ - // string must be a decoded URI component, searchParams.get() does this automatically - - // unsplit the string - let splitString = string.split('\u0001'); - - // JSUncrush algorithm - let a = splitString[0]; - let b = splitString[1]; - for(let c in b) - { - let d = a.split(b[c]); - a=d.join(d.pop()); - } - - // unswap the json characters in reverse direction - return JSONCrushSwap(a, 0); -} - -function JSONCrushSwap(string, forward=true) -{ - // swap out characters for lesser used ones that wont get escaped - const swapGroups = - [ - ['"', "'"], - ["':", "!"], - [",'", "~"], - ['}', ")", '\\', '\\'], - ['{', "(", '\\', '\\'], - ]; - - function Swap(string, g) - { - let regex = new RegExp(`${(g[2]?g[2]:'')+g[0]}|${(g[3]?g[3]:'')+g[1]}`,'g'); - return string.replace(regex, $1 => ( $1 === g[0] ? g[1] : g[0] )); - } - - // need to be able to swap characters in reverse direction for uncrush - if (forward) - for (let i=0; iUncrushed URI Encoded Component:

- +