1 // Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
2 function getSort (value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
4 let direction: 'ASC' | 'DESC'
6 if (value.substring(0, 1) === '-') {
8 field = value.substring(1)
14 return [ [ field, direction ], lastSort ]
17 function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
18 let [ firstSort ] = getSort(value)
20 if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ]
21 return [ firstSort, lastSort ]
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}.`)
30 // ---------------------------------------------------------------------------