]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/models/utils.ts
Avoid too many requests and fetching outbox
[github/Chocobozzz/PeerTube.git] / server / models / utils.ts
... / ...
CommitLineData
1// Translate for example "-name" to [ 'name', 'DESC' ]
2function getSort (value: string) {
3 let field: string
4 let direction: 'ASC' | 'DESC'
5
6 if (value.substring(0, 1) === '-') {
7 direction = 'DESC'
8 field = value.substring(1)
9 } else {
10 direction = 'ASC'
11 field = value
12 }
13
14 return [ field, direction ]
15}
16
17function getSortOnModel (model: any, value: string) {
18 let sort = getSort(value)
19
20 if (model) return [ model, sort[0], sort[1] ]
21 return sort
22}
23
24function 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
30// ---------------------------------------------------------------------------
31
32export {
33 getSort,
34 getSortOnModel,
35 throwIfNotValid
36}