Skip to content

Commit

Permalink
Allow config-less DateTime, Duration, and Interval
Browse files Browse the repository at this point in the history
DateTime, Duration, and Interval constructors now
return invalid when no config is supplied. This
should fix any code which relies on default new
while still keeping that invariant that configs
are supplied.
  • Loading branch information
mbStavola committed Oct 4, 2024
1 parent cea7b5f commit 278cee2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
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: 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

0 comments on commit 278cee2

Please sign in to comment.