.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / utils / blurFunctionArguments.js
1 /* @flow */
2 "use strict";
3 const _ = require("lodash");
4 const balancedMatch = require("balanced-match");
5
6 /**
7  * Replace all of the characters that are arguments to a certain
8  * CSS function with some innocuous character.
9  *
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).
13  *
14  * For example:
15  * blurFunctionArguments("abc url(abc) abc", "url") === "abc url(```) abc"
16  *
17  * @param {string} source
18  * @param {string} functionName
19  * @param {[string]} blurChar="`"
20  * @return {string} - The result string, with the function arguments "blurred"
21  */
22 module.exports = function(
23   source /*: string*/,
24   functionName /*: string*/
25 ) /*: string*/ {
26   const blurChar /*: string*/ =
27     arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "`";
28
29   const nameWithParen = `${functionName.toLowerCase()}(`;
30   const lowerCaseSource = source.toLowerCase();
31   if (!_.includes(lowerCaseSource, nameWithParen)) {
32     return source;
33   }
34
35   const functionNameLength /*: number*/ = functionName.length;
36
37   let result = source;
38   let searchStartIndex = 0;
39   while (lowerCaseSource.indexOf(nameWithParen, searchStartIndex) !== -1) {
40     const openingParenIndex =
41       lowerCaseSource.indexOf(nameWithParen, searchStartIndex) +
42       functionNameLength;
43     const closingParenIndex =
44       balancedMatch("(", ")", lowerCaseSource.slice(openingParenIndex)).end +
45       openingParenIndex;
46     const argumentsLength = closingParenIndex - openingParenIndex - 1;
47     result =
48       result.slice(0, openingParenIndex + 1) +
49       _.repeat(blurChar, argumentsLength) +
50       result.slice(closingParenIndex);
51     searchStartIndex = closingParenIndex;
52   }
53   return result;
54 };