UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

82 lines (81 loc) 2.65 kB
import type { SortOptions } from './array/array.util.js'; export type SemverInput = string | Semver; export type SemverInputNullable = SemverInput | null | undefined; export type SemverTokens = [major: number, minor: number, patch: number]; /** * Simple Semver implementation. * * Suitable for Browser usage, unlike npm `semver` which is Node-targeted and simply too big for the Browser. * * Parsing algorithm is simple: * 1. Split by `.` * 2. parseInt each of 3 tokens, set to 0 if falsy * * toString returns `major.minor.patch` * Missing tokens are replaced with 0. * * _semver('1').toString() === '1.0.0' * * @experimental */ export declare class Semver { tokens: SemverTokens; constructor(tokens: SemverTokens); get major(): number; get minor(): number; get patch(): number; isAfter: (other: SemverInput) => boolean; isSameOrAfter: (other: SemverInput) => boolean; isBefore: (other: SemverInput) => boolean; isSameOrBefore: (other: SemverInput) => boolean; isSame: (other: SemverInput) => boolean; /** * Returns 1 if this > other * returns 0 if they are equal * returns -1 if this < other */ compare(other: SemverInput): -1 | 0 | 1; toJSON: () => string; toString(): string; } declare class SemverFactory { fromInput(input: SemverInput): Semver; fromInputOrUndefined(input: SemverInputNullable): Semver | undefined; /** * Returns the highest (max) Semver from the array, or undefined if the array is empty. */ maxOrUndefined(items: SemverInputNullable[]): Semver | undefined; /** * Returns the highest Semver from the array. * Throws if the array is empty. */ max(items: SemverInputNullable[]): Semver; /** * Returns the lowest (min) Semver from the array, or undefined if the array is empty. */ minOrUndefined(items: SemverInputNullable[]): Semver | undefined; /** * Returns the lowest Semver from the array. * Throws if the array is empty. */ min(items: SemverInputNullable[]): Semver; /** * Sorts an array of Semvers in `dir` order (ascending by default). */ sort(items: Semver[], opt?: SortOptions): Semver[]; } interface SemverFn extends SemverFactory { (input: SemverInput): Semver; } export declare const semver2: SemverFn; /** * Returns 1 if a > b * returns 0 if they are equal * returns -1 if a < b * * Quick&dirty implementation, which should suffice for 95% of the cases. * * Credit: https://stackoverflow.com/a/47159772/4919972 */ export declare function _quickSemverCompare(a: string, b: string): -1 | 0 | 1; export {};