0.0.0 • Published 7 years ago
optima v0.0.0
array-to-sentence
Join all elements of an array and create a human-readable string
arrayToSentence(['foo', 'bar', 'baz', 'qux']); //=> 'foo, bar, baz and qux'
Installation
Package managers
npm
npm install array-to-sentence
bower
bower install array-to-sentence
Standalone
Download the script file directly.
API
arrayToSentence(array , options)
array: Array
of any values
options: Object
Return: String
It joins all elements of an array, and returns a string in the form A, B, ... and X
.
arrayToSentence(['one', 'two', 3]); //=> 'one, two and 3'
arrayToSentence(['one', 'two']); //=> 'one and two'
arrayToSentence(['one']); //=> 'one'
It returns an empty string if the array is empty.
arrayToSentence([]); //=> ''
options.separator
Type: String
Default: ', '
Set the separator string of each word.
options.lastSeparator
Type: String
Default: ' and '
Set the separator string before the last word.
arrayToSentence(['A', 'B', 'C'], {
separator: '-',
lastSeparator: '-'
}); //=> 'A-B-C'
arrayToSentence(['Earth', 'Wind', 'Fire'], {
lastSeparator: ' & '
}); //=> 'Earth, Wind & Fire'
Acknowledgement
For designing API, I used .toSentence()
method of underscore.string as reference. Thanks, Esa-Matti Suuronen and the contributors.
License
Copyright (c) 2014 - 2015 Shinnosuke Watanabe
Licensed under the MIT License.
0.0.0
7 years ago