Skip to content

Commit

Permalink
Merge pull request #2287 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun authored Apr 17, 2023
2 parents f72b537 + b3ab757 commit 35338f2
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 13 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
<img width="70" src="https://avatars.githubusercontent.com/u/18175329?s=52&v=4">
</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="https://opencollective.com/sight-and-sound-ministries" target="_blank">
<img width="70" src="https://user-images.githubusercontent.com/17680888/232316426-cb99b4cf-0ccb-4e73-a6ce-e16dba6aadf4.png">
</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="https://www.exoflare.com/open-source/?utm_source=dayjs&utm_campaign=open_source" target="_blank">
<img width="70" src="https://user-images.githubusercontent.com/17680888/162761622-1407a849-0c41-4591-8aa9-f98114ec2092.png">
</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="https://rxdb.info/?utm_source=day.js.org&utm_medium=banner&utm_campaign=day.js.org-sponsored" target="_blank"><img width="70" src="https://user-images.githubusercontent.com/17680888/200301812-9c9bd523-5dc4-4cab-b380-543fbcd3802c.svg"></a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="https://github.com/vendure-ecommerce" target="_blank"><img width="70" src="https://avatars.githubusercontent.com/u/39629390?s=52&v=4"></a>
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class Dayjs {

const matches = {
YY: String(this.$y).slice(-2),
YYYY: this.$y,
YYYY: Utils.s(this.$y, 4, '0'),
M: $M + 1,
MM: Utils.s($M + 1, 2, '0'),
MMM: getShort(locale.monthsShort, $M, months, 3),
Expand Down
4 changes: 4 additions & 0 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ class Duration {
.fromNow(!withSuffix)
}

valueOf() {
return this.asMilliseconds()
}

milliseconds() { return this.get('milliseconds') }
asMilliseconds() { return this.as('milliseconds') }
seconds() { return this.get('seconds') }
Expand Down
12 changes: 12 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,15 @@ it('As JSON -> toJSON', () => {
it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
expect(dayjs().toISOString()).toBe(moment().toISOString())
})

it('Year 1 formatted with YYYY should pad with zeroes', () => {
const date = new Date(1, 0, 1)
date.setUTCFullYear(1) // Required because 0-99 are parsed as 19xx in JS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#year
expect(dayjs(date).format('YYYY')).toBe('0001')
})

it('Year 1 formatting matches moment format', () => {
const date = new Date(1, 0, 1)
date.setUTCFullYear(1)
expect(dayjs(date).format('YYYY')).toBe(moment(date).format('YYYY'))
})
3 changes: 3 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ describe('Creating', () => {
ms: -1
}).toISOString()).toBe('-PT0.001S')
})
it('convert to milliseconds', () => {
expect(+dayjs.duration(100)).toBe(100)
})
})

describe('Parse ISO string', () => {
Expand Down
7 changes: 7 additions & 0 deletions test/plugin/isSameOrAfter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ test('is same or after without units', () => {
expect(+m).toEqual(+mCopy, 'isSameOrAfter second should not change moment')
})

test('is same or after without date', () => {
const past = dayjs().subtract(1, 'day')
const future = dayjs().add(1, 'day')
expect(past.isSameOrAfter()).toBe(false, 'past is before now')
expect(future.isSameOrAfter()).toBe(true, 'future is not before now')
})

test('is same or after month', () => {
const m = dayjs(new Date(2011, 2, 3, 4, 5, 6, 7))
const mCopy = dayjs(m)
Expand Down
7 changes: 7 additions & 0 deletions test/plugin/isSameOrBefore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ test('is same or before without units', () => {
expect(+m).toEqual(+mCopy, 'isSameOrBefore second should not change moment')
})

test('is same or before without date', () => {
const past = dayjs().subtract(1, 'day')
const future = dayjs().add(1, 'day')
expect(past.isSameOrBefore()).toBe(true, 'past is before now')
expect(future.isSameOrBefore()).toBe(false, 'future is not before now')
})

test('is same or before year', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
Expand Down
6 changes: 3 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ declare namespace dayjs {
*
* Docs: https://day.js.org/docs/en/query/is-before
*/
isBefore(date: ConfigType, unit?: OpUnitType): boolean
isBefore(date?: ConfigType, unit?: OpUnitType): boolean
/**
* This indicates whether the Day.js object is the same as the other supplied date-time.
* ```
Expand All @@ -394,7 +394,7 @@ declare namespace dayjs {
* ```
* Docs: https://day.js.org/docs/en/query/is-same
*/
isSame(date: ConfigType, unit?: OpUnitType): boolean
isSame(date?: ConfigType, unit?: OpUnitType): boolean
/**
* This indicates whether the Day.js object is after the other supplied date-time.
* ```
Expand All @@ -408,7 +408,7 @@ declare namespace dayjs {
*
* Docs: https://day.js.org/docs/en/query/is-after
*/
isAfter(date: ConfigType, unit?: OpUnitType): boolean
isAfter(date?: ConfigType, unit?: OpUnitType): boolean

locale(): string

Expand Down
2 changes: 1 addition & 1 deletion types/plugin/isSameOrAfter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export = plugin

declare module 'dayjs' {
interface Dayjs {
isSameOrAfter(date: ConfigType, unit?: OpUnitType): boolean
isSameOrAfter(date?: ConfigType, unit?: OpUnitType): boolean
}
}
2 changes: 1 addition & 1 deletion types/plugin/isSameOrBefore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export = plugin

declare module 'dayjs' {
interface Dayjs {
isSameOrBefore(date: ConfigType, unit?: OpUnitType): boolean
isSameOrBefore(date?: ConfigType, unit?: OpUnitType): boolean
}
}
6 changes: 3 additions & 3 deletions types/plugin/isoWeek.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ declare module 'dayjs' {

endOf(unit: ISOUnitType): Dayjs

isSame(date: ConfigType, unit?: ISOUnitType): boolean
isSame(date?: ConfigType, unit?: ISOUnitType): boolean

isBefore(date: ConfigType, unit?: ISOUnitType): boolean
isBefore(date?: ConfigType, unit?: ISOUnitType): boolean

isAfter(date: ConfigType, unit?: ISOUnitType): boolean
isAfter(date?: ConfigType, unit?: ISOUnitType): boolean
}
}
6 changes: 3 additions & 3 deletions types/plugin/quarterOfYear.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ declare module 'dayjs' {

endOf(unit: QUnitType | OpUnitType): Dayjs

isSame(date: ConfigType, unit?: QUnitType): boolean
isSame(date?: ConfigType, unit?: QUnitType): boolean

isBefore(date: ConfigType, unit?: QUnitType): boolean
isBefore(date?: ConfigType, unit?: QUnitType): boolean

isAfter(date: ConfigType, unit?: QUnitType): boolean
isAfter(date?: ConfigType, unit?: QUnitType): boolean
}
}
2 changes: 1 addition & 1 deletion types/plugin/timezone.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare module 'dayjs' {
}

interface DayjsTimezone {
(date: ConfigType, timezone?: string): Dayjs
(date?: ConfigType, timezone?: string): Dayjs
(date: ConfigType, format: string, timezone?: string): Dayjs
guess(): string
setDefault(timezone?: string): void
Expand Down

0 comments on commit 35338f2

Please sign in to comment.