source: node_modules/ramda-adjunct/src/internal/ponyfills/String.replaceAll.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import { replace } from 'ramda';
2
3import isRegExp from '../../isRegExp';
4import escapeRegExp from '../../escapeRegExp';
5
6const checkArguments = (searchValue, replaceValue, str) => {
7 if (str == null || searchValue == null || replaceValue == null) {
8 throw TypeError('Input values must not be `null` or `undefined`');
9 }
10};
11
12const checkValue = (value, valueName) => {
13 if (typeof value !== 'string') {
14 if (!(value instanceof String)) {
15 throw TypeError(`\`${valueName}\` must be a string`);
16 }
17 }
18};
19
20const checkSearchValue = (searchValue) => {
21 if (
22 typeof searchValue !== 'string' &&
23 !(searchValue instanceof String) &&
24 !(searchValue instanceof RegExp)
25 ) {
26 throw TypeError('`searchValue` must be a string or an regexp');
27 }
28};
29
30const replaceAll = (searchValue, replaceValue, str) => {
31 checkArguments(searchValue, replaceValue, str);
32 checkValue(str, 'str');
33 checkValue(replaceValue, 'replaceValue');
34 checkSearchValue(searchValue);
35
36 const regexp = new RegExp(
37 isRegExp(searchValue) ? searchValue : escapeRegExp(searchValue),
38 'g'
39 );
40
41 return replace(regexp, replaceValue, str);
42};
43
44export default replaceAll;
Note: See TracBrowser for help on using the repository browser.