Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for oxford comma. #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ arrayToSentence(['Earth', 'Wind', 'Fire'], {
}); //=> 'Earth, Wind & Fire'
```

### options.oxfordComma
Copy link
Owner

@shinnn shinnn Feb 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The option name is confusing when you use this library for non-English languages; separator could not be a comma in those cases.


Type: `boolean`
Default: `false`

Includes the oxford comma. A comma after the second to last word.

```javascript
arrayToSentence(['A', 'B', 'C'], {
oxfordComma: true
}); //=> 'A, B, and C'

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

```

## License

[ISC License](./LICENSE) © 2018 Shinnosuke Watanabe
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* array-to-sentence | ISC (c) Shinnosuke Watanabe
* https://github.com/shinnn/array-to-sentence
*/
var OPTION_NAMES = ['separator', 'lastSeparator'];
var STRING_OPTION_NAMES = ['separator', 'lastSeparator'];

function arrayToSentence(arr, options) {
if (!Array.isArray(arr)) {
Expand All @@ -16,13 +16,13 @@ function arrayToSentence(arr, options) {
lastSeparator: ' and ',
}, options);

for (var i = 0; i < 2; i++) {
if (typeof options[OPTION_NAMES[i]] !== 'string') {
for (var i = 0; i < STRING_OPTION_NAMES.length; i++) {
if (typeof options[STRING_OPTION_NAMES[i]] !== 'string') {
throw new TypeError(
'Expected `' +
OPTION_NAMES[i] +
STRING_OPTION_NAMES[i] +
'` option to be a string, but got a non-string value ' +
options[OPTION_NAMES[i]] +
options[STRING_OPTION_NAMES[i]] +
'.'
);
}
Expand All @@ -36,7 +36,9 @@ function arrayToSentence(arr, options) {
return arr[0];
}

return arr.slice(0, -1).join(options.separator) + options.lastSeparator + arr[arr.length - 1];
const oxfordComma = options.oxfordComma && arr.length > 2 ? options.separator.trim() : '';

return arr.slice(0, -1).join(options.separator) + oxfordComma + options.lastSeparator + arr[arr.length - 1];
}

module.exports = arrayToSentence;
14 changes: 8 additions & 6 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* array-to-sentence | ISC (c) Shinnosuke Watanabe
* https://github.com/shinnn/array-to-sentence
*/
var OPTION_NAMES = ['separator', 'lastSeparator'];
var STRING_OPTION_NAMES = ['separator', 'lastSeparator'];

export default function arrayToSentence(arr, options) {
if (!Array.isArray(arr)) {
Expand All @@ -14,13 +14,13 @@ export default function arrayToSentence(arr, options) {
lastSeparator: ' and ',
}, options);

for (var i = 0; i < 2; i++) {
if (typeof options[OPTION_NAMES[i]] !== 'string') {
for (var i = 0; i < STRING_OPTION_NAMES.length; i++) {
if (typeof options[STRING_OPTION_NAMES[i]] !== 'string') {
throw new TypeError(
'Expected `' +
OPTION_NAMES[i] +
STRING_OPTION_NAMES[i] +
'` option to be a string, but got a non-string value ' +
options[OPTION_NAMES[i]] +
options[STRING_OPTION_NAMES[i]] +
'.'
);
}
Expand All @@ -34,5 +34,7 @@ export default function arrayToSentence(arr, options) {
return arr[0];
}

return arr.slice(0, -1).join(options.separator) + options.lastSeparator + arr[arr.length - 1];
const oxfordComma = options.oxfordComma && arr.length > 2 ? options.separator.trim() : ''

return arr.slice(0, -1).join(options.separator) + oxfordComma + options.lastSeparator + arr[arr.length - 1];
}
Loading