datemath.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import moment from 'moment';
  4. var units = ['y', 'M', 'w', 'd', 'h', 'm', 's'];
  5. export function parse(text, roundUp?, timezone?) {
  6. if (!text) {
  7. return undefined;
  8. }
  9. if (moment.isMoment(text)) {
  10. return text;
  11. }
  12. if (_.isDate(text)) {
  13. return moment(text);
  14. }
  15. var time;
  16. var mathString = '';
  17. var index;
  18. var parseString;
  19. if (text.substring(0, 3) === 'now') {
  20. if (timezone === 'utc') {
  21. time = moment.utc();
  22. } else {
  23. time = moment();
  24. }
  25. mathString = text.substring('now'.length);
  26. } else {
  27. index = text.indexOf('||');
  28. if (index === -1) {
  29. parseString = text;
  30. mathString = ''; // nothing else
  31. } else {
  32. parseString = text.substring(0, index);
  33. mathString = text.substring(index + 2);
  34. }
  35. // We're going to just require ISO8601 timestamps, k?
  36. time = moment(parseString, moment.ISO_8601);
  37. }
  38. if (!mathString.length) {
  39. return time;
  40. }
  41. return parseDateMath(mathString, time, roundUp);
  42. }
  43. export function isValid(text) {
  44. var date = parse(text);
  45. if (!date) {
  46. return false;
  47. }
  48. if (moment.isMoment(date)) {
  49. return date.isValid();
  50. }
  51. return false;
  52. }
  53. export function parseDateMath(mathString, time, roundUp?) {
  54. var dateTime = time;
  55. var i = 0;
  56. var len = mathString.length;
  57. while (i < len) {
  58. var c = mathString.charAt(i++);
  59. var type;
  60. var num;
  61. var unit;
  62. if (c === '/') {
  63. type = 0;
  64. } else if (c === '+') {
  65. type = 1;
  66. } else if (c === '-') {
  67. type = 2;
  68. } else {
  69. return undefined;
  70. }
  71. if (isNaN(mathString.charAt(i))) {
  72. num = 1;
  73. } else if (mathString.length === 2) {
  74. num = mathString.charAt(i);
  75. } else {
  76. var numFrom = i;
  77. while (!isNaN(mathString.charAt(i))) {
  78. i++;
  79. if (i > 10) {
  80. return undefined;
  81. }
  82. }
  83. num = parseInt(mathString.substring(numFrom, i), 10);
  84. }
  85. if (type === 0) {
  86. // rounding is only allowed on whole, single, units (eg M or 1M, not 0.5M or 2M)
  87. if (num !== 1) {
  88. return undefined;
  89. }
  90. }
  91. unit = mathString.charAt(i++);
  92. if (!_.includes(units, unit)) {
  93. return undefined;
  94. } else {
  95. if (type === 0) {
  96. if (roundUp) {
  97. dateTime.endOf(unit);
  98. } else {
  99. dateTime.startOf(unit);
  100. }
  101. } else if (type === 1) {
  102. dateTime.add(num, unit);
  103. } else if (type === 2) {
  104. dateTime.subtract(num, unit);
  105. }
  106. }
  107. }
  108. return dateTime;
  109. }