3 const _ = require("lodash");
4 const balancedMatch = require("balanced-match");
7 * Replace all of the characters that are arguments to a certain
8 * CSS function with some innocuous character.
10 * This is useful if you need to use a RegExp to find a string
11 * but want to ignore matches in certain functions (e.g. `url()`,
12 * which might contain all kinds of false positives).
15 * blurFunctionArguments("abc url(abc) abc", "url") === "abc url(```) abc"
17 * @param {string} source
18 * @param {string} functionName
19 * @param {[string]} blurChar="`"
20 * @return {string} - The result string, with the function arguments "blurred"
22 module.exports = function(
24 functionName /*: string*/
26 const blurChar /*: string*/ =
27 arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "`";
29 const nameWithParen = `${functionName.toLowerCase()}(`;
30 const lowerCaseSource = source.toLowerCase();
31 if (!_.includes(lowerCaseSource, nameWithParen)) {
35 const functionNameLength /*: number*/ = functionName.length;
38 let searchStartIndex = 0;
39 while (lowerCaseSource.indexOf(nameWithParen, searchStartIndex) !== -1) {
40 const openingParenIndex =
41 lowerCaseSource.indexOf(nameWithParen, searchStartIndex) +
43 const closingParenIndex =
44 balancedMatch("(", ")", lowerCaseSource.slice(openingParenIndex)).end +
46 const argumentsLength = closingParenIndex - openingParenIndex - 1;
48 result.slice(0, openingParenIndex + 1) +
49 _.repeat(blurChar, argumentsLength) +
50 result.slice(closingParenIndex);
51 searchStartIndex = closingParenIndex;