contact-sort
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

contact-sort

Sort and group contacts by alphabet.

NPM version build status Test coverage npm download

Install

npm install contact-sort --save
# or
yarn add contact-sort

Usage

/**
 * @jest-environment jsdom
 */
 
import contactSort from 'contact-sort';
 
 
// field: the key of your contact data which is used for sort and group
// data: the array of you contacs whose item must have field value as key;
 
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',
          },
        ],
      },
    });
});

Package Sidebar

Install

npm i contact-sort

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

12.6 kB

Total Files

12

Last publish

Collaborators

  • dxd_sjtu