(source, options, context)
| 1 | function replace (source, options, context) { |
| 2 | const { flags, strict } = options |
| 3 | |
| 4 | let search |
| 5 | if (options.search instanceof RegExp) { |
| 6 | // if the `search` type is RegExp, we ignore `flags` |
| 7 | search = options.search |
| 8 | } else if (flags !== null) { |
| 9 | search = new RegExp(options.search, flags) |
| 10 | } else { |
| 11 | search = options.search |
| 12 | } |
| 13 | |
| 14 | let replace |
| 15 | if (typeof options.replace === 'function') { |
| 16 | replace = options.replace.bind(context) |
| 17 | } else { |
| 18 | replace = options.replace |
| 19 | } |
| 20 | |
| 21 | if (strict && (typeof search === 'undefined' || search === null || typeof replace === 'undefined' || replace === null)) { |
| 22 | throw new Error('Replace failed (strict mode) : options.search and options.replace are required') |
| 23 | } |
| 24 | |
| 25 | const newSource = source.replace(search, replace) |
| 26 | |
| 27 | if (strict && (newSource === source)) { |
| 28 | throw new Error('Replace failed (strict mode) : ' + options.search + ' → ' + options.replace) |
| 29 | } |
| 30 | |
| 31 | return newSource |
| 32 | } |
| 33 | |
| 34 | module.exports = replace |
no outgoing calls
no test coverage detected
searching dependent graphs…