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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 | 1×
1×
1×
1×
2×
26×
26×
26×
52×
36×
1×
792×
792×
499×
499×
49×
743×
702×
702×
1×
27×
1×
742×
1×
1207×
1×
387×
338×
49×
2×
49×
12×
49×
14×
10×
4×
35×
1×
18×
4×
1×
18×
18×
18×
870×
870×
1×
342×
342×
1×
910×
910×
910×
966×
966×
966×
910×
1×
968×
1×
1017×
| import Validators from './validators'
export const HAS_PROP = {}.hasOwnProperty
export const TO_STRING = {}.toString
export function regFormat (func, messageType) {
return memoize(function (options) {
options = options || {}
let msg = options.msg || options.message
return prepare(options['if'], options.unless, options.allowBlank, function (value) {
if (!value.match(func(options))) {
return Validators.formatMessage(prepareMsg(msg, messageType))
}
})
})
}
export function prepare (ifCond, unlessCond, allowBlank, func) {
return function (value, allValues = {}, ...args) {
if (!value || typeof value !== 'object') {
value = value == null ? '' : '' + value
if ((allowBlank != null ? allowBlank : Validators.defaultOptions.allowBlank) && !value.trim()) {
return
}
}
if (
(typeof ifCond !== 'function' || ifCond(allValues, value)) &&
(typeof unlessCond !== 'function' || !unlessCond(allValues, value))
) {
return func(value, allValues, ...args)
}
}
}
export function trunc (num) {
/* istanbul ignore next */
return Math.trunc ? Math.trunc(num) : num < 0 ? Math.ceil(num) : Math.floor(num)
}
export function selectNum (var1, var2) {
return isNumber(var1) ? +var1 : arguments.length > 1 && isNumber(var2) ? +var2 : null
}
export function isNumber (num) {
// eslint-disable-next-line
return !isNaN(num) && (0 != num || '' !== ('' + num).trim())
}
export function prepareMsg (msg, type, values) {
if (msg == null) {
return defaultMessage(type, values)
}
if (HAS_PROP.call(msg, 'props') && isReactElement(msg)) {
msg = msg.props
}
if (msg[type] != null) {
msg = msg[type]
}
if (isObject(msg)) {
if (HAS_PROP.call(msg, 'id') || HAS_PROP.call(msg, 'defaultMessage')) {
return Object.assign({}, msg, { values: values })
}
return defaultMessage(type, values)
}
return { id: msg, defaultMessage: msg, values: values }
}
export function toObjectMsg (msg) {
if (msg == null) return null
return isObject(msg) ? msg : { id: msg, defaultMessage: msg }
}
export function memoize (func) {
Eif (!func.cache) {
func.cache = {}
}
return function (options) {
let key = stringify(options)
return HAS_PROP.call(func.cache, key) ? func.cache[key] : (func.cache[key] = func(options))
}
}
// private
function defaultMessage (type, values) {
let msg = Validators.messages[type]
return typeof msg === 'string' ? { defaultMessage: msg, values: values } : Object.assign({}, msg, { values: values })
}
function stringify (options) {
let arr = []
let value
for (var k in options) {
Eif (HAS_PROP.call(options, k)) {
value = options[k]
arr.push(
k,
isReactElement(value) ? stringify(value.props) : isObject(value) ? stringify(value) : value.toString()
)
}
}
return JSON.stringify(arr)
}
function isReactElement (object) {
return typeof object === 'object' && object !== null && '$$typeof' in object
}
function isObject (obj) {
return typeof obj === 'object' && TO_STRING.call(obj) === '[object Object]' && obj !== null
}
|