UNPKG

@technobuddha/library

Version:
50 lines (49 loc) 2.04 kB
import isString from 'lodash/isString'; import isRegExp from 'lodash/isRegExp'; import escapeRegExp from 'lodash/escapeRegExp'; import { empty } from '../constants'; import splitChars from '../splitChars'; const trimEquivalent = /[\s\uFEFF\xA0]/u; /** * Remove all occurrences of characters from the beginning and end of the string * * @param input The string * @param characters The characters(s) to remove */ export function clean(input, characters = trimEquivalent) { const re = (isString(characters) ? splitChars(characters).map(ch => escapeRegExp(ch)).join('|') : isRegExp(characters) ? characters.source : characters.map(c => (isRegExp(c) ? c.source : splitChars(c).map(ch => escapeRegExp(ch)).join('|'))).join('|')); return input.replace(new RegExp(`^(${re})+|(${re})+$`, 'gu'), empty); } /** * Remove all occurrences of characters from the end of the string * * @param input The string * @param characters he characters(s) to remove */ export function cleanEnd(input, characters = trimEquivalent) { const re = (isString(characters) ? splitChars(characters).map(ch => escapeRegExp(ch)).join('|') : isRegExp(characters) ? characters.source : characters.map(c => (isRegExp(c) ? c.source : splitChars(c).map(ch => escapeRegExp(ch)).join('|'))).join('|')); return input.replace(new RegExp(`(${re})+$`, 'u'), empty); } /** * Remove all occurrences of characters from the start of the string * * @param input The string * @param characters The characters(s) to remove */ export function cleanStart(input, characters = trimEquivalent) { const re = (isString(characters) ? splitChars(characters).map(ch => escapeRegExp(ch)).join('|') : isRegExp(characters) ? characters.source : characters.map(c => (isRegExp(c) ? c.source : splitChars(c).map(ch => escapeRegExp(ch)).join('|'))).join('|')); return input.replace(new RegExp(`^(${re})+`, 'u'), empty); } export default clean;