shlink-web-client/shlink-web-component/utils/helpers/version.ts

22 lines
750 B
TypeScript
Raw Normal View History

import { compare } from 'compare-versions';
type SemVerPatternFragment = `${bigint | '*'}`;
type SemVerPattern = SemVerPatternFragment
| `${SemVerPatternFragment}.${SemVerPatternFragment}`
| `${SemVerPatternFragment}.${SemVerPatternFragment}.${SemVerPatternFragment}`;
type Versions = {
maxVersion?: SemVerPattern;
minVersion?: SemVerPattern;
};
export type SemVer = `${bigint}.${bigint}.${bigint}` | 'latest';
export const versionMatch = (versionToMatch: SemVer, { maxVersion, minVersion }: Versions): boolean => {
const matchesMinVersion = !minVersion || compare(versionToMatch, minVersion, '>=');
const matchesMaxVersion = !maxVersion || compare(versionToMatch, maxVersion, '<=');
return matchesMaxVersion && matchesMinVersion;
};