Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow default initialization of DateTime/Duration/Interval #1643

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ export default class DateTime {
* @access private
*/
constructor(config) {
if (!config) {
return DateTime.invalid("no config supplied");
}

const zone = config.zone || Settings.defaultZone;

let invalid =
Expand Down
4 changes: 4 additions & 0 deletions src/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ export default class Duration {
* @private
*/
constructor(config) {
if (!config) {
return Duration.invalid("no config supplied");
}

const accurate = config.conversionAccuracy === "longterm" || false;
let matrix = accurate ? accurateMatrix : casualMatrix;

Expand Down
4 changes: 3 additions & 1 deletion src/impl/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,9 @@ export default class Locale {
}

constructor(locale, numbering, outputCalendar, weekSettings, specifiedLocale) {
const [parsedLocale, parsedNumberingSystem, parsedOutputCalendar] = parseLocaleString(locale);
const [parsedLocale, parsedNumberingSystem, parsedOutputCalendar] = parseLocaleString(
locale || systemLocale()
);

this.locale = parsedLocale;
this.numberingSystem = numbering || parsedNumberingSystem || null;
Expand Down
4 changes: 4 additions & 0 deletions src/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export default class Interval {
* @private
*/
constructor(config) {
if (!config) {
return Interval.invalid("no config supplied");
}

/**
* @access private
*/
Expand Down
9 changes: 9 additions & 0 deletions test/datetime/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ const withDefaultLocale = Helpers.withDefaultLocale,
withthrowOnInvalid = Helpers.setUnset("throwOnInvalid"),
withDefaultZone = Helpers.withDefaultZone;

//------
// new
//------
test("Default constructor works and returns an invalid instance", () => {
const datetime = new DateTime();
expect(datetime.isValid).toBe(false);
expect(datetime.invalidReason).toBe("no config supplied");
});

//------
// .now()
//------
Expand Down
9 changes: 9 additions & 0 deletions test/duration/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

import { Duration } from "../../src/luxon";

//------
// new
//------
test("Default constructor works and returns an invalid instance", () => {
const duration = new Duration();
expect(duration.isValid).toBe(false);
expect(duration.invalidReason).toBe("no config supplied");
});

//------
// .fromObject()
//-------
Expand Down
9 changes: 9 additions & 0 deletions test/interval/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import Helpers from "../helpers";

const withThrowOnInvalid = Helpers.setUnset("throwOnInvalid");

//------
// new
//------
test("Default constructor works and returns an invalid instance", () => {
const interval = new Interval();
expect(interval.isValid).toBe(false);
expect(interval.invalidReason).toBe("no config supplied");
});

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