forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoment-util.js
75 lines (70 loc) · 2.23 KB
/
moment-util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import interopDefault from './interopDefault';
import moment from 'moment';
import warning from './warning';
import isNil from 'lodash-es/isNil';
export const TimeType = {
validator(value) {
return typeof value === 'string' || isNil(value) || moment.isMoment(value);
},
};
export const TimesType = {
validator(value) {
if (Array.isArray(value)) {
return (
value.length === 0 ||
value.findIndex(val => typeof val !== 'string') === -1 ||
value.findIndex(val => !isNil(val) && !moment.isMoment(val)) === -1
);
}
return false;
},
};
export const TimeOrTimesType = {
validator(value) {
if (Array.isArray(value)) {
return (
value.length === 0 ||
value.findIndex(val => typeof val !== 'string') === -1 ||
value.findIndex(val => !isNil(val) && !moment.isMoment(val)) === -1
);
} else {
return typeof value === 'string' || isNil(value) || moment.isMoment(value);
}
},
};
export function checkValidate(componentName, value, propName, valueFormat) {
const values = Array.isArray(value) ? value : [value];
values.forEach(val => {
if (!val) return;
valueFormat &&
warning(
interopDefault(moment)(val, valueFormat).isValid(),
componentName,
`When set \`valueFormat\`, \`${propName}\` should provides invalidate string time. `,
);
!valueFormat &&
warning(
interopDefault(moment).isMoment(val) && val.isValid(),
componentName,
`\`${propName}\` provides invalidate moment time. If you want to set empty value, use \`null\` instead.`,
);
});
}
export const stringToMoment = (value, valueFormat) => {
if (Array.isArray(value)) {
return value.map(val =>
typeof val === 'string' && val ? interopDefault(moment)(val, valueFormat) : val || null,
);
} else {
return typeof value === 'string' && value
? interopDefault(moment)(value, valueFormat)
: value || null;
}
};
export const momentToString = (value, valueFormat) => {
if (Array.isArray(value)) {
return value.map(val => (interopDefault(moment).isMoment(val) ? val.format(valueFormat) : val));
} else {
return interopDefault(moment).isMoment(value) ? value.format(valueFormat) : value;
}
};