Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db.json change array of objects to array of arrays for more compact #1

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
even more compact db.json
  • Loading branch information
Somsak Sae-Lim authored and Somsak Sae-Lim committed Apr 8, 2017
commit 1b818d4bfb922a0f39220b7266507b7f17441899
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
@@ -101,11 +101,11 @@ <h2>รหัสไปรษณีย์</h2>
<script type="text/javascript" src="jquery.Thailand.js/dependencies/JQL.min.js"></script>
<script type="text/javascript" src="jquery.Thailand.js/dependencies/typeahead.bundle.js"></script>

<script type="text/javascript" src="jquery.Thailand.js/jquery.Thailand2.min.js"></script>
<script type="text/javascript" src="jquery.Thailand.js/jquery.Thailand.js"></script>

<script type="text/javascript">
$.Thailand({
database: 'jquery.Thailand.js/db2.zip',
database: 'jquery.Thailand.js/db3.zip',
onComplete: function(){
$('#loader, #address').toggle();
}
27 changes: 27 additions & 0 deletions jquery.Thailand.js/convert-more.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const fs = require('fs')
const orig = require('./db.json')
let dict = {}
let count = 0
let data = []
orig.forEach((item) => {
data.push([
getIndex(item.d),
getIndex(item.a),
getIndex(item.p),
item.z
])
})
// transpose key, value
let lookup = []
Object.keys(dict).forEach((k) => {
lookup[dict[k]] = k
})
fs.writeFileSync('db3.json', JSON.stringify({ data, lookup }))
console.log('DONE')

function getIndex(word) {
if (!dict[word]) {
dict[word] = count++
}
return dict[word]
}
1 change: 1 addition & 0 deletions jquery.Thailand.js/db3.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions jquery.Thailand.js/jquery.Thailand.js
Original file line number Diff line number Diff line change
@@ -35,9 +35,10 @@ $.Thailand = function (options) {
JSZip.loadAsync(data).then(function (zip) {

// unzip db.json then parse into string
zip.file('db2.json').async('string').then(function (json) {
json = JSON.parse(json).map(function(item) {
return {d: item[0], a: item[1], p: item[2], z: item[3]}
zip.file('db3.json').async('string').then(function (text) {
var obj = JSON.parse(text)
var json = obj.data.map(function(item) {
return {d: obj.lookup[item[0]], a: obj.lookup[item[1]], p: obj.lookup[item[2]], z: item[3]}
})
var DB = new JQL(json), // make json query-able
typehead_options = {