Skip to content

Instantly share code, notes, and snippets.

@scharf
Created May 25, 2018 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scharf/9b24a30dddb211dea17db45342566a7e to your computer and use it in GitHub Desktop.
Save scharf/9b24a30dddb211dea17db45342566a7e to your computer and use it in GitHub Desktop.
Parse Date
/**
* Tries to return a valid date or null. It can parse (valid) date strings as well as numbers.
* @param date a valid date or null
*/
/**
* Tries to return a valid date or null. It can parse (valid) date strings as well as numbers.
*
* @param val
* @param defaultValue
* @returns {any}
*/
export function toDateOrNull(val: any): Date {
return toDate(val, null);
}
function toDate(val: any, defaultValue: any = null): Date|null {
if (val == null) {
return defaultValue;
}
if (typeof val === 'boolean') {
return defaultValue;
}
if (Array.isArray(val)) {
return defaultValue;
}
let date: Date = null;
if (val instanceof Date) {
date = val;
} else {
date = new Date(val);
}
// see: https://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript
if (isNaN(date.getTime())) {
return defaultValue;
}
return date;
}
export function toDateOrNow(val: any): Date {
let date = toDate(val, null);
if (date == null) {
date = new Date();
}
return date;
}
import { toDateOrNow, toDateOrNull } from './toDate';
import { assert } from 'chai';
describe('toDate module', function() {
describe('toDateOrNull', function() {
it('should return null on null', function() {
assert.isNull(toDateOrNull(null));
});
it('should return null on undefined', function() {
assert.isNull(toDateOrNull(undefined));
});
it('should return null on empty string', function() {
assert.isNull(toDateOrNull(''));
});
it('should return null on invalid string', function() {
assert.isNull(toDateOrNull('xxxx'));
});
it('should return null on boolean', function() {
assert.isNull(toDateOrNull(true));
assert.isNull(toDateOrNull(false));
});
it('should return null on array', function() {
assert.isNull(toDateOrNull([]));
assert.isNull(toDateOrNull([1, 2]));
assert.isNull(toDateOrNull([new Date()]));
});
it('should return null on object', function() {
assert.isNull(toDateOrNull({}));
assert.isNull(toDateOrNull({ date: new Date() }));
});
it('should return date on number', function() {
assert.equal(1, toDateOrNull(1).getTime());
assert.equal(20000, toDateOrNull(20000).getTime());
});
it('should return same valid date', function() {
const date = new Date();
assert(date === toDateOrNull(date));
});
it('should return null valid date', function() {
assert.isNull(toDateOrNull(new Date('invalid')));
});
it('should return date on valid date string', function() {
assert.equal(
new Date('2017-08-08T21:16:03.484Z').getTime(),
toDateOrNull('2017-08-08T21:16:03.484Z').getTime()
);
});
});
describe('toDateOrNow', function() {
function isNow(date: Date) {
// let's define now as with a 1 second epsilon
assert(Math.abs(date.getTime() - Date.now()) < 1000);
}
it('should return now on null', function() {
isNow(toDateOrNow(null));
});
it('should return now on undefined', function() {
isNow(toDateOrNow(undefined));
});
it('should return now on empty string', function() {
isNow(toDateOrNow(''));
});
it('should return now on invalid string', function() {
isNow(toDateOrNow('xxxx'));
});
it('should return now on boolean', function() {
isNow(toDateOrNow(true));
isNow(toDateOrNow(false));
});
it('should return now on array', function() {
isNow(toDateOrNow([]));
isNow(toDateOrNow([1, 2]));
isNow(toDateOrNow([new Date()]));
});
it('should return now on object', function() {
isNow(toDateOrNow({}));
isNow(toDateOrNow({ date: new Date() }));
});
it('should return date on number', function() {
assert.equal(1, toDateOrNow(1).getTime());
assert.equal(20000, toDateOrNow(20000).getTime());
});
it('should return same valid date', function() {
const date = new Date();
assert(date === toDateOrNow(date));
});
it('should return now valid date', function() {
isNow(toDateOrNow(new Date('invalid')));
});
it('should return date on valid date string', function() {
assert.equal(
new Date('2017-08-08T21:16:03.484Z').getTime(),
toDateOrNow('2017-08-08T21:16:03.484Z').getTime()
);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment