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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 | 1×
1×
1×
1×
1×
2×
26×
26×
26×
52×
36×
1×
489×
489×
489×
49×
440×
402×
1×
27×
1×
1042×
1×
1×
1×
10×
1×
10×
10×
14×
14×
14×
9×
5×
5×
5×
5×
3×
2×
1×
249×
205×
44×
2×
44×
12×
44×
14×
10×
4×
30×
1×
18×
4×
1×
17×
17×
17×
563×
563×
1×
1×
1×
603×
603×
603×
664×
664×
664×
603×
1×
666×
1×
710×
| import format from './format'
import Validators from './index'
import MESSAGES from './messages'
export var DEFAULT_OPTIONS = {
allowBlank: false,
urlProtocols: ['http', 'https'],
dateFormat: 'yyyy-mm-dd', // ISO
dateYmd: 'ymd',
accept: ['1', 'true'],
caseSensitive: true // confirmation, inclusion, exclusion
};
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={}) {
value = null == value ? '' : '' + value
if ((null != allowBlank ? allowBlank : Validators.defaultOptions.allowBlank) && !value.trim()) {
return
}
if (('function' !== typeof ifCond || ifCond(allValues, value)) &&
('function' !== typeof unlessCond || !unlessCond(allValues, value))) {
return func(value, allValues)
}
}
}
export function trunc (num) {
/* istanbul ignore next */
return Math.trunc ? Math.trunc(num) : num < 0 ? Math.ceil(num) : Math.floor(num)
}
export function isNumber (num) {
return !isNaN(num) && '' !== ('' + num).trim()
}
let pluralRules = {
0: /zero\s*\{(.*?)\}/,
1: /one\s*\{(.*?)\}/,
other: /other\s*\{(.*?)\}/
}
let TEMPLATE_REG = /\{([^{}]*\{[^{}]*\}[^{}]*)+\}|\{(.*?)\}/g
export function formatMsg (msg) {
if (msg.props) {
msg = msg.props
}
let text = msg.defaultMessage || msg.id || '';
return !msg.values ? text : text.replace(TEMPLATE_REG, function(content) {
let parts = content.slice(1, -1).split(',')
let count = msg.values[parts[0]]
if (parts.length <= 2) {
return null == count ? '' : count
}
let plural = parts[2].trim()
let rules = pluralRules[+count]
let result
if (rules && (result = plural.match(rules))) {
return result[1]
}
return (plural.match(pluralRules.other) || [])[1] || ''
})
}
export function prepareMsg (msg, type, values) {
if (null == msg) {
return Object.assign({}, MESSAGES[type], { values: values })
}
if (HAS_PROP.call(msg, 'props') && isReactElement(msg)) {
msg = msg.props
}
if (null != msg[type]) {
msg = msg[type]
}
if (isObject(msg)) {
if (HAS_PROP.call(msg, 'id') || HAS_PROP.call(msg, 'defaultMessage')) {
return Object.assign({}, msg, { values: values })
}
return Object.assign({}, MESSAGES[type], { values: values })
}
return { id: msg, defaultMessage: msg, values: values }
}
export function toObjectMsg (msg) {
if (null == msg) 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
const HAS_PROP = ({}).hasOwnProperty
const TO_STRING = ({}).toString
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 'object' === typeof obj && '[object Object]' === TO_STRING.call(obj) && null !== obj;
} |