Skip to content
Merged
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
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
strategy:
matrix:
node-version:
- 18.20.8 # latest 18.x
- 20.19.4 # latest 20.x
- 22.17.1 # latest 22.x
- 24.4.1 # latest 22.x
- 20.20.1 # latest 20.x
- 22.22.1 # latest 22.x
- 24.14.0 # latest 22.x
- 25.8.1 # bleeding edge

steps:
- uses: actions/checkout@v3
Expand All @@ -35,4 +35,4 @@ jobs:
- run: npm run format-check
- run: npm run test
- run: npm run site
- run: bash <(curl -s https://codecov.io/bash)
- run: bash <(curl -s https://codecov.io/bash)
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ module.exports = {
transform: {
"^.+\\.js$": "babel-jest",
},
setupFilesAfterEnv: ["<rootDir>/test/setupTests.js"],
};
12 changes: 4 additions & 8 deletions src/impl/tokenParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ function intUnit(regex, post = (i) => i) {
return { regex, deser: ([s]) => post(parseDigits(s)) };
}

const NBSP = String.fromCharCode(160);
const spaceOrNBSP = `[ ${NBSP}]`;
const spaceOrNBSPRegExp = new RegExp(spaceOrNBSP, "g");

function fixListRegex(s) {
// make dots optional and also make them literal
// make space and non breakable space characters interchangeable
return s.replace(/\./g, "\\.?").replace(spaceOrNBSPRegExp, spaceOrNBSP);
return s
.replace(/\./g, "\\.?") // make dots optional and also make them literal
.replace(/\s/g, "\\s"); // make all whitespace literals into any-whitespace-here markers
}

function stripInsensitivities(s) {
return s
.replace(/\./g, "") // ignore dots that were made optional
.replace(spaceOrNBSPRegExp, " ") // interchange space and nbsp
.replace(/\s/g, " ") // replace other whitespace with simple spaces
.toLowerCase();
}

Expand Down
2 changes: 1 addition & 1 deletion test/datetime/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ test("DateTime#toLocaleString can override the dateTime's numbering system", ()
test("DateTime#toLocaleString can override the dateTime's output calendar", () => {
expect(
dt.reconfigure({ outputCalendar: "islamic" }).toLocaleString({}, { outputCalendar: "coptic" })
).toBe("9/17/1698 ERA1");
).toBe("9/17/1698 AM");
});

test("DateTime#toLocaleString() returns something different for invalid DateTimes", () => {
Expand Down
14 changes: 5 additions & 9 deletions test/datetime/tokenParse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,17 +1117,13 @@ test("DateTime.fromFormatExplain() parses localized string with numberingSystem
expect(ex17.result).toBeInstanceOf(Object);
expect(keyCount(ex17.result)).toBe(6);

const ex18 = DateTime.fromFormatExplain(
"๐๓-เมษายน-๒๐๑๙ ๐๔:๐๒:๒๔ หลังเที่ยง",
"dd-MMMM-yyyy hh:mm:ss a",
{
locale: "th",
numberingSystem: "thai",
}
);
const ex18 = DateTime.fromFormatExplain("๑๒-มกราคม-๒๐๒๖ ๐๓:๔๖:๒๖", "dd-MMMM-yyyy hh:mm:ss", {
locale: "th",
numberingSystem: "thai",
});
expect(ex18.rawMatches).toBeInstanceOf(Array);
expect(ex18.matches).toBeInstanceOf(Object);
expect(keyCount(ex18.matches)).toBe(7);
expect(keyCount(ex18.matches)).toBe(6);
expect(ex18.result).toBeInstanceOf(Object);
expect(keyCount(ex18.result)).toBe(6);

Expand Down
32 changes: 16 additions & 16 deletions test/interval/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test("Interval#toLocaleString accepts locale settings from the start DateTime",
interval.start.reconfigure({ locale: "be" }),
interval.end
).toLocaleString()
).toBe("25.5.1982 – 14.10.1983");
).toMatchIgnoringWeirdSpaces("25.5.1982 – 14.10.1983");
});

test("Interval#toLocaleString accepts numbering system settings from the start DateTime", () => {
Expand All @@ -49,7 +49,7 @@ test("Interval#toLocaleString accepts numbering system settings from the start D
interval.start.reconfigure({ numberingSystem: "beng" }),
interval.end
).toLocaleString()
).toBe("৫/২৫/১৯৮২ – ১০/১৪/১৯৮৩");
).toMatchIgnoringWeirdSpaces("৫/২৫/১৯৮২ – ১০/১৪/১৯৮৩");
});

test("Interval#toLocaleString accepts ouptput calendar settings from the start DateTime", () => {
Expand All @@ -58,11 +58,11 @@ test("Interval#toLocaleString accepts ouptput calendar settings from the start D
interval.start.reconfigure({ outputCalendar: "islamic" }),
interval.end
).toLocaleString()
).toBe("8/2/1402 – 1/8/1404 AH");
).toMatchIgnoringWeirdSpaces("8/2/1402 – 1/8/1404 AH");
});

test("Interval#toLocaleString accepts options to the formatter", () => {
expect(interval.toLocaleString({ weekday: "short" })).toBe("Tue – Fri");
expect(interval.toLocaleString({ weekday: "short" })).toMatchIgnoringWeirdSpaces("Tue – Fri");
});

test("Interval#toLocaleString can override the start DateTime's locale", () => {
Expand All @@ -71,7 +71,7 @@ test("Interval#toLocaleString can override the start DateTime's locale", () => {
interval.start.reconfigure({ locale: "be" }),
interval.end
).toLocaleString({}, { locale: "fr" })
).toBe("25/05/1982 – 14/10/1983");
).toMatchIgnoringWeirdSpaces("25/05/1982 – 14/10/1983");
});

test("Interval#toLocaleString can override the start DateTime's numbering system", () => {
Expand All @@ -80,7 +80,7 @@ test("Interval#toLocaleString can override the start DateTime's numbering system
interval.start.reconfigure({ numberingSystem: "beng" }),
interval.end
).toLocaleString({ numberingSystem: "mong" })
).toBe("᠕/᠒᠕/᠑᠙᠘᠒ – ᠑᠐/᠑᠔/᠑᠙᠘᠓");
).toMatchIgnoringWeirdSpaces("᠕/᠒᠕/᠑᠙᠘᠒ – ᠑᠐/᠑᠔/᠑᠙᠘᠓");
});

test("Interval#toLocaleString can override the start DateTime's output calendar", () => {
Expand All @@ -89,7 +89,7 @@ test("Interval#toLocaleString can override the start DateTime's output calendar"
interval.start.reconfigure({ outputCalendar: "islamic" }),
interval.end
).toLocaleString({}, { outputCalendar: "coptic" })
).toBe("9/17/1698 – 2/3/1700 ERA1");
).toMatchIgnoringWeirdSpaces("9/17/1698 – 2/3/1700 AM");
});

test("Interval#toLocaleString shows things in the right IANA zone", () => {
Expand All @@ -98,64 +98,64 @@ test("Interval#toLocaleString shows things in the right IANA zone", () => {
interval.start.setZone("Australia/Melbourne"),
interval.end
).toLocaleString(DateTime.DATETIME_SHORT)
).toBe("5/25/1982, 7:00 PM – 10/14/1983, 11:30 PM");
).toMatchIgnoringWeirdSpaces("5/25/1982, 7:00 PM – 10/14/1983, 11:30 PM");
});

test("Interval#toLocaleString shows things in the right fixed-offset zone", () => {
expect(
Interval.fromDateTimes(interval.start.setZone("UTC-8"), interval.end).toLocaleString(
DateTime.DATETIME_SHORT
)
).toBe("5/25/1982, 1:00 AM – 10/14/1983, 5:30 AM");
).toMatchIgnoringWeirdSpaces("5/25/1982, 1:00 AM – 10/14/1983, 5:30 AM");
});

test("Interval#toLocaleString shows things in the right fixed-offset zone when showing the zone", () => {
expect(
Interval.fromDateTimes(interval.start.setZone("UTC-8"), interval.end).toLocaleString(
DateTime.DATETIME_FULL
)
).toBe("May 25, 1982 at 1:00 AM GMT-8 – October 14, 1983 at 5:30 AM GMT-8");
).toMatchIgnoringWeirdSpaces("May 25, 1982 at 1:00 AM GMT-8 – October 14, 1983 at 5:30 AM GMT-8");
});

test("Interval#toLocaleString shows things with UTC if fixed-offset with 0 offset is used", () => {
expect(
Interval.fromDateTimes(interval.start.setZone("UTC"), interval.end).toLocaleString(
DateTime.DATETIME_FULL
)
).toBe("May 25, 1982 at 9:00 AM UTC – October 14, 1983 at 1:30 PM UTC");
).toMatchIgnoringWeirdSpaces("May 25, 1982 at 9:00 AM UTC – October 14, 1983 at 1:30 PM UTC");
});

test("Interval#toLocaleString does the best it can with unsupported fixed-offset zone when showing the zone", () => {
expect(
Interval.fromDateTimes(interval.start.setZone("UTC+4:30"), interval.end).toLocaleString(
DateTime.DATETIME_FULL
)
).toBe("May 25, 1982 at 9:00 AM UTC – October 14, 1983 at 1:30 PM UTC");
).toMatchIgnoringWeirdSpaces("May 25, 1982 at 9:00 AM UTC – October 14, 1983 at 1:30 PM UTC");
});

test("Interval#toLocaleString uses locale-appropriate time formats", () => {
expect(
Interval.after(interval.start.reconfigure({ locale: "en-US" }), { hour: 2 }).toLocaleString(
DateTime.TIME_SIMPLE
)
).toBe("9:00 – 11:00 AM");
).toMatchIgnoringWeirdSpaces("9:00 – 11:00 AM");
expect(
Interval.after(interval.start.reconfigure({ locale: "en-US" }), { hour: 2 }).toLocaleString(
DateTime.TIME_24_SIMPLE
)
).toBe("09:00 – 11:00");
).toMatchIgnoringWeirdSpaces("09:00 – 11:00");

// France has 24-hour by default
expect(
Interval.after(interval.start.reconfigure({ locale: "fr" }), { hour: 2 }).toLocaleString(
DateTime.TIME_SIMPLE
)
).toBe("09:00 – 11:00");
).toMatchIgnoringWeirdSpaces("09:00 – 11:00");
expect(
Interval.after(interval.start.reconfigure({ locale: "fr" }), { hour: 2 }).toLocaleString(
DateTime.TIME_24_SIMPLE
)
).toBe("09:00 – 11:00");
).toMatchIgnoringWeirdSpaces("09:00 – 11:00");

// Spain does't prefix with "0" and doesn't use spaces
expect(
Expand Down
18 changes: 18 additions & 0 deletions test/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// jest.setup.js

expect.extend({
toMatchIgnoringWeirdSpaces(received, expected) {
const replaceNbspReceived = (str) => str.replace(/\s/g, " ");
const replaceNbspExpected = (str) => str.replace(/\s/g, " ");

const pass = replaceNbspReceived(received) === replaceNbspExpected(expected);

return {
pass,
message: () =>
pass
? `Expected ${String(received)} to be (ignorning weird spaces) ${String(expected)}`
: `Expected ${String(received)} to be (ignorning weird spaces) ${String(expected)}`,
};
},
});
Loading