diff --git a/src/date/core.ts b/src/date/core.ts index 3037fd20..fd750e55 100644 --- a/src/date/core.ts +++ b/src/date/core.ts @@ -11,3 +11,7 @@ export let gxToLibLangMapping = (gxlang: string): string => { let luxonLang = gxtoluxonLang.get(gxlang); return luxonLang || "en"; }; + + +export const EMPTY_DATE_VALUE = new Date(0, 0, 0, 0, 0, 0, 0); + diff --git a/src/date/fromString.ts b/src/date/fromString.ts new file mode 100644 index 00000000..2573f522 --- /dev/null +++ b/src/date/fromString.ts @@ -0,0 +1,25 @@ +/** + * Returns a Date from string value format espected format dd[/]mm[/]yyyy + * @param {string} dateFrom + * @return Date + */ + +import { DateTime } from "luxon"; +import { EMPTY_DATE_VALUE } from "./core"; + +export const fromString = (dateFrom: string): Date => { + const dateParts = dateFrom.match( + /([0-9]?[0-9])\/?([0-9]?[0-9])\/?([0-9][0-9][0-9][0-9])/ + ); + return dateParts && dateParts.length > 2 + ? new Date( + Number(dateParts[3]), + Number(dateParts[2]) - 1, + Number(dateParts[1]), + 0, + 0, + 0, + 0 + ) + : EMPTY_DATE_VALUE; +}; diff --git a/src/date/isEmpty.ts b/src/date/isEmpty.ts index 4dea92ca..5118dba3 100644 --- a/src/date/isEmpty.ts +++ b/src/date/isEmpty.ts @@ -4,7 +4,7 @@ * @return boolean */ -const EMPTY_DATE_VALUE = new Date(0, 0, 0, 0, 0, 0, 0); + import {EMPTY_DATE_VALUE} from "./core"; export const isEmpty = (target: Date): boolean => { return target.getTime() === EMPTY_DATE_VALUE.getTime(); diff --git a/src/date/newInstance.ts b/src/date/newInstance.ts new file mode 100644 index 00000000..416f16b1 --- /dev/null +++ b/src/date/newInstance.ts @@ -0,0 +1,15 @@ +/** + * Returns a Date from its parts + * @param {number} year + * @param {number} month + * @param {number} day + * @return Date + */ + +import { DateTime } from "luxon"; +import { EMPTY_DATE_VALUE } from "./core"; + +export const newInstance = (year: number, month: number, day: number): Date => { + const ret = new Date( year, month - 1, day, 0, 0, 0 ,0); + return (ret.getFullYear() === year && (ret.getMonth() === month - 1) && ret.getDate() === day) ? ret: EMPTY_DATE_VALUE; +}; \ No newline at end of file diff --git a/src/date/set.ts b/src/date/set.ts new file mode 100644 index 00000000..811b1651 --- /dev/null +++ b/src/date/set.ts @@ -0,0 +1,21 @@ +/** + * Returns a Date from its parts + * @param {Date} targetDate + * @param {number} year + * @param {number} month + * @param {number} day + * @return Date + */ + +import { DateTime } from "luxon"; +import { EMPTY_DATE_VALUE } from "./core"; + +export const set = (targetDate: Date, year: number, month: number, day: number): Date => { + targetDate.setFullYear(year); + targetDate.setMonth(month - 1); + targetDate.setDate(day); + if (targetDate.getFullYear() !== year || (targetDate.getMonth() !== month - 1) && targetDate.getDate() !== day) { + targetDate.setTime(EMPTY_DATE_VALUE.getTime()); + } + return targetDate; +}; \ No newline at end of file diff --git a/src/date/test/fromstring.spec.ts b/src/date/test/fromstring.spec.ts new file mode 100644 index 00000000..4077e77b --- /dev/null +++ b/src/date/test/fromstring.spec.ts @@ -0,0 +1,19 @@ +import { fromString } from "../fromString"; +import { EMPTY_DATE_VALUE } from "../core"; + +export const testCases: Array<[string, Date]> = [ + ["28091891", new Date(1891, 8, 28)], + ["2891891", new Date(1891, 8, 28)], + ["28/09/1891", new Date(1891, 8, 28)], + ["28/9/1891", new Date(1891, 8, 28)], + ["28-9-1891", EMPTY_DATE_VALUE], + ["TEXTO", EMPTY_DATE_VALUE] +]; + +describe("fromString operation", () => { + for (const t of testCases) { + it(`fromString of ${t[1]} should be equal to "${t[1]}"`, () => { + expect(fromString(t[0])).toEqual(t[1]); + }); + } +}); diff --git a/src/date/test/newinstance.spec.ts b/src/date/test/newinstance.spec.ts new file mode 100644 index 00000000..c0126b6d --- /dev/null +++ b/src/date/test/newinstance.spec.ts @@ -0,0 +1,16 @@ +import { newInstance } from "../newInstance"; +import { EMPTY_DATE_VALUE } from "../core"; + +export const testCases: Array<[number, number, number, Date]> = [ + [1891, 9, 28, new Date(1891, 8, 28, 0, 0, 0, 0)], + [1891, 13, 1, EMPTY_DATE_VALUE], + [1891, 1891, 1891, EMPTY_DATE_VALUE] +]; + +describe("newInstance operation", () => { + for (const t of testCases) { + it(`newInstance of ${t[0]}, ${t[1]}, ${t[2]} should be equal to "${t[3]}"`, () => { + expect(newInstance(t[0], t[1], t[2]).getTime()).toBe(t[3].getTime()); + }); + } +}); diff --git a/src/date/test/set.spec.ts b/src/date/test/set.spec.ts new file mode 100644 index 00000000..483089b5 --- /dev/null +++ b/src/date/test/set.spec.ts @@ -0,0 +1,18 @@ +import { set } from "../set"; +import { EMPTY_DATE_VALUE } from "../core"; +import { clear } from "../../generator/out/not_implemented"; + +export const testCases: Array<[Date, number, number, number, Date]> = [ + [new Date(2015, 3, 2, 0, 0, 0, 0), 1891, 9, 28, new Date(1891, 8, 28, 0, 0, 0, 0)], + [new Date(2015, 3, 2, 0, 0, 0, 0), 1891, 13, 1, EMPTY_DATE_VALUE], + [new Date(2015, 3, 2, 0, 0, 0, 0), 1891, 1891, 1891, EMPTY_DATE_VALUE] +]; + +describe("set operation", () => { + for (const t of testCases) { + it(`set ${t[0]} year ${t[1]} month ${t[2]} day ${t[3]} should be equal to "${t[4]}"`, () => { + expect(set(t[0], t[1], t[2], t[3]).getTime()).toBe(t[4].getTime()); + }); + } +}); +clear \ No newline at end of file diff --git a/src/date/test/todate.spec.ts b/src/date/test/todate.spec.ts new file mode 100644 index 00000000..19462a07 --- /dev/null +++ b/src/date/test/todate.spec.ts @@ -0,0 +1,16 @@ +import { toDate } from "../toDate"; + +export const testCases: Array<[Date, Date]> = [ + [new Date(1891, 8, 28), new Date(1891, 8, 28)], + [new Date(2018, 0, 1), new Date(2018, 0, 1)], + [new Date(2018, 0, 30), new Date(2018, 0, 30)], + [new Date(2018, 0, 31), new Date(2018, 0, 31)] +]; + +describe("toDate operation", () => { + for (const t of testCases) { + it(`toDate of ${t[1]} should be equal to "${t[1]}"`, () => { + expect(toDate(t[0])).toEqual(t[1]); + }); + } +}); diff --git a/src/date/test/tostring.spec.ts b/src/date/test/tostring.spec.ts new file mode 100644 index 00000000..768f61dd --- /dev/null +++ b/src/date/test/tostring.spec.ts @@ -0,0 +1,17 @@ +import { toString } from "../toString"; +import {EMPTY_DATE_VALUE} from "../core"; +export const testCases: Array<[Date, string]> = [ + [new Date(1891, 8, 28), "28/9/1891"], + [new Date(2018, 0, 1), "1/1/2018"], + [new Date(2018, 0, 30), "30/1/2018"], + [EMPTY_DATE_VALUE, ""], + [new Date(2018, 0, 31), "31/1/2018"] +]; + +describe("toString operation", () => { + for (const t of testCases) { + it(`toString of ${t[0]} should be equal to "${t[1]}"`, () => { + expect(toString(t[0])).toEqual(t[1]); + }); + } +}); diff --git a/src/date/toDate.ts b/src/date/toDate.ts new file mode 100644 index 00000000..fb918475 --- /dev/null +++ b/src/date/toDate.ts @@ -0,0 +1,11 @@ +/** + * Returns a native type date + * @param {Date} dateFrom + * @return Date + */ + +import { DateTime } from "luxon"; + +export const toDate = (dateFrom: Date): Date => { + return dateFrom; +}; diff --git a/src/date/toString.ts b/src/date/toString.ts new file mode 100644 index 00000000..e8666fa0 --- /dev/null +++ b/src/date/toString.ts @@ -0,0 +1,9 @@ +/** + * Returns a string represantation of a date. dd[/]mm[/]yyyy + * @return Date + */ +import {isEmpty} from './isEmpty'; + +export const toString = (targetDate: Date): string => { + return isEmpty(targetDate) ? "" : `${targetDate.getDate()}/${targetDate.getMonth() + 1}/${targetDate.getFullYear()}`; +}; diff --git a/src/generator/options.json b/src/generator/options.json index e7b7fd1a..c18aeb52 100644 --- a/src/generator/options.json +++ b/src/generator/options.json @@ -577,6 +577,26 @@ "MonthName": { "path": "@genexus/web-standard-functions/dist/lib-esm/date/monthName", "name": "monthName" + } , + "ToDate": { + "path": "@genexus/web-standard-functions/dist/lib-esm/date/toDate", + "name": "toDate" + }, + "FromString": { + "path": "@genexus/web-standard-functions/dist/lib-esm/date/fromString", + "name": "fromString" + }, + "Set": { + "path": "@genexus/web-standard-functions/dist/lib-esm/date/set", + "name": "set" + }, + "New": { + "path": "@genexus/web-standard-functions/dist/lib-esm/date/newInstance", + "name": "newInstance" + } , + "ToString": { + "path": "@genexus/web-standard-functions/dist/lib-esm/date/toString", + "name": "toString" } } } diff --git a/src/types/test/guid.spec.ts b/src/types/test/guid.spec.ts index 89ddfecd..496294a6 100644 --- a/src/types/test/guid.spec.ts +++ b/src/types/test/guid.spec.ts @@ -55,7 +55,6 @@ describe("check if a GUID is empty", () => { }); it("should return true for a GUID created from an invalid GUID string", () => { const guid = GUID.fromString("xyz"); - console.log(guid.toString()); expect(guid.isEmpty()).toBe(true); }); it("should return false for a new GUID", () => {