Skip to content

Commit 184876a

Browse files
committed
dayjs 过滤器移动到 packages 文件件内,保持单独发布,但是改变包名
1 parent 583f5bb commit 184876a

File tree

7 files changed

+476
-379
lines changed

7 files changed

+476
-379
lines changed

package-lock.json

Lines changed: 347 additions & 376 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
},
1515
"dependencies": {
1616
"@babel/runtime": "^7.2.0",
17+
"@d2-admin/filters-dayjs": "^1.0.1",
1718
"@d2-projects/d2-crud": "^2.0.3",
18-
"@d2-projects/vue-filters-date": "^1.0.2",
1919
"@d2-projects/vue-table-export": "^1.0.1",
2020
"@d2-projects/vue-table-import": "^1.0.0",
2121
"axios": "^0.17.1",

packages/filters-date/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/filters-date/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@d2-admin/filters-dayjs",
3+
"version": "1.0.1",
4+
"description": "dayjs to vue filters",
5+
"main": "src/index.js",
6+
"scripts": {},
7+
"author": "",
8+
"license": "ISC",
9+
"dependencies": {
10+
"dayjs": "^1.8.12"
11+
}
12+
}

packages/filters-date/src/filters.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// 日期时间相关 filter
2+
// https://github.com/iamkun/dayjs/blob/master/docs/zh-cn/API-reference.md
3+
4+
import dayjs from 'dayjs'
5+
6+
// 对象代理
7+
const P = Day => {
8+
return new Proxy(Day, {
9+
get (target, key) {
10+
if (dayjs.isDayjs(target)) {
11+
// 是 Dayjs 对象,正常返回
12+
return target[key]
13+
} else {
14+
// 不是 Dayjs 对象
15+
if (dayjs(target).isValid()) {
16+
// 尝试帮用户解析成 Dayjs 对象
17+
return dayjs(target)[key]
18+
} else {
19+
// 无法解析
20+
return function () {
21+
return '无效日期'
22+
}
23+
}
24+
}
25+
},
26+
set (target, key, value) {
27+
target[key] = value
28+
}
29+
})
30+
}
31+
32+
export default {
33+
// ---------- [ dayjs 解析 ] ----------
34+
// 时间字符串 | Date 对象 | Unix 时间戳 (毫秒)
35+
day: value => dayjs(value),
36+
// Unix 时间戳 (秒)
37+
date_unix: value => dayjs.unix(value),
38+
// ---------- [ 获取 ] ----------
39+
date_year: Day => P(Day).year(),
40+
date_month: Day => P(Day).month(),
41+
date_date: Day => P(Day).date(),
42+
date_day: Day => P(Day).day(),
43+
date_hour: Day => P(Day).hour(),
44+
date_minute: Day => P(Day).minute(),
45+
date_second: Day => P(Day).second(),
46+
date_millisecond: Day => P(Day).millisecond(),
47+
// ---------- [ 设置 ] ----------
48+
// date | day | month | year | hour | minute | second | millisecond
49+
// 对大小写不敏感
50+
date_set: (Day, unit, value) => P(Day).set(unit, value),
51+
// ---------- [ 操作 ] ----------
52+
// 增加
53+
date_add: (Day, value, unit) => P(Day).add(value, unit),
54+
// 减少
55+
date_subtract: (Day, value, unit) => P(Day).subtract(value, unit),
56+
// 开头时间
57+
date_startof: (Day, unit) => P(Day).startOf(unit),
58+
// 末尾时间
59+
date_endof: (Day, unit) => P(Day).endOf(unit),
60+
// ---------- [ 显示 ] ----------
61+
// 格式化
62+
date_format: (Day, setting = 'YYYY-MM-DD HH:mm:ss') => P(Day).format(setting),
63+
// 时间差
64+
date_diff: (Day, Day2 = '', unit = 'millisecond', accurate = false) => P(Day).diff(dayjs(Day2), unit, accurate),
65+
// Unix 时间戳 (毫秒)
66+
date_value_millisecond: Day => P(Day).valueOf(),
67+
// Unix 时间戳 (秒)
68+
date_value_second: Day => P(Day).unix(),
69+
// 月份的天数
70+
date_days_in_month: Day => P(Day).daysInMonth(),
71+
// Date 对象
72+
date_to_date: Day => P(Day).toDate(),
73+
// JSON
74+
date_to_json: Day => P(Day).toJSON(),
75+
// ISO8601 格式
76+
date_to_iso: Day => P(Day).toISOString(),
77+
// 字符
78+
date_to_string: Day => P(Day).toString(),
79+
// ---------- [ 查询 ] ----------
80+
// 是否之前
81+
date_is_before: (Day, Day2, unit = 'millisecond') => P(Day).isBefore(dayjs(Day2), unit),
82+
// 是否之后
83+
date_is_after: (Day, Day2, unit = 'millisecond') => P(Day).isAfter(dayjs(Day2), unit),
84+
// 是否相同
85+
date_is_same: (Day, Day2, unit = 'millisecond') => P(Day).isSame(dayjs(Day2), unit)
86+
}

packages/filters-date/src/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import filters from './filters'
2+
3+
const vueFiltersDate = {
4+
install: function (Vue, options) {
5+
Object.keys(filters).forEach(name => {
6+
Vue.filter(name, filters[name])
7+
})
8+
}
9+
}
10+
11+
if (typeof window !== 'undefined' && window.Vue) {
12+
window.Vue.use(vueFiltersDate)
13+
}
14+
15+
export default vueFiltersDate

src/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import VueUeditorWrap from 'vue-ueditor-wrap'
2727
import pluginExport from '@d2-projects/vue-table-export'
2828
import pluginImport from '@d2-projects/vue-table-import'
2929
// [ 可选过滤器 ] 日期相关过滤器
30-
import d2VueFiltersDate from '@d2-projects/vue-filters-date'
30+
import d2VueFiltersDayjs from '@d2-admin/filters-dayjs'
3131

3232
// 菜单和路由设置
3333
import router from './router'
@@ -44,7 +44,7 @@ Vue.use(contentmenu)
4444
Vue.use(vueJsonTreeView)
4545
Vue.use(pluginExport)
4646
Vue.use(pluginImport)
47-
Vue.use(d2VueFiltersDate)
47+
Vue.use(d2VueFiltersDayjs)
4848
Vue.component('d2-grid-layout', GridLayout)
4949
Vue.component('d2-grid-item', GridItem)
5050
Vue.component('SplitPane', SplitPane)

0 commit comments

Comments
 (0)