1 | 'use strict';
|
2 |
|
3 | const balancedMatch = require('balanced-match');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | module.exports = function (source, functionName, blurChar = '`') {
|
21 | const nameWithParen = `${functionName.toLowerCase()}(`;
|
22 | const lowerCaseSource = source.toLowerCase();
|
23 |
|
24 | if (!lowerCaseSource.includes(nameWithParen)) {
|
25 | return source;
|
26 | }
|
27 |
|
28 | const functionNameLength = functionName.length;
|
29 |
|
30 | let result = source;
|
31 | let searchStartIndex = 0;
|
32 |
|
33 | while (lowerCaseSource.includes(nameWithParen, searchStartIndex)) {
|
34 | const openingParenIndex =
|
35 | lowerCaseSource.indexOf(nameWithParen, searchStartIndex) + functionNameLength;
|
36 | const parensMatch = balancedMatch('(', ')', lowerCaseSource.slice(openingParenIndex));
|
37 |
|
38 | if (!parensMatch) {
|
39 | throw new Error(`No parens match: "${source}"`);
|
40 | }
|
41 |
|
42 | const closingParenIndex = parensMatch.end + openingParenIndex;
|
43 | const argumentsLength = closingParenIndex - openingParenIndex - 1;
|
44 |
|
45 | result =
|
46 | result.slice(0, openingParenIndex + 1) +
|
47 | blurChar.repeat(argumentsLength) +
|
48 | result.slice(closingParenIndex);
|
49 | searchStartIndex = closingParenIndex;
|
50 | }
|
51 |
|
52 | return result;
|
53 | };
|