contact-sort
Sort and group contacts by alphabet.
Install
npm install contact-sort --save
# or
yarn add contact-sort
Usage
import contactSort from 'contact-sort';
const mockData = {
field: 'name',
data: [
{
name: 'Tom',
description: 'A smart boy',
avatar: 'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
{
name: 'Sylvia',
description: 'A beautiful girl',
avatar: 'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
{
name: 'Jack',
description: 'A handsome man',
avatar: 'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
{
name: 'Bill Gates',
description: 'A successful entrepreneur',
avatar: 'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
{
name: 'Bryant',
description: 'A basketball player',
avatar: 'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
{
name: '小明',
description: 'A poor guy',
avatar: 'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
],
};
describe('index.ts', () => {
it('should return correct data', () => {
expect(contactSort(mockData)).toEqual({
catalogue: ['B', 'J', 'S', 'T', '#'],
data: {
B: [
{
name: 'Bill Gates',
description: 'A successful entrepreneur',
avatar:
'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
{
name: 'Bryant',
description: 'A basketball player',
avatar:
'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
],
J: [
{
name: 'Jack',
description: 'A handsome man',
avatar:
'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
],
S: [
{
name: 'Sylvia',
description: 'A beautiful girl',
avatar:
'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
],
T: [
{
name: 'Tom',
description: 'A smart boy',
avatar:
'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
],
'#': [
{
name: '小明',
description: 'A poor guy',
avatar:
'https://avatars1.githubusercontent.com/u/26563778?s=40&v=4',
},
],
},
});
});