Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added lingo.join()

  • Loading branch information...
commit dc94d418278d1e055cbbe5afb75e2758bc1a9639 1 parent b8fd599
@tj authored
Showing with 46 additions and 0 deletions.
  1. +12 −0 Readme.md
  2. +27 −0 lib/lingo.js
  3. +7 −0 test/lingo.test.js
View
12 Readme.md
@@ -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:
View
27 lib/lingo.js
@@ -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;
+};
View
7 test/lingo.test.js
@@ -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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.