-
Notifications
You must be signed in to change notification settings - Fork 13
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
1 parent
77f0d3c
commit 132f2d6
Showing
23 changed files
with
660 additions
and
285 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
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './datetime/date-adapter.js'; | ||
export * from './datetime/native-date-adapter.js'; | ||
export * from './datetime/time-adapter.js'; |
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,104 @@ | ||
import { expect } from '@open-wc/testing'; | ||
|
||
import { TimeAdapter } from './time-adapter.js'; | ||
|
||
describe('TimeAdapter', () => { | ||
let timeAdapter: TimeAdapter; | ||
|
||
beforeEach(() => { | ||
timeAdapter = new TimeAdapter(); | ||
}); | ||
|
||
it('addMilliseconds should return the right value', () => { | ||
let date = new Date(2023, 4, 1, 20, 5, 20, 200); | ||
|
||
expect(date.toISOString()).to.be.equal('2023-05-01T18:05:20.200Z'); | ||
|
||
date = timeAdapter.addMilliseconds(date, 200); | ||
expect(date.toISOString()).to.be.equal('2023-05-01T18:05:20.400Z'); | ||
|
||
date = timeAdapter.addMilliseconds(date, -300); | ||
expect(date.toISOString()).to.be.equal('2023-05-01T18:05:20.100Z'); | ||
}); | ||
|
||
it('addMinutes should return the right value', () => { | ||
let date = new Date(2023, 4, 1, 20, 5); | ||
|
||
expect(date.toISOString()).to.be.equal('2023-05-01T18:05:00.000Z'); | ||
|
||
date = timeAdapter.addMinutes(date, 20); | ||
expect(date.toISOString()).to.be.equal('2023-05-01T18:25:00.000Z'); | ||
|
||
date = timeAdapter.addMinutes(date, 150); | ||
expect(date.toISOString()).to.be.equal('2023-05-01T20:55:00.000Z'); | ||
}); | ||
|
||
it('differenceInMilliseconds should return the right value', () => { | ||
let firstDate = new Date(2023, 4, 1, 18, 5); | ||
let secondDate = new Date(2023, 4, 1, 20, 5); | ||
|
||
expect(timeAdapter.differenceInMilliseconds(firstDate, secondDate)).to.be.equal(-7200000); | ||
|
||
firstDate = new Date(2023, 4, 3, 16, 5); | ||
secondDate = new Date(2023, 4, 3, 8, 5); | ||
|
||
expect(timeAdapter.differenceInMilliseconds(firstDate, secondDate)).to.be.equal(28800000); | ||
}); | ||
|
||
it('differenceInMinutes should return the right value', () => { | ||
let firstDate = new Date(2023, 4, 1, 18, 5); | ||
let secondDate = new Date(2023, 4, 1, 20, 5); | ||
|
||
expect(timeAdapter.differenceInMinutes(firstDate, secondDate)).to.be.equal(-120); | ||
|
||
firstDate = new Date(2023, 4, 3, 16, 55); | ||
secondDate = new Date(2023, 4, 3, 16, 5); | ||
|
||
expect(timeAdapter.differenceInMinutes(firstDate, secondDate)).to.be.equal(50); | ||
}); | ||
|
||
it('isBefore should return the right value', () => { | ||
let firstDate = new Date(2023, 4, 1, 18, 5); | ||
let secondDate = new Date(2023, 4, 1, 20, 5); | ||
|
||
expect(timeAdapter.isBefore(firstDate, secondDate)).to.be.equal(true); | ||
|
||
firstDate = new Date(2023, 4, 3, 16, 55); | ||
secondDate = new Date(2023, 4, 3, 16, 5); | ||
|
||
expect(timeAdapter.isBefore(firstDate, secondDate)).to.be.equal(false); | ||
}); | ||
|
||
it('isAfter should return the right value', () => { | ||
let firstDate = new Date(2023, 4, 1, 18, 5); | ||
let secondDate = new Date(2023, 4, 1, 20, 5); | ||
|
||
expect(timeAdapter.isAfter(firstDate, secondDate)).to.be.equal(false); | ||
|
||
firstDate = new Date(2023, 4, 3, 16, 55); | ||
secondDate = new Date(2023, 4, 3, 16, 5); | ||
|
||
expect(timeAdapter.isAfter(firstDate, secondDate)).to.be.equal(true); | ||
}); | ||
|
||
it('isValid should return the right value', () => { | ||
expect(timeAdapter.isValid(new Date(2023, 4, 1, 18, 5))).to.be.equal(true); | ||
expect(timeAdapter.isValid(new Date(NaN))).to.be.equal(false); | ||
}); | ||
|
||
it('deserialize should return the right value', () => { | ||
expect(timeAdapter.deserialize(new Date(2023, 4, 1, 18, 5)).toISOString()).to.be.equal( | ||
'2023-05-01T16:05:00.000Z', | ||
); | ||
expect(timeAdapter.deserialize('2022-08-18T04:00').toISOString()).to.be.equal( | ||
'2022-08-18T02:00:00.000Z', | ||
); | ||
expect(timeAdapter.deserialize('1661788000').toISOString()).to.be.equal( | ||
'2022-08-29T15:46:40.000Z', | ||
); | ||
expect(timeAdapter.deserialize(1660628000).toISOString()).to.be.equal( | ||
'2022-08-16T05:33:20.000Z', | ||
); | ||
expect(timeAdapter.isValid(timeAdapter.deserialize('Invalid input'))).to.be.equal(false); | ||
}); | ||
}); |
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,56 @@ | ||
export class TimeAdapter { | ||
public constructor() {} | ||
|
||
public addMilliseconds(date: Date, amount: number): Date { | ||
const timestamp: number = date.getTime(); | ||
return new Date(timestamp + amount); | ||
} | ||
|
||
public addMinutes(date: Date, amount: number): Date { | ||
return this.addMilliseconds(date, amount * 60000); | ||
} | ||
|
||
public differenceInMilliseconds(firstDate: Date, secondDate: Date): number { | ||
return firstDate.getTime() - secondDate.getTime(); | ||
} | ||
|
||
public differenceInMinutes(firstDate: Date, secondDate: Date): number { | ||
return this.differenceInMilliseconds(firstDate, secondDate) / 60000; | ||
} | ||
|
||
public isBefore(firstDate: Date, secondDate: Date): boolean { | ||
return this.differenceInMilliseconds(firstDate, secondDate) < 0; | ||
} | ||
|
||
public isAfter(firstDate: Date, secondDate: Date): boolean { | ||
return this.differenceInMilliseconds(firstDate, secondDate) > 0; | ||
} | ||
|
||
/** Checks whether the given `date` is a valid Date. */ | ||
public isValid(date: Date | null | undefined): boolean { | ||
return !!date && !isNaN(date.valueOf()); | ||
} | ||
|
||
/** Creates a Date from a valid input (Date, string or number in seconds). */ | ||
public deserialize(date: Date | string | number | null | undefined): Date { | ||
if (typeof date === 'object' && date instanceof Date) { | ||
return date; | ||
} else if (typeof date === 'string') { | ||
if (!date) { | ||
return new Date(NaN); | ||
} else if (!Number.isNaN(+date)) { | ||
return new Date(+date * 1000); | ||
} else { | ||
return new Date(date.includes('T') ? date : date + 'T00:00:00'); | ||
} | ||
} else if (typeof date === 'number') { | ||
return new Date(date * 1000); | ||
} | ||
|
||
return new Date(NaN); | ||
} | ||
|
||
public invalid(): Date { | ||
return new Date(NaN); | ||
} | ||
} |
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.