spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / hooks / turns / turn-of / file-edit-of / hunks-of / hunks-of.ts
33 lines · 958 BRaw
1import { arrayOf } from '../../../../array-of'
2import type Git from '../../../../git'
3import { isRecord } from '../../../../is-record'
4
5/**
6 * A tool result's `structuredPatch` as hunks, keeping only entries shaped
7 * like one (numeric starts, string lines).
8 *
9 * @param patch the result's `structuredPatch` field
10 * @returns the well-formed hunks; empty when the field is not an array
11 */
12export const hunksOf = (patch: unknown): Git.Hunk[] =>
13 arrayOf(patch)
14 .filter(isRecord)
15 .flatMap(hunk => {
16 const { oldStart, newStart, lines } = hunk
17 const isHunk =
18 typeof oldStart === 'number' &&
19 typeof newStart === 'number' &&
20 Array.isArray(lines)
21
22 return isHunk
23 ? [
24 {
25 oldStart,
26 newStart,
27 lines: arrayOf(lines).filter(
28 (line): line is string => typeof line === 'string',
29 ),
30 },
31 ]
32 : []
33 })