lucid-ui
Version:
A UI component library from AppNexus.
86 lines • 2.71 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.propsSearch = exports.getCombinedChildText = exports.partitionText = void 0;
var lodash_1 = __importDefault(require("lodash"));
var react_1 = __importDefault(require("react"));
/**
* Performs a regex search and returns a partitioning around the matching substring: [pre, match, post]
*
* @param text: string to search
* @param pattern: RegExp patten
* @param length (optional): provide a max length for the matching substring
*
* @return string[]
*/
function partitionText(text, pattern, length) {
var index;
if (length) {
index = text.search(pattern);
}
else {
var result = pattern.exec(text);
if (result) {
length = result[0].length;
index = result.index;
}
else {
length = 0;
index = -1;
}
}
if (index === -1) {
return ['', '', text];
}
else if (index === 0) {
return ['', text.substr(0, length), text.substring(length)];
}
else {
return [
text.substring(0, index),
text.substr(index, length),
text.substring(index + length),
];
}
}
exports.partitionText = partitionText;
/**
* Returns the combined text of all descendant strings
*
* @param node: a component props object
*
* @return string
*/
function getCombinedChildText(node) {
if (!node || !node.children) {
return '';
}
if (lodash_1.default.isString(node.children)) {
return node.children;
}
return react_1.default.Children.toArray(node.children)
.filter(function (child) { return lodash_1.default.has(child, 'props'); }) // filter out primitive types
.map(function (child) { return getCombinedChildText(child.props); })
.reduce(function (combinedText, childText) { return combinedText + childText; }, lodash_1.default.find(react_1.default.Children.toArray(node.children), lodash_1.default.isString) || '');
}
exports.getCombinedChildText = getCombinedChildText;
/**
* Perform a regex search on all text found in a component's descendants
*
* @param text
* @param node
*
* @return boolean
*/
function propsSearch(text, node) {
if (text === void 0) { text = null; }
if (node === void 0) { node = null; }
if (!text) {
return true;
}
return new RegExp(lodash_1.default.escapeRegExp(text), 'i').test(getCombinedChildText(node));
}
exports.propsSearch = propsSearch;
//# sourceMappingURL=text-manipulation.js.map