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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ <h2>รหัสไปรษณีย์</h2>

<script type="text/javascript">
$.Thailand({
database: 'jquery.Thailand.js/db.zip',
database: 'jquery.Thailand.js/db3.zip',
onComplete: function(){
$('#loader, #address').toggle();
}
Expand Down
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]
}
5 changes: 5 additions & 0 deletions jquery.Thailand.js/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fs = require('fs')
const orig = require('./db.json')

fs.writeFileSync('db2.json', JSON.stringify(orig.map((row => [row.d, row.a, row.p, row.z]))))
console.log('DONE')
Binary file added jquery.Thailand.js/db2.zip
Binary file not shown.
1 change: 1 addition & 0 deletions jquery.Thailand.js/db3.json

Large diffs are not rendered by default.

Binary file added jquery.Thailand.js/db3.zip
Binary file not shown.
9 changes: 6 additions & 3 deletions jquery.Thailand.js/jquery.Thailand.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $.Thailand = function (options) {

options = $.extend({

database: './db.zip',
database: './db2.zip',
autocomplete_size: 20,
onComplete: function () {},

Expand All @@ -35,8 +35,11 @@ $.Thailand = function (options) {
JSZip.loadAsync(data).then(function (zip) {

// unzip db.json then parse into string
zip.file('db.json').async('string').then(function (json) {

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 = {
hint: true,
Expand Down
2 changes: 1 addition & 1 deletion jquery.Thailand.js/jquery.Thailand.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.