enumerate-words
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -1,9 +0,12 @@ | ||
// (words: string[], cutoff: number, placeholder: string) => string | ||
const enumerate = (words, cutoff = 4, placeholder) => { | ||
if (words.length === 0) return '' | ||
else if (words.length === 1) return words[0] | ||
else if (words.length >= cutoff) return `${words.length} ${placeholder}` | ||
else return `${words.slice(0, words.length-1).join(', ')}${words.length === 2 ? '' : ','} and ${[...words].pop()}` | ||
} | ||
module.exports = enumerate | ||
"use strict"; | ||
exports.enumerate = function (_a) { | ||
var words = _a.words, _b = _a.cutoff, cutoff = _b === void 0 ? 4 : _b, _c = _a.placeholder, placeholder = _c === void 0 ? 'Things' : _c, _d = _a.andStr, andStr = _d === void 0 ? 'and' : _d, _e = _a.noOxford, noOxford = _e === void 0 ? false : _e; | ||
if (words.length === 0) | ||
return ''; | ||
else if (words.length === 1) | ||
return words[0]; | ||
else if (words.length >= cutoff) | ||
return words.length + " " + placeholder; | ||
else | ||
return "" + words.slice(0, words.length - 1).join(', ') + (words.length === 2 || noOxford ? '' : ',') + " " + andStr + " " + words.slice().pop(); | ||
}; |
{ | ||
"name": "enumerate-words", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "human-friendly enumeration of words", | ||
"main": "enumerate.js", | ||
"main": "./enumerate.js", | ||
"main:esnext": "./enumerate.es6.js", | ||
"typings": "./enumerate.d.ts", | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha" | ||
"build": "./node_modules/typescript/bin/tsc -d -t es6 -m es2015 enumerate.ts && mv enumerate.js enumerate.es6.js && tsc -t es5 -m commonjs ./enumerate.ts;", | ||
"test": "./node_modules/mocha/bin/mocha", | ||
"watch": "./node_modules/typescript/bin/tsc -w -t es5 -m commonjs ./enumerate.ts;" | ||
}, | ||
@@ -22,4 +26,5 @@ "repository": { | ||
"expect": "1.20.2", | ||
"mocha": "3.0.2" | ||
"mocha": "3.0.2", | ||
"typescript": "^1.8.10" | ||
} | ||
} |
@@ -9,11 +9,28 @@ ## enumerate-words | ||
```typescript | ||
enumerate(words: string[], cutoff: number, placeholder: string) => string | ||
enumerate({ | ||
words: string[], | ||
cutoff?: number, | ||
placeholder?: string, | ||
andStr?: string, | ||
noOxford?: boolean | ||
}) => string | ||
``` | ||
## examples | ||
```javascript | ||
console.log(enumerate(['Boo'], 4, 'People')) // 'Boo' | ||
console.log(enumerate(['Boo', 'Bob'], 4, 'People')) // 'Boo and Bob' | ||
console.log(enumerate(['Boo', 'Bob', 'Foo'], 4, 'People')) // 'Boo, Bob and Foo' | ||
console.log(enumerate(['Boo', 'Bob', 'Foo', 'Moo'], 4, 'People')) // '4 People' | ||
``` | ||
See [tests](https://github.com/albertywu/enumerate/blob/master/test/enumerate.spec.js) for all supported usages. | ||
## contributing | ||
Install | ||
`npm install` | ||
Build | ||
`npm run build` | ||
`npm run watch` | ||
Test | ||
`npm test` | ||
Put up a PR when tests pass! |
@@ -1,36 +0,102 @@ | ||
const enumerate = require('../enumerate') | ||
const enumerate = require('../enumerate').enumerate | ||
const expect = require('expect') | ||
const runTests = (fn, tests) => tests.forEach(([args, output]) => expect(fn.apply(this, args)).toBe(output)) | ||
describe('enumerate', function() { | ||
it('should pass all tests', function() { | ||
const tests = [ | ||
[ | ||
[[], 4, 'People'], | ||
'' | ||
], | ||
[ | ||
[['Rick'], 4, 'People'], | ||
'Rick' | ||
], | ||
[ | ||
[['Rick', 'Boris'], 4, 'People'], | ||
'Rick and Boris' | ||
], | ||
[ | ||
[['Rick', 'Boris', 'Albert'], 4, 'People'], | ||
'Rick, Boris, and Albert' | ||
], | ||
[ | ||
[['Rick', 'Boris', 'Albert', 'Parag'], 4, 'People'], | ||
'4 People' | ||
] | ||
] | ||
describe('defaults', function() { | ||
it('should return an empty string, when words is length 0', function() { | ||
expect( | ||
enumerate({ | ||
words: [] | ||
}) | ||
).toBe('') | ||
}) | ||
runTests(enumerate, tests) | ||
it('should return "X", when words is length 1', function() { | ||
expect( | ||
enumerate({ | ||
words: ['Screwdriver'] | ||
}) | ||
).toBe('Screwdriver') | ||
}) | ||
it('should return "X and Y" when words is length 2', function() { | ||
expect( | ||
enumerate({ | ||
words: ['Screwdriver', 'Hammer'] | ||
}) | ||
).toBe('Screwdriver and Hammer') | ||
}) | ||
it('should return "X, Y, and Z" when words is length 3', function() { | ||
expect( | ||
enumerate({ | ||
words: ['Screwdriver', 'Hammer', 'Drill'] | ||
}) | ||
).toBe('Screwdriver, Hammer, and Drill') | ||
}) | ||
it('should return "4 Things" when words is length 4', function() { | ||
expect( | ||
enumerate({ | ||
words: ['Screwdriver', 'Hammer', 'Drill', 'Saw'] | ||
}) | ||
).toBe('4 Things') | ||
}) | ||
}) | ||
describe('cutoff option', function() { | ||
it('should cut off of at 3, when words is length 3 and cutoff is 3', function() { | ||
expect( | ||
enumerate({ | ||
words: ['Screwdriver', 'Hammer', 'Drill'], | ||
cutoff: 3 | ||
}) | ||
).toBe('3 Things') | ||
}) | ||
it('should not cut off of at 2, when words is length 2 and cutoff is 3', function() { | ||
expect( | ||
enumerate({ | ||
words: ['Screwdriver', 'Hammer'], | ||
cutoff: 3 | ||
}) | ||
).toBe('Screwdriver and Hammer') | ||
}) | ||
}) | ||
describe('placeholder option', function() { | ||
it('should use a difference placeholder word, when placeholder is specified and cutoff is reached', function() { | ||
expect( | ||
enumerate({ | ||
words: ['Screwdriver', 'Hammer', 'Drill'], | ||
cutoff: 3, | ||
placeholder: 'Thingamabobs' | ||
}) | ||
).toBe('3 Thingamabobs') | ||
}) | ||
}) | ||
describe('andStr option', function() { | ||
it('should use &, when an alternate andStr is specified', function() { | ||
expect( | ||
enumerate({ | ||
words: ['Rob', 'Bob', 'Todd'], | ||
andStr: '&' | ||
}) | ||
).toBe('Rob, Bob, & Todd') | ||
}) | ||
}) | ||
describe('noOxford option', function() { | ||
it('should not insert the oxford comma, if noOxford is true', function() { | ||
expect( | ||
enumerate({ | ||
words: ['Rob', 'Bob', 'Todd'], | ||
andStr: '&', | ||
noOxford: true | ||
}) | ||
).toBe('Rob, Bob & Todd') | ||
}); | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7505
10
154
36
3