diff --git a/README.md b/README.md index 92cae04..2478e25 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,3 @@ -NOT YET PUSHED -==== - -I got tired last night and didn't finish the npm publish script. -Will do in the morning... - human-readable-ids-js ===================== @@ -50,6 +44,12 @@ All of the words are sorted alphabetically (`sort -u`) and stored in the `*.txt` files. The pre-publish script outputs the formatted javascript. +### Bad Examples + + * wednesday, hamster, ostrich (difficult to spell) + * grey, gray, bore, boar (two ways of spelling the same word or sound) + * prawn (not well-known) + TODO ==== diff --git a/assets/adjectives.js b/assets/adjectives.js new file mode 100644 index 0000000..761d3b7 --- /dev/null +++ b/assets/adjectives.js @@ -0,0 +1,7 @@ +/*jshint -W054 */ +;(function (exports) { + 'use strict'; + + exports.humanReadableIds = exports.humanReadableIds || {}; + exports.humanReadableIds.adjectives = ["afraid","ancient","angry","average","bad","big","bitter","black","blue","brave","breezy","bright","brown","calm","chatty","chilly","clever","cold","cowardly","cuddly","curly","curvy","dangerous","dry","dull","empty","evil","fast","fat","fluffy","foolish","fresh","friendly","funny","fuzzy","gentle","giant","good","great","green","grumpy","happy","hard","heavy","helpless","honest","horrible","hot","hungry","itchy","jolly","kind","lazy","light","little","loud","lovely","lucky","massive","mean","mighty","modern","moody","nasty","neat","nervous","new","nice","odd","old","orange","ordinary","perfect","pink","plastic","polite","popular","pretty","proud","purple","quick","quiet","rare","red","rotten","rude","selfish","serious","shaggy","sharp","short","shy","silent","silly","slimy","slippery","smart","smooth","soft","sour","spicy","splendid","spotty","stale","strange","strong","stupid","sweet","swift","tall","tame","tasty","tender","terrible","thin","tidy","tiny","tough","tricky","ugly","unlucky","warm","weak","wet","white","wicked","wise","witty","wonderful","yellow","young"]; +}('undefined' !== typeof exports && exports || new Function('return this')())); diff --git a/assets/animals.js b/assets/animals.js new file mode 100644 index 0000000..8ea450e --- /dev/null +++ b/assets/animals.js @@ -0,0 +1,7 @@ +/*jshint -W054 */ +;(function (exports) { + 'use strict'; + + exports.humanReadableIds = exports.humanReadableIds || {}; + exports.humanReadableIds.animals = ["ape","baboon","badger","bat","bear","bird","bobcat","bulldog","bullfrog","cat","catfish","cheetah","chicken","chipmunk","cobra","cougar","cow","crab","deer","dingo","dodo","dog","dolphin","donkey","dragon","dragonfly","duck","eagle","earwig","eel","elephant","emu","falcon","fireant","firefox","fish","fly","fox","frog","gecko","goat","goose","grasshopper","horse","hound","husky","impala","insect","jellyfish","kangaroo","ladybug","liger","lion","lionfish","lizard","mayfly","mole","monkey","moose","moth","mouse","mule","newt","octopus","otter","owl","panda","panther","parrot","penguin","pig","puma","pug","quail","rabbit","rat","rattlesnake","robin","seahorse","sheep","shrimp","skunk","sloth","snail","snake","squid","starfish","stingray","swan","termite","tiger","treefrog","turkey","turtle","vampirebat","walrus","warthog","wasp","wolverine","wombat","yak","zebra"]; +}('undefined' !== typeof exports && exports || new Function('return this')())); diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..c929ac3 --- /dev/null +++ b/bower.json @@ -0,0 +1,36 @@ +{ + "name": "human-readable-ids", + "main": "index.js", + "version": "1.0.0", + "homepage": "https://github.com/coolaj86/human-readable-ids-js", + "authors": [ + "AJ ONeal " + ], + "description": "Generate human-readable ids from lists of easy-to-spell nouns and adjectives", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "hri", + "human", + "readable", + "ids", + "string", + "english", + "noun", + "adjective", + "nouns", + "adjectives" + ], + "license": "Apache2", + "ignore": [ + "**/.*", + "src/*", + "lists.js", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/genids.js b/genids.js deleted file mode 100644 index b06d310..0000000 --- a/genids.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict'; - -var fs = require('fs') - , path = require('path') - , shuffle = require('knuth-shuffle').knuthShuffle - , animalsFile = path.join(__dirname, 'animals.txt') - , adjectivesFile = path.join(__dirname, 'adjectives.txt') - , animals = [] - , animalsMaster - , adjectives = [] - , adjectivesMaster - , numbers = [] - , i = 0 - , k = 0 - , id - ; - -animalsMaster = fs.readFileSync(animalsFile, 'utf8').trim().split('\n'); -adjectivesMaster = fs.readFileSync(adjectivesFile, 'utf8').trim().split('\n'); - -function genNumbers() { - numbers.push(0); - i = 2; - // 1 is not plural, so we skip it - while (i <= 100) { - numbers.push(i); - i += 1; - } - - return shuffle(numbers); -} - -for (k = 0; k < 100; k += 1) { - if (!adjectives.length) { - adjectives = shuffle(adjectivesMaster.slice(0)); - } - if (!animals.length) { - animals = shuffle(animalsMaster.slice(0)); - } - if (!numbers.length) { - numbers = genNumbers(); - } - - id = adjectives.pop() - + '-' + animals.pop() - + '-' + numbers.pop() - ; - - console.log(id); -} diff --git a/index.js b/index.js new file mode 100644 index 0000000..ab07355 --- /dev/null +++ b/index.js @@ -0,0 +1,46 @@ +/*jshint -W054 */ +;(function (exports) { + 'use strict'; + + var lists = exports.humanReadableIds || require('./lists') + , shuffle = exports.knuthShuffle || require('knuth-shuffle').knuthShuffle + , animals = [] + , adjectives = [] + , numbers = [] + ; + + function genNumbers() { + var i = 2 + ; + + numbers = []; + numbers.push(0); + // 1 is not plural, so we skip it + for (i = 2; i <= 100; i += 1) { + numbers.push(i); + } + + return shuffle(numbers); + } + + function random() { + if (!adjectives.length) { + adjectives = shuffle(lists.adjectives.slice(0)); + } + if (!animals.length) { + animals = shuffle(lists.animals.slice(0)); + } + if (!numbers.length) { + numbers = shuffle(genNumbers()); + } + + return adjectives.pop() + + '-' + animals.pop() + + '-' + numbers.pop() + ; + } + + exports.humanReadableIds = { random: random }; + exports.hri = exports.humanReadableIds; + +}('undefined' !== typeof exports && exports || new Function('return this')())); diff --git a/lists.js b/lists.js new file mode 100644 index 0000000..4d8f80c --- /dev/null +++ b/lists.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = { + animals: require('./assets/animals').humanReadableIds.animals +, adjectives: require('./assets/adjectives').humanReadableIds.adjectives +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..3930e5e --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "human-readable-ids", + "version": "1.0.0", + "description": "Generate human-readable ids from lists of easy-to-spell nouns and adjectives", + "main": "index.js", + "dependencies": { + "knuth-shuffle": "^1.0.0" + }, + "devDependencies": {}, + "scripts": { + "test": "node tests/run-in-node.js" + , "prepublish": "node src/generate-lists" + }, + "repository": { + "type": "git", + "url": "https://github.com/coolaj86/human-readable-ids-js.git" + }, + "keywords": [ + "hri", + "human", + "readable", + "ids", + "string", + "english", + "noun", + "adjective", + "nouns", + "adjectives" + ], + "author": "AJ ONeal (http://coolaj86.com/)", + "license": "Apache2", + "bugs": { + "url": "https://github.com/coolaj86/human-readable-ids-js/issues" + }, + "homepage": "https://github.com/coolaj86/human-readable-ids-js" +} diff --git a/adjectives.txt b/src/adjectives.txt similarity index 99% rename from adjectives.txt rename to src/adjectives.txt index 376cdea..6c2ba64 100644 --- a/adjectives.txt +++ b/src/adjectives.txt @@ -10,6 +10,7 @@ blue brave breezy bright +brown calm chatty chilly diff --git a/animals.txt b/src/animals.txt similarity index 100% rename from animals.txt rename to src/animals.txt diff --git a/src/generate-lists.js b/src/generate-lists.js new file mode 100644 index 0000000..1161a86 --- /dev/null +++ b/src/generate-lists.js @@ -0,0 +1,21 @@ +'use strict'; + +var fs = require('fs') + , path = require('path') + , files = {} + , master = {} + , tplFile + ; + +tplFile = fs.readFileSync(path.join(__dirname, '..', 'src', 'tpl.js'), 'utf8'); + +['animals', 'adjectives'].forEach(function (key) { + files[key] = path.join(__dirname, '..', 'src', key + '.txt'); + master[key] = fs.readFileSync(files[key], 'utf8').trim().split('\n'); + + fs.writeFileSync( + path.join(__dirname, '..', 'assets', key + '.js') + , tplFile.replace(/{{\s*setname\s*}}/, key).replace(/{{\s*set\s*}}/, JSON.stringify(master[key])) + , 'utf8' + ); +}); diff --git a/tpl.js b/src/tpl.js similarity index 100% rename from tpl.js rename to src/tpl.js diff --git a/tests/run-in-node.js b/tests/run-in-node.js new file mode 100644 index 0000000..bc69b00 --- /dev/null +++ b/tests/run-in-node.js @@ -0,0 +1,12 @@ +/*jshint -W054 */ +;(function (exports) { + 'use strict'; + + var hri = require('./index').hri + , i + ; + + for (i = 0; i < 100; i += 1) { + console.log(hri.random()); + } +}('undefined' !== typeof exports && exports || new Function('return this')()));