@hyperfrontend/versioning/commits/parse

parse

Conventional-commit message parser: header, body, and footers parsed without regex.

parseConventionalCommit is the main entry point — given a raw commit message it returns a ConventionalCommit (or throws if the format is invalid). isConventionalCommit is the non-throwing predicate for guard checks. The lower-level helpers (parseHeader, parseBody, parseFooters) are exposed for callers that need to operate on individual sections of a message; each returns a focused ParsedHeader / ParsedBody / ParsedFooters shape. All parsing is character-by-character with explicit state transitions, giving O(n) behavior and no catastrophic-backtracking risk; input length is capped at 10 KB.

API Reference

ƒ Functions

§function

isConventionalCommit(message: string): boolean

Checks if a commit message follows conventional commit format.

Parameters

NameTypeDescription
§message
string
The commit message to check

Returns

boolean
true if the message appears to be a conventional commit

Example

Checking if a message is a conventional commit

isConventionalCommit('feat(auth): add OAuth login')
// => true

isConventionalCommit('WIP: still working on this')
// => false

isConventionalCommit('fix: resolve bug')
// => true
§function

parseBody(lines: string[], startIndex: number): ParsedBody

Parses the body section of a commit message.
The body starts after the first blank line and continues until we encounter a footer (key: value or key #value pattern) or end of message.

Parameters

NameTypeDescription
§lines
string[]
All lines of the commit message
§startIndex
number
Index to start looking for body (after header)

Returns

ParsedBody
Parsed body or undefined if no body

Example

Parsing commit body

const lines = [
  'feat: add login',
  '',
  'Implements OAuth flow.',
  '',
  'Refs: #123'
]
const result = parseBody(lines, 1)
// => { body: 'Implements OAuth flow.', endIndex: 4 }
§function

parseConventionalCommit(message: string): ConventionalCommit

Parses a conventional commit message.

Parameters

NameTypeDescription
§message
string
The complete commit message

Returns

ConventionalCommit
Parsed ConventionalCommit object

Example

Parsing a complete conventional commit message

parseConventionalCommit('feat(auth): add login\n\nImplements OAuth.\n\nRefs: #123')
// => {
//   type: 'feat',
//   scope: ['auth'],
//   subject: 'add login',
//   body: 'Implements OAuth.',
//   footers: [{ key: 'Refs', value: '#123', separator: ':' }],
//   breaking: false,
//   raw: '...'
// }
§function

parseFooters(lines: string[], startIndex: number): ParsedFooters

Parses the footer section of a commit message.

Parameters

NameTypeDescription
§lines
string[]
All lines of the commit message
§startIndex
number
Index where footers start

Returns

ParsedFooters
Parsed footers

Example

Parsing commit footers

const lines = ['feat: add feature', '', 'Refs: #123', 'Fixes #456']
const result = parseFooters(lines, 2)
// => {
//   footers: [
//     { key: 'Refs', value: '#123', separator: ':' },
//     { key: 'Fixes', value: '456', separator: ' #' }
//   ],
//   breakingDescription: undefined
// }
§function

parseHeader(line: string): ParsedHeader

Parses a conventional commit header line.
Supports comma-separated multi-scope headers such as feat(a,b): x which produce a multi-element scope array. Single-scope headers produce a one-element array, and scopeless headers produce an empty array.

Parameters

NameTypeDescription
§line
string
The first line of the commit message

Returns

ParsedHeader
Parsed header with type, scope array, subject, and breaking flag

Example

Parsing conventional commit headers

parseHeader('feat(auth): add OAuth login')
// => { type: 'feat', scope: ['auth'], subject: 'add OAuth login', breaking: false }

parseHeader('fix!: critical security patch')
// => { type: 'fix', scope: [], subject: 'critical security patch', breaking: true }

parseHeader('feat(versioning,questions): add searchable select')
// => { type: 'feat', scope: ['versioning', 'questions'], subject: 'add searchable select', breaking: false }

Interfaces

§interface

ParsedBody

Parsed body section of a commit message.

Properties

§body:string
The body text (may be multiline)
§endIndex:number
Index where the body ends (start of footers or end of message)
§interface

ParsedFooters

Parsed footer section of a commit message.

Properties

§breakingDescription?:string
Breaking change description if found
§footers:CommitFooter[]
All parsed footers
§interface

ParsedHeader

Parsed conventional commit header components.

Properties

§breaking:boolean
Whether this is a breaking change
§scope:unknown
Scopes from parentheses (empty array when header has no scope)
§subject:string
Commit subject line
§type:string
Commit type (feat, fix, chore, etc.)