Skip to content
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
2 changes: 1 addition & 1 deletion packages/trevas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
"data-forge": "^1.8.8",
"date-fns": "^2.27.0"
}
}
}
7 changes: 7 additions & 0 deletions packages/trevas/src/tests/interpretors/arithmetic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
75 changes: 42 additions & 33 deletions packages/trevas/src/tests/interpretors/functions/cast.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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', () => {
Expand All @@ -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'
Expand All @@ -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] },
Expand All @@ -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', () => { });
});
});
26 changes: 26 additions & 0 deletions packages/trevas/src/tests/utils/dates.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
12 changes: 11 additions & 1 deletion packages/trevas/src/utils/dates.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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}`);
2 changes: 1 addition & 1 deletion packages/trevas/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down