Skip to content

Commit

Permalink
Merge pull request #437 from lencse/master
Browse files Browse the repository at this point in the history
[api] Added `lorem.slug` method
  • Loading branch information
Marak authored Feb 10, 2017
2 parents ecb42b1 + 8794efa commit 530c2ea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/lorem.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ var Lorem = function (faker) {
return sentence.charAt(0).toUpperCase() + sentence.slice(1) + '.';
};

/**
* slug
*
* @method faker.lorem.slug
* @param {number} wordCount number of words, defaults to 3
*/
self.slug = function (wordCount) {
var words = faker.lorem.words(wordCount);
return Helpers.slugify(words);
};

/**
* sentences
*
Expand Down
2 changes: 1 addition & 1 deletion test/all.functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var modules = {

internet: ['email', 'userName', 'domainName', 'domainWord', 'ip'],

lorem: ['words', 'sentence', 'sentences', 'paragraph', 'paragraphs'],
lorem: ['words', 'sentence', 'slug', 'sentences', 'paragraph', 'paragraphs'],

name: ['firstName', 'lastName', 'findName', 'jobTitle'],

Expand Down
30 changes: 30 additions & 0 deletions test/lorem.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ describe("lorem.js", function () {
});
});

describe("slug()", function () {
beforeEach(function () {
sinon.spy(faker.helpers, 'shuffle');
});

afterEach(function () {
faker.helpers.shuffle.restore();
});

var validateSlug = function (wordCount, str) {
assert.equal(1, str.match(/^[a-z][a-z-]*[a-z]$/).length);
assert.equal(wordCount - 1, str.match(/-/g).length);
};

context("when no 'wordCount' param passed in", function () {
it("returns a slug with three words", function () {
var str = faker.lorem.slug();
validateSlug(3, str);
});
});

context("when 'wordCount' param passed in", function () {
it("returns a slug with requested number of words", function () {
var str = faker.lorem.slug(7);
validateSlug(7, str);
});
});

});

/*
describe("sentence()", function () {
context("when no 'wordCount' or 'range' param passed in", function () {
Expand Down

0 comments on commit 530c2ea

Please sign in to comment.