-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing localization #77
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #77 +/- ##
===================================
Coverage 100% 100%
===================================
Files 3 3
Lines 175 188 +13
Branches 27 34 +7
===================================
+ Hits 175 188 +13
Continue to review full report at Codecov.
|
format(formatStr = 'YYYY-MM-DDTHH:mm:ssZ') { | ||
const weeks = 'Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday'.split('.') | ||
const months = 'January.February.March.April.May.June.July.August.September.October.November.December'.split('.') | ||
format(formatStr = this.$format, L = {}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be improved by using:
format(formatStr = this.$format, L = this.$L)
Then I think you can get rid of next lines short circuits ||
. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really depends, because you may do format.('...', { WD: [0,1,2,3,4,5,6] })
to override the WeekDays locale, but leave MONTHS
untouched with the instance's default locale. You're providing overrides for certain locales, not the whole localized object.
Switching target branch to |
@@ -163,9 +172,15 @@ class Dayjs { | |||
return this | |||
} | |||
|
|||
set(string, int) { | |||
set(unitOrDate, int) { | |||
if (typeof unitOrDate === 'object' && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
myDayjs.set(new Date(2018, 5, 1)) // you can now pass a Date or Dayjs instance to set!
Could you explain the meaning of you code here, please? Don't really understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, this code is kind of explained in the test case https://github.com/Frondor/dayjs/blob/5f574850d1cb75c6bd2a8b1684a94346f1d501b7/test/get-set.test.js#L69
In the past, you were limited to set certain time units over an already defined date ($d
). Now that code checks whether the unitOrDate
passed is either a time unit, a Date
object, or a Dayjs
instance. So you can set the whole date if needed, not just an unit. Useful for inheriting configurations like locale, plugins and default format, and create date objects out of a single source, and keeping the Dayjs immutable since that's how set
works.
By the way, just for the record, this is the old PR, the new one is #90
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set(unitOrDate, int) {
// if unitOrDate is a Date or Dayjs object
if (typeof unitOrDate === 'object' &&
unitOrDate.constructor.name.indexOf(['Date', 'Dayjs'] > -1)) {
const self = this.clone() // don't mutate this instance
// set $d date object depending on whether its a native Date object, or a Dayjs instance
self.$d = unitOrDate.toDate ? unitOrDate.toDate() : unitOrDate
return self // done
}
// if this is not a date object nor an int just fail silently (this code was already here)
if (!Utils.isNumber(int)) return this
// unitOrDate is definitely a integer (unit)
return this.clone().mSet(unitOrDate, int)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Frondor Got it, thanks.
First try, WIP, waiting for reviews
Besides of implementing localization, this PR adds a new way to work with the library with more flexibility without breaking changes.
A full example of the new APIs and method features
closes #36