Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ A name generator. Be sure to `.use()` some dictionaries
afterward. Options may include:

{
glue: '-'
capitalize: true,
noglue: true,
}

**generator.use(dictionary, options)**
Expand Down
6 changes: 6 additions & 0 deletions lib/moniker.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ Dictionary.prototype.choose = function() {
function Generator(opt) {
this.dicts = [];
this.glue = (opt && opt.glue) || '-';
this.capitalize = (opt && opt.capitalize) || false;
if (opt && opt.noglue) this.glue = '';
}

Generator.prototype.choose = function() {
var dicts = this.dicts,
size = dicts.length;
capitalize = this.capitalize;

if (size === 0)
throw new Error('no available dictionaries.');
Expand All @@ -100,6 +103,9 @@ Generator.prototype.choose = function() {
throw new Error('too many tries to find a unique word');

used[probe] = true;

if (capitalize) probe = probe.charAt(0).toUpperCase() + probe.slice(1);

return probe;
});

Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
"version": "0.1.2",
"description": "Generate random names.",
"author": "Ben Weaver <ben@orangesoda.net>",
"main": "./lib/moniker"
"main": "./lib/moniker",
"scripts": {
"test": "mocha && eslint lib/ test/"
},
"devDependencies": {
"eslint": "^1.1.0",
"mocha": "^2.2.5"
}
}
26 changes: 15 additions & 11 deletions test/moniker.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
var Assert = require('assert'),
M = require('../lib/moniker');

module.exports = {
'the default generator works': function() {
Assert.equal(typeof M.choose(), 'string');
},

'names are random': function() {
var names = M.generator([M.adjective, M.noun], { maxSize: 7 });
Assert.equal(typeof names.choose(), 'string');
Assert.notEqual(names.choose(), names.choose());
}
};
describe('all', function() {
it('the default generator works', function() {
var G = M.generator([M.adjective, M.noun], { maxSize: 7, capitalize: true });
console.log(G);
var name = G.choose();
console.log(name);
Assert.equal(typeof name, 'string');
});
it('names are random', function() {
var names = M.generator([M.adjective, M.noun], { maxSize: 7 });
console.log(names);
Assert.equal(typeof names.choose(), 'string');
Assert.notEqual(names.choose(), names.choose());
});
});