tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

indentation.js (982B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 export function getIndentation(line) {
      6  if (!line) {
      7    return 0;
      8  }
      9 
     10  const lineMatch = line.match(/^\s*/);
     11  if (!lineMatch) {
     12    return 0;
     13  }
     14 
     15  return lineMatch[0].length;
     16 }
     17 
     18 function getMaxIndentation(lines) {
     19  const firstLine = lines[0];
     20  const secondLine = lines[1];
     21  const lastLine = lines[lines.length - 1];
     22 
     23  const indentations = [
     24    getIndentation(firstLine),
     25    getIndentation(secondLine),
     26    getIndentation(lastLine),
     27  ];
     28 
     29  return Math.max(...indentations);
     30 }
     31 
     32 export function correctIndentation(text) {
     33  const lines = text.trim().split("\n");
     34  const indentation = getMaxIndentation(lines);
     35  const formattedLines = lines.map(_line =>
     36    _line.replace(new RegExp(`^\\s{0,${indentation - 1}}`), "")
     37  );
     38 
     39  return formattedLines.join("\n");
     40 }