{"version":3,"file":"dedent.cjs","names":[],"sources":["../../src/string/dedent.ts"],"sourcesContent":["const INDENT = /^[ \\t]*/;\n\n/**\n * Removes the common leading indentation from every line of a string.\n *\n * A template literal inside an indented block carries that indentation into the\n * string, so an SQL query, a help message or a fixture written where it is used\n * comes out with the surrounding code's whitespace in it. The alternative is to\n * unindent the literal to the left margin, which makes the code harder to read\n * to keep the string right. This does it the other way round.\n *\n * The smallest indentation of any non-blank line is removed from all of them.\n * Relative indentation is therefore preserved: a nested clause stays nested.\n * Blank lines are ignored when measuring — one is often left with no\n * whitespace at all by an editor trimming it, and counting it would remove\n * nothing from anything — and are emptied rather than sliced, since a blank\n * line's whitespace is only ever trailing.\n *\n * A leading newline and a trailing line of only whitespace are dropped, since\n * both are artefacts of putting the opening and closing backticks on their own\n * lines rather than content anyone meant.\n *\n * Indentation is compared as a count of leading spaces and tabs, so a block\n * indented with a mixture of the two may not line up the way it looks. Consistent\n * indentation is measured exactly.\n *\n * @param {string} str The string to dedent.\n * @returns {string} The string with its common indentation removed.\n *\n * @example\n * dedent(`\n *     SELECT *\n *       FROM users\n *      WHERE id = 1\n * `)\n * //=> 'SELECT *\\n  FROM users\\n WHERE id = 1'\n *\n * @example\n * dedent('  a\\n    b') //=> 'a\\n  b'\n */\nexport default function dedent(str = ''): string {\n  const lines = str.split('\\n');\n\n  // The backticks of a multi-line template sit on their own lines, leaving an\n  // empty first line and a whitespace-only last one that were never content.\n  if (lines.length > 1 && lines[0].trim() === '') {\n    lines.shift();\n  }\n\n  if (lines.length > 1 && lines[lines.length - 1].trim() === '') {\n    lines.pop();\n  }\n\n  let common = Number.POSITIVE_INFINITY;\n\n  for (const line of lines) {\n    if (line.trim() === '') {\n      continue;\n    }\n\n    const indent = (line.match(INDENT) as RegExpMatchArray)[0].length;\n\n    if (indent < common) {\n      common = indent;\n    }\n  }\n\n  // Every line is blank, so there is no indentation to measure against.\n  const width = common === Number.POSITIVE_INFINITY ? 0 : common;\n\n  // A blank line is emptied rather than sliced. It may hold less whitespace\n  // than the common indent, or more that is only trailing.\n  return lines\n    .map((line) => (line.trim() === '' ? '' : line.slice(width)))\n    .join('\\n');\n}\n"],"mappings":"+FAAA,MAAM,EAAS,UAwCf,SAAwB,EAAO,EAAM,GAAY,CAC/C,IAAM,EAAQ,EAAI,MAAM;CAAI,EAIxB,EAAM,OAAS,GAAK,EAAM,EAAE,CAAC,KAAK,IAAM,IAC1C,EAAM,MAAM,EAGV,EAAM,OAAS,GAAK,EAAM,EAAM,OAAS,EAAE,CAAC,KAAK,IAAM,IACzD,EAAM,IAAI,EAGZ,IAAI,EAAS,IAEb,IAAK,IAAM,KAAQ,EAAO,CACxB,GAAI,EAAK,KAAK,IAAM,GAClB,SAGF,IAAM,EAAU,EAAK,MAAM,CAAM,CAAC,CAAsB,EAAE,CAAC,OAEvD,EAAS,IACX,EAAS,EAEb,CAGA,IAAM,EAAQ,IAAW,IAA2B,EAAI,EAIxD,OAAO,EACJ,IAAK,GAAU,EAAK,KAAK,IAAM,GAAK,GAAK,EAAK,MAAM,CAAK,CAAE,CAAC,CAC5D,KAAK;CAAI,CACd"}