Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/array-to-sentence
Join all elements of an array and create a human-readable string
https://github.com/shinnn/array-to-sentence
array concatenation javascript natural-language sentence
Last synced: 26 days ago
JSON representation
Join all elements of an array and create a human-readable string
- Host: GitHub
- URL: https://github.com/shinnn/array-to-sentence
- Owner: shinnn
- License: isc
- Created: 2014-11-17T09:22:07.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2019-03-17T23:12:52.000Z (over 5 years ago)
- Last Synced: 2024-10-11T09:34:45.169Z (about 1 month ago)
- Topics: array, concatenation, javascript, natural-language, sentence
- Language: JavaScript
- Homepage: https://npm.runkit.com/array-to-sentence
- Size: 42 KB
- Stars: 33
- Watchers: 1
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# array-to-sentence
[![npm version](https://img.shields.io/npm/v/array-to-sentence.svg)](https://www.npmjs.com/package/array-to-sentence)
[![Build Status](https://travis-ci.com/shinnn/array-to-sentence.svg?branch=master)](https://travis-ci.com/shinnn/array-to-sentence)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/array-to-sentence.svg)](https://coveralls.io/github/shinnn/array-to-sentence)Join all elements of an array and create a human-readable string
```javascript
arrayToSentence(['foo', 'bar', 'baz', 'qux']); //=> 'foo, bar, baz and qux'
```## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install array-to-sentence
```## API
```javascript
import arrayToSentence from 'array-to-sentence';
```### arrayToSentence(*array* [, *options*])
*array*: `Array`
*options*: `Object`
Return: `string`It joins all elements of an array, and returns a string in the form `A, B, ... and X`.
```javascript
arrayToSentence(['one', 'two', 3]); //=> 'one, two and 3'
arrayToSentence(['one', 'two']); //=> 'one and two'
arrayToSentence(['one']); //=> 'one'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.
```javascript
arrayToSentence(['A', 'B', 'C'], {
separator: '-',
lastSeparator: '-'
}); //=> 'A-B-C'arrayToSentence(['Earth', 'Wind', 'Fire'], {
lastSeparator: ' & '
}); //=> 'Earth, Wind & Fire'
```## License
[ISC License](./LICENSE) © 2018 Shinnosuke Watanabe