refactor as npm and bower packages

This commit is contained in:
AJ ONeal 2014-07-21 09:06:16 -06:00
parent 73397726ab
commit 3f5b72b144
13 changed files with 178 additions and 56 deletions

View File

@ -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 human-readable-ids-js
===================== =====================
@ -50,6 +44,12 @@ All of the words are sorted alphabetically (`sort -u`) and stored in
the `*.txt` files. the `*.txt` files.
The pre-publish script outputs the formatted javascript. 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 TODO
==== ====

7
assets/adjectives.js Normal file
View File

@ -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 Normal file
View File

@ -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 Normal file
View File

@ -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"
]
}

View File

@ -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 Normal file
View File

@ -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 Normal file
View File

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

36
package.json Normal file
View File

@ -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"
}

View File

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

21
src/generate-lists.js Normal file
View File

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

View File

12
tests/run-in-node.js Normal file
View File

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