Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"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": "./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",
Expand Down Expand Up @@ -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",
Expand Down
25 changes: 24 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing the MONTH_DAYS constant might increase the bundle size, which may not be an ideal optimization?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @iamkun, if you prefer, I can roll back this change. But with minification, it compresses to roughly:

const MONTH_DAYS={0:31,1:28,2:31,3:30,4:31,5:30,6:31,7:31,8:30,9:31,10:30,11:31}

That’s 80 bytes. After gzip compression, it’s 70 bytes (source).

So in practice, this code would increase the bundle size by 0.07 KB (virtually negligible).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed this PR introduces some additional code into both utils and index.

A core principle of Day.js is its minimal bundle size, and we're highly sensitive to any changes that might increase it. For a change like this to be a worthwhile trade-off, we would need to see a significant performance improvement over the current endOf implementation.

Have you conducted any performance benchmarks to demonstrate the gains of this new approach? This data would be very helpful for us to evaluate if the change is a net positive for the project.

Looking forward to your thoughts!

const IS_DAYJS = '$isDayjsObject'

// eslint-disable-next-line no-use-before-define
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/plugin/isLeapYear/index.js
Original file line number Diff line number Diff line change
@@ -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)
}
}

5 changes: 4 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
* ```
Expand Down