What is listify?
The listify npm package is a utility that converts arrays into human-readable lists. It is particularly useful for formatting lists in a way that is grammatically correct and easy to read.
What are listify's main functionalities?
Basic List Formatting
This feature allows you to convert an array of items into a human-readable list. By default, it uses commas to separate items and adds 'and' before the last item.
const listify = require('listify');
const items = ['apple', 'banana', 'cherry'];
console.log(listify(items)); // Output: 'apple, banana, and cherry'
Custom Separator
This feature allows you to specify a custom separator for the list items. In this example, a semicolon followed by a space is used as the separator.
const listify = require('listify');
const items = ['apple', 'banana', 'cherry'];
console.log(listify(items, { separator: '; ' })); // Output: 'apple; banana; and cherry'
Custom Final Word
This feature allows you to customize the final word used before the last item in the list. In this example, 'or' is used instead of the default 'and'.
const listify = require('listify');
const items = ['apple', 'banana', 'cherry'];
console.log(listify(items, { finalWord: 'or' })); // Output: 'apple, banana, or cherry'
Oxford Comma
This feature allows you to enable or disable the Oxford comma. By setting oxfordComma to false, the comma before the final word is omitted.
const listify = require('listify');
const items = ['apple', 'banana', 'cherry'];
console.log(listify(items, { oxfordComma: false })); // Output: 'apple, banana and cherry'
Other packages similar to listify
humanize-list
The humanize-list package provides similar functionality to listify by converting arrays into human-readable lists. It also supports custom separators and final words, but it may have different default behaviors and additional options.
array-to-sentence
The array-to-sentence package is another alternative that converts arrays into readable sentences. It focuses on simplicity and ease of use, offering basic customization options for separators and conjunctions.
join-array
The join-array package allows you to join array elements into a string with custom separators and conjunctions. It is similar to listify but may offer different customization options and default settings.
#listify
Turn an array into a list of comma-separated values, appropriate for use in an English sentence.
Example
var listify = require('listify');
assert(listify([1, 2]) === '1 and 2');
assert(listify([1, 2, 3]) === '1, 2, and 3');
assert(listify([1, 2, 3, 4]) === '1, 2, 3, and 4');
assert(listify([1, 2, 3], { separator: '… ' }) === '1… 2… and 3');
assert(listify([1, 2, 3], { finalWord: false }) === '1, 2, 3');
assert(listify([1, 2, 3], { separator: '… ', finalWord: 'or' }) === '1… 2… or 3');
Tests
Simply clone the repo, npm install
, and run npm test