X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Futils.ts;h=1bf61d2a693021aff86a88732587dfabe3ccd87b;hb=493799609519c71a5f79b3e92444c5fed6f772dd;hp=601811913d5d24dde4aedcd7486882638ab3ef86;hpb=65fcc3119c334b75dd13bcfdebf186afdc580a8f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/utils.ts b/server/models/utils.ts index 601811913..1bf61d2a6 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,7 +1,7 @@ // Translate for example "-name" to [ 'name', 'DESC' ] -function getSort (value) { - let field - let direction +function getSort (value: string) { + let field: string + let direction: 'ASC' | 'DESC' if (value.substring(0, 1) === '-') { direction = 'DESC' @@ -14,8 +14,22 @@ function getSort (value) { return [ field, direction ] } +function addMethodsToModel (model: any, classMethods: Function[], instanceMethods: Function[] = []) { + classMethods.forEach(m => model[m.name] = m) + instanceMethods.forEach(m => model.prototype[m.name] = m) +} + +function getSortOnModel (model: any, value: string) { + let sort = getSort(value) + + if (model) return [ { model: model }, sort[0], sort[1] ] + return sort +} + // --------------------------------------------------------------------------- export { - getSort + addMethodsToModel, + getSort, + getSortOnModel }