diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f81b358c7..cc3d8c81a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 @@ -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) \ No newline at end of file + - run: bash <(curl -s https://codecov.io/bash) diff --git a/jest.config.js b/jest.config.js index edbcded91..bc7d0f792 100644 --- a/jest.config.js +++ b/jest.config.js @@ -6,4 +6,5 @@ module.exports = { transform: { "^.+\\.js$": "babel-jest", }, + setupFilesAfterEnv: ["/test/setupTests.js"], }; diff --git a/src/impl/tokenParser.js b/src/impl/tokenParser.js index 48a7595ed..0190efc5f 100644 --- a/src/impl/tokenParser.js +++ b/src/impl/tokenParser.js @@ -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(); } diff --git a/test/datetime/format.test.js b/test/datetime/format.test.js index 20efe0fab..fcc0801af 100644 --- a/test/datetime/format.test.js +++ b/test/datetime/format.test.js @@ -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", () => { diff --git a/test/datetime/tokenParse.test.js b/test/datetime/tokenParse.test.js index 0bb6e2d51..c35e1dafe 100644 --- a/test/datetime/tokenParse.test.js +++ b/test/datetime/tokenParse.test.js @@ -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); diff --git a/test/interval/format.test.js b/test/interval/format.test.js index f15e55f51..3422de1d8 100644 --- a/test/interval/format.test.js +++ b/test/interval/format.test.js @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -98,7 +98,7 @@ 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", () => { @@ -106,7 +106,7 @@ test("Interval#toLocaleString shows things in the right fixed-offset zone", () = 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", () => { @@ -114,7 +114,7 @@ test("Interval#toLocaleString shows things in the right fixed-offset zone when s 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", () => { @@ -122,7 +122,7 @@ test("Interval#toLocaleString shows things with UTC if fixed-offset with 0 offse 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", () => { @@ -130,7 +130,7 @@ test("Interval#toLocaleString does the best it can with unsupported fixed-offset 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", () => { @@ -138,24 +138,24 @@ test("Interval#toLocaleString uses locale-appropriate time formats", () => { 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( diff --git a/test/setupTests.js b/test/setupTests.js new file mode 100644 index 000000000..7d916f9a9 --- /dev/null +++ b/test/setupTests.js @@ -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)}`, + }; + }, +});