-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters