-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.d.ts
61 lines (45 loc) · 1.97 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
export type Property<T> = string | ((element: T) => unknown) | Array<string | ((element: T) => unknown)>;
export type Options = {
/**
One or more locales to use when sorting strings.
Should be a locale string or array of locale strings that contain one or more language or locale tags.
If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale.
If you omit this parameter, the default locale of the JavaScript runtime is used.
This parameter must conform to BCP 47 standards. See {@link Intl.Collator} for more details.
*/
readonly locales?: string | readonly string[];
/**
Comparison options.
See {@link Intl.Collator} for more details.
*/
readonly localeOptions?: Intl.CollatorOptions;
};
/**
Sort an array on an object property.
@param array - The array to sort.
@param property - The string can be a [dot path](https://github.com/sindresorhus/dot-prop) to a nested object property. Prefix it with `-` to sort it in descending order.
@returns A new sorted version of the given array.
@example
```
import sortOn from 'sort-on';
// Sort by an object property
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], 'x');
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]
// Sort descending by an object property
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], '-x');
//=> [{x: 'c'}, {x: 'b'}, {x: 'a'}]
// Sort by a nested object property
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], 'x.y');
//=> [{x: {y: 'a'}}, {x: {y: 'b'}}]
// Sort descending by a nested object property
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], '-x.y');
//=> [{x: {y: 'b'}, {x: {y: 'a'}}}]
// Sort by the `x` property, then `y`
sortOn([{x: 'c', y: 'c'}, {x: 'b', y: 'a'}, {x: 'b', y: 'b'}], ['x', 'y']);
//=> [{x: 'b', y: 'a'}, {x: 'b', y: 'b'}, {x: 'c', y: 'c'}]
// Sort by the returned value
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], el => el.x);
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]
```
*/
export default function sortOn<T>(array: readonly T[], property: Property<T>, options?: Options): T[];