Comparing version
17
index.js
'use strict'; | ||
module.exports = function (input, oxford) { | ||
module.exports = function (input, oxford, join) { | ||
const argLength = arguments.length; | ||
@@ -7,2 +7,5 @@ if (oxford === undefined) { | ||
} | ||
if (join === undefined) { | ||
join = 'and'; | ||
} | ||
@@ -15,2 +18,6 @@ if (!Array.isArray(input)) { | ||
} | ||
if (typeof join !== 'string') { | ||
throw new TypeError(`Expected join to be a string, got ${typeof join}`); | ||
} | ||
const allStrings = input.every(curr => { | ||
@@ -23,4 +30,4 @@ return (typeof curr === 'string'); | ||
if (argLength !== 1 && argLength !== 2) { | ||
throw new Error(`Expected 1 or 2 arguments, got ${argLength}`); | ||
if (argLength !== 1 && argLength !== 2 && argLength !== 3) { | ||
throw new Error(`Expected 1, 2 or 3 arguments, got ${argLength}`); | ||
} | ||
@@ -34,3 +41,3 @@ | ||
} else if (len === 2) { | ||
return `${input[0]} and ${input[1]}`; | ||
return `${input[0]} ${join} ${input[1]}`; | ||
} else { | ||
@@ -42,3 +49,3 @@ let myString = `${input[0]}`; | ||
} else { | ||
myString += oxford === true ? `, and ${input[i]}` : ` and ${input[i]}`; | ||
myString += oxford === true ? `, ${join} ${input[i]}` : ` ${join} ${input[i]}`; | ||
} | ||
@@ -45,0 +52,0 @@ } |
{ | ||
"name": "arrford", | ||
"version": "1.0.2", | ||
"version": "1.2.0", | ||
"description": "Array to human readable list converter", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -47,12 +47,27 @@ # arrford | ||
<br> | ||
#### Replace the word 'and' | ||
```js | ||
arrford(['spring', 'klättra', 'hoppa!'], true, 'och'); | ||
//=> 'spring, klättra, och hoppa!' | ||
``` | ||
<br> | ||
#### Replace the word 'and' and abort the Oxford comma | ||
```js | ||
arrford(['spring', 'klättra', 'hoppa!'], false, 'och'); | ||
//=> 'spring, klättra och hoppa!' | ||
``` | ||
<br> | ||
## API | ||
### arrford(target [, oxford]) | ||
### arrford(target [, oxford, join]) | ||
##### target | ||
Type: `Array` of strings | ||
Type: `Array` of `string`s | ||
@@ -66,5 +81,14 @@ ##### oxford | ||
Take an array of strings and return a string of these items as an english list. Insert "," and "and" in a grammatically correct way. Follow the Oxford comma trend unless `false` is passed in for the second argument. | ||
Returns: `string` | ||
##### join | ||
*optional* | ||
Type: `string` | ||
Default: 'and' | ||
Description: word used to join the list with the last element | ||
#### returns | ||
Type: `string` | ||
Take an array of strings and return a string of these items as an list. Insert "," and "and" in a grammatically correct way. Follow the Oxford comma trend unless `false` is passed in for the second argument. Use the word 'and' to join the list unless some other word is specified for the third argument. | ||
<br> | ||
@@ -71,0 +95,0 @@ |
5585
15.2%46
15%97
32.88%