Skip to content

Commit

Permalink
Added lingo.join()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 27, 2010
1 parent b8fd599 commit dc94d41
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Readme.md
Expand Up @@ -75,6 +75,18 @@ Camelcase with first character upppercase:
lingo.camelcase('foo bar baz', true);
// => "FooBarBaz"

## lingo.join()

Defaults to "and":

lingo.join(['fruits', 'veggies', 'sugar']);
// => "fruits, veggies and sugar"

Supplying "or":

lingo.join(['fruits', 'veggies', 'sugar'], 'or');
// => "fruits, veggies or sugar"

## i18n (translations)

New languages can be defined as shown below:
Expand Down
27 changes: 27 additions & 0 deletions lib/lingo.js
Expand Up @@ -90,3 +90,30 @@ exports.camelcase = function(str, uppercaseFirst){
return word;
}).join('');
};

/**
* Join an array with the given `last` string
* which defaults to "and".
*
* Examples:
*
* lingo.join(['fruits', 'veggies', 'sugar']);
* // => "fruits, veggies and sugar"
*
* lingo.join(['fruits', 'veggies', 'sugar'], 'or');
* // => "fruits, veggies or sugar"
*
* @param {Array} arr
* @param {String} last
* @return {String}
* @api public
*/

exports.join = function(arr, last){
var str = arr.pop()
, last = last || 'and';
if (arr.length) {
str = arr.join(', ') + ' ' + last + ' ' + str;
}
return str;
};
7 changes: 7 additions & 0 deletions test/lingo.test.js
Expand Up @@ -23,6 +23,13 @@ module.exports = {
assert.equal('UserRole', lingo.camelcase('user role', true));
},

'test .join()': function(assert){
assert.equal('foo', lingo.join(['foo']));
assert.equal('foo and bar', lingo.join(['foo', 'bar']));
assert.equal('foo, bar and baz', lingo.join(['foo', 'bar', 'baz']));
assert.equal('foo, bar or baz', lingo.join(['foo', 'bar', 'baz'], 'or'));
},

'test Language mapping': function(assert){
assert.equal('en', lingo.Language.en.code);
}
Expand Down

0 comments on commit dc94d41

Please sign in to comment.