type Result = { error?: Error; value?: T; }; type Mention = { index: number; handle: string; mention: string; }; type Header = { type: string; scope?: string | null; subject: string; }; type SimpleHeader = { value: string; }; type GenericCommit = { header: T; body?: string | null; footer?: string | null; increment?: string; isBreaking?: boolean; mentions?: Array; }; type Commit = GenericCommit
; type BasicCommit = GenericCommit; type PossibleCommit = string | Commit | BasicCommit | Commit[] | BasicCommit[]; type Plugin = (commit: PossibleCommit, normalize?: boolean) => void | Record | Commit | BasicCommit; type Plugins = Plugin | Array; type Options = { caseSensitive?: boolean; headerRegex?: RegExp | string; mentionsWithDot?: boolean; normalize?: boolean; }; declare const errorMsg = "parse-commit-message: expect `commit` to follow:\n[optional scope]: \n\n[optional body]\n\n[optional footer]"; declare function isValidString(x: string): boolean; /** * When `options.headerRegex` is passed, * it should have 4 capturing groups: type, scope, bang, subject! * * @param {string} val * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` */ declare function stringToHeader(val: string, options?: Options): Header; declare function normalizeCommit(commit: Commit, options?: Options): Commit; declare function toArray(val: T): T[]; declare function isBreakingChange(commit: Commit): boolean; declare function cleaner(x: any): any; /** * Receives and parses a single or multiple commit message(s) in form of string, * object, array of strings, array of objects or mixed. * * @example * import { parse } from 'parse-commit-message'; * * const commits = [ * 'fix(ci): tweaks for @circleci config', * 'chore: bar qux' * ]; * const result = parse(commits); * console.log(result); * // => [{ * // header: { type: 'fix', scope: 'ci', subject: 'tweaks for @circleci config' }, * // body: null, * // footer: null, * // }, { * // header: { type: 'chore', scope: null, subject: 'bar qux' }, * // body: null, * // footer: null, * // }] * * const commitMessage = `feat: awesome yeah * * Awesome body! * resolves #123 * * Signed-off-by: And Footer `; * * const res = parse(commitMessage); * * console.log(res); * // => { * // header: { type: 'feat', scope: null, subject: 'awesome yeah' }, * // body: 'Awesome body!\nresolves #123', * // footer: 'Signed-off-by: And Footer ', * // } * * @name .parse * @param {PossibleCommit} commits a value to be parsed into an object like `Commit` type * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Array} if array of commit objects * @public */ declare function parse(commits: PossibleCommit, options?: Options): Commit[]; /** * Receives a `Commit` object, validates it using `validate`, * builds a "commit" message string and returns it. * * This method does checking and validation too, * so if you pass a string, it will be parsed and validated, * and after that turned again to string. * * @example * import { parse, stringify } from 'parse-commit-message'; * * const commitMessage = `feat: awesome yeah * * Awesome body! * resolves #123 * * Signed-off-by: And Footer `; * * const flat = true; * const res = parse(commitMessage, flat); * * const str = stringify(res, flat); * console.log(str); * console.log(str === commitMessage); * * @name .stringify * @param {PossibleCommit} commits a `Commit` object, or anything that can be passed to `check` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Array} an array of commit strings like `'fix(foo): bar baz'` * @public */ declare function stringify(commits: PossibleCommit, options?: Options): string[]; /** * Validates a single or multiple commit message(s) in form of string, * object, array of strings, array of objects or mixed. * * @example * import { validate } from 'parse-commit-message'; * * console.log(validate('foo bar qux')); // false * console.log(validate('foo: bar qux')); // true * console.log(validate('fix(ci): bar qux')); // true * * console.log(validate(['a bc cqux', 'foo bar qux'])); // false * * console.log(validate({ qux: 1 })); // false * console.log(validate({ header: { type: 'fix' } })); // false * console.log(validate({ header: { type: 'fix', subject: 'ok' } })); // true * * const commitObject = { * header: { type: 'test', subject: 'updating tests' }, * foo: 'bar', * isBreaking: false, * body: 'oh ah', * }; * console.log(validate(commitObject)); // true * * const result = validate('foo bar qux'); * console.log(result.error); * // => Error: expect \`commit\` to follow: * // [optional scope]: * // * // [optional body] * // * // [optional footer] * * const res = validate('fix(ci): okey barry'); * console.log(result.value); * // => [{ * // header: { type: 'fix', scope: 'ci', subject: 'okey barry' }, * // body: null, * // footer: null, * // }] * * const commit = { header: { type: 'fix' } }; * const { error } = validate(commit); * console.log(error); * // => TypeError: header.subject should be non empty string * * * const commit = { header: { type: 'fix', scope: 123, subject: 'okk' } }; * const { error } = validate(commit); * console.log(error); * // => TypeError: header.scope should be non empty string when given * * @name .validate * @param {PossibleCommit} commits a value to be parsed & validated into an object like `Commit` type * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Result} an object like `{ value: Array, error: Error }` * @public */ declare function validate(commits: PossibleCommit, options?: Options): Result; /** * Receives a single or multiple commit message(s) in form of string, * object, array of strings, array of objects or mixed. * Throws if find some error. Think of it as "assert", it's basically that. * * @example * import { check } from 'parse-commit-message'; * * try { * check({ header: { type: 'fix' } }); * } catch(err) { * console.log(err); * // => TypeError: header.subject should be non empty string * } * * // Can also validate/check a strings, array of strings, * // or even mixed - array of strings and objects * try { * check('fix(): invalid scope, it cannot be empty') * } catch(err) { * console.log(err); * // => TypeError: header.scope should be non empty string when given * } * * @name .check * @param {PossibleCommit} commits a value to be parsed & validated into an object like `Commit` type * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Array} returns the same as given if no problems, otherwise it will throw; * @public */ declare function check(commits: PossibleCommit, options?: Options): Commit[]; /** * Parses given `header` string into an header object. * Basically the same as [.parse](#parse), except that * it only can accept single string and returns a `Header` object. * * _The `parse*` methods are not doing any checking and validation, * so you may want to pass the result to `validateHeader` or `checkHeader`, * or to `validateHeader` with `ret` option set to `true`._ * * @example * import { parseHeader } from 'parse-commit-message'; * * const longCommitMsg = `fix: bar qux * * Awesome body!`; * * const headerObj = parseCommit(longCommitMsg); * console.log(headerObj); * // => { type: 'fix', scope: null, subject: 'bar qux' } * * @name .parseHeader * @param {string} header a header stirng like `'fix(foo): bar baz'` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Header} a `Header` object like `{ type, scope?, subject }` * @public */ declare function parseHeader(header: string, options?: Options): Header; /** * Receives a `header` object, validates it using `validateHeader`, * builds a "header" string and returns it. Method throws if problems found. * Basically the same as [.stringify](#stringify), except that * it only can accept single `Header` object. * * @example * import { stringifyHeader } from 'parse-commit-message'; * * const headerStr = stringifyCommit({ type: 'foo', subject: 'bar qux' }); * console.log(headerStr); // => 'foo: bar qux' * * @name .stringifyHeader * @param {Header|SimpleHeader} header a `Header` object like `{ type, scope?, subject }` or `{ value: string }` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {string} a header stirng like `'fix(foo): bar baz'` * @public */ declare function stringifyHeader(header: Header | SimpleHeader, options?: Options): `${string}: ${string}`; /** * Validates given `header` object and returns `boolean`. * You may want to pass `ret` to return an object instead of throwing. * Basically the same as [.validate](#validate), except that * it only can accept single `Header` object. * * @example * import { validateHeader } from 'parse-commit-message'; * * const header = { type: 'foo', subject: 'bar qux' }; * * const headerIsValid = validateHeader(header); * console.log(headerIsValid); // => true * * const { value } = validateHeader(header, true); * console.log(value); * // => { * // header: { type: 'foo', scope: null, subject: 'bar qux' }, * // body: 'okey dude', * // footer: null, * // } * * const { error } = validateHeader({ * type: 'bar' * }, true); * * console.log(error); * // => TypeError: header.subject should be non empty string * * @name .validateHeader * @param {Header|SimpleHeader} header a `Header` object like `{ type, scope?, subject }` or `{ value: string }` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Result} an object like `{ value: Array, error: Error }` * @public */ declare function validateHeader(header: Header | SimpleHeader, options?: Options): Result; /** * Receives a `Header` and checks if it is valid. * Basically the same as [.check](#check), except that * it only can accept single `Header` object. * * @example * import { checkHeader } from 'parse-commit-message'; * * try { * checkHeader({ type: 'fix' }); * } catch(err) { * console.log(err); * // => TypeError: header.subject should be non empty string * } * * // throws because can accept only Header objects * checkHeader('foo bar baz'); * checkHeader(123); * checkHeader([]); * checkHeader([{ type: 'foo', subject: 'bar' }]); * * @name .checkHeader * @param {Header|SimpleHeader} header a `Header` object like `{ type, scope?, subject }` or `{ value: string }` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Header} returns the same as given if no problems, otherwise it will throw. * @public */ declare function checkHeader(header: Header | SimpleHeader, options?: Options): Header; /** * Receives a full commit message `string` and parses it into an `Commit` object * and returns it. * Basically the same as [.parse](#parse), except that * it only can accept single string. * * _The `parse*` methods are not doing any checking and validation, * so you may want to pass the result to `validateCommit` or `checkCommit`, * or to `validateCommit` with `ret` option set to `true`._ * * @example * import { parseCommit } from 'parse-commit-message'; * * const commitObj = parseCommit('foo: bar qux\n\nokey dude'); * console.log(commitObj); * // => { * // header: { type: 'foo', scope: null, subject: 'bar qux' }, * // body: 'okey dude', * // footer: null, * // } * * @name .parseCommit * @param {string} commit a message like `'fix(foo): bar baz\n\nSome awesome body!'` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Commit} a standard object like `{ header: Header, body?, footer? }` * @public */ declare function parseCommit(commit: string, options?: Options): Commit; /** * Receives a `Commit` object, validates it using `validateCommit`, * builds a "commit" string and returns it. Method throws if problems found. * Basically the same as [.stringify](#stringify), except that * it only can accept single `Commit` object. * * @example * import { stringifyCommit } from 'parse-commit-message'; * * const commitStr = stringifyCommit({ * header: { type: 'foo', subject: 'bar qux' }, * body: 'okey dude', * }); * console.log(commitStr); // => 'foo: bar qux\n\nokey dude' * * @name .stringifyCommit * @param {Commit} commit a `Commit` object like `{ header: Header, body?, footer? }` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {string} a commit nessage stirng like `'fix(foo): bar baz'` * @public */ declare function stringifyCommit(commit: Commit | BasicCommit, options?: Options): string; /** * Validates given `Commit` object and returns `Result`. * Basically the same as [.validate](#validate), except that * it only can accept single `Commit` object. * * @example * import { validateCommit } from 'parse-commit-message'; * * const commit = { * header: { type: 'foo', subject: 'bar qux' }, * body: 'okey dude', * }; * * const commitIsValid = validateCommit(commit); * console.log(commitIsValid); // => true * * const { value } = validateCommit(commit, true); * console.log(value); * // => { * // header: { type: 'foo', scope: null, subject: 'bar qux' }, * // body: 'okey dude', * // footer: null, * // } * * @name .validateCommit * @param {Commit|BasicCommit} commit a `Commit` like `{ header: Header, body?, footer? }` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Result} an object like `{ value: Array, error: Error }` * @public */ declare function validateCommit(commit: Commit | BasicCommit, options?: Options): Result; /** * Receives a `Commit` and checks if it is valid. Method throws if problems found. * Basically the same as [.check](#check), except that * it only can accept single `Commit` object. * * @example * import { checkCommit } from 'parse-commit-message'; * * try { * checkCommit({ header: { type: 'fix' } }); * } catch(err) { * console.log(err); * // => TypeError: header.subject should be non empty string * } * * // throws because can accept only Commit objects * checkCommit('foo bar baz'); * checkCommit(123); * checkCommit([{ header: { type: 'foo', subject: 'bar' } }]); * * @name .checkCommit * @param {Commit} commit a `Commit` like `{ header: Header, body?, footer? }` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Commit} returns the same as given if no problems, otherwise it will throw. * @public */ declare function checkCommit(commit: Commit | BasicCommit, options?: Options): Commit; /** * Apply a set of `plugins` over all of the given `commits`. * A plugin is a simple function passed with `Commit` object, * which may be returned to modify and set additional properties * to the `Commit` object. * * _The `commits` should be coming from `parse`, `validate` (with `ret` option) * or the `check` methods. It does not do checking and validation._ * * @example * import dedent from 'dedent'; * import { applyPlugins, plugins, parse, check } from 'parse-commit-message'; * * const commits = [ * 'fix: bar qux', * dedent`feat(foo): yea yea * * Awesome body here with @some mentions * resolves #123 * * BREAKING CHANGE: ouch!`, * 'chore(ci): updates for ci config', * { * header: { type: 'fix', subject: 'Barry White' }, * body: 'okey dude', * foo: 'possible', * }, * ]; * * // Parses, normalizes, validates * // and applies plugins * const results = applyPlugins(plugins, check(parse(commits))); * * console.log(results); * // => [ { body: null, * // footer: null, * // header: { scope: null, type: 'fix', subject: 'bar qux' }, * // mentions: [], * // increment: 'patch', * // isBreaking: false }, * // { body: 'Awesome body here with @some mentions\nresolves #123', * // footer: 'BREAKING CHANGE: ouch!', * // header: { scope: 'foo', type: 'feat', subject: 'yea yea' }, * // mentions: [ [Object] ], * // increment: 'major', * // isBreaking: true }, * // { body: null, * // footer: null, * // header: * // { scope: 'ci', type: 'chore', subject: 'updates for ci config' }, * // mentions: [], * // increment: false, * // isBreaking: false }, * // { body: 'okey dude', * // footer: null, * // header: { scope: null, type: 'fix', subject: 'Barry White' }, * // foo: 'possible', * // mentions: [], * // increment: 'patch', * // isBreaking: false } ] * * @name .applyPlugins * @param {Plugins} plugins a simple function like `(commit) => {}` * @param {Array|PossibleCommit} commits a PossibleCommit or an array of strings; a value which should already be gone through `parse` * @param {object} options options to control the header regex and case sensitivity * @param {RegExp|string} options.headerRegex string regular expression or instance of RegExp * @param {boolean} options.caseSensitive whether or not to be case sensitive, defaults to `false` * @returns {Array} plus the modified or added properties from each function in `plugins` * @public */ declare function applyPlugins(plugins: Plugins, commits: string[] | PossibleCommit, options?: any): Commit[]; /** * An array which includes `mentions`, `isBreakingChange` and `increment` built-in plugins. * The `mentions` is an array of objects - basically what's returned from * the [collect-mentions][] package. * * @example * import { plugins, applyPlugins, parse } from 'parse-commit-message'; * * console.log(plugins); // => [mentions, increment] * console.log(plugins[0]); // => [Function mentions] * console.log(plugins[1]); // => [Function increment] * * const cmts = parse([ * 'fix: foo @bar @qux haha', * 'feat(cli): awesome @tunnckoCore feature\n\nSuper duper baz!', * 'fix: ooh\n\nBREAKING CHANGE: some awful api change' * ]); * * const commits = applyPlugins(plugins, cmts); * console.log(commits); * // => [ * // { * // header: { type: 'fix', scope: '', subject: 'foo bar baz' }, * // body: '', * // footer: '', * // increment: 'patch', * // isBreaking: false, * // mentions: [ * // { handle: '@bar', mention: 'bar', index: 8 }, * // { handle: '@qux', mention: 'qux', index: 13 }, * // ] * // }, * // { * // header: { type: 'feat', scope: 'cli', subject: 'awesome feature' }, * // body: 'Super duper baz!', * // footer: '', * // increment: 'minor', * // isBreaking: false, * // mentions: [ * // { handle: '@tunnckoCore', mention: 'tunnckoCore', index: 18 }, * // ] * // }, * // { * // header: { type: 'fix', scope: '', subject: 'ooh' }, * // body: 'BREAKING CHANGE: some awful api change', * // footer: '', * // increment: 'major', * // isBreaking: true, * // mentions: [], * // }, * // ] * * @name .plugins * @public */ declare const plugins: Plugin[]; /** * An object (named set) which includes `mentions` and `increment` built-in plugins. * * @example * import { mappers, applyPlugins, parse } from 'parse-commit-message'; * * console.log(mappers); // => { mentions, increment } * console.log(mappers.mentions); // => [Function mentions] * console.log(mappers.increment); // => [Function increment] * * const flat = true; * const parsed = parse('fix: bar', flat); * console.log(parsed); * // => { * // header: { type: 'feat', scope: 'cli', subject: 'awesome feature' }, * // body: 'Super duper baz!', * // footer: '', * // } * * const commit = applyPlugins([mappers.increment], parsed); * console.log(commit) * // => [{ * // header: { type: 'feat', scope: 'cli', subject: 'awesome feature' }, * // body: 'Super duper baz!', * // footer: '', * // increment: 'patch', * // }] * * @name .mappers * @public */ declare const mappers: Record; export { applyPlugins, check, checkCommit, checkHeader, cleaner, errorMsg, isBreakingChange, isValidString, mappers, normalizeCommit, parse, parseCommit, parseHeader, plugins, stringToHeader, stringify, stringifyCommit, stringifyHeader, toArray, validate, validateCommit, validateHeader };