UNPKG

@enact/sandstone

Version:

Large-screen/TV support library for Enact, containing a variety of UI components.

79 lines (76 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.onlyUpdateForProps = exports.extractVoiceProps = exports.compareChildren = void 0; var _equals = _interopRequireDefault(require("ramda/src/equals")); var _react = require("react"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } /** * Removes voice control related props from `props` and returns them in a new object. * * @function * @param {Object} props Props object * * @returns {Object} voice control related props * @memberof sandstone/internal/util * @private */ var extractVoiceProps = exports.extractVoiceProps = function extractVoiceProps(props) { var obj = {}; Object.keys(props).forEach(function (key) { if (key.indexOf('data-webos-voice-') === 0) { obj[key] = props[key]; delete props[key]; } }); return obj; }; /** * Compares two children and returns true if they are equivalent, false otherwise. * * @function * @param {children} a children props * @param {children} b children props * * @returns {Boolean} `true` if same * @memberof sandstone/internal/util * @private */ var compareChildren = exports.compareChildren = function compareChildren(a, b) { if (!a || !b || a.length !== b.length) return false; var type = null; for (var i = 0; i < a.length; i++) { type = type || typeof a[i]; if (type === 'string') { if (a[i] !== b[i]) { return false; } } else if (!(0, _equals["default"])(a[i], b[i])) { return false; } } return true; }; /** * Updates component only when given props are not shallowly equivalent, not updating otherwise. * * @function * @param {any} wrapped A component * @param {Array} propKeys Prop keys to compare * * @returns {any} Conditionally memoized component * @memberof sandstone/internal/util * @private */ var onlyUpdateForProps = exports.onlyUpdateForProps = function onlyUpdateForProps(wrapped, propKeys) { return /*#__PURE__*/(0, _react.memo)(wrapped, function (prevProps, nextProps) { var hasOwn = Object.prototype.hasOwnProperty; if (Array.isArray(propKeys)) { return propKeys.every(function (key) { return hasOwn.call(prevProps, key) && hasOwn.call(nextProps, key) && Object.is(prevProps[key], nextProps[key]); }); } return false; }); };