redux-form-validators
Version:
Simple validations with redux-form / react-final-form
210 lines (168 loc) • 5.87 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.stringToReg = stringToReg;
exports.regFormat = regFormat;
exports.prepare = prepare;
exports.selectNum = selectNum;
exports.isNumber = isNumber;
exports.isObject = isObject;
exports.getIn = getIn;
exports.prepareMsg = prepareMsg;
exports.toObjectMsg = toObjectMsg;
exports.memoize = memoize;
exports.assign = exports.trunc = exports.TO_STRING = exports.HAS_PROP = void 0;
var _validators = _interopRequireDefault(require("./validators"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
var HAS_PROP = {}.hasOwnProperty;
exports.HAS_PROP = HAS_PROP;
var TO_STRING = {}.toString; // eslint-disable-next-line no-useless-escape
exports.TO_STRING = TO_STRING;
var ESCAPE_REG = /([.+?^=!:${}()|[\]\/\\])/g; // Removed star char
var ANY_REG = /\*/g; // string with "*" => RegExp
function stringToReg(str) {
return new RegExp('^' + str.replace(ESCAPE_REG, '\\$1').replace(ANY_REG, '.*') + '$', 'i');
}
function regFormat(func, messageType) {
return memoize(function (options) {
options = options || {};
var msg = options.msg || options.message;
return prepare(options['if'], options.unless, options.allowBlank, function (value) {
if (!value.match(func(options))) {
return _validators["default"].formatMessage(prepareMsg(msg, messageType));
}
});
});
}
function prepare(ifCond, unlessCond, allowBlank, func) {
return function (value) {
var allValues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!value || _typeof(value) !== 'object') {
value = value == null ? '' : '' + value;
if ((allowBlank != null ? allowBlank : _validators["default"].defaultOptions.allowBlank) && !value.trim()) {
return;
}
}
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
args[_key - 2] = arguments[_key];
}
if ((typeof ifCond !== 'function' || ifCond.apply(void 0, [allValues, value].concat(args))) && (typeof unlessCond !== 'function' || !unlessCond.apply(void 0, [allValues, value].concat(args)))) {
return func.apply(void 0, [value, allValues].concat(args));
}
};
}
var trunc = Math.trunc ||
/* istanbul ignore next */
function trunc(num) {
return num < 0 ? Math.ceil(num) : Math.floor(num);
};
exports.trunc = trunc;
var assign = Object.assign ||
/* istanbul ignore next */
function (obj) {
for (var i = 1, len = arguments.length; i < len; ++i) {
var source = arguments[i];
if (source != null) for (var key in source) {
if (HAS_PROP.call(source, key)) obj[key] = source[key];
}
}
return obj;
};
exports.assign = assign;
function selectNum(var1, var2) {
return isNumber(var1) ? +var1 : arguments.length > 1 && isNumber(var2) ? +var2 : null;
}
function isNumber(num) {
// eslint-disable-next-line
return !isNaN(num) && (0 != num || '' !== ('' + num).trim());
}
function isObject(obj) {
return _typeof(obj) === 'object' && TO_STRING.call(obj) === '[object Object]' && obj !== null;
} // Immutable.js compatibility
function getIn(h, keys) {
/* istanbul ignore next */
if (typeof h.getIn === 'function') return h.getIn(keys);
for (var i = 0, len = keys.length; i < len; ++i) {
h = (h ||
/* istanbul ignore next */
{})[keys[i]];
}
return h;
}
function prepareMsg(msg, type) {
var args = arguments;
var lastIndex = args.length - 1;
var values = args[lastIndex];
if (typeof values === 'string') {
values = void 0;
++lastIndex;
}
if (msg == null) {
return defaultMessage(type, values);
}
if (HAS_PROP.call(msg, 'props') && isReactElement(msg)) {
msg = msg.props;
}
for (var i = lastIndex - 1; i >= 1; --i) {
if (msg[args[i]] != null) {
msg = msg[args[i]];
break;
}
}
if (isObject(msg)) {
if (HAS_PROP.call(msg, 'id') || HAS_PROP.call(msg, 'defaultMessage')) {
return assign({}, msg, {
values: values
});
}
return defaultMessage(type, values);
}
return {
id: msg,
defaultMessage: msg,
values: values
};
}
function toObjectMsg(msg) {
if (msg == null) return null;
return isObject(msg) ? msg : {
id: msg,
defaultMessage: msg
};
}
function memoize(func) {
func.cache = {};
return function (options) {
var memoize = options ? options.memoize : null;
if (memoize == null) memoize = _validators["default"].defaultOptions.memoize;
if (memoize != null && !memoize) return func(options);
var key = typeof memoize === 'function' ? memoize(options, stringify) : stringify(options);
return HAS_PROP.call(func.cache, key) ? func.cache[key] : func.cache[key] = func(options);
};
} // private
function defaultMessage(type, values) {
var msg = _validators["default"].messages[type];
return typeof msg === 'string' ? {
defaultMessage: msg,
values: values
} : assign({}, msg, {
values: values
});
}
function stringify(options) {
var arr = [];
var value;
for (var k in options) {
/* istanbul ignore else */
if (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;
}
;