Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce dependencies #6

Merged
merged 7 commits into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 3 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
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
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)
}
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
Original file line number Diff line number Diff line change
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
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])
})
})