Skip to content

Commit

Permalink
Date type methods (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmendez authored and Marcos Crispino committed Apr 30, 2019
1 parent 5619461 commit 7b33db0
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/date/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

25 changes: 25 additions & 0 deletions src/date/fromString.ts
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 1 addition & 1 deletion src/date/isEmpty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions src/date/newInstance.ts
Original file line number Diff line number Diff line change
@@ -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;
};
21 changes: 21 additions & 0 deletions src/date/set.ts
Original file line number Diff line number Diff line change
@@ -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;
};
19 changes: 19 additions & 0 deletions src/date/test/fromstring.spec.ts
Original file line number Diff line number Diff line change
@@ -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]);
});
}
});
16 changes: 16 additions & 0 deletions src/date/test/newinstance.spec.ts
Original file line number Diff line number Diff line change
@@ -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());
});
}
});
18 changes: 18 additions & 0 deletions src/date/test/set.spec.ts
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions src/date/test/todate.spec.ts
Original file line number Diff line number Diff line change
@@ -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]);
});
}
});
17 changes: 17 additions & 0 deletions src/date/test/tostring.spec.ts
Original file line number Diff line number Diff line change
@@ -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]);
});
}
});
11 changes: 11 additions & 0 deletions src/date/toDate.ts
Original file line number Diff line number Diff line change
@@ -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;
};
9 changes: 9 additions & 0 deletions src/date/toString.ts
Original file line number Diff line number Diff line change
@@ -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()}`;
};
20 changes: 20 additions & 0 deletions src/generator/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/types/test/guid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 7b33db0

Please sign in to comment.