Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
56 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Convert items in an array into a sentence-style list. | ||
* @param {Array} a Array of items to be formatted | ||
* @param {String} [sep=', '] Separator between list items | ||
* @param {String} [and=' and '] Separator between last two list items | ||
* @return {String} Sentence-style list | ||
*/ | ||
module.exports = (a, { sep = ', ', and = ' and ' } = {}) => { | ||
if (!Array.isArray(a)) throw new TypeError(`Expected array, got ${typeof a}`) | ||
if (a.length === 0) return '' | ||
if (a.length === 1) return a[0] | ||
return a.slice(0, -1).join(sep) + and + a.slice(-1) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict' | ||
|
||
const DESCRIBE = require('mocha').describe | ||
const IT = require('mocha').it | ||
const EXPECT = require('chai').expect | ||
const LIST = require('../lib/list') | ||
|
||
DESCRIBE('lib/list', function () { | ||
this.timeout(10000) | ||
|
||
IT('should throw an error if passed a non-array', () => { | ||
EXPECT(() => LIST('i am already a string')).to.throw(TypeError) | ||
}) | ||
|
||
IT('should return an empty string from an empty array', () => { | ||
EXPECT(LIST([])).to.equal('') | ||
}) | ||
|
||
IT('should return the only item from a 1-item array', () => { | ||
const a = ['item'] | ||
EXPECT(LIST(a)).to.equal(a[0]) | ||
}) | ||
|
||
IT('should join a 2-item array with “and”', () => { | ||
const a = ['Elena', 'Lila'] | ||
EXPECT(LIST(a)).to.equal(`${a[0]} and ${a[1]}`) | ||
}) | ||
|
||
IT('should join a multi-item array with commas and “and”', () => { | ||
const a = ['Elena', 'Lila', 'Pasqualino'] | ||
EXPECT(LIST(a)).to.equal(`${a[0]}, ${a[1]} and ${a[2]}`) | ||
}) | ||
|
||
IT('should accept options for the list seperators', () => { | ||
const a = ['First', 'Second', 'Third'] | ||
const o = { sep: '. ', and: '. And finally: ' } | ||
EXPECT(LIST(a, o)).to.equal(a[0] + o.sep + a[1] + o.and + a[2]) | ||
}) | ||
}) |