This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): improve dates parsing and formatting
- Loading branch information
Showing
22 changed files
with
914 additions
and
12,205 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,61 @@ | ||
const dayjs = require('dayjs') | ||
const utc = require('dayjs/plugin/utc') | ||
|
||
const logger = require('./logger') | ||
|
||
dayjs.extend(utc) | ||
|
||
export default () => { | ||
return { | ||
addLocales(extraLangs) { | ||
if (extraLangs && extraLangs.length > 0) { | ||
extraLangs.forEach(l => dayjs.locale(l)) | ||
} | ||
}, | ||
format(date, template, locale = 'en') { | ||
return dayjs.utc(date).locale(locale).format(template) | ||
}, | ||
parse(inputDate) { | ||
const date = inputDate ? inputDate : new Date() | ||
return dayjs.utc(date) | ||
}, | ||
range(start, end, by = 'day') { | ||
const startDate = this.parse(start) | ||
const endDate = this.parse(end) | ||
let dates = [] | ||
let diff = {}; | ||
|
||
if (endDate.diff(startDate) < 0) { | ||
logger.fatal('End Date must be greater than Start Date') | ||
process.exit(0) | ||
} | ||
|
||
if (by === 'day') { | ||
const startD = startDate.startOf('day'); | ||
const endD = endDate.startOf('day'); | ||
|
||
diff.day = endD.diff(startD, 'day', true) | ||
dates.push(startD) | ||
|
||
for (let i = 1; i <= diff.day; i++) { | ||
dates.push(startD.add(i, 'day')) | ||
} | ||
} else if (by === 'month') { | ||
const startM = startDate.startOf('month'); | ||
const endM = endDate.startOf('month'); | ||
|
||
diff.month = endM.diff(startM, 'month', true) | ||
dates.push(startM) | ||
|
||
for (let i = 1; i <= diff.month; i++) { | ||
dates.push(startM.add(i, 'month')) | ||
} | ||
} | ||
|
||
return { | ||
diff, | ||
dates | ||
} | ||
} | ||
} | ||
} |
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
99 changes: 99 additions & 0 deletions
99
packages/@statusfy/common/test/__snapshots__/dates.spec.js.snap
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,99 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`common:dates range 1`] = ` | ||
Array [ | ||
"2019-03-06T00:00:00.000Z", | ||
"2019-03-07T00:00:00.000Z", | ||
"2019-03-08T00:00:00.000Z", | ||
"2019-03-09T00:00:00.000Z", | ||
"2019-03-10T00:00:00.000Z", | ||
"2019-03-11T00:00:00.000Z", | ||
"2019-03-12T00:00:00.000Z", | ||
"2019-03-13T00:00:00.000Z", | ||
] | ||
`; | ||
|
||
exports[`common:dates range 2`] = ` | ||
Array [ | ||
"2019-01-11T00:00:00.000Z", | ||
"2019-01-12T00:00:00.000Z", | ||
"2019-01-13T00:00:00.000Z", | ||
"2019-01-14T00:00:00.000Z", | ||
"2019-01-15T00:00:00.000Z", | ||
"2019-01-16T00:00:00.000Z", | ||
"2019-01-17T00:00:00.000Z", | ||
"2019-01-18T00:00:00.000Z", | ||
"2019-01-19T00:00:00.000Z", | ||
"2019-01-20T00:00:00.000Z", | ||
"2019-01-21T00:00:00.000Z", | ||
"2019-01-22T00:00:00.000Z", | ||
"2019-01-23T00:00:00.000Z", | ||
"2019-01-24T00:00:00.000Z", | ||
"2019-01-25T00:00:00.000Z", | ||
"2019-01-26T00:00:00.000Z", | ||
"2019-01-27T00:00:00.000Z", | ||
"2019-01-28T00:00:00.000Z", | ||
"2019-01-29T00:00:00.000Z", | ||
"2019-01-30T00:00:00.000Z", | ||
"2019-01-31T00:00:00.000Z", | ||
"2019-02-01T00:00:00.000Z", | ||
"2019-02-02T00:00:00.000Z", | ||
"2019-02-03T00:00:00.000Z", | ||
"2019-02-04T00:00:00.000Z", | ||
"2019-02-05T00:00:00.000Z", | ||
"2019-02-06T00:00:00.000Z", | ||
"2019-02-07T00:00:00.000Z", | ||
"2019-02-08T00:00:00.000Z", | ||
"2019-02-09T00:00:00.000Z", | ||
"2019-02-10T00:00:00.000Z", | ||
"2019-02-11T00:00:00.000Z", | ||
"2019-02-12T00:00:00.000Z", | ||
"2019-02-13T00:00:00.000Z", | ||
"2019-02-14T00:00:00.000Z", | ||
"2019-02-15T00:00:00.000Z", | ||
"2019-02-16T00:00:00.000Z", | ||
"2019-02-17T00:00:00.000Z", | ||
"2019-02-18T00:00:00.000Z", | ||
"2019-02-19T00:00:00.000Z", | ||
"2019-02-20T00:00:00.000Z", | ||
"2019-02-21T00:00:00.000Z", | ||
"2019-02-22T00:00:00.000Z", | ||
"2019-02-23T00:00:00.000Z", | ||
"2019-02-24T00:00:00.000Z", | ||
"2019-02-25T00:00:00.000Z", | ||
"2019-02-26T00:00:00.000Z", | ||
"2019-02-27T00:00:00.000Z", | ||
"2019-02-28T00:00:00.000Z", | ||
"2019-03-01T00:00:00.000Z", | ||
"2019-03-02T00:00:00.000Z", | ||
"2019-03-03T00:00:00.000Z", | ||
"2019-03-04T00:00:00.000Z", | ||
"2019-03-05T00:00:00.000Z", | ||
"2019-03-06T00:00:00.000Z", | ||
"2019-03-07T00:00:00.000Z", | ||
"2019-03-08T00:00:00.000Z", | ||
"2019-03-09T00:00:00.000Z", | ||
"2019-03-10T00:00:00.000Z", | ||
"2019-03-11T00:00:00.000Z", | ||
"2019-03-12T00:00:00.000Z", | ||
"2019-03-13T00:00:00.000Z", | ||
] | ||
`; | ||
|
||
exports[`common:dates range 3`] = ` | ||
Array [ | ||
"2019-01-01T00:00:00.000Z", | ||
"2019-02-01T00:00:00.000Z", | ||
"2019-03-01T00:00:00.000Z", | ||
] | ||
`; | ||
|
||
exports[`common:dates range 4`] = ` | ||
Array [ | ||
"2018-11-01T00:00:00.000Z", | ||
"2018-12-01T00:00:00.000Z", | ||
"2019-01-01T00:00:00.000Z", | ||
"2019-02-01T00:00:00.000Z", | ||
"2019-03-01T00:00:00.000Z", | ||
] | ||
`; |
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,91 @@ | ||
import Dates from '@/common/lib/dates' | ||
|
||
const dates = Dates() | ||
|
||
describe('common:dates', () => { | ||
test('parse', () => { | ||
const date1 = dates.parse('2019-03-13T15:30:13.758Z') | ||
const date2 = dates.parse('2019-03-10T02:30:13Z') | ||
const date3 = dates.parse('2019-03-11T13:06:03.407Z') | ||
const date4 = dates.parse(new Date('2019-03-11T13:06:03.407Z')) | ||
const date5 = dates.parse(new Date('05 October 2011 14:48 UTC')) | ||
|
||
expect(date1.isValid()).toBeTruthy() | ||
expect(date1.utcOffset()).toBe(0) | ||
|
||
expect(date2.isValid()).toBeTruthy() | ||
expect(date2.utcOffset()).toBe(0) | ||
|
||
expect(date3.isValid()).toBeTruthy() | ||
expect(date3.utcOffset()).toBe(0) | ||
|
||
expect(date4.isValid()).toBeTruthy() | ||
expect(date4.utcOffset()).toBe(0) | ||
|
||
expect(date5.isValid()).toBeTruthy() | ||
expect(date5.utcOffset()).toBe(0) | ||
}) | ||
|
||
test('format', () => { | ||
const date1 = dates.parse('2019-03-13T15:30:13.758Z') | ||
const date2 = dates.parse('2019-03-10T02:30:13Z') | ||
const date3 = dates.parse('2019-03-11T13:06:03.407Z') | ||
const date4 = dates.parse(new Date('2019-03-11T13:06:03.407Z')) | ||
const date5 = dates.parse(new Date('05 October 2011 14:48 UTC')) | ||
|
||
expect(dates.format(date1)).toBe('2019-03-13T15:30:13Z') | ||
expect(date1.toISOString()).toBe('2019-03-13T15:30:13.758Z') | ||
expect(date1.toString()).toBe('Wed, 13 Mar 2019 15:30:13 GMT') | ||
|
||
expect(dates.format(date2)).toBe('2019-03-10T02:30:13Z') | ||
expect(date2.toISOString()).toBe('2019-03-10T02:30:13.000Z') | ||
expect(date2.toString()).toBe('Sun, 10 Mar 2019 02:30:13 GMT') | ||
|
||
expect(dates.format(date3)).toBe('2019-03-11T13:06:03Z') | ||
expect(date3.toISOString()).toBe('2019-03-11T13:06:03.407Z') | ||
expect(date3.toString()).toBe('Mon, 11 Mar 2019 13:06:03 GMT') | ||
|
||
expect(dates.format(date4)).toBe('2019-03-11T13:06:03Z') | ||
expect(date4.toISOString()).toBe('2019-03-11T13:06:03.407Z') | ||
expect(date4.toString()).toBe('Mon, 11 Mar 2019 13:06:03 GMT') | ||
|
||
expect(dates.format(date5)).toBe('2011-10-05T14:48:00Z') | ||
expect(date5.toISOString()).toBe('2011-10-05T14:48:00.000Z') | ||
expect(date5.toString()).toBe('Wed, 05 Oct 2011 14:48:00 GMT') | ||
}) | ||
|
||
test('range', () => { | ||
const date1 = dates.parse('2019-03-13T15:30:13.758Z') | ||
const date2 = dates.parse('2019-03-06T02:30:13Z') | ||
const date3 = dates.parse('2019-01-11T13:06:03.407Z') | ||
const date4 = dates.parse('2018-11-11T13:06:03.407Z') | ||
|
||
const date2date1DayRange = dates.range(date2, date1) | ||
const date3date1DayRange = dates.range(date3, date1) | ||
|
||
const date3date1MonthRange = dates.range(date3, date1, 'month') | ||
const date4date1MonthRange = dates.range(date4, date1, 'month') | ||
|
||
// diff day | ||
expect(date2date1DayRange.diff.day).toBe(7) | ||
expect(date2date1DayRange.dates).toMatchSnapshot(); | ||
expect(date2date1DayRange.dates[0]).toEqual(date2.startOf('day')) | ||
expect(date2date1DayRange.dates[date2date1DayRange.dates.length - 1]).toEqual(date1.startOf('day')) | ||
|
||
expect(date3date1DayRange.diff.day).toBe(61) | ||
expect(date3date1DayRange.dates).toMatchSnapshot(); | ||
expect(date3date1DayRange.dates[0]).toEqual(date3.startOf('day')) | ||
expect(date3date1DayRange.dates[date3date1DayRange.dates.length - 1]).toEqual(date1.startOf('day')) | ||
|
||
// diff month | ||
expect(date3date1MonthRange.diff.month).toBe(2) | ||
expect(date3date1MonthRange.dates).toMatchSnapshot(); | ||
expect(date3date1MonthRange.dates[0]).toEqual(date3.startOf('month')) | ||
expect(date3date1MonthRange.dates[date3date1MonthRange.dates.length - 1]).toEqual(date1.startOf('month')) | ||
|
||
expect(date4date1MonthRange.diff.month).toBe(4) | ||
expect(date4date1MonthRange.dates).toMatchSnapshot(); | ||
expect(date4date1MonthRange.dates[0]).toEqual(date4.startOf('month')) | ||
expect(date4date1MonthRange.dates[date4date1MonthRange.dates.length - 1]).toEqual(date1.startOf('month')) | ||
}) | ||
}) |
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
Oops, something went wrong.