truncate-people

2.0.0 • Public • Published

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:

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) =>
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

Readme

Keywords

Package Sidebar

Install

npm i truncate-people

Weekly Downloads

1

Version

2.0.0

License

MIT

Last publish

Collaborators

  • amowu