Home / File/ splice-changes-into-string.ts — tailwindcss Source File

splice-changes-into-string.ts — tailwindcss Source File

Architecture documentation for splice-changes-into-string.ts, a typescript file in the tailwindcss codebase. 0 imports, 2 dependents.

File typescript UpgradeToolkit Codemods 2 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  d8b3b0ab_e770_e110_7520_a6fdabeda83a["splice-changes-into-string.ts"]
  31e76df5_525e_7bba_21f3_1c1c32817d76["candidates.test.ts"]
  31e76df5_525e_7bba_21f3_1c1c32817d76 --> d8b3b0ab_e770_e110_7520_a6fdabeda83a
  75ba60a9_2614_1c57_ad40_3663d4315f3b["migrate.ts"]
  75ba60a9_2614_1c57_ad40_3663d4315f3b --> d8b3b0ab_e770_e110_7520_a6fdabeda83a
  style d8b3b0ab_e770_e110_7520_a6fdabeda83a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

export interface StringChange {
  start: number
  end: number
  replacement: string
}

/**
 * Apply the changes to the string such that a change in the length
 * of the string does not break the indexes of the subsequent changes.
 */
export function spliceChangesIntoString(str: string, changes: StringChange[]) {
  // If there are no changes, return the original string
  if (!changes[0]) return str

  // Sort all changes in order to make it easier to apply them
  changes.sort((a, b) => {
    return a.end - b.end || a.start - b.start
  })

  // Append original string between each chunk, and then the chunk itself
  // This is sort of a String Builder pattern, thus creating less memory pressure
  let result = ''

  let previous = changes[0]

  result += str.slice(0, previous.start)
  result += previous.replacement

  for (let i = 1; i < changes.length; ++i) {
    let change = changes[i]

    result += str.slice(previous.end, change.start)
    result += change.replacement

    previous = change
  }

  // Add leftover string from last chunk to end
  result += str.slice(previous.end)

  return result
}

Subdomains

Types

Frequently Asked Questions

What does splice-changes-into-string.ts do?
splice-changes-into-string.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the UpgradeToolkit domain, Codemods subdomain.
What functions are defined in splice-changes-into-string.ts?
splice-changes-into-string.ts defines 1 function(s): spliceChangesIntoString.
What files import splice-changes-into-string.ts?
splice-changes-into-string.ts is imported by 2 file(s): candidates.test.ts, migrate.ts.
Where is splice-changes-into-string.ts in the architecture?
splice-changes-into-string.ts is located at packages/@tailwindcss-upgrade/src/utils/splice-changes-into-string.ts (domain: UpgradeToolkit, subdomain: Codemods, directory: packages/@tailwindcss-upgrade/src/utils).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free