Skip to content

Commit

Permalink
v0.1.0, delimiter added and some bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Atrox committed Mar 18, 2015
1 parent 8b05f2a commit e699d06
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 62 deletions.
27 changes: 1 addition & 26 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Users Environment Variables
.lock-wscript
.idea/
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,31 @@ npm install haikunator

## Usage

Haikunator is pretty simple. There is nothing to configure.
Haikunator is pretty simple.

```javascript
var haikunate = require('haikunator');

haikunate() // => "caring-butterfly-1337"
// default usage
haikunate() // => 'wispy-dust-1337'

// custom length (default=4)
haikunate(6) // => "caring-butterfly-133337"
haikunate(6) // => 'patient-king-887265'

// use hex instead of only numbers
haikunate(4, true) // => "caring-butterfly-4cdc"
haikunate(4, true) // => 'purple-breeze-98e1'

// don't include a token
haikunate(0) // => 'cold-wildflower'

// use a different delimiter
haikunate(4, false, '.') // => 'restless.sea.7976'

// no token, space delimiter
haikunate(0, false, ' ') // => 'delicate haze'

// no token, empty delimiter
haikunate(0, false, '') // => 'billowingleaf'
```

## Contributing
Expand Down
27 changes: 14 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var adjs = [
"free", "dry", "yellow", "orange", "gentle", "tight", "super", "royal", "broad",
"steep", "flat", "square", "round", "mute", "noisy", "hushy", "raspy", "soft",
"shrill", "rapid", "sweet", "curly", "calm", "jolly", "fancy", "plain", "shinny"
]
];
var nouns = [
"waterfall", "river", "breeze", "moon", "rain", "wind", "sea", "morning",
"snow", "lake", "sunset", "pine", "shadow", "leaf", "dawn", "glitter",
Expand All @@ -26,26 +26,27 @@ var nouns = [
"scene", "heart", "recipe", "union", "limit", "bread", "toast", "bonus",
"lab", "mud", "mode", "poetry", "tooth", "hall", "king", "queen", "lion", "tiger",
"penguin", "kiwi", "cake", "mouse", "rice", "coke", "hola", "salad", "hat"
]
];

var haikunator = function(max, hex) {
var haikunator = function (max, hex, delimiter) {
var suffix, adj, noun, sections, text = '';

max = typeof max !== 'undefined' ? max : 4;
delimiter = typeof delimiter !== 'undefined' ? delimiter : '-';
if (hex === true) {
var suffix = '0123456789abcdef';
suffix = '0123456789abcdef';
} else {
var suffix = '0123456789';
}
if (typeof max === 'undefined') {
max = 4;
suffix = '0123456789';
}

var text = "";
var adj = adjs[Math.floor(Math.random() * adjs.length)];
var noun = nouns[Math.floor(Math.random() * nouns.length)];
adj = adjs[Math.floor(Math.random() * adjs.length)];
noun = nouns[Math.floor(Math.random() * nouns.length)];

for (var i = 0; i < max; i++)
text += suffix.charAt(Math.floor(Math.random() * suffix.length));

return adj + '-' + noun + '-' + text
}
sections = [adj, noun, text];
return sections.filter(function (e) {return e === 0 || e}).join(delimiter);
};

module.exports = haikunator;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "haikunator",
"version": "0.0.1",
"description": "Generate Heroku-like random names to use in your applications.",
"version": "0.1.0",
"description": "Generate Heroku-like random names to use in your node applications.",
"main": "index.js",
"scripts": {
"test": "mocha test"
Expand Down
54 changes: 37 additions & 17 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
var assert = require('chai').assert,
haikunate = require('../index');


describe('test haikunate()', function() {
it('default use', function() {
assert.match(haikunate(), /((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(\d)(\d)(\d)(\d)/i);
});
it('matching with hex turned on', function() {
assert.match(haikunate(4, true), /((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(.)(.)(.)(.)/i);
});
it('matching with 9 ints', function() {
assert.match(haikunate(9), /((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/i);
});
it('matching with hex tuned on', function() {
assert.match(haikunate(9, true), /((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(.)(.)(.)(.)(.)(.)(.)(.)(.)/i);
});
})
haikunate = require('../index');


describe('testing haikunate', function () {

it('haikunate() should return 4 digits', function () {
assert.match(haikunate(), /((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(\d{4})/);
});

it('haikunate(4, true) should return 4 digits as hex', function () {
assert.match(haikunate(4, true), /((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(.{4})/i);
});

it('haikunate(9) should return 9 digits', function () {
assert.match(haikunate(9), /((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(\d{9})/i);
});

it('haikunate(9, true) should return 9 digits as hex', function () {
assert.match(haikunate(9, true), /((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))(-)(.{9})/i);
});

it('wont return the same name for subsequent calls', function () {
assert.notStrictEqual(haikunate(), haikunate());
});

it('drops the token if token range is 0', function () {
assert.match(haikunate(0), /((?:[a-z][a-z]+))(-)((?:[a-z][a-z]+))/);
});

it('permits optional configuration of the delimiter', function () {
assert.match(haikunate(4, false, '.'), /((?:[a-z][a-z]+))(\.)((?:[a-z][a-z]+))(\.)(\d+)/i);
});

it('drops the token and delimiter if token range is 0 and delimiter is an empty space', function () {
assert.match(haikunate(0, false, ' '), /((?:[a-z][a-z]+))( )((?:[a-z][a-z]+))/i);
});
});

0 comments on commit e699d06

Please sign in to comment.