2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2024-07-31 06:26:57 +00:00
|
|
|
import { ANY } from "./_constants.ts";
|
2023-06-12 03:47:03 +00:00
|
|
|
import type { SemVer } from "./types.ts";
|
|
|
|
import { isValidNumber, isValidString } from "./_shared.ts";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to see if value is a valid SemVer object. It does a check
|
|
|
|
* into each field including prerelease and build.
|
|
|
|
*
|
2023-06-21 16:27:37 +00:00
|
|
|
* 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
|
2023-06-12 03:47:03 +00:00
|
|
|
* 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.
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { isSemVer } from "@std/semver/is-semver";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* const value = {
|
|
|
|
* major: 1,
|
|
|
|
* minor: 2,
|
|
|
|
* patch: 3,
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* assert(isSemVer(value));
|
2024-06-28 05:47:34 +00:00
|
|
|
* assert(!isSemVer({ major: 1, minor: 2 }));
|
2024-05-27 10:03:20 +00:00
|
|
|
* ```
|
|
|
|
*
|
2023-06-12 03:47:03 +00:00
|
|
|
* @param value The value to check to see if its a valid SemVer object
|
|
|
|
* @returns True if value is a valid SemVer otherwise false
|
|
|
|
*/
|
|
|
|
export function isSemVer(value: unknown): value is SemVer {
|
2023-08-25 09:04:43 +00:00
|
|
|
if (value === null || value === undefined) return false;
|
2023-06-12 03:47:03 +00:00
|
|
|
if (Array.isArray(value)) return false;
|
|
|
|
if (typeof value !== "object") return false;
|
|
|
|
if (value === ANY) return true;
|
|
|
|
|
2024-01-03 22:01:51 +00:00
|
|
|
const {
|
|
|
|
major,
|
|
|
|
minor,
|
|
|
|
patch,
|
|
|
|
build = [],
|
|
|
|
prerelease = [],
|
|
|
|
} = value as Record<string, unknown>;
|
|
|
|
return (
|
|
|
|
isValidNumber(major) &&
|
|
|
|
isValidNumber(minor) &&
|
|
|
|
isValidNumber(patch) &&
|
2023-06-12 03:47:03 +00:00
|
|
|
Array.isArray(prerelease) &&
|
2024-01-03 22:01:51 +00:00
|
|
|
prerelease.every((v) => isValidString(v) || isValidNumber(v)) &&
|
2023-06-12 03:47:03 +00:00
|
|
|
Array.isArray(build) &&
|
2024-01-03 22:01:51 +00:00
|
|
|
build.every(isValidString)
|
|
|
|
);
|
2023-06-12 03:47:03 +00:00
|
|
|
}
|