{"version":3,"sources":["../src/index.ts"],"names":["Chars","nonNegative","count","toSpacesCount","line","chars","idx","char","un$untagged","s","shouldIgnoreFirstLine","lines","rest","counts","spaces","result","un$tagged","strings","values","un","src_default"],"mappings":"AAEA,IAAMA,EAAQ,CAEZ,GAAI,IAEJ,GAAI,IAEJ,GAAI;AAAA,EAEJ,KAAM;AAAA,CACR,EAEA,SAASC,EAAYC,EAAwB,CAC3C,OAAOA,EAAQ,EACjB,CAEA,SAASC,EAAcC,EAAc,CACnC,IAAMC,EAAQ,CAAC,GAAGD,CAAI,EAAE,QAAQ,EAEhC,OAAW,CAACE,EAAKC,CAAI,IAAKF,EACxB,GAAIE,IAASP,EAAM,IAAMO,IAASP,EAAM,GACtC,OAAOM,EAIX,MAAO,EACT,CAEA,SAASE,EAAYC,EAAmB,CAEtC,IAAMC,EAAwBD,EAAE,WAAWT,EAAM,EAAE,EAG7CW,EAAQF,EAAE,MAAMT,EAAM,EAAE,EAGxB,CAAC,CAAE,GAAGY,CAAI,EAAID,EAGdE,EAASD,EAAK,OAASA,EAAK,IAAIT,CAAa,EAAE,OAAOF,CAAW,EAAI,CAAC,CAAC,EAGvEa,EAAS,KAAK,IAAI,GAAGD,CAAM,EAG7BE,EAAS,GAEb,OAAW,CAACT,EAAKF,CAAI,IAAKO,EAAM,QAAQ,GAClCL,EAAM,GAAMA,IAAQ,GAAK,CAACI,KAC5BK,GAAUf,EAAM,IAIdM,IAAQ,EACVS,GAAUX,EAGHA,EAAK,OAASU,IACrBC,GAAUX,EAAK,MAAMU,CAAM,GAI/B,OAAOC,CACT,CAEA,SAASC,EAAUC,KAAkCC,EAAgC,CACnF,OAAOV,EAAY,OAAO,IAAI,CAAE,IAAKS,CAAQ,EAAG,GAAGC,CAAM,CAAC,CAC5D,CAUA,SAASC,EAAGV,KAAqCS,EAAgC,CAC/E,GAAI,OAAOT,GAAM,SACf,OAAOD,EAAYC,CAAC,EACf,GAAI,MAAM,QAAQA,CAAC,EACxB,OAAOO,EAAUP,EAAG,GAAGS,CAAM,EAG/B,MAAM,IAAI,MAAM,gEAAgE,OAAOT,CAAC,IAAI,CAC9F,CAEA,IAAOW,EAAQD","sourcesContent":["const NG_ONE = -1\n\nconst Chars = {\n  /** Space. */\n  SP: '\\u0020',\n  /** Horizontal tab. */\n  HT: '\\u0009',\n  /** Line feed (Unix/macOS). */\n  LF: '\\u000A',\n  /** Carriage return + Line feed (Windows). */\n  CRLF: '\\u000D\\u000A'\n} as const\n\nfunction nonNegative(count: number): boolean {\n  return count > NG_ONE\n}\n\nfunction toSpacesCount(line: string) {\n  const chars = [...line].entries()\n\n  for (const [idx, char] of chars) {\n    if (char !== Chars.SP && char !== Chars.HT) {\n      return idx\n    }\n  }\n\n  return NG_ONE\n}\n\nfunction un$untagged(s: string): string {\n  // Document may start either on the same line as opening quote/backtick or on the next line.\n  const shouldIgnoreFirstLine = s.startsWith(Chars.LF)\n\n  // TODO: Handle the `\\r\\n` case as well.\n  const lines = s.split(Chars.LF)\n\n  // Get everything but the 1st line.\n  const [, ...rest] = lines\n\n  // Get number of spaces for each line and leave only non-negative values.\n  const counts = rest.length ? rest.map(toSpacesCount).filter(nonNegative) : [0]\n\n  // Largest number of spaces that can be removed from every non-whitespace-only line after the 1st.\n  const spaces = Math.min(...counts)\n\n  // Resulting string.\n  let result = String()\n\n  for (const [idx, line] of lines.entries()) {\n    if (idx > 1 || (idx === 1 && !shouldIgnoreFirstLine)) {\n      result += Chars.LF\n    }\n\n    // Do not unindent anything on same line as opening quote/backtick.\n    if (idx === 0) {\n      result += line\n    }\n    // Whitespace-only lines may have fewer than the number of spaces being removed.\n    else if (line.length > spaces) {\n      result += line.slice(spaces)\n    }\n  }\n\n  return result\n}\n\nfunction un$tagged(strings: TemplateStringsArray, ...values: Array<unknown>): string {\n  return un$untagged(String.raw({ raw: strings }, ...values))\n}\n\n/**\n * Unindents multiline string.\n *\n * This function takes a multiline string and unindents it so the leftmost non-space character is in\n * the first column.\n */\nfunction un(s: string): string\nfunction un(s: TemplateStringsArray, ...values: Array<unknown>): string\nfunction un(s: string | TemplateStringsArray, ...values: Array<unknown>): string {\n  if (typeof s === 'string') {\n    return un$untagged(s)\n  } else if (Array.isArray(s)) {\n    return un$tagged(s, ...values)\n  }\n\n  throw new Error(`Only 'string' and 'template strings array' allowed, but got '${typeof s}'.`)\n}\n\nexport default un\n"]}