UNPKG

rc-tween-one

Version:
108 lines (85 loc) 2.19 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.findChildInChildrenByKey = findChildInChildrenByKey; exports.getChildrenFromProps = getChildrenFromProps; exports.mergeChildren = mergeChildren; exports.toArrayChildren = toArrayChildren; exports.transformArguments = transformArguments; var _react = _interopRequireDefault(require("react")); function toArrayChildren(children) { var ret = []; _react.default.Children.forEach(children, function (c) { ret.push(c); }); return ret; } function findChildInChildrenByKey(children, key) { var ret = null; if (children) { children.forEach(function (c) { if (ret || !c) { return; } if (c.key === key) { ret = c; } }); } return ret; } function mergeChildren(prev, next) { var ret = []; // For each key of `next`, the list of keys to insert before that key in // the combined list var nextChildrenPending = {}; var pendingChildren = []; var followChildrenKey = null; prev.forEach(function (c) { if (!c) { return; } if (c.key && findChildInChildrenByKey(next, c.key)) { if (pendingChildren.length) { nextChildrenPending[c.key] = pendingChildren; pendingChildren = []; } followChildrenKey = c.key; } else if (c.key) { pendingChildren.push(c); } }); if (!followChildrenKey) { ret = ret.concat(pendingChildren); } next.forEach(function (c) { if (!c) { return; } if (c.key && nextChildrenPending.hasOwnProperty(c.key)) { // eslint-disable-line no-prototype-builtins ret = ret.concat(nextChildrenPending[c.key]); } ret.push(c); if (c.key === followChildrenKey) { ret = ret.concat(pendingChildren); } }); return ret; } function transformArguments(arg, key, i) { var result; if (typeof arg === 'function') { result = arg({ key: key, index: i }); } else { result = arg; } return result; } function getChildrenFromProps(props) { return props && props.children; }