basant307/AI_Governance_Project
048
1import tabsToSpaces from 'convert-to-spaces';2const generateLineNumbers = (line, around) => {3 const lineNumbers = [];4 const min = line - around;5 const max = line + around;6 for (let lineNumber = min; lineNumber <= max; lineNumber++) {7 lineNumbers.push(lineNumber);8 }9 return lineNumbers;10};11const codeExcerpt = (source, line, options = {}) => {12 var _a;13 if (typeof source !== 'string') {14 throw new TypeError('Source code is missing.');15 }16 if (!line || line < 1) {17 throw new TypeError('Line number must start from `1`.');18 }19 const lines = tabsToSpaces(source).split(/\r?\n/);20 if (line > lines.length) {21 return;22 }23 return generateLineNumbers(line, (_a = options.around) !== null && _a !== void 0 ? _a : 3)24 .filter(line => lines[line - 1] !== undefined)25 .map(line => ({ line, value: lines[line - 1] }));26};27export default codeExcerpt;28 