Skip to content

Commit

Permalink
Merge f1213ae into 49867bc
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Aug 19, 2018
2 parents 49867bc + f1213ae commit 989c883
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
14 changes: 3 additions & 11 deletions index.js
Expand Up @@ -4,19 +4,11 @@ const { URL } = require('url')
const isUrl = require('is-url-superb')
const scrape = require('html-metadata')
const jp = require('jsonpath')
const arrayToSentence = require('array-to-sentence')
const upperfirst = require('lodash.upperfirst')
const arrayToSentence = require('./lib/list')
const fromTwitter = require('./lib/twitter')

function toStartCase (string) {
string = string.toLowerCase()
string = string.split(' ')
string.forEach((word, i, a) => {
a[i] = upperfirst(word)
})
string = string.join(' ')
return string
}
const upperfirst = s => s[0].toUpperCase() + s.slice(1)
const toStartCase = s => s.toLowerCase().split(' ').map(upperfirst).join(' ')

function reduceWhitespace (string) {
return string.replace(/\s+/g, ' ')
Expand Down
13 changes: 13 additions & 0 deletions lib/list.js
@@ -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)
}
10 changes: 0 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -44,10 +44,8 @@
"tty-table": "^2.6.8"
},
"dependencies": {
"array-to-sentence": "^2.0.0",
"html-metadata": "^1.7.0",
"is-url-superb": "^2.0.0",
"jsonpath": "^1.0.0",
"lodash.upperfirst": "^4.3.1"
"jsonpath": "^1.0.0"
}
}
39 changes: 39 additions & 0 deletions test/list.js
@@ -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])
})
})

0 comments on commit 989c883

Please sign in to comment.