UNPKG

baseui

Version:

A React Component library implementing the Base design language

37 lines (32 loc) 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTextFromChildren = getTextFromChildren; var React = _interopRequireWildcard(require("react")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /* Copyright (c) Uber Technologies, Inc. This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree. */ /* eslint-disable import/prefer-default-export */ const JOINABLE_TYPES = new Set(['string', 'number']); // Checks the children of a React component to ensure every value is a number // or a string. If they are, they are joined and returned. Useful for collecting // text from the child of a node to use as an attribute. function getTextFromChildren(children) { const childList = React.Children.toArray(children).filter(child => child !== null && child !== undefined); if (!childList.length) { return null; } const isJoinable = childList.every(child => JOINABLE_TYPES.has(typeof child)); if (!isJoinable) { return null; } // Join on an empty string to preserve React's whitespace handling: // <Tag>foo{'bar'}baz</Tag> => 'foobar' // <Tag>foo {'bar'} baz</Tag> => 'foo bar baz' return childList.join(''); }