{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-mask",
  "type": "registry:lib",
  "title": "core/mask",
  "description": "Framework-free behaviour shared by the ui-* components (mask); no DOM, no lit. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/core/mask.ts",
      "type": "registry:lib",
      "target": "components/ui/core/mask.ts",
      "content": "/**\n * Input masks: the field formats as the user types (no Carbon counterpart — Carbon puts the format\n * in helper text; this follows USWDS / imask). A pattern is slots and separators:\n * `0` a digit, `a` a letter, `*` either; any other character is a separator the field adds for\n * the user, and `\\` makes the next pattern character a separator (`\\0`).\n * E.g. `0-0000-00000-00-0` (Thai citizen ID), `000-000-0000` (phone).\n */\n\ninterface Token {\n    /** A separator: added for the user. */\n    readonly literal?: string;\n    /** A slot: what the user may type there. */\n    readonly accepts?: RegExp;\n}\n\nconst SLOTS: Readonly<Record<string, RegExp>> = {\n    '0': /^\\d$/,\n    a: /^\\p{L}$/u,\n    '*': /^[\\p{L}\\d]$/u,\n};\n\nfunction tokensOf(pattern: string): Token[] {\n    const tokens: Token[] = [];\n    let escaped = false;\n    for (const char of pattern) {\n        if (escaped) {\n            tokens.push({ literal: char });\n            escaped = false;\n        } else if (char === '\\\\') {\n            escaped = true;\n        } else {\n            tokens.push(SLOTS[char] ? { accepts: SLOTS[char] } : { literal: char });\n        }\n    }\n    return tokens;\n}\n\n/** What the user typed into the slots, left to right: separators in their place pass, characters a slot refuses are dropped. */\nfunction read(text: string, tokens: readonly Token[]): string {\n    let raw = '';\n    let at = 0;\n    for (const char of text) {\n        if (at >= tokens.length) break;\n        // separators the user didn't type are skipped; a slot is never skipped\n        let next = at;\n        while (next < tokens.length && tokens[next].literal !== undefined && tokens[next].literal !== char) next++;\n        const token = tokens[next];\n        if (token?.literal === char) {\n            at = next + 1;\n        } else if (token?.accepts?.test(char)) {\n            raw += char;\n            at = next + 1;\n        }\n    }\n    return raw;\n}\n\n/** The slots filled with `raw`, separators between them (none after the last character typed). */\nfunction format(raw: string, tokens: readonly Token[]): string {\n    let out = '';\n    let used = 0;\n    for (const token of tokens) {\n        if (used >= raw.length) break;\n        if (token.literal === undefined) {\n            out += raw[used];\n            used++;\n        } else {\n            out += token.literal;\n        }\n    }\n    return out;\n}\n\n/** Where the caret goes in a formatted value: just after its `count`-th typed character. */\nfunction caretAfter(value: string, tokens: readonly Token[], count: number): number {\n    if (count === 0) return 0;\n    let seen = 0;\n    for (let i = 0; i < value.length; i++) {\n        if (tokens[i]?.literal === undefined) seen++;\n        if (seen === count) return i + 1;\n    }\n    return value.length;\n}\n\n/** `text` with the mask applied (for a value set by the app, raw or already formatted). */\nexport function applyMask(text: string, pattern: string): string {\n    const tokens = tokensOf(pattern);\n    return format(read(text, tokens), tokens);\n}\n\n/** The characters typed, without the separators. */\nexport function unmask(text: string, pattern: string): string {\n    return read(text, tokensOf(pattern));\n}\n\n/** The virtual keyboard a mask wants: `numeric` when every slot is a digit. */\nexport function maskInputMode(pattern: string): 'numeric' | undefined {\n    const slots = tokensOf(pattern).filter((t) => t.accepts);\n    return slots.length > 0 && slots.every((t) => t.accepts === SLOTS['0']) ? 'numeric' : undefined;\n}\n\nexport type MaskDeletion = 'backward' | 'forward';\n\n/** The direction of an `InputEvent.inputType` that deletes one character, else `undefined`. */\nexport function maskDeletion(inputType: string | undefined): MaskDeletion | undefined {\n    if (inputType === 'deleteContentBackward') return 'backward';\n    if (inputType === 'deleteContentForward') return 'forward';\n    return undefined;\n}\n\nexport interface MaskEdit {\n    /** The formatted text to show (and submit). */\n    readonly value: string;\n    /** The characters typed, without separators. */\n    readonly raw: string;\n    /** The caret in `value`. */\n    readonly caret: number;\n}\n\n/**\n * One edit in a masked field: `text` is the field after the browser applied the keystroke or\n * paste, `caret` where the browser left the caret, `previous` the value before. Backspace /\n * Delete on a separator alone also takes the character beyond it, so the key never seems dead.\n */\nexport function maskEdit(previous: string, text: string, caret: number, pattern: string, deletion?: MaskDeletion): MaskEdit {\n    const tokens = tokensOf(pattern);\n    let raw = read(text, tokens);\n    let before = read(text.slice(0, caret), tokens).length;\n    if (deletion && raw === read(previous, tokens)) {\n        if (deletion === 'backward' && before > 0) {\n            raw = raw.slice(0, before - 1) + raw.slice(before);\n            before--;\n        } else if (deletion === 'forward' && before < raw.length) {\n            raw = raw.slice(0, before) + raw.slice(before + 1);\n        }\n    }\n    const value = format(raw, tokens);\n    return { value, raw, caret: caretAfter(value, tokens, before) };\n}\n"
    }
  ]
}
