Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date type methods #31

Merged
merged 2 commits into from
Apr 30, 2019
Merged
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
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(
mcrispino marked this conversation as resolved.
Show resolved Hide resolved
/([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()}`;
mcrispino marked this conversation as resolved.
Show resolved Hide resolved
};
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