Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
188 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { IPackageVersionInfo } from '../types' | ||
import { getClosestMatchingVersion } from '../versioning' | ||
|
||
describe('getClosestMatchingVersion', () => { | ||
it('returns the closest matching version', () => { | ||
const inputVersions: IPackageVersionInfo[] = [ | ||
{ | ||
containsInternalTypings: false, | ||
version: '1.16.0', | ||
}, | ||
{ | ||
containsInternalTypings: false, | ||
version: '1.15.2', | ||
}, | ||
{ | ||
containsInternalTypings: false, | ||
version: '1.15.1', | ||
}, | ||
{ | ||
containsInternalTypings: false, | ||
version: '1.15.0', | ||
}, | ||
{ | ||
containsInternalTypings: false, | ||
version: '1.14.2', | ||
}, | ||
{ | ||
containsInternalTypings: false, | ||
version: '1.14.1', | ||
}, | ||
{ | ||
containsInternalTypings: false, | ||
version: '1.14.0', | ||
}, | ||
{ | ||
containsInternalTypings: false, | ||
version: '1.13.0', | ||
}, | ||
] | ||
|
||
expect(getClosestMatchingVersion(inputVersions, '^1.17.0').version).toBe( | ||
'1.16.0', | ||
) | ||
expect(getClosestMatchingVersion(inputVersions, '^1.16.1').version).toBe( | ||
'1.16.0', | ||
) | ||
expect(getClosestMatchingVersion(inputVersions, '^1.16.0').version).toBe( | ||
'1.16.0', | ||
) | ||
expect(getClosestMatchingVersion(inputVersions, '^1.15.4').version).toBe( | ||
'1.15.2', | ||
) | ||
expect(getClosestMatchingVersion(inputVersions, '^1.15.3').version).toBe( | ||
'1.15.2', | ||
) | ||
expect(getClosestMatchingVersion(inputVersions, '^1.15.2').version).toBe( | ||
'1.15.2', | ||
) | ||
expect(getClosestMatchingVersion(inputVersions, '^1.15.1').version).toBe( | ||
'1.15.2', | ||
) | ||
expect(getClosestMatchingVersion(inputVersions, '^1.14.0').version).toBe( | ||
'1.14.2', | ||
) | ||
}) | ||
|
||
it('throws when unable to parse version', () => { | ||
expect(() => | ||
getClosestMatchingVersion( | ||
[ | ||
{ | ||
containsInternalTypings: false, | ||
version: 'not a version', | ||
}, | ||
], | ||
'also not a version', | ||
), | ||
).toThrow() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { IPackageVersionInfo } from './types' | ||
import { parse } from 'semver' | ||
|
||
/** | ||
* Gets the closest matching package version info. | ||
* | ||
* @param availableVersions | ||
* @param version | ||
*/ | ||
export function getClosestMatchingVersion( | ||
availableVersions: IPackageVersionInfo[], | ||
version: string, | ||
) { | ||
const parsedVersion = parseOrThrow(version) | ||
|
||
return ( | ||
availableVersions.find((v) => { | ||
const parsedAvailableVersion = parseOrThrow(v.version) | ||
if (parsedVersion.major !== parsedAvailableVersion.major) { | ||
return false | ||
} | ||
|
||
if (parsedVersion.minor !== parsedAvailableVersion.minor) { | ||
return false | ||
} | ||
|
||
return true | ||
}) || availableVersions[0] | ||
) | ||
} | ||
|
||
/** | ||
* Parses the version or throws an error. | ||
* | ||
* @param version | ||
* @returns | ||
*/ | ||
function parseOrThrow(version: string) { | ||
const parsed = parse(cleanVersion(version)) | ||
if (!parsed) { | ||
throw new Error(`Could not parse version '${version}'`) | ||
} | ||
|
||
return parsed | ||
} | ||
|
||
/** | ||
* Cleans the version of any semver range specifiers. | ||
* @param version | ||
* @returns | ||
*/ | ||
function cleanVersion(version: string) { | ||
return version.replace(/^[\^~=\s]/, '') | ||
} |