Skip to content

Commit

Permalink
dayjs 过滤器移动到 packages 文件件内,保持单独发布,但是改变包名
Browse files Browse the repository at this point in the history
  • Loading branch information
FairyEver committed Apr 6, 2019
1 parent 583f5bb commit 184876a
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 379 deletions.
723 changes: 347 additions & 376 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
},
"dependencies": {
"@babel/runtime": "^7.2.0",
"@d2-admin/filters-dayjs": "^1.0.1",
"@d2-projects/d2-crud": "^2.0.3",
"@d2-projects/vue-filters-date": "^1.0.2",
"@d2-projects/vue-table-export": "^1.0.1",
"@d2-projects/vue-table-import": "^1.0.0",
"axios": "^0.17.1",
Expand Down
13 changes: 13 additions & 0 deletions packages/filters-date/package-lock.json

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

12 changes: 12 additions & 0 deletions packages/filters-date/package.json
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"
}
}
86 changes: 86 additions & 0 deletions packages/filters-date/src/filters.js
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)
}
15 changes: 15 additions & 0 deletions packages/filters-date/src/index.js
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
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import VueUeditorWrap from 'vue-ueditor-wrap'
import pluginExport from '@d2-projects/vue-table-export'
import pluginImport from '@d2-projects/vue-table-import'
// [ 可选过滤器 ] 日期相关过滤器
import d2VueFiltersDate from '@d2-projects/vue-filters-date'
import d2VueFiltersDayjs from '@d2-admin/filters-dayjs'

// 菜单和路由设置
import router from './router'
Expand All @@ -44,7 +44,7 @@ Vue.use(contentmenu)
Vue.use(vueJsonTreeView)
Vue.use(pluginExport)
Vue.use(pluginImport)
Vue.use(d2VueFiltersDate)
Vue.use(d2VueFiltersDayjs)
Vue.component('d2-grid-layout', GridLayout)
Vue.component('d2-grid-item', GridItem)
Vue.component('SplitPane', SplitPane)
Expand Down

0 comments on commit 184876a

Please sign in to comment.