-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2db9bf9
commit 95b3af8
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
function escape(str) { | ||
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); | ||
} | ||
|
||
function ensureFunction(functionOrValue) { | ||
if (typeof functionOrValue === 'function') { | ||
return functionOrValue; | ||
} | ||
return function () { | ||
return functionOrValue; | ||
}; | ||
} | ||
|
||
function longest(a, b) { | ||
return b.length - a.length; | ||
} | ||
|
||
function mapToFunctions(object) { | ||
return Object.keys(object).reduce(function (functions, key) { | ||
functions[key] = ensureFunction(object[key]); | ||
return functions; | ||
}, {}); | ||
} | ||
|
||
function replace(options) { | ||
const functionValues = mapToFunctions(options); | ||
const keys = Object.keys(functionValues).sort(longest).map(escape); | ||
|
||
const pattern = new RegExp('\\b(' + keys.join('|') + ')\\b', 'g'); | ||
|
||
return { | ||
name: 'replace', | ||
|
||
transform: function transform(code, id) { | ||
let hasReplacements = false; | ||
let match; | ||
let start; | ||
let end; | ||
let replacement; | ||
|
||
code = code.replace(pattern, (_, key) => { | ||
hasReplacements = true; | ||
return String(functionValues[key](id)); | ||
}); | ||
|
||
if (!hasReplacements) { | ||
return null; | ||
} | ||
|
||
return { | ||
code, | ||
map: null | ||
}; | ||
} | ||
}; | ||
} | ||
|
||
export default replace; |
95b3af8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: