@bigfishtv/cockpit
Version:
132 lines (117 loc) • 3.71 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, search = [], caseSensitive = false, delimiter = /[\s,]+/) {
if (typeof string != 'string') return string
if (!caseSensitive) {
search = typeof search == 'string' ? search.toLowerCase() : search.map(term => term.toLowerCase())
string = string.toLowerCase()
}
const terms = (typeof search == 'string' ? [search] : search.split(delimiter)).filter(
(item, pos, self) => self.indexOf(item) == pos
)
for (let term of terms) {
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
const 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, (x, y) => '_' + y.toLowerCase()).replace(/^_/, '')
}
/**
* Strips html tags from string
* @param {String} string
* @return {String}
*/
export function stripHtml(string) {
const 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) {
const 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) {
let label = (isArray(keyPath) ? keyPath.filter(value => !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, (match, offset, string) => {
const before = string.substr(0, offset)
// don't change if previous string is a
if (before.match(/ $/)) return match
const 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 ' '
})
}