JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 132743
  • Score
    100M100P100Q159398F

Join all elements of an array and create a human-readable string

Package Exports

  • array-to-sentence

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (array-to-sentence) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

array-to-sentence

NPM version Bower version Build Status Coverage Status devDependency Status

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

Duo

var arrayToSentence = require('shinnn/array-to-sentence');

Standalone

Download the script file directly.

API

arrayToSentence(array [, options])

array: Array of any values
options: Object
Return: Array

It joins all elements of an array, and returns a string in the form A, B, ... and X.

Note that it doesn't support serial comma.

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 Shinnosuke Watanabe

Licensed under the MIT License.