Skip to content

Commit 2613a2d

Browse files
authored
Merge pull request #53 from brockpetrie/input-parsing
Input parsing
2 parents ef8b82a + 11161dc commit 2613a2d

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-moment",
3-
"version": "2.2.0",
3+
"version": "3.0.0",
44
"description": "Handy Moment.js filters for your Vue.js project",
55
"main": "vue-moment.js",
66
"scripts": {

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Moment.js expects your input to be either: a valid ISO 8601 formatted string (se
3737
<span>{{ [ someDate, ["MM.DD.YY", "MM-DD-YY", "MM-DD-YYYY"] ] | moment("dddd, MMMM Do YYYY") }}</span>
3838
```
3939

40+
As of 3.0.0, passing an empty or invalid input will no longer initiate moment with a new `Date` object.
41+
4042
## Filtering Methods
4143

4244
### format (default)

tests/vue-moment.spec.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Vue.use(VueMoment, {
66
moment,
77
})
88

9-
const now = moment()
9+
let now = moment()
1010
const tomorrow = moment().add(1, 'day')
1111

1212
const vm = new Vue({
@@ -117,4 +117,69 @@ describe('VueMoment', () => {
117117
})
118118
})
119119

120+
describe('handle inputs', () => {
121+
beforeEach(() => {
122+
global.console.warn = jest.fn()
123+
})
124+
125+
afterAll(() => {
126+
vm.now = moment()
127+
})
128+
129+
it('handles string', (done) => {
130+
vm.now = '2017-01-01'
131+
vm.args = ['YYYY-MM-DD']
132+
vm.$nextTick(() => {
133+
expect(console.warn).not.toBeCalled()
134+
expect(vm.$el.textContent).toContain('2017-01-01')
135+
done()
136+
})
137+
})
138+
139+
it('handles object', (done) => {
140+
vm.now = {y: 2017, m: 1, d: 1}
141+
vm.args = ['YYYY-MM-DD']
142+
vm.$nextTick(() => {
143+
expect(console.warn).not.toBeCalled()
144+
expect(vm.$el.textContent).toContain('2017-01-01')
145+
done()
146+
})
147+
})
148+
149+
it('handles Date object', (done) => {
150+
vm.now = new Date(2017, 0, 1);
151+
vm.args = ['YYYY-MM-DD']
152+
vm.$nextTick(() => {
153+
expect(console.warn).not.toBeCalled()
154+
expect(vm.$el.textContent).toContain('2017-01-01')
155+
done()
156+
})
157+
})
158+
159+
it('handles Moment object', (done) => {
160+
vm.now = moment('2017-01-01')
161+
vm.args = ['YYYY-MM-DD']
162+
vm.$nextTick(() => {
163+
expect(console.warn).not.toBeCalled()
164+
expect(vm.$el.textContent).toContain('2017-01-01')
165+
done()
166+
})
167+
})
168+
169+
it('handles undefined', (done) => {
170+
vm.now = undefined
171+
vm.$nextTick(() => {
172+
expect(console.warn).toBeCalled()
173+
done()
174+
})
175+
})
176+
177+
it('handles invalid string', (done) => {
178+
vm.now = 'foo'
179+
vm.$nextTick(() => {
180+
expect(console.warn).toBeCalled()
181+
done()
182+
})
183+
})
184+
})
120185
})

vue-moment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
date = moment(input);
3232
}
3333

34-
if (!date.isValid()) {
34+
if (!input || !date.isValid()) {
3535
// Log a warning if moment couldn't reconcile the input. Better than throwing an error?
3636
console.warn('Could not build a valid `moment` object from input.');
3737
return input;

0 commit comments

Comments
 (0)