Skip to content

Commit fb48e1c

Browse files
authored
updated readme with all the infos (#9)
1 parent 00fb75d commit fb48e1c

1 file changed

Lines changed: 90 additions & 1 deletion

File tree

README.md

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,90 @@
1-
# ts-array-sort
1+
# ts-array-sort
2+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/sullyTheDev/ts-array-sort/NPM%20publish%20CD%20workflow?style=flat-square) ![npm](https://img.shields.io/npm/v/ts-array-sort?style=flat-square) ![NPM License](https://img.shields.io/npm/l/ts-array-sort?style=flat-square) ![npm bundle size](https://img.shields.io/bundlephobia/min/ts-array-sort?style=flat-square)
3+
4+
ts-array-sort is a simple utility to assist in sorting arrays for Typescript/Javascript. It supports sorting of numbers, strings, and objects out of the box and is very flexible when it comes to sorting objects with multiple or nested properties.
5+
6+
- **number** and **string** sorting made easy
7+
- **nested object property** sorting is supported.
8+
- **multiple object property** sorting in a single, readable statement.
9+
- **No** run-time dependencies
10+
- **~3kb** in size
11+
12+
## Usage
13+
---
14+
Install the package from npm
15+
```
16+
npm i ts-array-sort
17+
```
18+
19+
Import the `ArraySorter` and `SortOrder` module members.
20+
```ts
21+
import { ArraySorter, SortOrder } from 'ts-array-sorter';
22+
```
23+
Build the sort function and pass it into `Array.sort()`
24+
```ts
25+
import { ArraySorter, SortOrder } from 'ts-array-sorter';
26+
// ...
27+
28+
// Inline example using config object
29+
console.log([3,5,7,1].sort(new ArraySorter({sortOrder: SortOrder.desc}).sort()))
30+
// outputs [7,5,3,1]
31+
```
32+
33+
## API
34+
---
35+
### constructor
36+
```ts
37+
const sorter = new ArraySorter();
38+
```
39+
or you can pass in an optional config object
40+
```ts
41+
const sorter = new ArraySorter({sortOrder: SortOrder.desc, properties: ['myProp1', 'myProp2']})
42+
```
43+
### Methods
44+
---
45+
#### **sortOrder**
46+
Method that changes the sort order used by the built sort function.
47+
the sort order defaults to ascending, meaning you should only ever have to use this function to change to descending or back from descending.
48+
- @param `sortOrder: SortOrder`
49+
- @returns `ArraySorter` (this)
50+
```ts
51+
new ArraySorter().sortOrder(SortOrder.desc);
52+
```
53+
---
54+
#### **sortBy**
55+
Method that adds a property to be sorted on. This function should only need to be called when sorting objects.
56+
*Note: Any calls to this function when sorting string or number arrays will be ignored.*
57+
58+
- @param `property: PropertyKey`
59+
- @returns `ArraySorter` (this)
60+
61+
```ts
62+
// basic usage
63+
new ArraySorter().sortBy('myProperty');
64+
```
65+
66+
When sorting objects and no properties are provided or properties that do not exist on the objects are provided, an error will be thrown with a corresponding error message.
67+
68+
You can specify a *n* amount of properties as long as they exist on every object in the array. If you specify more then 1 property to sort on, **the sort is applied in the order in which you add them**.
69+
70+
Additionally, ts-array-sort supports sorting on properties that are contained within nested data structures. To do this, seperate the object properties with a `.` as you would normally do with dot notation in JS, just in string form.
71+
```ts
72+
// nested property example
73+
new ArraySorter().sortBy('nestedProp.id');
74+
// adds an entry to sort on that looks like {nestedProp: {id: 1}}
75+
76+
// double nested
77+
new ArraySorter().sortBy('user.name.firstName');
78+
// adds an entry to sort on that looks like
79+
// {user: {name: {firstName: 'Chris'}}}
80+
```
81+
---
82+
#### **sort**
83+
Method which provides the sort function that takes into account all the previously selected config options (i.e. sort order, properties). Pass this function into the built in `Array.propotype.sort()` method and it will sort the array based on the previously entered configuration.
84+
- @returns `(a: T, b: T) => number`
85+
86+
```ts
87+
// example usage
88+
const sorter = new ArraySorter().sortOrder(SortOrder.desc).sort();
89+
myArray.sort(sorter);
90+
```

0 commit comments

Comments
 (0)