1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 'use strict';
- const balancedMatch = require('balanced-match');
- module.exports = function (source, functionName, blurChar = '`') {
- const nameWithParen = `${functionName.toLowerCase()}(`;
- const lowerCaseSource = source.toLowerCase();
- if (!lowerCaseSource.includes(nameWithParen)) {
- return source;
- }
- const functionNameLength = functionName.length;
- let result = source;
- let searchStartIndex = 0;
- while (lowerCaseSource.includes(nameWithParen, searchStartIndex)) {
- const openingParenIndex =
- lowerCaseSource.indexOf(nameWithParen, searchStartIndex) + functionNameLength;
- const parensMatch = balancedMatch('(', ')', lowerCaseSource.slice(openingParenIndex));
- if (!parensMatch) {
- throw new Error(`No parens match: "${source}"`);
- }
- const closingParenIndex = parensMatch.end + openingParenIndex;
- const argumentsLength = closingParenIndex - openingParenIndex - 1;
- result =
- result.slice(0, openingParenIndex + 1) +
- blurChar.repeat(argumentsLength) +
- result.slice(closingParenIndex);
- searchStartIndex = closingParenIndex;
- }
- return result;
- };
|