helpers.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const moment = require('moment');
  2. module.exports = {
  3. /**
  4. * Takes an expression such as 30d and returns a moment object of that date in future
  5. *
  6. * Key Shorthand
  7. * ==================
  8. * years y
  9. * quarters Q
  10. * months M
  11. * weeks w
  12. * days d
  13. * hours h
  14. * minutes m
  15. * seconds s
  16. * milliseconds ms
  17. *
  18. * @param {String} expression
  19. * @returns {Object}
  20. */
  21. parseDatePeriod: function (expression) {
  22. let matches = expression.match(/^([0-9]+)(y|Q|M|w|d|h|m|s|ms)$/m);
  23. if (matches) {
  24. return moment().add(matches[1], matches[2]);
  25. }
  26. return null;
  27. },
  28. convertIntFieldsToBool: function (obj, fields) {
  29. fields.forEach(function (field) {
  30. if (typeof obj[field] !== 'undefined') {
  31. obj[field] = obj[field] === 1;
  32. }
  33. });
  34. return obj;
  35. },
  36. convertBoolFieldsToInt: function (obj, fields) {
  37. fields.forEach(function (field) {
  38. if (typeof obj[field] !== 'undefined') {
  39. obj[field] = obj[field] ? 1 : 0;
  40. }
  41. });
  42. return obj;
  43. }
  44. };