You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
881 B

  1. const _ = require('lodash')
  2. const Filter = require('scim-query-filter-parser')
  3. module.exports = {
  4. generateSuccess (msg) {
  5. return {
  6. succeeded: true,
  7. errorCode: 0,
  8. slug: 'ok',
  9. message: _.defaultTo(msg, 'Operation succeeded.')
  10. }
  11. },
  12. generateError (err, complete = true) {
  13. const error = {
  14. succeeded: false,
  15. errorCode: err.code || 1,
  16. slug: err.name,
  17. message: err.message || 'An unexpected error occured.'
  18. }
  19. return (complete) ? { responseResult: error } : error
  20. },
  21. filter (arr, filterString) {
  22. const prvFilter = new Filter(_.toString(filterString).replace(/'/g, `"`))
  23. return arr.filter(prvFilter.test)
  24. },
  25. orderBy (arr, orderString) {
  26. let orderParams = _.zip(orderString.split(',').map(ord => _.trim(ord).split(' ').map(_.trim)))
  27. return _.orderBy(arr, orderParams[0], orderParams[1])
  28. }
  29. }