Skip to content

Commit

Permalink
fix: Add plugin objectSupport (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
zerooverture committed Apr 30, 2020
1 parent 058d624 commit 52dfb13
Show file tree
Hide file tree
Showing 3 changed files with 419 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/plugin/objectSupport/index.js
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)
}
}
Loading

0 comments on commit 52dfb13

Please sign in to comment.