Skip to content

parseConventionalCommit

Parses a Conventional Commits message into a structured object.

The first line is matched against the regex produced by buildConventionalCommitRegex(options). The remaining content is split into a body and an optional trailing footer block (lines matching Token: value / Token #value, including BREAKING CHANGE: ...).

Available since v2.0.0

import { parseConventionalCommit } from '@helpers4/commit';
parseConventionalCommit(message: string, options?: ConventionalCommitOptions): ParsedConventionalCommit | null
ParameterTypeDescription
messagestringFull commit message (subject + optional body/footer).
optionsConventionalCommitOptionsOptional constraints forwarded to the regex builder. (optional)

ParsedConventionalCommit | null — Parsed commit object, or null when the subject is not conventional.

Extracts type, scope, breaking flag, and description.

parseConventionalCommit('feat(api)!: add v2')
// => { type: 'feat', scope: 'api', breaking: true, description: 'add v2', body: '', footer: '' }

A BREAKING CHANGE: footer flags the commit as breaking even without the ! marker.

parseConventionalCommit('feat: add option\n\nBREAKING CHANGE: drops old config').breaking
// => true

Returns null on a non-conventional message

Section titled “Returns null on a non-conventional message”

Non-matching subjects return null rather than throwing.

parseConventionalCommit('hello world') // => null

Options shared by buildConventionalCommitRegex, parseConventionalCommit, and isConventionalCommit to constrain the accepted commit format.

interface ConventionalCommitOptions {
  requireScope?: boolean;
  scopes?: readonly string[];
  types?: readonly string[];
}

Parsed representation of a Conventional Commit message.

interface ParsedConventionalCommit {
  body: string;
  breaking: boolean;
  description: string;
  footer: string;
  scope: string | null;
  type: string;
}

View source on GitHub