Skip to content

Moment -> fecha #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": ["es2015", "stage-2"],
"plugins": ["transform-runtime"],
"plugins": ["transform-runtime", "lodash"],
"comments": false
}
13 changes: 3 additions & 10 deletions dist/vue-form-generator.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"devDependencies": {
"babel-core": "6.22.1",
"babel-loader": "6.2.10",
"babel-plugin-lodash": "3.2.11",
"babel-plugin-transform-runtime": "6.22.0",
"babel-preset-es2015": "6.22.0",
"babel-preset-stage-2": "6.22.0",
Expand All @@ -56,6 +57,7 @@
"eslint-plugin-vue": "1.0.0",
"extract-text-webpack-plugin": "1.0.1",
"fakerator": "0.3.0",
"fecha": "2.3.0",
"gitbook-cli": "2.3.0",
"inject-loader": "2.0.1",
"isparta-loader": "2.0.0",
Expand Down
14 changes: 8 additions & 6 deletions src/fields/fieldDateTimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<script>
/* global $ */
import abstractField from "./abstractField";
import moment from "moment/min/moment.min";
import fecha from "fecha";
import { defaults } from "lodash";

let inputFormat = "YYYY-MM-DD HH:mm:ss";
Expand All @@ -26,19 +26,21 @@
},

formatValueToField(value) {
if (value != null)
return moment(value, this.schema.format).format(this.getDateFormat());
if (value != null) {
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
return fecha.format(dt, this.getDateFormat());
}

return value;
},

formatValueToModel(value) {
if (value != null) {
let m = moment(value, this.getDateFormat());
let m = fecha.parse(value, this.getDateFormat());
if (this.schema.format)
value = m.format(this.schema.format);
value = fecha.format(m, this.schema.format);
else
value = m.toDate().valueOf();
value = m.valueOf();
}

return value;
Expand Down
22 changes: 12 additions & 10 deletions src/fields/fieldInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,23 @@

<script>
import abstractField from "./abstractField";
import moment from "moment/min/moment.min";
import fecha from "fecha";

export default {
mixins: [ abstractField ],
methods: {
formatValueToField(value) {
switch(this.schema.inputType){
case "date":
return moment(value).format("YYYY-MM-DD");
case "datetime":
return moment(value).format();
case "datetime-local":
return moment(value).format("YYYY-MM-DDTHH:mm:ss");
default:
return value;
if (value != null) {
switch(this.schema.inputType){
case "date":
return fecha.format(value, "YYYY-MM-DD");
case "datetime":
return fecha.format(value);
case "datetime-local":
return fecha.format(value, "YYYY-MM-DDTHH:mm:ss");
default:
return value;
}
}
},
formatValueToModel(value) {
Expand Down
15 changes: 9 additions & 6 deletions src/fields/fieldPikaday.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<script>
import abstractField from "./abstractField";
import moment from "moment/min/moment.min";
import fecha from "fecha";
import { defaults } from "lodash";

let inputFormat = "YYYY-MM-DD";
Expand All @@ -25,20 +25,23 @@
},

formatValueToField(value) {
if (value != null)
return moment(value, this.schema.format).format(this.getDateFormat());
if (value != null) {
let dt = this.schema.format ? fecha.parse(value, this.schema.format) : new Date(value);
return fecha.format(dt, this.getDateFormat());
}

return value;
},

formatValueToModel(value) {
if (value != null) {
let m = moment(value, this.getDateFormat());
let m = fecha.parse(value, this.getDateFormat());
if (this.schema.format)
value = m.format(this.schema.format);
value = fecha.format(m, this.schema.format);
else
value = m.toDate().valueOf();
value = m.valueOf();
}

return value;
}

Expand Down
18 changes: 9 additions & 9 deletions src/utils/validators.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNil, isNumber, isString, isArray } from "lodash";
import moment from "moment/min/moment.min";
import fecha from "fecha";

function checkEmpty(value, required) {
if (isNil(value) || value === "") {
Expand Down Expand Up @@ -130,22 +130,22 @@ module.exports = {
date(value, field) {
let res = checkEmpty(value, field.required); if (res != null) return res;

let m = moment(value);
if (!m.isValid())
let m = new Date(value);
if (!m)
return [msg(resources.invalidDate)];

let err = [];

if (!isNil(field.min)) {
let min = moment(field.min);
if (m.isBefore(min))
err.push(msg(resources.dateIsEarly, m.format("L"), min.format("L")));
let min = new Date(field.min);
if (m.valueOf() < min.valueOf())
err.push(msg(resources.dateIsEarly, fecha.format(m), fecha.format(min)));
}

if (!isNil(field.max)) {
let max = moment(field.max);
if (m.isAfter(max))
err.push(msg(resources.dateIsLate, m.format("L"), max.format("L")));
let max = new Date(field.max);
if (m.valueOf() > max.valueOf())
err.push(msg(resources.dateIsLate, fecha.format(m), fecha.format(max)));
}

return err;
Expand Down
Loading