-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathhelpers.js
87 lines (65 loc) · 2.08 KB
/
helpers.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
76
77
78
79
80
81
82
83
84
85
86
87
/* eslint-disable no-unused-vars */
import { isValidElement } from 'react';
import Validators from '../validators';
const HAS_PROP = {}.hasOwnProperty;
export const TO_STRING = {}.toString;
const isObject = (obj) => typeof obj === 'object' && TO_STRING.call(obj) === '[object Object]' && obj !== null;
const stringify = (value) => {
if (value === null) {
return 'null';
}
if (typeof value === 'boolean') {
return value ? 'true' : 'false';
}
if (!value) {
return '';
}
if (typeof value === 'string') {
return value;
}
if (typeof value === 'number') {
return value.toString();
}
return JSON.stringify(value);
};
export const memoize = (func) => {
if (!func.cache) {
func.cache = {};
}
return (value, allValues, ...options) => {
const key = stringify(allValues);
return HAS_PROP.call(func.cache, key) ? func.cache[key] : (func.cache[key] = func(value, allValues, ...options));
};
};
const defaultMessage = (type, values) => {
let msg = Validators.messages[type];
return typeof msg === 'string' ? { defaultMessage: msg, values } : Object.assign({}, msg, { values });
};
export const prepareMsg = (msg, type, values) => {
if (msg == null) {
return defaultMessage(type, values);
}
if (HAS_PROP.call(msg, 'props') && isValidElement(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 });
}
return defaultMessage(type, values);
}
return { id: msg, defaultMessage: msg, values };
};
export const assign = Object.assign;
export const prepare =
(func) =>
(value, allValues, ...args) =>
func(value, allValues, ...args);
export const isNumber = (num) => !isNaN(num) && (num !== 0 || ('' + num).trim() !== '');
export function selectNum(var1, var2) {
return isNumber(var1) ? +var1 : arguments.length > 1 && isNumber(var2) ? +var2 : null;
}
export const trunc = (num) => (Math.trunc ? Math.trunc(num) : num < 0 ? Math.ceil(num) : Math.floor(num));