or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

comparison.mdindex.mdranges.mdvalidation.md
tile.json

comparison.mddocs/

Version Comparison

Core version comparison functionality providing both numeric and boolean comparison modes. Supports the full semver specification with flexible format handling.

Capabilities

compareVersions Function

Numeric comparison of version strings returning values compatible with Array.sort().

/**
 * Compare semver version strings to find greater, equal or lesser.
 * This library supports the full semver specification, including comparing versions 
 * with different number of digits like `1.0.0`, `1.0`, `1`, and pre-release versions like `1.0.0-alpha`.
 * @param v1 - First version to compare
 * @param v2 - Second version to compare
 * @returns Numeric value compatible with the Array.sort(fn) interface.
 *   Returns 1 if v1 > v2, 0 if v1 === v2, -1 if v1 < v2
 * @throws TypeError if either argument is not a string
 * @throws Error if version string is not valid semver format
 */
function compareVersions(v1: string, v2: string): number;

Usage Examples:

import { compareVersions } from "compare-versions";

// Basic comparison
compareVersions('1.0.0', '2.0.0');   // -1 (first is smaller)
compareVersions('2.0.0', '1.0.0');   //  1 (first is larger)
compareVersions('1.0.0', '1.0.0');   //  0 (equal)

// Pre-release versions
compareVersions('1.0.0-alpha', '1.0.0');        // -1 (pre-release < release)
compareVersions('1.0.0-alpha', '1.0.0-beta');   // -1 (alpha < beta lexically)
compareVersions('1.0.0-alpha.1', '1.0.0-alpha.2'); // -1

// Different digit counts
compareVersions('1.0', '1.0.0');     //  0 (equivalent)
compareVersions('1', '1.0.0');       //  0 (equivalent)
compareVersions('1.0.1', '1.0');     //  1 (1.0.1 > 1.0.0)

// Sorting with Array.sort()
const versions = ['1.5.19', '1.2.3', '1.5.5', '2.0.0'];
versions.sort(compareVersions);
// Result: ['1.2.3', '1.5.5', '1.5.19', '2.0.0']

// Reverse sorting
versions.sort((a, b) => compareVersions(b, a));
// Result: ['2.0.0', '1.5.19', '1.5.5', '1.2.3']

Error Handling:

The compareVersions function validates inputs and throws descriptive errors:

  • TypeError: When either argument is not a string type
  • Error: When version string is not valid semver format
// These will throw errors
compareVersions(123, '1.0.0');        // TypeError: Invalid argument expected string
compareVersions('1.0.0', undefined);  // TypeError: Invalid argument expected string
compareVersions('invalid', '1.0.0');  // Error: Invalid argument not valid semver ('invalid' received)
compareVersions('1.0.0', '6.3.');     // Error: Invalid argument not valid semver ('6.3.' received)

compare Function

Human-readable comparison using arithmetic operators.

/**
 * Compare semver version strings using the specified operator.
 * @param v1 First version to compare
 * @param v2 Second version to compare
 * @param operator Allowed arithmetic operator to use
 * @returns `true` if the comparison between the firstVersion and the secondVersion satisfies the operator, `false` otherwise.
 * @throws TypeError if operator is not a string
 * @throws Error if operator is not one of: >, >=, =, <=, <, !=
 */
function compare(v1: string, v2: string, operator: CompareOperator): boolean;

type CompareOperator = '>' | '>=' | '=' | '<' | '<=' | '!=';

Usage Examples:

import { compare } from "compare-versions";

// Greater than
compare('10.1.8', '10.0.4', '>');   // true
compare('10.0.4', '10.1.8', '>');   // false

// Equal
compare('10.0.1', '10.0.1', '=');   // true
compare('1.0', '1.0.0', '=');       // true

// Less than or equal
compare('10.1.1', '10.2.2', '<=');  // true
compare('10.2.2', '10.1.1', '<=');  // false

// Not equal
compare('1.0.0', '1.0.1', '!=');    // true
compare('1.0.0', '1.0.0', '!=');    // false

// With pre-release versions
compare('1.0.0-alpha', '1.0.0', '<');      // true
compare('1.0.0-beta', '1.0.0-alpha', '>'); // true

Error Handling

The compare function validates input operators and throws descriptive errors:

  • TypeError: When operator is not a string type
  • Error: When operator is not one of the allowed values (>, >=, =, <=, <, !=)
// These will throw errors
compare('1.0.0', '2.0.0', 123);        // TypeError: Invalid operator type, expected string but got number
compare('1.0.0', '2.0.0', 'invalid');  // Error: Invalid operator, expected one of >|>=|=|<=|<|!=

Version Format Compatibility

Both functions support all the flexible version formats:

  • Standard semver: 1.0.0, 2.1.3-beta.1
  • Short formats: 1.0, 1 (treated as 1.0.0)
  • Wildcards: 1.0.x, 1.0.* (in version strings)
  • Chromium versions: 25.0.1364.126
  • Version prefixes: v1.0.0 (v is ignored)
  • Leading zeros: 1.01.1 becomes 1.1.1

Pre-release versions are always considered less than their corresponding release versions, and pre-release identifiers are compared lexically when the main version numbers are equal.