2.1.0 • Published 2 years ago

property-sort v2.1.0

Weekly downloads
11
License
ISC
Repository
github
Last release
2 years ago

Property Sort

npm

property-sort provides a method that lets you setup sorting in alphabetical, numerical ordering and more. No more hassle sorting your arrays.

Example

let collection = [
  {
    user: { name: "Denzil Norris", age: 22 }
  },
  {
    user: { name: "Clarissa London", age: 20 }
  }
];

let options = {
  direction: SortDirections.Ascending,
  sortKey: ['user', 'name'],
  locale: LanguageCode.en
};

sortByProperty(collection, options);
/*[
  {
    user: { name: "Clarissa London", age: 20 }
  },
  {
    user: { name: "Denzil Norris", age: 22 }
  }
]*/

options = {
  direction: SortDirections.Descending,
  sortKey: ['user', 'age'],
  locale: LanguageCode.en
};

sortByProperty(collection, options);
/*[
  {
    user: { name: "Denzil Norris", age: 22 }
  },
  {
    user: { name: "Clarissa London", age: 20 }
  }
]*/

Getting started

Install the package...

npm install property-sort

Usage

Plain TypeScript:

import sortByProperty, { SortDirections, SortOptions, SortObject } from 'property-sort';
import LanguageCode from 'language-code';

...
let collection: SortObject<YourItem[]> = ...;
let options: SortOptions = {
  direction: SortDirections.Ascending,
  sortKey: ['adress', 'suite'],
  locale: LanguageCode.en,
  numeric: true
};

collection = sortByProperty(collection, options);

Properties

sortByProperty (method):

sortByProperty(collection: T[], options: SortOptions): T[]
PropertyTypeDescription
collectionSortObject<T>array to sort
optionsSortOptionsoptions on sorting

SortOptions (interface):

PropertyTypeDescription
sortKeySortTypeKey to sort the array by
directionSortDirectionSets sort to ascending/descending order
locale?LocaleTypeUsed to compare in chosen locale
numeric?booleanUsed when comparing strings using numeric values

SortDirection (interface)

KeyValueDescription
None0Used to reset sorting
Ascending1Used to sort in ascending order
Descending2Used to sort in descending order

SortObject<T> (type)

SortObject is a generic type, which takes in an array of unknown objects. This will allow you to to work with your own interfaces while benefitting from writing typed.

LocaleType (type)

A standardized nomenclature used to classify languages. The LocaleType accepts a wildcard string or an ENUM using LanguageCode. As a fallback LocaleType will be set to "en" (English), this will be done when leaving it as undefined or an invalid LocaleType.

SortType (type)

SortType is a type which accepts a string or array of strings, which represents the key you would like to sort the array by. Example: 'key' or following, if the key is in an a nested object: ['nested','key'].

2.1.0

2 years ago

1.2.2

2 years ago

2.0.0-next.2

2 years ago

2.0.0-next.3

2 years ago

2.0.0-next.0

2 years ago

2.0.0-next.1

2 years ago

2.0.1

2 years ago

2.0.1-next.0

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.0

3 years ago

0.0.14

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.5

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago