@bigfishtv/cockpit
Version:
153 lines (135 loc) • 4.44 kB
JavaScript
/**
* String Utilities
* @module Utilities/stringUtils
*/
import toTitleCase from './titleCase';
import { isArray, isNumeric } from './typeUtils';
/**
* Searches for string in another string, breaks search string up by delimiter. Used for keywordsearching
* @param {String} string - string to search
* @param {Array} search - query string
* @param {Boolean} caseSensitive
* @param {RegExp} delimiter
* @return {Boolean}
*/
export function stringContains(string) {
var search = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var caseSensitive = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var delimiter = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : /[\s,]+/;
if (typeof string != 'string') return string;
if (!caseSensitive) {
search = typeof search == 'string' ? search.toLowerCase() : search.map(function (term) {
return term.toLowerCase();
});
string = string.toLowerCase();
}
var terms = (typeof search == 'string' ? [search] : search.split(delimiter)).filter(function (item, pos, self) {
return self.indexOf(item) == pos;
});
for (var _iterator = terms, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var term = _ref;
if (string.indexOf(term) > -1) return true;
}
return false;
}
/**
* Capitalizes first letter of string
* @param {String} string
* @return {String}
*/
export function capitalizeFirstLetter(string) {
if (typeof string != 'string') return string;
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Escapes regex characters in string
* @param {String} string
* @return {String}
*/
export function escapeRegexCharacters(string) {
if (typeof string != 'string') return string;
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/**
* Converts string To Title Case
* @param {String} string
* @return {String}
*/
export function titleCase(string) {
if (typeof string != 'string') return string;
var cleanString = string.replace(/([_\-])/g, ' ');
return toTitleCase(cleanString);
}
/**
* Converts camelCase or PascalCase to underscore_case
* Adapted from https://stackoverflow.com/questions/30521224/javascript-convert-pascalcase-to-underscore-case
* @param {String} string
* @return {String}
*/
export function underscoreCase(string) {
return string.replace(/\.?([A-Z]+)/g, function (x, y) {
return '_' + y.toLowerCase();
}).replace(/^_/, '');
}
/**
* Strips html tags from string
* @param {String} string
* @return {String}
*/
export function stripHtml(string) {
var div = document.createElement('div');
div.innerHTML = string.replace(/<br ?\/?>/g, ' ');
return div.textContent || div.innerText || string;
}
/**
* Returns word count
* @param {String} value
* @return {Number}
*/
export function wordCount(value) {
var words = String(value == null ? '' : value).replace(/\//g, ' ') // convert forward-slashes to spaces
.replace(/[^A-Z\s]/gi, '') // remove all non-word characters
.replace(/^\s+/, '') // trim start
.replace(/\s+$/, '') // trim end
.split(/\s+/); // split on the spaces
return words[0].length ? words.length : 0;
}
/**
* Returns titlized label from a path description
* @param {String|Array} keyPath
* @return {String}
*/
export function labelFromKeyPath(keyPath) {
var label = (isArray(keyPath) ? keyPath.filter(function (value) {
return !isNumeric(value);
}).pop() : keyPath) || '';
label = label.split('.').pop();
return titleCase(label.replace('_', ' '));
}
/**
* Replaces unnecessary tags with normal spaces in html strings.
*
* @param {String} html
* @return {String}
*/
export function simplifyNonBreakingSpaces(html) {
return html.replace(/ /gi, function (match, offset, string) {
var before = string.substr(0, offset);
// don't change if previous string is a
if (before.match(/ $/)) return match;
var after = string.substr(offset + match.length);
// don't replace before closing html tag
if (after.match(/^<\//)) return match;
// return a normal space in all other cases
return ' ';
});
}