Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bubkoo committed Apr 25, 2016
1 parent 087afb3 commit 8362edc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
9 changes: 6 additions & 3 deletions README.md
Expand Up @@ -21,13 +21,16 @@ $ npm install --save random-syllable
var randomSyllable = require('random-syllable');

// API
// - randomSyllable();
// - randomSyllable(length);
// - randomSyllable([options]);

// options
// - length


randomSyllable();
// => fop

randomSyllable(2);
randomSyllable({ length: 2 });
// => ji
```

Expand Down
27 changes: 15 additions & 12 deletions index.js
@@ -1,38 +1,41 @@
'use strict';

var clamp = require('clamp');
var isObject = require('is-object');
var toInteger = require('to-integer');
var randomChar = require('random-char');
var randomNatural = require('random-natural');

module.exports = function (length) {
module.exports = function (options) {

var count;
var length = isObject(options)
? options.length
: options;

if (length) {
count = toInteger(length);
count = clamp(count, 2, 3);
length = toInteger(length);
length = clamp(length, 2, 3);
} else {
count = randomNatural(2, 3);
length = randomNatural({ min: 2, max: 3 });
}


var consonants = 'bcdfghjklmnprstvwz'; // consonants except hard to speak ones
var vowels = 'aeiou'; // vowels
var all = consonants + vowels; // all
var vowels = 'aeiou'; // vowels
var all = consonants + vowels; // all

var text = '';
var char;

for (var i = 0; i < count; i++) {
for (var i = 0; i < length; i++) {
if (i === 0) {
// First character can be anything
char = randomChar(all);
char = randomChar({ pool: all });
} else if (consonants.indexOf(char) === -1) {
// Last character was a vowel, now we want a consonant
char = randomChar(consonants);
char = randomChar({ pool: consonants });
} else {
// Last character was a consonant, now we want a vowel
char = randomChar(vowels);
char = randomChar({ pool: vowels });
}

text += char;
Expand Down
7 changes: 4 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "random-syllable",
"version": "1.0.0",
"version": "1.0.1",
"description": "Return a semi-speakable syllable, 2 or 3 letters.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -44,8 +44,9 @@
},
"dependencies": {
"clamp": "^1.0.1",
"random-char": "^1.0.2",
"random-natural": "^1.0.2",
"is-object": "^1.0.1",
"random-char": "^1.0.4",
"random-natural": "^1.0.3",
"to-integer": "^1.0.1"
}
}
6 changes: 3 additions & 3 deletions test/spec/index.js
Expand Up @@ -4,17 +4,17 @@ describe('random-syllable: ', function () {

var randomSyllable = require('../../');

it('randomLorem()', function () {
it('randomSyllable()', function () {
expect(randomSyllable()).to.be.a('string');

for (var i = 0; i < 1000; i++) {
expect(randomSyllable()).to.be.have.length.within(2, 3);
}
});

it('randomLorem(2)', function () {
it('randomSyllable({ length: 2})', function () {
for (var i = 0; i < 1000; i++) {
expect(randomSyllable(2)).to.be.have.length(2);
expect(randomSyllable({ length: 2 })).to.be.have.length(2);
}
});
});

0 comments on commit 8362edc

Please sign in to comment.