2.0.0 • Published 8 years ago
truncate-people v2.0.0
truncate-people
Facebook-like truncate text by the number of people
Install
npm install --save truncate-peopleUsage
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): stringParams:
| Name | Type | Description | 
|---|---|---|
| list | string[] | A list of names. | 
| options | Object | The custom truncate options (optional). | 
| options.noOneTemplate | string | Text of nobody, default is ''. | 
| options.compileOnePeopleTemplate | function(a: string): string | Default is (a) => '${a}'. | 
| options.compileTwoPeopleTemplate | function(a: string, b: string): string | Default is (a, b) => '${a} and ${b}'. | 
| options.compileManyPeopleTemplate | function(a: string, b: string, numberOfOthers: number): string | Default is (a, b, numberOfOthers) => | '${a}, ${b} and ${numberOfOthers} other people'. | 
| options.compileThreePeopleTemplate | function(a: string, b: string, c: string): string | Default 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
2.0.0
8 years ago