semver.ts
utils/semver.ts
No strong subsystem tag
60
Lines
1713
Bytes
6
Exports
0
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with general runtime concerns. It contains 60 lines, 0 detected imports, and 6 detected exports.
Important relationships
Detected exports
gtgteltltesatisfiesorder
Keywords
semvertypeoforderloosegetnpmsemvernpmsemversatisfiesversionrangecomparison
Detected imports
- No import paths detected.
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
/**
* Semver comparison utilities that use Bun.semver when available
* and fall back to the npm `semver` package in Node.js environments.
*
* Bun.semver.order() is ~20x faster than npm semver comparisons.
* The npm semver fallback always uses { loose: true }.
*/
let _npmSemver: typeof import('semver') | undefined
function getNpmSemver(): typeof import('semver') {
if (!_npmSemver) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
_npmSemver = require('semver') as typeof import('semver')
}
return _npmSemver
}
export function gt(a: string, b: string): boolean {
if (typeof Bun !== 'undefined') {
return Bun.semver.order(a, b) === 1
}
return getNpmSemver().gt(a, b, { loose: true })
}
export function gte(a: string, b: string): boolean {
if (typeof Bun !== 'undefined') {
return Bun.semver.order(a, b) >= 0
}
return getNpmSemver().gte(a, b, { loose: true })
}
export function lt(a: string, b: string): boolean {
if (typeof Bun !== 'undefined') {
return Bun.semver.order(a, b) === -1
}
return getNpmSemver().lt(a, b, { loose: true })
}
export function lte(a: string, b: string): boolean {
if (typeof Bun !== 'undefined') {
return Bun.semver.order(a, b) <= 0
}
return getNpmSemver().lte(a, b, { loose: true })
}
export function satisfies(version: string, range: string): boolean {
if (typeof Bun !== 'undefined') {
return Bun.semver.satisfies(version, range)
}
return getNpmSemver().satisfies(version, range, { loose: true })
}
export function order(a: string, b: string): -1 | 0 | 1 {
if (typeof Bun !== 'undefined') {
return Bun.semver.order(a, b)
}
return getNpmSemver().compare(a, b, { loose: true })
}