2.0.0 • Published 7 years ago

truncate-people v2.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

truncate-people

NPM version Build Status Coveralls Status Dependency Status Downloads

Facebook-like truncate text by the number of people

Install

npm install --save truncate-people

Usage

import truncatePeople from 'truncate-people'

// return 'Ashin and Monster'
truncatePeople(['Ashin', 'Monster'])

// return 'Ashin, Monster and Stone'
truncatePeople(['Ashin', 'Monster', 'Stone'])

// return 'Ashin, Monster and 2 other people'
truncatePeople(['Ashin', 'Monster', 'Stone', 'Masa'])

// return 'Ashin, Monster and 3 other people'
truncatePeople(['Ashin', 'Monster', 'Stone', 'Masa', 'Ming'])

Documentation

API:

truncatePeople(list: string[], options: Object): string

Params:

NameTypeDescription
liststring[]A list of names.
optionsObjectThe custom truncate options (optional).
options.noOneTemplatestringText of nobody, default is ''.
options.compileOnePeopleTemplatefunction(a: string): stringDefault is (a) => '${a}'.
options.compileTwoPeopleTemplatefunction(a: string, b: string): stringDefault is (a, b) => '${a} and ${b}'.
options.compileManyPeopleTemplatefunction(a: string, b: string, numberOfOthers: number): stringDefault is (a, b, numberOfOthers) => | '${a}, ${b} and ${numberOfOthers} other people'.
options.compileThreePeopleTemplatefunction(a: string, b: string, c: string): stringDefault is (a, b, c) => '${a}, ${b} and ${c}'.

Return:

string Result of the truncated text.

Example:

// return 'Ashin 和 Monster'
truncatePeople(['Ashin', 'Monster'], {
  compileTwoPeopleTemplate: (a, b) => `${a} 和 ${b}`
})

// return 'Ashin、Monster 和 Stone'
truncatePeople(['Ashin', 'Monster', 'Stone'], {
  compileThreePeopleTemplate: (a, b, c) => `${a}、${b} 和 ${c}`
})

// return 'Ashin、Monster 和其他 2 人'
truncatePeople(['Ashin', 'Monster', 'Stone', 'Masa'], {
  compileManyPeopleTemplate: (a, b, numberOfOthers) => `${a}、${b} 和其他 ${numberOfOthers} 人`
})

// return 'Ashin, Monster and 98 other people'
truncatePeople(['Ashin', 'Monster', 'Stone', 'Masa', 'Ming'], {
  compileManyPeopleTemplate: (a, b, numberOfOthers) => `${a}, ${b} and ${100 - 2} other people`
})

License

MIT © Amo Wu