What is humanize-list?
The humanize-list npm package is designed to convert arrays into human-readable strings. It is particularly useful for creating natural language lists from array data, making it easier to display lists in a user-friendly format.
What are humanize-list's main functionalities?
Basic List Humanization
This feature converts an array of items into a human-readable string, using commas and the word 'and' before the last item.
const humanizeList = require('humanize-list');
const items = ['apples', 'oranges', 'bananas'];
const humanized = humanizeList(items);
console.log(humanized); // Output: 'apples, oranges, and bananas'
Custom Conjunction
This feature allows you to customize the conjunction used in the list. In this example, the word 'or' is used instead of 'and'.
const humanizeList = require('humanize-list');
const items = ['apples', 'oranges', 'bananas'];
const humanized = humanizeList(items, { conjunction: 'or' });
console.log(humanized); // Output: 'apples, oranges, or bananas'
Serial Comma
This feature allows you to control the use of the serial comma (Oxford comma). Setting `serialComma` to false omits the comma before the conjunction.
const humanizeList = require('humanize-list');
const items = ['apples', 'oranges', 'bananas'];
const humanized = humanizeList(items, { serialComma: false });
console.log(humanized); // Output: 'apples, oranges and bananas'
Other packages similar to humanize-list
oxford-comma
The oxford-comma package provides similar functionality by converting arrays into human-readable strings with an emphasis on the Oxford comma. It offers customization options for conjunctions and serial commas, similar to humanize-list.
listify
The listify package also converts arrays into human-readable strings. It is highly customizable, allowing for different conjunctions, separators, and formatting options. It is comparable to humanize-list in terms of flexibility and ease of use.
array-to-sentence
The array-to-sentence package focuses on converting arrays into readable sentences. It provides options for conjunctions and serial commas, making it a good alternative to humanize-list for creating natural language lists.
humanize-list
Comma delimit an array for human readability, the Oxford comma is optional.
Installation
npm install --save humanize-list
Usage
var humanizeList = require('humanize-list');
humanizeList(['apples', 'tomatoes', 'unicorns']);
humanizeList(['apples', 'tomatoes', 'unicorns'], { oxfordComma: true });
humanizeList(['apples', 'tomatoes', 'unicorns'], { conjunction: 'or' });
humanizeList(['apples', 'tomatoes', 'unicorns'], { skipConjunction: true });
Options
oxfordComma
Boolean - Specify whether the Oxford comma should be included. Default: false
conjunction
String - Specify a conjunction. Default: 'and'
skipConjunction
Boolean - Skip the conjunction altogether. Default: false
License
MIT
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Crafted with <3 by John Otander (@4lpine).
This package was initially generated with yeoman and the p generator.