Skip to content

Commit

Permalink
fix(transforms): handle array of strings as input to toProperList
Browse files Browse the repository at this point in the history
  • Loading branch information
cahilfoley committed Sep 23, 2018
1 parent 9e49ff6 commit 7d9ae22
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
28 changes: 16 additions & 12 deletions src/transforms/toProperList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,17 @@ import toProperList from './toProperList'

const batch = partial(batchTest, toProperList)

describe('ProperList String (toProperList)', () => {
describe('Properly Formatted List (toProperList)', () => {
test(`When the list is empty`, () => {
batch([
[[], '']
])
batch([[[], '']])
})


test(`No commas and o 'and'`, () => {
batch([
[['apples'], 'apples']
])
test(`No commas and no 'and'`, () => {
batch([[['apples'], 'apples']])
})

test(`With only two items`, () => {
batch([
[['apples','bananas'], 'apples and bananas']
])
batch([[['apples', 'bananas'], 'apples and bananas']])
})

test(`With three or more items`, () => {
Expand All @@ -31,4 +24,15 @@ describe('ProperList String (toProperList)', () => {
[['apples', 'pears', 'bananas', 'grape', 'lemon'], 'apples, pears, bananas, grape and lemon']
])
})

test('With an array of inputs', () => {
batch([
[[['apples', 'pears', 'bananas']], 'apples, pears and bananas'],
[[['apples', 'pears', 'bananas', 'grape']], 'apples, pears, bananas and grape'],
[
[['apples', 'pears', 'bananas', 'grape', 'lemon']],
'apples, pears, bananas, grape and lemon'
]
])
})
})
26 changes: 16 additions & 10 deletions src/transforms/toProperList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,41 @@
* @example const itemsString = toProperList(['apples', 'pears', 'bananas']) // => 'apples, pears and bananas'
*
*/
export default function toProperList(...items: string[]): string {
let itemsString = ''
let length = items.length
export function toProperList(items: string[]): string
export function toProperList(...items: string[]): string
export function toProperList(...items: any[]): string {
const parts: string[] = Array.isArray(items[0]) ? items[0] : items

let output = ''
const length = parts.length
// If you have only one item in the array
if (length === 1) {
itemsString = items[0]
output = parts[0]
}
// If you have only two items in the array
else if (length === 2) {
itemsString = `${items[0]} and ${items[1]}`
output = `${parts[0]} and ${parts[1]}`
}
// If you have 3 or more items in the array,
else if (length >= 3) {
// It traverses all elements of the array adds the fixed dots according to the English language
items.forEach((item, index) => {
parts.forEach((item, index) => {
// If it is the penultimate array item
if (index === length - 2) {
itemsString += `${item} and `
output += `${item} and `
}
// If it is the last item in the array
else if (index === length - 1) {
itemsString += `${item}`
output += `${item}`
}
// If it is the first or some element before the penultimate one
else {
itemsString += `${item}, `
output += `${item}, `
}
})
}

return itemsString
return output
}

export default toProperList

0 comments on commit 7d9ae22

Please sign in to comment.