-
-
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
feat:add plugin objectSupport #887
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
954db86
feat add plugin objectSupport
zerooverture 89596ca
refactor:remove unnecessary proto methods
zerooverture 5963241
refactor:remove unnecessary proto methods
zerooverture 9e8fef1
fix:action scope of this
zerooverture 1487db5
fix:remove unnecessary symbols
zerooverture fe7fb05
refactor:optimized parseDate code
zerooverture 7d7db50
refactor:optimized code
zerooverture 60f59be
refactor:removed ...arr
zerooverture 460e312
refactor:reduce the compression
zerooverture fa8528d
test:add default year,month,date
zerooverture b9f7f46
refactor:redundant methods
zerooverture 46ecb61
test:relativeTime line 23 covered
zerooverture 3c51c1c
Revert "test:relativeTime line 23 covered"
zerooverture 83f3206
refactor:optimized code
zerooverture File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
export default (o, c) => { | ||
const proto = c.prototype | ||
const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object | ||
const prettyUnit = (u) => { | ||
const unit = proto.$utils().p(u) | ||
return unit === 'date' ? 'day' : unit | ||
} | ||
const parseDate = (cfg) => { | ||
const { date, utc } = cfg | ||
const $d = {} | ||
if (isObject(date)) { | ||
Object.keys(date).forEach((k) => { | ||
$d[prettyUnit(k)] = date[k] | ||
}) | ||
const y = $d.year || 1970 | ||
const M = $d.month - 1 || 0 | ||
const d = $d.day || 1 | ||
const h = $d.hour || 0 | ||
const m = $d.minute || 0 | ||
const s = $d.second || 0 | ||
const ms = $d.millisecond || 0 | ||
if (utc) { | ||
return new Date(Date.UTC(y, M, d, h, m, s, ms)) | ||
} | ||
return new Date(y, M, d, h, m, s, ms) | ||
} | ||
return date | ||
} | ||
|
||
const oldParse = proto.parse | ||
proto.parse = function (cfg) { | ||
cfg.date = parseDate.bind(this)(cfg) | ||
oldParse.bind(this)(cfg) | ||
} | ||
|
||
const oldSet = proto.set | ||
const oldAdd = proto.add | ||
|
||
const callObject = function (call, argument, string, offset = 1) { | ||
if (argument instanceof Object) { | ||
const keys = Object.keys(argument) | ||
let chain = this | ||
keys.forEach((key) => { | ||
chain = call.bind(chain)(argument[key] * offset, key) | ||
}) | ||
return chain | ||
} | ||
return call.bind(this)(argument * offset, string) | ||
} | ||
|
||
proto.set = function (string, int) { | ||
int = int === undefined ? string : int | ||
return callObject.bind(this)(function (i, s) { | ||
return oldSet.bind(this)(s, i) | ||
}, int, string) | ||
} | ||
proto.add = function (number, string) { | ||
return callObject.bind(this)(oldAdd, number, string) | ||
} | ||
proto.subtract = function (number, string) { | ||
return callObject.bind(this)(oldAdd, number, string, -1) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
substract
could be implemented as negativeadd
, subtract { day: 1 } => add { day: 1 * -1 }, so that we could only careadd
andset
logicThere 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.
the three methods have been merged into one.