MCPcopy Create free account
hub / github.com/parse-community/parse-server / relativeTimeToDate

Method relativeTimeToDate

src/Utils.js:274–398  ·  view source on GitHub ↗

* Computes the relative date based on a string. * @param {String} text The string to interpret the date from. * @param {Date} now The date the string is comparing against. * @returns {Object} The relative date object.

(text, now = new Date())

Source from the content-addressed store, hash-verified

272 * @returns {Object} The relative date object.
273 **/
274 static relativeTimeToDate(text, now = new Date()) {
275 text = text.toLowerCase();
276 let parts = text.split(' ');
277
278 // Filter out whitespace
279 parts = parts.filter(part => part !== '');
280
281 const future = parts[0] === 'in';
282 const past = parts[parts.length - 1] === 'ago';
283
284 if (!future && !past && text !== 'now') {
285 return {
286 status: 'error',
287 info: "Time should either start with 'in' or end with 'ago'",
288 };
289 }
290
291 if (future && past) {
292 return {
293 status: 'error',
294 info: "Time cannot have both 'in' and 'ago'",
295 };
296 }
297
298 // strip the 'ago' or 'in'
299 if (future) {
300 parts = parts.slice(1);
301 } else {
302 // past
303 parts = parts.slice(0, parts.length - 1);
304 }
305
306 if (parts.length % 2 !== 0 && text !== 'now') {
307 return {
308 status: 'error',
309 info: 'Invalid time string. Dangling unit or number.',
310 };
311 }
312
313 const pairs = [];
314 while (parts.length) {
315 pairs.push([parts.shift(), parts.shift()]);
316 }
317
318 let seconds = 0;
319 for (const [num, interval] of pairs) {
320 const val = Number(num);
321 if (!Number.isInteger(val)) {
322 return {
323 status: 'error',
324 info: `'${num}' is not an integer.`,
325 };
326 }
327
328 switch (interval) {
329 case 'yr':
330 case 'yrs':
331 case 'year':

Callers 3

transformConstraintFunction · 0.80
buildWhereClauseFunction · 0.80

Calls 1

filterMethod · 0.80

Tested by

no test coverage detected