UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

199 lines (166 loc) 5.1 kB
"use strict"; exports.__esModule = true; exports.noUndefinedProps = noUndefinedProps; exports.isStringProp = isStringProp; exports.isObject = isObject; exports.now = now; exports.extend = extend; exports.flattenArray = flattenArray; exports.classNames = classNames; exports.getSlots = getSlots; exports.emit = emit; exports.getExtraAttrs = getExtraAttrs; exports.unsetRouterIds = unsetRouterIds; exports.getRouterId = getRouterId; exports.getComponentId = getComponentId; function noUndefinedProps(obj) { var o = {}; Object.keys(obj).forEach(function (key) { if (typeof obj[key] !== 'undefined') o[key] = obj[key]; }); return o; } function isStringProp(val) { return typeof val === 'string' && val !== ''; } function isObject(o) { return typeof o === 'object' && o !== null && o.constructor && o.constructor === Object; } function now() { return Date.now(); } function extend() { var deep = true; var to; var from; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } if (typeof args[0] === 'boolean') { deep = args[0]; to = args[1]; args.splice(0, 2); from = args; } else { to = args[0]; args.splice(0, 1); from = args; } for (var i = 0; i < from.length; i += 1) { var nextSource = args[i]; if (nextSource !== undefined && nextSource !== null) { var keysArray = Object.keys(Object(nextSource)); for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex += 1) { var nextKey = keysArray[nextIndex]; var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); if (desc !== undefined && desc.enumerable) { if (!deep) { to[nextKey] = nextSource[nextKey]; } else if (isObject(to[nextKey]) && isObject(nextSource[nextKey])) { extend(to[nextKey], nextSource[nextKey]); } else if (!isObject(to[nextKey]) && isObject(nextSource[nextKey])) { to[nextKey] = {}; extend(to[nextKey], nextSource[nextKey]); } else { to[nextKey] = nextSource[nextKey]; } } } } } return to; } function flattenArray() { var arr = []; for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } args.forEach(function (arg) { if (Array.isArray(arg)) arr.push.apply(arr, flattenArray.apply(void 0, arg));else arr.push(arg); }); return arr; } function classNames() { var classes = []; for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { args[_key3] = arguments[_key3]; } args.forEach(function (arg) { if (typeof arg === 'object' && arg.constructor === Object) { Object.keys(arg).forEach(function (key) { if (arg[key]) classes.push(key); }); } else if (arg) classes.push(arg); }); var uniqueClasses = []; classes.forEach(function (c) { if (uniqueClasses.indexOf(c) < 0) uniqueClasses.push(c); }); return uniqueClasses.join(' '); } function getSlots(props) { if (props === void 0) { props = {}; } var slots = {}; if (!props) return slots; var children = props.children; if (!children || children.length === 0) { return slots; } function addChildToSlot(name, child) { if (!slots[name]) slots[name] = []; slots[name].push(child); } if (Array.isArray(children)) { children.forEach(function (child) { if (!child) return; var slotName = child.props && child.props.slot || 'default'; addChildToSlot(slotName, child); }); } else { var slotName = 'default'; if (children.props && children.props.slot) slotName = children.props.slot; addChildToSlot(slotName, children); } return slots; } function emit(props, events) { for (var _len4 = arguments.length, args = new Array(_len4 > 2 ? _len4 - 2 : 0), _key4 = 2; _key4 < _len4; _key4++) { args[_key4 - 2] = arguments[_key4]; } if (!events || !events.trim().length || typeof events !== 'string') return; events.trim().split(' ').forEach(function (event) { var eventName = (event || '').trim(); if (!eventName) return; eventName = eventName.charAt(0).toUpperCase() + eventName.slice(1); var propName = "on" + eventName; if (props[propName]) props[propName].apply(props, args); }); } function getExtraAttrs(props) { if (props === void 0) { props = {}; } var extraAttrs = {}; Object.keys(props).forEach(function (key) { if (key.indexOf('data-') === 0 || key.indexOf('aria-') === 0 || key === 'role') { extraAttrs[key] = props[key]; } }); return extraAttrs; } var routerIdCounter = 0; var routerComponentIdCounter = 0; function unsetRouterIds() { routerIdCounter = 0; routerComponentIdCounter = 0; } function getRouterId() { routerIdCounter += 1; return now() + "_" + routerIdCounter; } function getComponentId() { routerComponentIdCounter += 1; return now() + "_" + routerComponentIdCounter; }