diff --git a/packages/trevas/package.json b/packages/trevas/package.json index 37466d89..c3a45da2 100644 --- a/packages/trevas/package.json +++ b/packages/trevas/package.json @@ -30,4 +30,4 @@ "data-forge": "^1.8.8", "date-fns": "^2.27.0" } -} +} \ No newline at end of file diff --git a/packages/trevas/src/tests/interpretors/arithmetic.spec.js b/packages/trevas/src/tests/interpretors/arithmetic.spec.js index bd840d98..397322ef 100644 --- a/packages/trevas/src/tests/interpretors/arithmetic.spec.js +++ b/packages/trevas/src/tests/interpretors/arithmetic.spec.js @@ -214,4 +214,11 @@ describe('arithmetic', () => { ).toEqual(2); }); }); + describe('issues', () => { + it('#134', () => { + // currently stops at `10`, probably considering `-9` as another integer, dropping it + // commenting that for now because we know it is a VTL spec problem not an implementation one + //expect(interpret('10 -9')).toEqual(1); + }); + }); }); diff --git a/packages/trevas/src/tests/interpretors/functions/cast.spec.js b/packages/trevas/src/tests/interpretors/functions/cast.spec.js index d87ce2d9..dee38d62 100644 --- a/packages/trevas/src/tests/interpretors/functions/cast.spec.js +++ b/packages/trevas/src/tests/interpretors/functions/cast.spec.js @@ -123,28 +123,28 @@ describe('cast', () => { }); }); describe('time', () => { - it('cast time into integer', () => {}); - it('cast time into number', () => {}); - it('cast time into boolean', () => {}); - it('cast time into time', () => {}); - it('cast time into date', () => {}); - it('cast time into time_period', () => {}); - it('cast time into string', () => {}); - it('cast time into duration', () => {}); + it('cast time into integer', () => { }); + it('cast time into number', () => { }); + it('cast time into boolean', () => { }); + it('cast time into time', () => { }); + it('cast time into date', () => { }); + it('cast time into time_period', () => { }); + it('cast time into string', () => { }); + it('cast time into duration', () => { }); }); describe('date', () => { // There's no date literal. We use a first cast to define the date. const aDate = interpretVar('cast("1998-07-12", date, "YYYY-MM-DD")', {}); - it('cast date into integer', () => {}); - it('cast date into number', () => {}); - it('cast date into boolean', () => {}); - it('cast date into time', () => {}); + it('cast date into integer', () => { }); + it('cast date into number', () => { }); + it('cast date into boolean', () => { }); + it('cast date into time', () => { }); it('cast date into date', () => { expect(interpret('cast(aDate, date, "YYYY-MM")', { aDate })).toEqual( interpret('cast("1998-07-12", date, "YYYY-MM")', {}) ); }); - it('cast date into time_period', () => {}); + it('cast date into time_period', () => { }); it('cast date into string', () => { expect(interpret('cast(aDate, string, "YYYY-DD-MM")', { aDate })).toEqual( '1998-12-07' @@ -156,17 +156,26 @@ describe('cast', () => { // '1998-12-31' // ); }); - it('cast date into duration', () => {}); + it('cast date into duration', () => { }); + it("cast date to string with localization", () => { + navigatorLanguageSpy = jest.spyOn(Object.getPrototypeOf(navigator), 'language', 'get'); + expect(interpret('cast(aDate, string, "MMMM")', { aDate })).toEqual("July"); + navigatorLanguageSpy.mockReturnValue("fr"); + expect(interpret('cast(aDate, string, "MMMM")', { aDate })).toEqual("juillet"); + navigatorLanguageSpy.mockReturnValue("es"); + expect(interpret('cast(aDate, string, "MMMM")', { aDate })).toEqual("July"); + navigatorLanguageSpy.mockRestore(); + }); }); describe('time_period', () => { - it('cast time_period into integer', () => {}); - it('cast time_period into number', () => {}); - it('cast time_period into boolean', () => {}); - it('cast time_period into time', () => {}); - it('cast time_period into date', () => {}); - it('cast time_period into time_period', () => {}); - it('cast time_period into string', () => {}); - it('cast time_period into duration', () => {}); + it('cast time_period into integer', () => { }); + it('cast time_period into number', () => { }); + it('cast time_period into boolean', () => { }); + it('cast time_period into time', () => { }); + it('cast time_period into date', () => { }); + it('cast time_period into time_period', () => { }); + it('cast time_period into string', () => { }); + it('cast time_period into duration', () => { }); }); describe('string', () => { it('cast string into integer', () => { @@ -186,7 +195,7 @@ describe('cast', () => { CastTypeError ); }); - it('cast string into time', () => {}); + it('cast string into time', () => { }); it('should cast string into date', () => { expect(interpret('cast("1998-07-12", date, "YYYY-MM-DD")', {})).toEqual( '1998-07-12' @@ -195,11 +204,11 @@ describe('cast', () => { '1998-07-12' ); }); - it('cast string into time_period', () => {}); + it('cast string into time_period', () => { }); it('cast string into string', () => { expect(interpret('cast("aaa", string)', {})).toEqual('aaa'); }); - it('cast string into duration', () => {}); + it('cast string into duration', () => { }); const dsI = { dataStructure: { col_1: {}, col_2: {} }, dataPoints: { col_1: ['1', '2', '3'], col_2: ['-1', null, null] }, @@ -224,13 +233,13 @@ describe('cast', () => { }); }); describe('duration', () => { - it('cast duration into integer', () => {}); - it('cast duration into number', () => {}); - it('cast duration into boolean', () => {}); - it('cast duration into time', () => {}); - it('cast duration into date', () => {}); - it('cast duration into time_period', () => {}); - it('cast duration into string', () => {}); - it('cast duration into duration', () => {}); + it('cast duration into integer', () => { }); + it('cast duration into number', () => { }); + it('cast duration into boolean', () => { }); + it('cast duration into time', () => { }); + it('cast duration into date', () => { }); + it('cast duration into time_period', () => { }); + it('cast duration into string', () => { }); + it('cast duration into duration', () => { }); }); }); diff --git a/packages/trevas/src/tests/utils/dates.spec.js b/packages/trevas/src/tests/utils/dates.spec.js new file mode 100644 index 00000000..59ded290 --- /dev/null +++ b/packages/trevas/src/tests/utils/dates.spec.js @@ -0,0 +1,26 @@ +import { fromNavLangToLocale } from '../../utils'; + +describe("dates utils functions", () => { + let navigatorLanguageSpy; + + beforeEach(() => { + navigatorLanguageSpy = jest.spyOn(Object.getPrototypeOf(navigator), 'language', 'get'); + }); + + it("handles default localization", () => { + // navigator.language is `en-US` by default + let locale = fromNavLangToLocale(); + expect(locale.code).toEqual("en-US"); + }); + + it("handles french localization", () => { + navigatorLanguageSpy.mockReturnValue("fr"); + let locale = fromNavLangToLocale(); + expect(locale.code).toEqual("fr") + }); + + + afterEach(() => { + navigatorLanguageSpy.mockRestore(); + }); +}); \ No newline at end of file diff --git a/packages/trevas/src/utils/dates.js b/packages/trevas/src/utils/dates.js index deef4f68..b0e2487f 100644 --- a/packages/trevas/src/utils/dates.js +++ b/packages/trevas/src/utils/dates.js @@ -1,4 +1,14 @@ import { format } from 'date-fns'; +import { fr, enUS } from "date-fns/locale"; + +export const fromNavLangToLocale = () => { + switch (navigator.language) { + case "fr": + return fr + default: + return enUS + } +}; export const getCurrentDate = () => new Date(); @@ -10,5 +20,5 @@ export const getDate = (dateStr, mask) => export const getStringFromDate = (date, mask) => mask - ? format(new Date(`${date}`), buildDateFnsMask(mask)) + ? format(new Date(`${date}`), buildDateFnsMask(mask), { locale: fromNavLangToLocale() }) : new Date(`${date}`); diff --git a/packages/trevas/src/utils/index.js b/packages/trevas/src/utils/index.js index e7b5186d..cca85443 100644 --- a/packages/trevas/src/utils/index.js +++ b/packages/trevas/src/utils/index.js @@ -1,6 +1,6 @@ export { replaceConstantType } from './constants'; export { getTokenType } from './context'; -export { getDate, getStringFromDate, getCurrentDate } from './dates'; +export { getDate, getStringFromDate, getCurrentDate, fromNavLangToLocale } from './dates'; export { getTokenName } from './parser'; export * from './datasets'; export * from './null';