All files / util text-manipulation.js

100% Statements 17/17
100% Branches 12/12
100% Functions 6/6
100% Lines 17/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44      122x   122x 10x 112x 111x   1x                 13x 1x     12x 10x     2x 4x 4x   4x           125x 119x     6x    
import _ from 'lodash';
 
export function partitionText(text, pattern, length) {
	const index = text.search(pattern);
 
	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),
		];
	}
}
 
export function getCombinedChildText(node) {
	if (!node.children) {
		return '';
	}
 
	if (_.isString(node.children)) {
		return node.children;
	}
 
	return node.children
		.filter(child => !_.isString(child))
		.map(child => getCombinedChildText(child.props))
		.reduce(
			(combinedText, childText) => combinedText + childText,
			_.find(node.children, _.isString) || ''
		);
}
 
export function propsSearch(text, node) {
	if (!text) {
		return true;
	}
 
	return new RegExp(_.escapeRegExp(text), 'i').test(getCombinedChildText(node));
}