Commit 82a39aa1 authored by Aral Balkan's avatar Aral Balkan
Browse files

Cleaned up the source. Renamed the file. Ready for release.

parent 5ba57e58
node_modules
.DS_Store
secret.txt
secret.salt
codes.json
\ No newline at end of file
#!/bin/bash
set -e
node_modules/coffee-script/bin/coffee h2c2.coffee $1
################################################################################
#
# Hitchhiker’s Codes to the Galaxy
#
# Creates unique codes that include two random words from the
# Hitchhiker’s Guide to the Galaxy and a hashid.
#
# See readme.md for usage details.
#
# Copyright © 2015 Aral Balkan. Released with ♥ by Ind.ie under the MIT License.
#
################################################################################
assert = require 'assert'
fs = require 'fs'
path = require 'path'
Picker = (require 'RandomSelection').Picker
Hashids = require 'hashids'
secret = 'M&dGJ=Vv%H7gV@6Giv(pk7v38aMQ7G;vUNm2^Vb@$Ep9i7P9}r'
secret = fs.readFileSync path.join process.cwd(), 'secret.salt'
hashids = new Hashids secret
#
# Load all the unique words from The Hitchhiker’s Guide to the Galaxy
#
hhgttg = JSON.parse(fs.readFileSync('all-unique-words-in-the-hitchhikers-guide.json'))
hhgttg = JSON.parse fs.readFileSync 'all-unique-words-in-the-hitchhikers-guide.json'
hhgttgPicker = new Picker(hhgttg)
hhgttgPicker = new Picker hhgttg
#
# Implementation of hash sieve algorithm that I read
......@@ -44,31 +58,33 @@ generateCodes = (fromId, toId) ->
for i in [fromId..toId]
count++
process.stdout.write 'Generating code #' + count + ' (ID: '+i+') \r'
process.stdout.write "Generating code ##{count} (ID: #{i}) \r"
code = hashids.encode i
code = hhgttgPicker.pick().toLowerCase() + '-' + hhgttgPicker.pick().toLowerCase() + '-' + code
code = "#{hhgttgPicker.pick().toLowerCase()}-#{hhgttgPicker.pick().toLowerCase()}-#{code}"
codes.push code
codes = codes.unique()
assert.equal numCodes, codes.length, 'All codes should be unique.'
console.log 'Generated ' + numCodes + ' codes (from ID ' + fromId + ' to ID ' + toId + ')'
console.log "Generated #{numCodes} codes (from ID #{fromId} to ID #{toId})."
return codes
generateCodesAndSaveToFile = (fromId, toId, fileName) =>
codes = generateCodes fromId, toId
filePath = path.join process.cwd(), fileName + '.json'
filePath = path.join process.cwd(), fileName
fs.writeFileSync filePath, JSON.stringify codes
#
# Generate waystone codes
#
tenThousandCodes = generateCodesAndSaveToFile 1, 1000000, 'codes.json'
args = process.argv
if args.count != 3
console.log 'Usage: ./h2c2 <number of codes to generate>'
process.exit
numberOfCodesToGenerate = args[2]
generateCodesAndSaveToFile 0, numberOfCodesToGenerate-1, 'codes.json'
{
"name": "waystone-codes",
"version": "1.0.0",
"description": "Generates codes for Waystone",
"description": "Generates unique codes using words from HHGTTG and hashids.",
"main": "generate-codes.coffee",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
......@@ -10,13 +10,16 @@
"]"
],
"author": "Aral Balkan",
"license": "Closed Source (this needs to be private)",
"license": "MIT",
"dependencies": {
"RandomSelection": "^1.0.1",
"assert": "^1.1.2",
"RandomSelection": "1.0.1",
"assert": "1.1.2",
"hashids": "1.0.1",
"random-word": "^1.0.0",
"random-word": "1.0.0",
"shorthash": "0.0.2",
"uuid": "^2.0.1"
"uuid": "2.0.1"
},
"devDependencies": {
"coffee-script": "1.9.3"
}
}
# Hitchhiker’s Codes to the Galaxy
Creates unique codes that include two random words from the [Hitchhiker’s Guide to the Galaxy][1] and a [hashid][2].
Why not just hashids? 42.
## Usage
0. `npm install` to install the Node.js dependencies.
1. Create a file called `secret.salt`.
2. Put your secret salt in there (this can be any string).
h2c2 <numberOfCodesYouWantToGenerate>
Codes will be saved in a file called `codes.json`.
## License
Copyright © 2015, Aral Balkan. Released with ♥ by Ind.ie under the MIT License.
[1]:https://en.wikipedia.org/wiki/The_Hitchhiker%27s_Guide_to_the_Galaxy
[2]:http://hashids.org
\ No newline at end of file
#
# Just a quick test to make sure there are no performance overheads from reading in a large JSON array.
# (There aren’t.)
#
randomWord = require 'random-word'
fs = require 'fs'
a = []
s = Date.now()
for i in [1..64000]
a.push randomWord()
e = Date.now()
fs.writeFileSync 'test.json', JSON.stringify(a)
console.log('Generate + write took: ' + (e-s) + ' ms')
s = Date.now()
b = JSON.parse(fs.readFileSync 'test.json')
e = Date.now()
console.log('Read + parse took: ' + (e-s) + ' ms')
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment