Skip to content

Commit 3e05e14

Browse files
authored
Merge pull request #117 from icebob/moment-fecha
Moment -> fecha
2 parents a69e244 + 9f707dc commit 3e05e14

8 files changed

+95
-186
lines changed

.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"presets": ["es2015", "stage-2"],
3-
"plugins": ["transform-runtime"],
3+
"plugins": ["transform-runtime", "lodash"],
44
"comments": false
55
}

dist/vue-form-generator.js

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

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"devDependencies": {
4242
"babel-core": "6.22.1",
4343
"babel-loader": "6.2.10",
44+
"babel-plugin-lodash": "3.2.11",
4445
"babel-plugin-transform-runtime": "6.22.0",
4546
"babel-preset-es2015": "6.22.0",
4647
"babel-preset-stage-2": "6.22.0",
@@ -56,6 +57,7 @@
5657
"eslint-plugin-vue": "1.0.0",
5758
"extract-text-webpack-plugin": "1.0.1",
5859
"fakerator": "0.3.0",
60+
"fecha": "2.3.0",
5961
"gitbook-cli": "2.3.0",
6062
"inject-loader": "2.0.1",
6163
"isparta-loader": "2.0.0",

src/fields/fieldDateTimePicker.vue

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<script>
99
/* global $ */
1010
import abstractField from "./abstractField";
11-
import moment from "moment/min/moment.min";
11+
import fecha from "fecha";
1212
import { defaults } from "lodash";
1313
1414
let inputFormat = "YYYY-MM-DD HH:mm:ss";
@@ -26,19 +26,21 @@
2626
},
2727
2828
formatValueToField(value) {
29-
if (value != null)
30-
return moment(value, this.schema.format).format(this.getDateFormat());
29+
if (value != null) {
30+
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
31+
return fecha.format(dt, this.getDateFormat());
32+
}
3133
3234
return value;
3335
},
3436
3537
formatValueToModel(value) {
3638
if (value != null) {
37-
let m = moment(value, this.getDateFormat());
39+
let m = fecha.parse(value, this.getDateFormat());
3840
if (this.schema.format)
39-
value = m.format(this.schema.format);
41+
value = fecha.format(m, this.schema.format);
4042
else
41-
value = m.toDate().valueOf();
43+
value = m.valueOf();
4244
}
4345
4446
return value;

src/fields/fieldInput.vue

+12-10
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,23 @@
3737

3838
<script>
3939
import abstractField from "./abstractField";
40-
import moment from "moment/min/moment.min";
40+
import fecha from "fecha";
4141
4242
export default {
4343
mixins: [ abstractField ],
4444
methods: {
4545
formatValueToField(value) {
46-
switch(this.schema.inputType){
47-
case "date":
48-
return moment(value).format("YYYY-MM-DD");
49-
case "datetime":
50-
return moment(value).format();
51-
case "datetime-local":
52-
return moment(value).format("YYYY-MM-DDTHH:mm:ss");
53-
default:
54-
return value;
46+
if (value != null) {
47+
switch(this.schema.inputType){
48+
case "date":
49+
return fecha.format(value, "YYYY-MM-DD");
50+
case "datetime":
51+
return fecha.format(value);
52+
case "datetime-local":
53+
return fecha.format(value, "YYYY-MM-DDTHH:mm:ss");
54+
default:
55+
return value;
56+
}
5557
}
5658
},
5759
formatValueToModel(value) {

src/fields/fieldPikaday.vue

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<script>
66
import abstractField from "./abstractField";
7-
import moment from "moment/min/moment.min";
7+
import fecha from "fecha";
88
import { defaults } from "lodash";
99
1010
let inputFormat = "YYYY-MM-DD";
@@ -25,20 +25,23 @@
2525
},
2626
2727
formatValueToField(value) {
28-
if (value != null)
29-
return moment(value, this.schema.format).format(this.getDateFormat());
28+
if (value != null) {
29+
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
30+
return fecha.format(dt, this.getDateFormat());
31+
}
3032
3133
return value;
3234
},
3335
3436
formatValueToModel(value) {
3537
if (value != null) {
36-
let m = moment(value, this.getDateFormat());
38+
let m = fecha.parse(value, this.getDateFormat());
3739
if (this.schema.format)
38-
value = m.format(this.schema.format);
40+
value = fecha.format(m, this.schema.format);
3941
else
40-
value = m.toDate().valueOf();
42+
value = m.valueOf();
4143
}
44+
4245
return value;
4346
}
4447

src/utils/validators.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isNil, isNumber, isString, isArray } from "lodash";
2-
import moment from "moment/min/moment.min";
2+
import fecha from "fecha";
33

44
function checkEmpty(value, required) {
55
if (isNil(value) || value === "") {
@@ -130,22 +130,22 @@ module.exports = {
130130
date(value, field) {
131131
let res = checkEmpty(value, field.required); if (res != null) return res;
132132

133-
let m = moment(value);
134-
if (!m.isValid())
133+
let m = new Date(value);
134+
if (!m)
135135
return [msg(resources.invalidDate)];
136136

137137
let err = [];
138138

139139
if (!isNil(field.min)) {
140-
let min = moment(field.min);
141-
if (m.isBefore(min))
142-
err.push(msg(resources.dateIsEarly, m.format("L"), min.format("L")));
140+
let min = new Date(field.min);
141+
if (m.valueOf() < min.valueOf())
142+
err.push(msg(resources.dateIsEarly, fecha.format(m), fecha.format(min)));
143143
}
144144

145145
if (!isNil(field.max)) {
146-
let max = moment(field.max);
147-
if (m.isAfter(max))
148-
err.push(msg(resources.dateIsLate, m.format("L"), max.format("L")));
146+
let max = new Date(field.max);
147+
if (m.valueOf() > max.valueOf())
148+
err.push(msg(resources.dateIsLate, fecha.format(m), fecha.format(max)));
149149
}
150150

151151
return err;

0 commit comments

Comments
 (0)