Skip to content

Commit

Permalink
Implemented fromMillis() to verify args (#301)
Browse files Browse the repository at this point in the history
Made fromMillis() verify its arg is a number
  • Loading branch information
taeukkang authored and icambron committed Jul 19, 2018
1 parent 5a067d7 commit b937d0c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/datetime.js
Expand Up @@ -501,11 +501,15 @@ export default class DateTime {
* @return {DateTime}
*/
static fromMillis(milliseconds, options = {}) {
return new DateTime({
ts: milliseconds,
zone: normalizeZone(options.zone, Settings.defaultZone),
loc: Locale.fromObject(options)
});
if (!isNumber(milliseconds)) {
throw new InvalidArgumentError('fromMillis requires a numerical input');
} else {
return new DateTime({
ts: milliseconds,
zone: normalizeZone(options.zone, Settings.defaultZone),
loc: Locale.fromObject(options)
});
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions test/datetime/create.test.js
Expand Up @@ -256,6 +256,10 @@ test('DateTime.fromMillis accepts the default locale', () => {
withDefaultLocale('fr', () => expect(DateTime.fromMillis(391147200000).locale).toBe('fr'));
});

test('DateTime.fromMillis(ms) throws InvalidArgumentError for non-numeric input', () => {
expect(() => DateTime.fromMillis('slurp')).toThrow();
});

//------
// .fromObject()
//-------
Expand Down

0 comments on commit b937d0c

Please sign in to comment.