Browse Source

refactor as npm and bower packages

master
AJ ONeal 10 years ago
parent
commit
3f5b72b144
  1. 12
      README.md
  2. 7
      assets/adjectives.js
  3. 7
      assets/animals.js
  4. 36
      bower.json
  5. 50
      genids.js
  6. 46
      index.js
  7. 6
      lists.js
  8. 36
      package.json
  9. 1
      src/adjectives.txt
  10. 0
      src/animals.txt
  11. 21
      src/generate-lists.js
  12. 0
      src/tpl.js
  13. 12
      tests/run-in-node.js

12
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
====

7
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')()));

7
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')()));

36
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 <awesome@coolaj86.com>"
],
"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"
]
}

50
genids.js

@ -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);
}

46
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')()));

6
lists.js

@ -0,0 +1,6 @@
'use strict';
module.exports = {
animals: require('./assets/animals').humanReadableIds.animals
, adjectives: require('./assets/adjectives').humanReadableIds.adjectives
};

36
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 <coolaj86@gmail.com> (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"
}

1
adjectives.txt → src/adjectives.txt

@ -10,6 +10,7 @@ blue
brave
breezy
bright
brown
calm
chatty
chilly

0
animals.txt → src/animals.txt

21
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'
);
});

0
tpl.js → src/tpl.js

12
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')()));
Loading…
Cancel
Save