UNPKG

apply-multi-diff

Version:

A zero-dependency library to apply unified diffs and search-and-replace patches, with support for fuzzy matching.

1 lines 3.84 kB
{"version":3,"sources":["../../src/utils/string.ts"],"names":["levenshtein","s1","s2","previousRow","_","i","currentRow","j","insertions","deletions","substitutions","getIndent","line","getCommonIndent","text","lines","shortest","currentIndent","dedent","commonIndent"],"mappings":"aAAO,MAAMA,CAAAA,CAAc,CAACC,CAAAA,CAAYC,CAAAA,GAAuB,CAC7D,GAAID,CAAAA,CAAG,MAAA,CAASC,CAAAA,CAAG,MAAA,CACjB,OAAOF,CAAAA,CAAYE,CAAAA,CAAID,CAAE,CAAA,CAE3B,GAAIC,CAAAA,CAAG,MAAA,GAAW,CAAA,CAChB,OAAOD,CAAAA,CAAG,MAAA,CAEZ,IAAIE,CAAAA,CAAc,KAAA,CAAM,IAAA,CAAK,CAAE,MAAA,CAAQD,CAAAA,CAAG,MAAA,CAAS,CAAE,CAAA,CAAG,CAACE,CAAAA,CAAGC,CAAAA,GAAMA,CAAC,CAAA,CACnE,IAAA,IAAS,CAAA,CAAI,CAAA,CAAG,CAAA,CAAIJ,CAAAA,CAAG,MAAA,CAAQ,CAAA,EAAA,CAAK,CAClC,IAAIK,CAAAA,CAAa,CAAC,CAAA,CAAI,CAAC,CAAA,CACvB,IAAA,IAASC,CAAAA,CAAI,CAAA,CAAGA,CAAAA,CAAIL,CAAAA,CAAG,MAAA,CAAQK,CAAAA,EAAAA,CAAK,CAClC,MAAMC,CAAAA,CAAAA,CAAcL,EAAYI,CAAAA,CAAI,CAAC,CAAA,EAAK,CAAA,EAAK,CAAA,CACzCE,CAAAA,CAAAA,CAAaH,CAAAA,CAAWC,CAAC,CAAA,EAAK,CAAA,EAAK,CAAA,CACnCG,CAAAA,CAAAA,CAAiBP,CAAAA,CAAYI,CAAC,CAAA,EAAK,CAAA,GAAMN,CAAAA,CAAG,CAAC,CAAA,GAAMC,CAAAA,CAAGK,CAAC,CAAA,CAAI,CAAA,CAAI,CAAA,CAAA,CACrED,CAAAA,CAAW,IAAA,CAAK,IAAA,CAAK,GAAA,CAAIE,CAAAA,CAAYC,CAAAA,CAAWC,CAAa,CAAC,EAChE,CACAP,CAAAA,CAAcG,EAChB,CACA,OAAOH,CAAAA,CAAYA,CAAAA,CAAY,MAAA,CAAS,CAAC,CAAA,EAAK,CAChD,CAAA,CAEaQ,CAAAA,CAAaC,CAAAA,EACxBA,CAAAA,CAAK,KAAA,CAAM,SAAS,CAAA,GAAI,CAAC,CAAA,EAAK,EAAA,CAMnBC,CAAAA,CAAmBC,CAAAA,EAAyB,CACvD,MAAMC,CAAAA,CAAQD,CAAAA,CAAK,KAAA,CAAM;AAAA,CAAI,CAAA,CAAE,OAAQF,CAAAA,EAASA,CAAAA,CAAK,MAAK,GAAM,EAAE,CAAA,CAClE,OAAKG,CAAAA,CAAM,MAAA,CAEJA,EAAM,MAAA,CAAO,CAACC,CAAAA,CAAUJ,CAAAA,GAAS,CACtC,MAAMK,EAAgBN,CAAAA,CAAUC,CAAI,CAAA,CACpC,OAAIK,CAAAA,CAAc,MAAA,CAASD,EAAS,MAAA,CAC3BC,CAAAA,CAEFD,CACT,CAAA,CAAGL,CAAAA,CAAUI,EAAM,CAAC,CAAA,EAAK,EAAE,CAAC,CAAA,CARF,EAS5B,EAEaG,CAAAA,CAAUJ,CAAAA,EAAyB,CAC9C,MAAMK,CAAAA,CAAeN,CAAAA,CAAgBC,CAAI,CAAA,CACzC,OAAKK,CAAAA,CACEL,CAAAA,CACJ,KAAA,CAAM;AAAA,CAAI,CAAA,CACV,GAAA,CAAKF,CAAAA,EACJA,CAAAA,CAAK,WAAWO,CAAY,CAAA,CAAIP,CAAAA,CAAK,SAAA,CAAUO,CAAAA,CAAa,MAAM,CAAA,CAAIP,CACxE,EACC,IAAA,CAAK;AAAA,CAAI,EANcE,CAO5B","file":"string.cjs","sourcesContent":["export const levenshtein = (s1: string, s2: string): number => {\n if (s1.length < s2.length) {\n return levenshtein(s2, s1);\n }\n if (s2.length === 0) {\n return s1.length;\n }\n let previousRow = Array.from({ length: s2.length + 1 }, (_, i) => i);\n for (let i = 0; i < s1.length; i++) {\n let currentRow = [i + 1];\n for (let j = 0; j < s2.length; j++) {\n const insertions = (previousRow[j + 1] ?? 0) + 1;\n const deletions = (currentRow[j] ?? 0) + 1;\n const substitutions = (previousRow[j] ?? 0) + (s1[i] === s2[j] ? 0 : 1);\n currentRow.push(Math.min(insertions, deletions, substitutions));\n }\n previousRow = currentRow;\n }\n return previousRow[previousRow.length - 1] ?? 0;\n};\n\nexport const getIndent = (line: string): string =>\n line.match(/^[ \\t]*/)?.[0] || \"\";\n\n/**\n * Finds the shortest leading whitespace sequence among all non-empty lines,\n * which represents the common base indentation for a block of text.\n */\nexport const getCommonIndent = (text: string): string => {\n const lines = text.split(\"\\n\").filter((line) => line.trim() !== \"\");\n if (!lines.length) return \"\";\n\n return lines.reduce((shortest, line) => {\n const currentIndent = getIndent(line);\n if (currentIndent.length < shortest.length) {\n return currentIndent;\n }\n return shortest;\n }, getIndent(lines[0] ?? ''));\n};\n\nexport const dedent = (text: string): string => {\n const commonIndent = getCommonIndent(text);\n if (!commonIndent) return text;\n return text\n .split(\"\\n\")\n .map((line) =>\n line.startsWith(commonIndent) ? line.substring(commonIndent.length) : line\n )\n .join(\"\\n\");\n};"]}