Skip to content

Commit 4cedf43

Browse files
committed
feat(component): add component func $duration
1 parent bb90d41 commit 4cedf43

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

README.md

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212

1313
> A Vue.js 2.0 MomentJS library
1414
15-
> It just wired moment api to custom filters in Vue : moment() and duration().
16-
> And also inject moment as **this.$moment** in your components.
17-
> So you have complete access to the whole api of moment in your templates and also the documentation from [momentjs.com](https://momentjs.com/docs).
18-
> Just keep in mind that the left value of the filter is the first argument in any of the moment and duration functions.
19-
> You are good to go now :)
15+
> Make momentjs available in your template and Vue instance. Since it just try to map raw js function, api is same as[momentjs.com](https://momentjs.com/docs). Making it easier to use in your Vue.js projects.
16+
17+
> It added moment and duration as filters and component instance functions ($moment and $duration).
2018
2119
> Generated using [vue-cli-template-library](https://github.com/julon/vue-cli-template-library).
2220
@@ -60,16 +58,16 @@ Vue.use(VueMomentLib);
6058
<!-- Filter and moment are registered globally -->
6159
```
6260

63-
### After that, you can use the duration and moment filters in your templates:
61+
### After that, you can use the duration and moment filters in your templates, api is slightly different as the first argument is passed through pipe:
6462

6563
```html
64+
<!-- first argument of moment filter is a parameter for parsing to UTC, it is set by default to false so it is optional when you use default parsing -->
65+
6666
<!-- Local format -->
6767
<span>{{ Date.now() | moment().format("YYYY") }}</span>
6868

69-
<!-- first argument of moment filter is a parameter for parsing to UTC, it is set by default to false so it is optional when you use default parsing -->
70-
7169
<!-- isLocal + custom parsing + custom format -->
72-
<span>{{ Date.now() | moment(false, "12-25-1995", "MM-DD-YYYY").format("YYYY") }}</span>
70+
<span>{{ Date.now() | moment(false, "MM-DD-YYYY").format("YYYY") }}</span>
7371

7472
<!-- isUTC + custom format -->
7573
<span>{{ Date.now() | moment(true).format("YYYY") }}</span>
@@ -80,19 +78,39 @@ Vue.use(VueMomentLib);
8078
<!-- 1500 milliseconds -->
8179
<span>{{ 1500 | duration("milliseconds").milliseconds() }}</span>
8280
```
83-
Raw JS for moments `moment(Date.now()).format("YYYY")` becomes `Date.now() | moment().format("YYYY")`
8481

85-
Raw JS for duration `moment.duration(500).humanize()` becomes `500 | duration().humanize()`
82+
### And also, use the $duration and $moment component instance functions in your templates to really use the same apis as momentjs:
83+
84+
```html
85+
<!-- Local format -->
86+
<span>{{ $moment(Date.now()).format("YYYY") }}</span>
87+
88+
<!-- isLocal + custom parsing + custom format -->
89+
<span>{{ $moment(Date.now(), "12-25-1995", "MM-DD-YYYY").format("YYYY") }}</span>
90+
91+
<!-- isUTC + custom format -->
92+
<span>{{ $moment.utc(Date.now()).format("YYYY") }}</span>
93+
94+
<!-- Duration is supported -->
95+
<span>{{ $duration(500, "minutes").humanize() }}</span>
96+
97+
<!-- 1500 milliseconds -->
98+
<span>{{ $duration(1500, "milliseconds").milliseconds() }}</span>
99+
```
86100

101+
### or
87102
```js
88103
// in your components
89104
methods: {
90105
now () {
91106
return this.$moment(Date.now()).format()
107+
},
108+
humanize () {
109+
return this.$duration(500).humanize()
92110
}
93111
}
94112

95-
// it is also register as a global function in the Vue instance
113+
// it is also registered as a global function in the Vue instance
96114
// so you can do in vuex store or everywhere else to retrieve the same moment instance you initialized
97115
import Vue from 'vue'
98116
const thisYear = Vue.moment(Date.now()).format("YYYY")

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ const LibraryModule = {
1010
moment = options.moment;
1111
}
1212

13-
// make moment accessible in components with this.$moment
1413
/* eslint-disable */
14+
// make moment accessible in components with this.$moment
1515
Vue.prototype.$moment = moment;
16+
Vue.prototype.$duration = moment.duration;
1617

1718
// declare this moment instance as a global function for vuex, etc
18-
/* eslint-disable */
1919
Vue.moment = moment;
20+
/* eslint-enable */
2021

2122
// filter to wire moment
2223
Vue.filter("moment", (value, isUtc = false, ...args) => {

0 commit comments

Comments
 (0)