Skip to content

Commit 8742ad2

Browse files
authored
Merge pull request #10 from brockpetrie/develop
Allow target date pattern in input, no longer strip argument quotes
2 parents c82aeca + fffbdf6 commit 8742ad2

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-moment",
3-
"version": "1.0.8",
3+
"version": "2.0.0",
44
"description": "Handy Moment.js filters for your Vue.js project",
55
"main": "vue-moment.js",
66
"scripts": {
@@ -16,6 +16,7 @@
1616
"date",
1717
"time",
1818
"filter",
19+
"moment",
1920
"moment.js"
2021
],
2122
"author": "brockpetrie",
@@ -26,5 +27,8 @@
2627
"homepage": "https://github.com/brockpetrie/vue-moment#readme",
2728
"dependencies": {
2829
"moment": "^2.11.1"
30+
},
31+
"peerDependencies": {
32+
"vue": "^1.x.x"
2933
}
3034
}

readme.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ Simply set `moment` as the filtering function and you're good to go. At least on
1818

1919
```html
2020
<span>{{ someDate | moment "dddd, MMMM Do YYYY" }}</span>
21+
<!-- or create a new date from 'now' -->
22+
<span>{{ new Date() | moment "dddd, MMMM Do YYYY" }}</span>
2123
```
2224

25+
## Passing Your Date
26+
27+
Moment.js expects your input to be either: a valid ISO 8601 formatted string (see <http://momentjs.com/docs/#/parsing/string/>), a valid `Date` object, or a date string with an accompanying format pattern (i.e. when you know the format of the date input). For the latter, `vue-moment` allows you to pass your date and format pattern(s) as an array, like such:
28+
29+
```html
30+
<span>{{ [ someDate, "MM.DD.YY" ] | moment "dddd, MMMM Do YYYY" }}</span>
31+
<!-- or when you want to parse against more than one pattern -->
32+
<span>{{ [ someDate, ["MM.DD.YY", "MM-DD-YY", "MM-DD-YYYY"] ] | moment "dddd, MMMM Do YYYY" }}</span>
33+
```
2334

2435
## Filtering Methods
2536

@@ -62,11 +73,11 @@ Display a moment in relative time, either from now or from a specified date.
6273
**With suffix hidden** (e.g. '4 days ago' -> '4 days')
6374

6475
```html
65-
<span>{{ someDate | moment "from" "now" "true" }}</span>
76+
<span>{{ someDate | moment "from" "now" true }}</span>
6677
<!-- or -->
67-
<span>{{ someDate | moment "from" "true" }}</span>
78+
<span>{{ someDate | moment "from" true }}</span>
6879
<!-- or with a reference time -->
69-
<span>{{ someDate | moment "from" "Jan. 11th, 2000" "true" }}</span>
80+
<span>{{ someDate | moment "from" "Jan. 11th, 2000" true }}</span>
7081
```
7182

7283
For more information about `moment#fromNow` and `moment#from`, check out <http://momentjs.com/docs/#/displaying/fromnow/> and <http://momentjs.com/docs/#/displaying/from/>.

vue-moment.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,28 @@ module.exports = {
2020

2121
Vue.filter('moment', function() {
2222
var args = Array.prototype.slice.call(arguments),
23-
value = args.shift(),
24-
date = moment(value);
23+
input = args.shift(),
24+
date;
25+
26+
if (Array.isArray(input) && typeof input[0] === 'string') {
27+
// If input is array, assume we're being passed a format pattern to parse against.
28+
// Format pattern will accept an array of potential formats to parse against.
29+
// Date string should be at [0], format pattern(s) should be at [1]
30+
date = moment(string = input[0], formats = input[1], true);
31+
} else {
32+
// Otherwise, throw the input at moment and see what happens...
33+
date = moment(input);
34+
}
2535

26-
if (!date.isValid()) return '';
36+
if (!date.isValid()) {
37+
// Log a warning if moment couldn't reconcile the input. Better than throwing an error?
38+
console.warn('Could not build a valid `moment` object from input.');
39+
return input;
40+
}
2741

2842
function parse() {
29-
var args = Array.prototype.slice.call(arguments).map(function(str) { return str.replace(/^("|')|("|')$/g, ''); }),
30-
method = args.shift();
43+
var args = Array.prototype.slice.call(arguments);
44+
method = args.shift();
3145

3246
switch (method) {
3347
case 'add':
@@ -76,7 +90,7 @@ module.exports = {
7690
}
7791

7892
var removeSuffix = false;
79-
if (args[0] == 'true') {
93+
if (args[0] === true) {
8094
args.shift();
8195
var removeSuffix = true;
8296
}

vue-moment.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)