keep-a-changelog
Version:
Node package to parse and generate changelogs following the [keepachangelog](https://keepachangelog.com/) format.
57 lines (56 loc) • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSemVer = isSemVer;
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
const _constants_js_1 = require("./_constants.js");
const _shared_js_1 = require("./_shared.js");
/**
* Checks to see if value is a valid SemVer object. It does a check
* into each field including prerelease and build.
*
* Some invalid SemVer sentinels can still return true such as ANY and INVALID.
* An object which has the same value as a sentinel but isn't reference equal
* will still fail.
*
* Objects which are valid SemVer objects but have _extra_ fields are still
* considered SemVer objects and this will return true.
*
* A type assertion is added to the value.
*
* @example Usage
* ```ts
* import { isSemVer } from "@std/semver/is-semver";
* import { assert } from "@std/assert";
*
* const value = {
* major: 1,
* minor: 2,
* patch: 3,
* };
*
* assert(isSemVer(value));
* assert(!isSemVer({ major: 1, minor: 2 }));
* ```
*
* @param value The value to check to see if its a valid SemVer object
* @returns True if value is a valid SemVer otherwise false
*/
function isSemVer(value) {
if (value === null || value === undefined)
return false;
if (Array.isArray(value))
return false;
if (typeof value !== "object")
return false;
if (value === _constants_js_1.ANY)
return true;
const { major, minor, patch, build = [], prerelease = [], } = value;
return ((0, _shared_js_1.isValidNumber)(major) &&
(0, _shared_js_1.isValidNumber)(minor) &&
(0, _shared_js_1.isValidNumber)(patch) &&
Array.isArray(prerelease) &&
prerelease.every((v) => (0, _shared_js_1.isValidString)(v) || (0, _shared_js_1.isValidNumber)(v)) &&
Array.isArray(build) &&
build.every(_shared_js_1.isValidString));
}