X-Git-Url: https://jasonwoof.com/gitweb/?p=hexbog.git;a=blobdiff_plain;f=compress.coffee;fp=compress.coffee;h=6fc340f90f0cc4016d22ec31ccb730ec2cf6ff54;hp=0000000000000000000000000000000000000000;hb=c5ea138fb8cfce9977ce486c9fbceedebe8af2c4;hpb=2b426ca9467ccfa6ffb5a3e60905650c9f601d7a diff --git a/compress.coffee b/compress.coffee new file mode 100644 index 0000000..6fc340f --- /dev/null +++ b/compress.coffee @@ -0,0 +1,31 @@ +orig_file = 'wordlist.txt' +compressed_file = 'wordlist_compressed.js' +caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +fs = require 'fs' + +fs.readFile orig_file, 'utf8', (err, data) -> + if err? + console.log "Error reading #{orig_file}: #{err}" + process.exit(1) + else + fs.writeFile compressed_file, (compress data.split '\n'), (err) -> + if err? + console.log "Error writing to #{compressed_file}: #{err}" + process.exit(1) + +compress = (words) -> + out = 'parse_word_list([' + chunk = '' + prev = '' + for next in words + match = prev.length + while match and next.substr(0, match) isnt prev.substr(0, match) + --match + # now match is the number of prefix characters this word has in common with the previous + chunk += caps[prev.length - match] + if chunk.length > 2000 + out += "'" + chunk.substr(1) + "'," + chunk = '.' + chunk += next.substr match + prev = next + return out + "'" + chunk.substr(1) + "']);"