From cdae0ba4b7ec337d54fabd23f6a454c640e2675c Mon Sep 17 00:00:00 2001 From: Genaro Camele Date: Sat, 21 Jun 2025 14:21:07 -0300 Subject: [PATCH 1/6] Improved daysInMonth performance --- src/index.js | 25 ++++++++++++++++++++++++- src/plugin/isLeapYear/index.js | 4 +++- src/utils.js | 5 ++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 061ade178..ecafc3072 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,22 @@ let L = 'en' // global locale const Ls = {} // global loaded locale Ls[L] = en +/** Index to get the number of days */ +const MONTH_DAYS = { + 0: 31, // January + 1: 28, // February + 2: 31, // March + 3: 30, // April + 4: 31, // May + 5: 30, // June + 6: 31, // July + 7: 31, // August + 8: 30, // September + 9: 31, // October + 10: 30, // November + 11: 31 // December +} + const IS_DAYJS = '$isDayjsObject' // eslint-disable-next-line no-use-before-define @@ -389,7 +405,14 @@ class Dayjs { } daysInMonth() { - return this.endOf(C.M).$D + const numberOfDays = MONTH_DAYS[this.$M] + + // February (leap year) + if (this.$M === 1 && U.yearIsLeap(this.$y)) { + return numberOfDays + 1 + } + + return numberOfDays } $locale() { // get locale object diff --git a/src/plugin/isLeapYear/index.js b/src/plugin/isLeapYear/index.js index 35df7b46e..cfd126f86 100644 --- a/src/plugin/isLeapYear/index.js +++ b/src/plugin/isLeapYear/index.js @@ -1,7 +1,9 @@ +import U from '../../utils' + export default (o, c) => { const proto = c.prototype proto.isLeapYear = function () { - return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0) + return U.yearIsLeap(this.$y) } } diff --git a/src/utils.js b/src/utils.js index 324bc5e11..9dcf28a22 100644 --- a/src/utils.js +++ b/src/utils.js @@ -45,11 +45,14 @@ const prettyUnit = (u) => { const isUndefined = s => s === undefined +const yearIsLeap = year => ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0) + export default { s: padStart, z: padZoneStr, m: monthDiff, a: absFloor, p: prettyUnit, - u: isUndefined + u: isUndefined, + yearIsLeap } From 69295df90f30dc293fc12dda0b7c96ba6d4481c9 Mon Sep 17 00:00:00 2001 From: Genaro Camele Date: Sat, 21 Jun 2025 14:21:25 -0300 Subject: [PATCH 2/6] Fixed eslint command --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c76e01780..5669cdc58 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scripts": { "test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && TZ=America/Whitehorse npm run test-tz && npm run test-tz && jest", "test-tz": "date && jest test/timezone.test --coverage=false", - "lint": "./node_modules/.bin/eslint src/* test/* build/*", + "lint": "eslint src/* test/* build/*", "prettier": "prettier --write \"docs/**/*.md\"", "babel": "cross-env BABEL_ENV=build babel src --out-dir esm --copy-files && node build/esm", "build": "cross-env BABEL_ENV=build node build && npm run size", From 7dc95ff935cbc78bb58018a86f5eb1cc63d74b42 Mon Sep 17 00:00:00 2001 From: Genaro Camele Date: Sat, 21 Jun 2025 14:21:35 -0300 Subject: [PATCH 3/6] Added missing cross-env --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5669cdc58..9439a52c8 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "dayjs.min.js", "types": "index.d.ts", "scripts": { - "test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && TZ=America/Whitehorse npm run test-tz && npm run test-tz && jest", + "test": "cross-env TZ=Pacific/Auckland npm run test-tz && cross-env TZ=Europe/London npm run test-tz && cross-env TZ=America/Whitehorse npm run test-tz && npm run test-tz && jest", "test-tz": "date && jest test/timezone.test --coverage=false", "lint": "eslint src/* test/* build/*", "prettier": "prettier --write \"docs/**/*.md\"", From b3ce7b8e07d9961b1883d8c8de86dbd893aef4ff Mon Sep 17 00:00:00 2001 From: Genaro Camele Date: Sat, 21 Jun 2025 14:31:10 -0300 Subject: [PATCH 4/6] Improved daysInMonth return type --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index cd159dcae..5dc54d439 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -329,7 +329,7 @@ declare namespace dayjs { * ``` * Docs: https://day.js.org/docs/en/display/days-in-month */ - daysInMonth(): number + daysInMonth(): 28 | 29 | 30 | 31 /** * To get a copy of the native `Date` object parsed from the Day.js object use `dayjs#toDate`. * ``` From aefb632b06ce0c993ba6ac4058936280c248cce0 Mon Sep 17 00:00:00 2001 From: Genaro Camele Date: Sat, 21 Jun 2025 14:31:27 -0300 Subject: [PATCH 5/6] chore: update cross-env to version 5.2.1 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index fa9c756cb..ff2396125 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "babel-core": "^7.0.0-bridge.0", "babel-jest": "^22.4.3", "babel-plugin-external-helpers": "^6.22.0", - "cross-env": "^5.1.6", + "cross-env": "^5.2.1", "eslint": "^4.19.1", "eslint-config-airbnb-base": "^12.1.0", "eslint-plugin-import": "^2.10.0", diff --git a/package.json b/package.json index 9439a52c8..cb0b6030f 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "babel-core": "^7.0.0-bridge.0", "babel-jest": "^22.4.3", "babel-plugin-external-helpers": "^6.22.0", - "cross-env": "^5.1.6", + "cross-env": "^5.2.1", "eslint": "^4.19.1", "eslint-config-airbnb-base": "^12.1.0", "eslint-plugin-import": "^2.10.0", From 4cae57300bac4ea18da0e14d3fef544e964634b1 Mon Sep 17 00:00:00 2001 From: Genaro Camele Date: Sat, 4 Oct 2025 11:36:39 -0300 Subject: [PATCH 6/6] Reverts commits b3ce7b8e07d9961b1883d8c8de86dbd893aef4ff and cdae0ba4b7ec337d54fabd23f6a454c640e2675c --- src/index.js | 25 +------------------------ src/plugin/isLeapYear/index.js | 4 +--- src/utils.js | 5 +---- types/index.d.ts | 2 +- 4 files changed, 4 insertions(+), 32 deletions(-) diff --git a/src/index.js b/src/index.js index ecafc3072..061ade178 100644 --- a/src/index.js +++ b/src/index.js @@ -6,22 +6,6 @@ let L = 'en' // global locale const Ls = {} // global loaded locale Ls[L] = en -/** Index to get the number of days */ -const MONTH_DAYS = { - 0: 31, // January - 1: 28, // February - 2: 31, // March - 3: 30, // April - 4: 31, // May - 5: 30, // June - 6: 31, // July - 7: 31, // August - 8: 30, // September - 9: 31, // October - 10: 30, // November - 11: 31 // December -} - const IS_DAYJS = '$isDayjsObject' // eslint-disable-next-line no-use-before-define @@ -405,14 +389,7 @@ class Dayjs { } daysInMonth() { - const numberOfDays = MONTH_DAYS[this.$M] - - // February (leap year) - if (this.$M === 1 && U.yearIsLeap(this.$y)) { - return numberOfDays + 1 - } - - return numberOfDays + return this.endOf(C.M).$D } $locale() { // get locale object diff --git a/src/plugin/isLeapYear/index.js b/src/plugin/isLeapYear/index.js index cfd126f86..35df7b46e 100644 --- a/src/plugin/isLeapYear/index.js +++ b/src/plugin/isLeapYear/index.js @@ -1,9 +1,7 @@ -import U from '../../utils' - export default (o, c) => { const proto = c.prototype proto.isLeapYear = function () { - return U.yearIsLeap(this.$y) + return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0) } } diff --git a/src/utils.js b/src/utils.js index 9dcf28a22..324bc5e11 100644 --- a/src/utils.js +++ b/src/utils.js @@ -45,14 +45,11 @@ const prettyUnit = (u) => { const isUndefined = s => s === undefined -const yearIsLeap = year => ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0) - export default { s: padStart, z: padZoneStr, m: monthDiff, a: absFloor, p: prettyUnit, - u: isUndefined, - yearIsLeap + u: isUndefined } diff --git a/types/index.d.ts b/types/index.d.ts index 5dc54d439..cd159dcae 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -329,7 +329,7 @@ declare namespace dayjs { * ``` * Docs: https://day.js.org/docs/en/display/days-in-month */ - daysInMonth(): 28 | 29 | 30 | 31 + daysInMonth(): number /** * To get a copy of the native `Date` object parsed from the Day.js object use `dayjs#toDate`. * ```