| 
									
										
										
										
											2017-12-20 12:33:33 +01:00
										 |  |  | import _ from 'lodash'; | 
					
						
							|  |  |  | import moment from 'moment'; | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 14:26:50 +02:00
										 |  |  | const units = ['y', 'M', 'w', 'd', 'h', 'm', 's']; | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 09:55:57 -07:00
										 |  |  | export function parse(text, roundUp?, timezone?) { | 
					
						
							| 
									
										
										
										
											2017-12-19 16:06:54 +01:00
										 |  |  |   if (!text) { | 
					
						
							|  |  |  |     return undefined; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (moment.isMoment(text)) { | 
					
						
							|  |  |  |     return text; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (_.isDate(text)) { | 
					
						
							|  |  |  |     return moment(text); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-30 09:03:11 +02:00
										 |  |  |   let time; | 
					
						
							|  |  |  |   let mathString = ''; | 
					
						
							|  |  |  |   let index; | 
					
						
							|  |  |  |   let parseString; | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-20 12:33:33 +01:00
										 |  |  |   if (text.substring(0, 3) === 'now') { | 
					
						
							|  |  |  |     if (timezone === 'utc') { | 
					
						
							| 
									
										
										
										
											2017-07-24 09:55:57 -07:00
										 |  |  |       time = moment.utc(); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       time = moment(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-20 12:33:33 +01:00
										 |  |  |     mathString = text.substring('now'.length); | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2017-12-20 12:33:33 +01:00
										 |  |  |     index = text.indexOf('||'); | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |     if (index === -1) { | 
					
						
							|  |  |  |       parseString = text; | 
					
						
							| 
									
										
										
										
											2017-12-20 12:33:33 +01:00
										 |  |  |       mathString = ''; // nothing else
 | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |       parseString = text.substring(0, index); | 
					
						
							|  |  |  |       mathString = text.substring(index + 2); | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |     // We're going to just require ISO8601 timestamps, k?
 | 
					
						
							| 
									
										
										
										
											2016-05-19 07:58:44 +02:00
										 |  |  |     time = moment(parseString, moment.ISO_8601); | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |   if (!mathString.length) { | 
					
						
							|  |  |  |     return time; | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |   return parseDateMath(mathString, time, roundUp); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 12:21:13 +01:00
										 |  |  | export function isValid(text) { | 
					
						
							| 
									
										
										
										
											2018-08-29 14:26:50 +02:00
										 |  |  |   const date = parse(text); | 
					
						
							| 
									
										
										
										
											2015-09-17 11:21:38 +02:00
										 |  |  |   if (!date) { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (moment.isMoment(date)) { | 
					
						
							|  |  |  |     return date.isValid(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 12:21:13 +01:00
										 |  |  | export function parseDateMath(mathString, time, roundUp?) { | 
					
						
							| 
									
										
										
										
											2018-08-29 14:26:50 +02:00
										 |  |  |   const dateTime = time; | 
					
						
							| 
									
										
										
										
											2018-08-30 09:03:11 +02:00
										 |  |  |   let i = 0; | 
					
						
							| 
									
										
										
										
											2018-08-29 14:26:50 +02:00
										 |  |  |   const len = mathString.length; | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |   while (i < len) { | 
					
						
							| 
									
										
										
										
											2018-08-29 14:26:50 +02:00
										 |  |  |     const c = mathString.charAt(i++); | 
					
						
							| 
									
										
										
										
											2018-08-30 09:03:11 +02:00
										 |  |  |     let type; | 
					
						
							|  |  |  |     let num; | 
					
						
							|  |  |  |     let unit; | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-20 12:33:33 +01:00
										 |  |  |     if (c === '/') { | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |       type = 0; | 
					
						
							| 
									
										
										
										
											2017-12-20 12:33:33 +01:00
										 |  |  |     } else if (c === '+') { | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |       type = 1; | 
					
						
							| 
									
										
										
										
											2017-12-20 12:33:33 +01:00
										 |  |  |     } else if (c === '-') { | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |       type = 2; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return undefined; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (isNaN(mathString.charAt(i))) { | 
					
						
							|  |  |  |       num = 1; | 
					
						
							|  |  |  |     } else if (mathString.length === 2) { | 
					
						
							|  |  |  |       num = mathString.charAt(i); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-08-29 14:26:50 +02:00
										 |  |  |       const numFrom = i; | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |       while (!isNaN(mathString.charAt(i))) { | 
					
						
							|  |  |  |         i++; | 
					
						
							| 
									
										
										
										
											2017-12-19 16:06:54 +01:00
										 |  |  |         if (i > 10) { | 
					
						
							|  |  |  |           return undefined; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |       num = parseInt(mathString.substring(numFrom, i), 10); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |     if (type === 0) { | 
					
						
							|  |  |  |       // rounding is only allowed on whole, single, units (eg M or 1M, not 0.5M or 2M)
 | 
					
						
							|  |  |  |       if (num !== 1) { | 
					
						
							|  |  |  |         return undefined; | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     unit = mathString.charAt(i++); | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 22:10:44 +02:00
										 |  |  |     if (!_.includes(units, unit)) { | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |       return undefined; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  |       if (type === 0) { | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |         if (roundUp) { | 
					
						
							|  |  |  |           dateTime.endOf(unit); | 
					
						
							| 
									
										
										
										
											2016-01-13 21:07:57 +01:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |           dateTime.startOf(unit); | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |       } else if (type === 1) { | 
					
						
							|  |  |  |         dateTime.add(num, unit); | 
					
						
							|  |  |  |       } else if (type === 2) { | 
					
						
							|  |  |  |         dateTime.subtract(num, unit); | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-09-16 19:49:05 +02:00
										 |  |  |   return dateTime; | 
					
						
							| 
									
										
										
										
											2015-09-15 13:23:36 +02:00
										 |  |  | } |