Skip to content

Commit

Permalink
fix(util.convert): epoch times stored as strings convert correctly, u…
Browse files Browse the repository at this point in the history
…nit test fixup, parse iso dates in local tz (#699)

* fix: parse string dates directly with moment

* fix: error for convert unknown to moment more specific

Previously stated "Unable to convert to Date"

Now states "Unable to convert to moment"

* test: add html doc for testing offset behaviour

* test: test string to date conversions more rigorously

* refactor(utils.convert): remove repeated code, throw TypeErrors

Previously, "Date" & "Moment" had identical switch branches, but
each return statement in "Date" had `moment(...).toDate()`.

Now, Date branch is just `convert(object, "Moment").toDate()`.

Also, each now throw TypeErrors, & Date branch catches "TypeError" & rethrows
its own.

* test(util.convert): test for "TypeError" when testing throws

* test(util.convert): more robust output checking for date conversion

* fix(util.convert): convert epoch times in strings to number before converting to moment

* test(util.convert): improve "testOffset" example

* Delete testOffset.html

as per review comment

Co-authored-by: Yotam Berkowitz <[email protected]>
  • Loading branch information
strazto and yotamberk authored Oct 5, 2020
1 parent 287ddac commit 3363147
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
40 changes: 18 additions & 22 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import moment from "moment";
// for example '/Date(1198908717056)/' or '/Date(1198908717056-0700)/'
// code from http://momentjs.com/
const ASPDateRegex = /^\/?Date\((-?\d+)/i;

const NumericRegex = /^\d+$/;
/**
* Convert an object into another type
*
Expand Down Expand Up @@ -59,26 +59,17 @@ export function convert(object, type) {
return String(object);

case "Date":
if (isNumber(object)) {
return new Date(object);
try {
return convert(object, "Moment").toDate();
}
if (object instanceof Date) {
return new Date(object.valueOf());
} else if (moment.isMoment(object)) {
return new Date(object.valueOf());
}
if (isString(object)) {
match = ASPDateRegex.exec(object);
if (match) {
// object is an ASP date
return new Date(Number(match[1])); // parse number
catch(e){
if (e instanceof TypeError) {
throw new TypeError(
"Cannot convert object of type " + getType(object) + " to type " + type
);
} else {
return moment(new Date(object)).toDate(); // parse string
throw e;
}
} else {
throw new Error(
"Cannot convert object of type " + getType(object) + " to type Date"
);
}

case "Moment":
Expand All @@ -95,12 +86,17 @@ export function convert(object, type) {
if (match) {
// object is an ASP date
return moment(Number(match[1])); // parse number
} else {
return moment(object); // parse string
}
match = NumericRegex.exec(object);

if (match) {
return moment(Number(object));
}

return moment(object); // parse string
} else {
throw new Error(
"Cannot convert object of type " + getType(object) + " to type Date"
throw new TypeError(
"Cannot convert object of type " + getType(object) + " to type " + type
);
}

Expand Down
35 changes: 30 additions & 5 deletions test/util-convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,36 @@ describe("util", function() {
});

it("converts to Date from Number", function() {
assert(util.convert(1198908717056, "Date") instanceof Date);
const d1 = util.convert(1198908717056, "Date");
assert(d1 instanceof Date);
assert(!isNaN(d1.valueOf()));
});

it("converts to Date from String", function() {
assert(util.convert("1198908717056", "Date") instanceof Date);
it("converts to Date from epoch int stored as String", function() {
const d1 = util.convert("1198908717056", "Date");
assert(d1 instanceof Date);
assert(!isNaN(d1.valueOf()));
});

it("converting epoch int stored as String === epoch int stored as int",
function() {
const d1 = util.convert("1198908717056", "Date");
const d2 = util.convert(1198908717056, "Date");
assert.strictEqual(d1.valueOf(), d2.valueOf());
}
);

it("converts ISO dates to midnight in local TZ", function() {
const dateISO = util.convert("2020-04-20", "Date");
const dttmISO = util.convert("2020-04-20T00:00:00", "Date");

assert(dateISO instanceof Date);
assert(dttmISO instanceof Date);

assert(!isNaN(dateISO.valueOf()));
assert(!isNaN(dttmISO.valueOf()));

assert.strictEqual(dateISO.valueOf(),dttmISO.valueOf());
});

it("converts to Date from Moment", function() {
Expand All @@ -58,7 +83,7 @@ describe("util", function() {
function() {
util.convert({}, "Date");
},
Error,
TypeError,
null
);
});
Expand Down Expand Up @@ -86,7 +111,7 @@ describe("util", function() {
function() {
util.convert({}, "Moment");
},
Error,
TypeError,
null
);
});
Expand Down

0 comments on commit 3363147

Please sign in to comment.