Skip to content

Commit

Permalink
Further compress json data
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopding committed Dec 18, 2018
1 parent 80cf205 commit d97bf46
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ es/
lib/
unicode-properties.js
unicode-properties.min.js
src/data.json
src/trie.json
8 changes: 4 additions & 4 deletions Makefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ target.generateTrieJson = () => {
mkdir('lib')
mkdir('dist')
exec('babel-node src/generate.js');
cp('data.json', 'trie.json', 'src');
cp('data.json', 'trie.json', 'es');
cp('data.json', 'trie.json', 'lib');
cp('src/data.json', 'src/trie.json', 'es');
cp('src/data.json', 'src/trie.json', 'lib');
};

target.compileBabel = () => {
target.generateTrieJson();
env.MODULE_TYPE = 'es6';
exec(`babel src/index.js --out-dir es`);
env.MODULE_TYPE = 'commonjs';
Expand All @@ -44,5 +44,5 @@ target.rollupUMDMin = () => {
};

target.clean = () => {
rm('-rf', 'data.json', 'trie.json', 'src/data.json', 'src/trie.json', 'dist', 'lib', 'es');
rm('-rf', 'src/data.json', 'src/trie.json', 'dist', 'lib', 'es');
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"unpkg": "dist/unicode-properties.min.js",
"module": "es/index.js",
"dependencies": {
"base64-arraybuffer": "^0.1.5",
"pako": "^1.0.6",
"unicode-trie": "^0.3.0"
},
"devDependencies": {
Expand Down
15 changes: 12 additions & 3 deletions src/generate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const codePoints = require('codepoints');
const fs = require('fs');
const UnicodeTrieBuilder = require('unicode-trie/builder');
const pako = require('pako');
const base64 = require('base64-arraybuffer');

const log2 = Math.log2 || (n => Math.log(n) / Math.LN2);

Expand Down Expand Up @@ -96,11 +98,18 @@ for (codePoint of Array.from(codePoints)) {

// Trie is serialized suboptimally as JSON so it can be loaded via require,
// allowing unicode-properties to work in the browser
fs.writeFileSync('./trie.json', JSON.stringify(trie.toBuffer()));
const trieFilePath = __dirname + '/trie.json';
const jsonBase64DeflatedTrie = JSON.stringify(base64.encode(pako.deflate(trie.toBuffer())));
fs.writeFileSync(trieFilePath, jsonBase64DeflatedTrie);

fs.writeFileSync('./data.json', JSON.stringify({
const data = {
categories: Object.keys(categories),
combiningClasses: Object.keys(combiningClasses),
scripts: Object.keys(scripts),
eaw: Object.keys(eaws)
}));
};

const dataFilePath = __dirname + '/data.json';
const dataJsonBytes = JSON.stringify(data).split('').map(c => c.charCodeAt(0));
const jsonBase64DeflatedData = JSON.stringify(base64.encode(pako.deflate(dataJsonBytes)));
fs.writeFileSync(dataFilePath, jsonBase64DeflatedData);
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import UnicodeTrie from 'unicode-trie';
import data from './data.json';
import pako from 'pako';
import * as base64 from 'base64-arraybuffer';

import base64DeflatedData from './data.json';
import base64DeflatedTrie from './trie.json';

// Trie is serialized as a Buffer in node, but here
// we may be running in a browser so we make an Uint8Array
const data = JSON.parse(String.fromCharCode(...pako.inflate(base64.decode(base64DeflatedData))));
const trieData = pako.inflate(base64.decode(base64DeflatedTrie));

import trieBuffer from './trie.json';
const trieData = new Uint8Array(trieBuffer.data);
const trie = new UnicodeTrie(trieData);

const log2 = Math.log2 || (n => Math.log(n) / Math.LN2);
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,11 @@ balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"

base64-arraybuffer@^0.1.5:
version "0.1.5"
resolved "https://registry.yarnpkg.com/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz#73926771923b5a19747ad666aa5cd4bf9c6e9ce8"
integrity sha1-c5JncZI7Whl0etZmqlzUv5xunOg=

base@^0.11.1:
version "0.11.2"
resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"
Expand Down Expand Up @@ -1975,6 +1980,11 @@ pako@^0.2.5:
version "0.2.9"
resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"

pako@^1.0.6:
version "1.0.7"
resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.7.tgz#2473439021b57f1516c82f58be7275ad8ef1bb27"
integrity sha512-3HNK5tW4x8o5mO8RuHZp3Ydw9icZXx0RANAOMzlMzx7LVXhMJ4mo3MOBpzyd7r/+RUu8BmndP47LXT+vzjtWcQ==

parse-glob@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/parse-glob/-/parse-glob-3.0.4.tgz#b2c376cfb11f35513badd173ef0bb6e3a388391c"
Expand Down

0 comments on commit d97bf46

Please sign in to comment.