JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
gets harder (way too slowly) as you play
[hexbog.git] / compress.coffee
1 orig_file = 'wordlist.txt'
2 compressed_file = 'wordlist_compressed.js'
3 caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
4 fs = require 'fs'
5
6 fs.readFile orig_file, 'utf8', (err, data) ->
7         if err?
8                 console.log "Error reading #{orig_file}: #{err}"
9                 process.exit(1)
10         else
11                 fs.writeFile compressed_file, (compress data.split '\n'), (err) ->
12                         if err?
13                                 console.log "Error writing to #{compressed_file}: #{err}"
14                                 process.exit(1)
15
16 compress = (words) ->
17         out = 'parse_word_list(['
18         chunk = ''
19         prev = ''
20         for next in words
21                 match = prev.length
22                 while match and next.substr(0, match) isnt prev.substr(0, match)
23                         --match
24                 # now match is the number of prefix characters this word has in common with the previous
25                 chunk += caps[prev.length - match]
26                 if chunk.length > 2000
27                         out += "'" + chunk.substr(1) + "',"
28                         chunk = '.'
29                 chunk += next.substr match
30                 prev = next
31         return out + "'" + chunk.substr(1) + "']);"