MCPcopy Create free account
hub / github.com/Va1/string-replace-loader / replace

Function replace

lib/replace.js:1–32  ·  view source on GitHub ↗
(source, options, context)

Source from the content-addressed store, hash-verified

1function 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
34module.exports = replace

Callers 1

loaderFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…