ramda-adjunct
Version:
Ramda Adjunct is the most popular and most comprehensive set of utilities for use with Ramda, providing a variety of useful, well tested functions with excellent documentation.
29 lines • 1.21 kB
JavaScript
import { replace } from 'ramda';
import isRegExp from '../../isRegExp.js';
import escapeRegExp from '../../escapeRegExp.js';
var checkArguments = function checkArguments(searchValue, replaceValue, str) {
if (str == null || searchValue == null || replaceValue == null) {
throw TypeError('Input values must not be `null` or `undefined`');
}
};
var checkValue = function checkValue(value, valueName) {
if (typeof value !== 'string') {
if (!(value instanceof String)) {
throw TypeError("`".concat(valueName, "` must be a string"));
}
}
};
var checkSearchValue = function checkSearchValue(searchValue) {
if (typeof searchValue !== 'string' && !(searchValue instanceof String) && !(searchValue instanceof RegExp)) {
throw TypeError('`searchValue` must be a string or an regexp');
}
};
var replaceAll = function replaceAll(searchValue, replaceValue, str) {
checkArguments(searchValue, replaceValue, str);
checkValue(str, 'str');
checkValue(replaceValue, 'replaceValue');
checkSearchValue(searchValue);
var regexp = new RegExp(isRegExp(searchValue) ? searchValue : escapeRegExp(searchValue), 'g');
return replace(regexp, replaceValue, str);
};
export default replaceAll;