UNPKG

@data-driven-forms/react-form-renderer

Version:

React Form Renderer. Data Driven Forms converts JSON form definitions into fully functional React forms.

82 lines 3.32 kB
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; /* eslint-disable no-unused-vars */ import { isValidElement } from 'react'; import Validators from '../validators'; var HAS_PROP = {}.hasOwnProperty; export var TO_STRING = {}.toString; var isObject = function (obj) { return typeof obj === 'object' && TO_STRING.call(obj) === '[object Object]' && obj !== null; }; var stringify = function (args) { var arr = []; var value; var options = args; if (typeof options === 'number') { options = options.toString(); } for (var k in options) { if (HAS_PROP.call(options, k)) { value = options[k]; arr.push(k, isValidElement(value) ? stringify(value.props) : isObject(value) ? stringify(value) : value != null ? value.toString() : 'undefined'); } } return JSON.stringify(arr); }; export var memoize = function (func) { if (!func.cache) { func.cache = {}; } return function (value, allValues) { var options = []; for (var _i = 2; _i < arguments.length; _i++) { options[_i - 2] = arguments[_i]; } var key = stringify(value); return HAS_PROP.call(func.cache, key) ? func.cache[key] : (func.cache[key] = func.apply(void 0, __spreadArray([value, allValues], options, false))); }; }; var defaultMessage = function (type, values) { var msg = Validators.messages[type]; return typeof msg === 'string' ? { defaultMessage: msg, values: values } : Object.assign({}, msg, { values: values }); }; export var prepareMsg = function (msg, type, values) { if (msg == null) { return defaultMessage(type, values); } var processedMsg = msg; if (HAS_PROP.call(processedMsg, 'props') && isValidElement(processedMsg)) { processedMsg = processedMsg.props; } if (processedMsg[type] != null) { processedMsg = processedMsg[type]; } if (isObject(processedMsg)) { if (HAS_PROP.call(processedMsg, 'id') || HAS_PROP.call(processedMsg, 'defaultMessage')) { return Object.assign({}, processedMsg, { values: values }); } return defaultMessage(type, values); } return { id: processedMsg, defaultMessage: processedMsg, values: values }; }; export var assign = Object.assign; export var prepare = function (func) { return function (value, allValues) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } return func.apply(void 0, __spreadArray([value, allValues], args, false)); }; }; export var isNumber = function (num) { return !isNaN(num) && (num !== 0 || ('' + num).trim() !== ''); }; export function selectNum(var1, var2) { return isNumber(var1) ? +var1 : arguments.length > 1 && isNumber(var2) ? +var2 : null; } export var trunc = function (num) { return (Math.trunc ? Math.trunc(num) : num < 0 ? Math.ceil(num) : Math.floor(num)); }; //# sourceMappingURL=helpers.js.map