From 6af1d9ddee77f43cc6a721974e01f1d69344457f Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 29 Jul 2026 13:30:54 +0100 Subject: [PATCH] feat: add `isPrerelease` (#6) --- src/index.ts | 1 + src/version.ts | 9 +++++++++ tests/version.test.ts | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/src/index.ts b/src/index.ts index a1a2660..8e9d013 100644 --- a/src/index.ts +++ b/src/index.ts @@ -55,6 +55,7 @@ export { getPatch, getPrerelease, increment, + isPrerelease, isValid, normalize, normalizeFull, diff --git a/src/version.ts b/src/version.ts index 7d39773..76df825 100644 --- a/src/version.ts +++ b/src/version.ts @@ -29,6 +29,15 @@ export function isValid( return tryParse(version, options) !== null } +export function isPrerelease( + version: VersionInput, + options: VersionOptions = {}, +): boolean | null { + const parsed = tryParse(version, options) + if (!parsed) return null + return !!parsed.prerelease?.length +} + export function normalizeFull( version: VersionInput, options: VersionOptions = {}, diff --git a/tests/version.test.ts b/tests/version.test.ts index 78fe8ac..6f39777 100644 --- a/tests/version.test.ts +++ b/tests/version.test.ts @@ -10,6 +10,7 @@ import { getPatch, getPrerelease, increment, + isPrerelease, isValid, normalize, normalizeFull, @@ -109,6 +110,7 @@ describe('version parsing and accessors', () => { version.build = ['next'] expect(isValid(version)).toBe(true) + expect(isPrerelease(version)).toBe(true) expect(normalizeFull(version)).toBe('1.2.4-beta.1+next') expect(normalize(version)).toBe('1.2.4-beta.1') expect(clean(version)).toBe('1.2.4-beta.1') @@ -133,6 +135,7 @@ describe('version parsing and accessors', () => { expect(increment(version, 'patch')).toBe('1.2.4') expect(getPrerelease(version)).toEqual([]) expect(getBuild(version)).toEqual([]) + expect(isPrerelease(version)).toBe(false) }) it('accepts every valid node-semver fixture', () => { @@ -142,6 +145,7 @@ describe('version parsing and accessors', () => { const full = `${comparable}${build.length ? `+${build.join('.')}` : ''}` expect(isValid(version)).toBe(true) + expect(isPrerelease(version)).toBe(prerelease.length > 0) expect(normalizeFull(version)).toBe(full) expect(normalize(version)).toBe(comparable) expect(getMajor(version)).toBe(major) @@ -158,6 +162,7 @@ describe('version parsing and accessors', () => { if (typeof version !== 'string') continue const options = versionOptions(rawOptions) expect(isValid(version, options)).toBe(false) + expect(isPrerelease(version, options)).toBeNull() expect(normalizeFull(version, options)).toBeNull() expect(normalize(version, options)).toBeNull() } -- 2.51.2