]>
Commit | Line | Data |
---|---|---|
3bb6c526 C |
1 | // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ] |
2 | function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) { | |
69818c93 C |
3 | let field: string |
4 | let direction: 'ASC' | 'DESC' | |
5c39adb7 | 5 | |
feb4bdfd C |
6 | if (value.substring(0, 1) === '-') { |
7 | direction = 'DESC' | |
8 | field = value.substring(1) | |
9 | } else { | |
10 | direction = 'ASC' | |
11 | field = value | |
12 | } | |
5c39adb7 | 13 | |
3bb6c526 | 14 | return [ [ field, direction ], lastSort ] |
5c39adb7 C |
15 | } |
16 | ||
3bb6c526 C |
17 | function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) { |
18 | let [ firstSort ] = getSort(value) | |
792dbaf0 | 19 | |
3bb6c526 C |
20 | if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ] |
21 | return [ firstSort, lastSort ] | |
792dbaf0 GS |
22 | } |
23 | ||
3fd3ab2d C |
24 | function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') { |
25 | if (validator(value) === false) { | |
26 | throw new Error(`"${value}" is not a valid ${fieldName}.`) | |
27 | } | |
28 | } | |
29 | ||
5c39adb7 C |
30 | // --------------------------------------------------------------------------- |
31 | ||
65fcc311 | 32 | export { |
792dbaf0 | 33 | getSort, |
3fd3ab2d C |
34 | getSortOnModel, |
35 | throwIfNotValid | |
65fcc311 | 36 | } |