Files
portfolio2023/build/node_modules/listify/test/index.js
T
2023-11-24 22:35:41 +01:00

64 lines
2.0 KiB
JavaScript

'use strict';
var test = require('tape');
var listify = require('../');
test('throws when not given an array', function (t) {
t['throws'](function () { listify(); }, TypeError, 'requires an array');
t.end();
});
test('listifies 0 items', function (t) {
t.equal(listify([]), '', 'empty list gives empty string');
t.end();
});
test('listifies 1 item', function (t) {
t.equal(listify([1]), '1', 'one item is just toStringed');
t.end();
});
test('listifies 2 items', function (t) {
t.equal(listify([1, 2]), '1 and 2', 'two items gives no separator');
t.end();
});
test('listifies 2 items, supports no finalWord', function (t) {
t.equal(listify([1, 2], { finalWord: false }), '1, 2', 'two items, no final word, gives only separator');
t.end();
});
test('listifies 3 items', function (t) {
t.equal(listify([1, 2, 3]), '1, 2, and 3', 'listifies three items');
t.end();
});
test('supports separator', function (t) {
t.equal(listify([1, 2, 3], { separator: '… ' }), '1… 2… and 3', 'listifies with separator');
t.end();
});
test('supports finalWord', function (t) {
t.equal(listify([1, 2, 3], { finalWord: 'or' }), '1, 2, or 3', 'listifies with no finalWord');
t.end();
});
test('stringifies separator and finalWord', function (t) {
var sep = { toString: function () { return 'foo'; } };
var word = { toString: function () { return 'bar'; } };
t.equal(listify([1, 2, 3], { finalWord: word, separator: sep }), '1foo2foobar3', 'stringifies options');
t.end();
});
test('properly handles whitespace-only strings', function (t) {
t.equal(listify(['a', ' ', 'b', 'c']), 'a, b, and c', 'ignores whitespace-only items (3 + 1)');
t.equal(listify(['a', ' ', 'b']), 'a and b', 'ignores whitespace-only items (2 + 1)');
t.equal(listify(['a', ' ']), 'a', 'ignores whitespace-only items (1 + 1)');
t.end();
});
test('properly handles trailing whitespace', function (t) {
t.equal(listify([' a', 'b ', ' c ', 'd']), ' a, b , c , and d', 'does not trim whitespace from items');
t.end();
});