-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dayjs 过滤器移动到 packages 文件件内,保持单独发布,但是改变包名
- Loading branch information
Showing
7 changed files
with
476 additions
and
379 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
{ | ||
"name": "@d2-admin/filters-dayjs", | ||
"version": "1.0.1", | ||
"description": "dayjs to vue filters", | ||
"main": "src/index.js", | ||
"scripts": {}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"dayjs": "^1.8.12" | ||
} | ||
} |
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,86 @@ | ||
// 日期时间相关 filter | ||
// https://github.com/iamkun/dayjs/blob/master/docs/zh-cn/API-reference.md | ||
|
||
import dayjs from 'dayjs' | ||
|
||
// 对象代理 | ||
const P = Day => { | ||
return new Proxy(Day, { | ||
get (target, key) { | ||
if (dayjs.isDayjs(target)) { | ||
// 是 Dayjs 对象,正常返回 | ||
return target[key] | ||
} else { | ||
// 不是 Dayjs 对象 | ||
if (dayjs(target).isValid()) { | ||
// 尝试帮用户解析成 Dayjs 对象 | ||
return dayjs(target)[key] | ||
} else { | ||
// 无法解析 | ||
return function () { | ||
return '无效日期' | ||
} | ||
} | ||
} | ||
}, | ||
set (target, key, value) { | ||
target[key] = value | ||
} | ||
}) | ||
} | ||
|
||
export default { | ||
// ---------- [ dayjs 解析 ] ---------- | ||
// 时间字符串 | Date 对象 | Unix 时间戳 (毫秒) | ||
day: value => dayjs(value), | ||
// Unix 时间戳 (秒) | ||
date_unix: value => dayjs.unix(value), | ||
// ---------- [ 获取 ] ---------- | ||
date_year: Day => P(Day).year(), | ||
date_month: Day => P(Day).month(), | ||
date_date: Day => P(Day).date(), | ||
date_day: Day => P(Day).day(), | ||
date_hour: Day => P(Day).hour(), | ||
date_minute: Day => P(Day).minute(), | ||
date_second: Day => P(Day).second(), | ||
date_millisecond: Day => P(Day).millisecond(), | ||
// ---------- [ 设置 ] ---------- | ||
// date | day | month | year | hour | minute | second | millisecond | ||
// 对大小写不敏感 | ||
date_set: (Day, unit, value) => P(Day).set(unit, value), | ||
// ---------- [ 操作 ] ---------- | ||
// 增加 | ||
date_add: (Day, value, unit) => P(Day).add(value, unit), | ||
// 减少 | ||
date_subtract: (Day, value, unit) => P(Day).subtract(value, unit), | ||
// 开头时间 | ||
date_startof: (Day, unit) => P(Day).startOf(unit), | ||
// 末尾时间 | ||
date_endof: (Day, unit) => P(Day).endOf(unit), | ||
// ---------- [ 显示 ] ---------- | ||
// 格式化 | ||
date_format: (Day, setting = 'YYYY-MM-DD HH:mm:ss') => P(Day).format(setting), | ||
// 时间差 | ||
date_diff: (Day, Day2 = '', unit = 'millisecond', accurate = false) => P(Day).diff(dayjs(Day2), unit, accurate), | ||
// Unix 时间戳 (毫秒) | ||
date_value_millisecond: Day => P(Day).valueOf(), | ||
// Unix 时间戳 (秒) | ||
date_value_second: Day => P(Day).unix(), | ||
// 月份的天数 | ||
date_days_in_month: Day => P(Day).daysInMonth(), | ||
// Date 对象 | ||
date_to_date: Day => P(Day).toDate(), | ||
// JSON | ||
date_to_json: Day => P(Day).toJSON(), | ||
// ISO8601 格式 | ||
date_to_iso: Day => P(Day).toISOString(), | ||
// 字符 | ||
date_to_string: Day => P(Day).toString(), | ||
// ---------- [ 查询 ] ---------- | ||
// 是否之前 | ||
date_is_before: (Day, Day2, unit = 'millisecond') => P(Day).isBefore(dayjs(Day2), unit), | ||
// 是否之后 | ||
date_is_after: (Day, Day2, unit = 'millisecond') => P(Day).isAfter(dayjs(Day2), unit), | ||
// 是否相同 | ||
date_is_same: (Day, Day2, unit = 'millisecond') => P(Day).isSame(dayjs(Day2), unit) | ||
} |
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,15 @@ | ||
import filters from './filters' | ||
|
||
const vueFiltersDate = { | ||
install: function (Vue, options) { | ||
Object.keys(filters).forEach(name => { | ||
Vue.filter(name, filters[name]) | ||
}) | ||
} | ||
} | ||
|
||
if (typeof window !== 'undefined' && window.Vue) { | ||
window.Vue.use(vueFiltersDate) | ||
} | ||
|
||
export default vueFiltersDate |
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