Skip to content

Commit

Permalink
chore: add jsdocs to semver functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Jan 6, 2021
1 parent efcf981 commit 8310269
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions packages/opentelemetry-api/src/internal/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,29 @@ import { VERSION } from '../version';

const re = /^(\d+)\.(\d+)\.(\d+)(?:-(.*))?$/;

/**
* Create a function to test an API version to see if it is compatible with the provided ownVersion.
*
* The returned function has the following semantics:
* - Exact match is always compatible
* - Major versions must always match
* - The minor version of the API module requesting access to the global API must be greater or equal to the minor version of this API
* - Patch and build tag differences are not considered at this time
*
* @param ownVersion version which should be checked against
*/
export function _makeCompatibilityCheck(
myVersion: string
ownVersion: string
): (version: string) => boolean {
const acceptedVersions = new Set<string>([myVersion]);
const acceptedVersions = new Set<string>([ownVersion]);
const rejectedVersions = new Set<string>();

const myVersionMatch = myVersion.match(re);
const myVersionMatch = ownVersion.match(re);
if (!myVersionMatch) {
throw new Error('Cannot parse own version');
}

const ownVersion = {
const ownVersionParsed = {
major: +myVersionMatch[1],
minor: +myVersionMatch[2],
patch: +myVersionMatch[3],
Expand All @@ -51,26 +62,26 @@ export function _makeCompatibilityCheck(
return false;
}

const other = {
const otherVersionParsed = {
major: +m[1],
minor: +m[2],
patch: +m[3],
};

// major versions must match
if (ownVersion.major != other.major) {
if (ownVersionParsed.major != otherVersionParsed.major) {
rejectedVersions.add(version);
return false;
}

// if major version is 0, minor is treated like major and patch is treated like minor
if (ownVersion.major === 0) {
if (ownVersion.minor != other.minor) {
if (ownVersionParsed.major === 0) {
if (ownVersionParsed.minor != otherVersionParsed.minor) {
rejectedVersions.add(version);
return false;
}

if (ownVersion.patch < other.patch) {
if (ownVersionParsed.patch < otherVersionParsed.patch) {
rejectedVersions.add(version);
return false;
}
Expand All @@ -79,7 +90,7 @@ export function _makeCompatibilityCheck(
return true;
}

if (ownVersion.minor < other.minor) {
if (ownVersionParsed.minor < otherVersionParsed.minor) {
rejectedVersions.add(version);
return false;
}
Expand All @@ -89,4 +100,14 @@ export function _makeCompatibilityCheck(
};
}

/**
* Test an API version to see if it is compatible with this API.
*
* - Exact match is always compatible
* - Major versions must always match
* - The minor version of the API module requesting access to the global API must be greater or equal to the minor version of this API
* - Patch and build tag differences are not considered at this time
*
* @param version version of the API requesting an instance of the global API
*/
export const isCompatible = _makeCompatibilityCheck(VERSION);

0 comments on commit 8310269

Please sign in to comment.