1 | import { curryN, invoker } from 'ramda';
|
---|
2 |
|
---|
3 | import isFunction from './isFunction';
|
---|
4 | import ponyfill from './internal/ponyfills/String.replaceAll';
|
---|
5 |
|
---|
6 | export const replaceAllPonyfill = curryN(3, ponyfill);
|
---|
7 |
|
---|
8 | export const replaceAllInvoker = invoker(2, 'replaceAll');
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Replaces all substring matches in a string with a replacement.
|
---|
12 | *
|
---|
13 | * @func replaceAll
|
---|
14 | * @memberOf RA
|
---|
15 | * @since {@link https://char0n.github.io/ramda-adjunct/2.17.0|v2.17.0}
|
---|
16 | * @category String
|
---|
17 | * @sig String -> String -> String -> String
|
---|
18 | * @param {string} searchValue The substring or a global RegExp to match
|
---|
19 | * @param {string} replaceValue The string to replace the matches with
|
---|
20 | * @param {string} str The String to do the search and replacement in
|
---|
21 | * @return {string} A new string containing all the `searchValue` replaced with the `replaceValue`
|
---|
22 | * @throws {TypeError} When invalid arguments provided
|
---|
23 | * @see {@link http://ramdajs.com/docs/#replace|R.replace}, {@link https://github.com/tc39/proposal-string-replaceall|TC39 proposal}
|
---|
24 | * @example
|
---|
25 | *
|
---|
26 | * RA.replaceAll('ac', 'ef', 'ac ab ac ab'); //=> 'ef ab ef ab'
|
---|
27 | * RA.replaceAll('', '_', 'xxx'); //=> '_x_x_x_'
|
---|
28 | * RA.replaceAll(/x/g, 'v', 'xxx'); //=> 'vvv'
|
---|
29 | * RA.replaceAll(/x/, 'v', 'xxx'); //=> TypeError
|
---|
30 | */
|
---|
31 | const replaceAll = isFunction(String.prototype.replaceAll)
|
---|
32 | ? replaceAllInvoker
|
---|
33 | : replaceAllPonyfill;
|
---|
34 |
|
---|
35 | export default replaceAll;
|
---|