-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move version comparison to utils
- post-review refactoring Signed-off-by: Maksim Sukharev <[email protected]>
- Loading branch information
Showing
4 changed files
with
91 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import { satisfyVersion } from '../satisfyVersion.ts' | ||
|
||
describe('satisfyVersion', () => { | ||
const testCases = [ | ||
// Before required | ||
['29.1.4.3', '28.2.5.4', true], | ||
['29.1.4.3', '29.0.15.3', true], | ||
['29.1.4.3', '29.1.1.3', true], | ||
['29.1.4.3', '29.1.4.0', true], | ||
['29.1.4.3', '29.1.4', true], | ||
['29.1.4.3', '29.1', true], | ||
['29.1.4.3', '29', true], | ||
['29.1.4', '29.1.3.9', true], | ||
['29.1', '29.0.9.1', true], | ||
['29', '28.1.5.6', true], | ||
// Exact match | ||
['29.1.4.3', '29.1.4.3', true], | ||
['29', '29.0.0.0', true], | ||
// After required | ||
['29.1.4.3', '29.1.4.4', false], | ||
['29.1.4.3', '29.1.5.0', false], | ||
['29.1.4.3', '29.2.0.0', false], | ||
['29.1.4.3', '30.0.0.0', false], | ||
['29.1.4.3', '29.1.5', false], | ||
['29.1.4.3', '29.2', false], | ||
['29.1.4.3', '30', false], | ||
['29.1.4', '29.1.4.3', false], | ||
['29.1', '29.1.4.3', false], | ||
['29', '29.1.4.3', false], | ||
] | ||
|
||
it.each(testCases)('check if %s should satisfy requirement %s returns %s', (a, b, c) => { | ||
expect(satisfyVersion(a, b)).toBe(c) | ||
}) | ||
}) |
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,26 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
/** | ||
* Checks if given version satisfy requirements (newer than) | ||
* Versions are expected to have format like '29.1.4.3' | ||
* @param version given version | ||
* @param requirement version to compare against | ||
*/ | ||
function satisfyVersion(version: string, requirement: string): boolean { | ||
const versionMap = version.split('.').map(Number) | ||
const requirementMap = requirement.split('.').map(Number) | ||
|
||
for (let i = 0; i < Math.max(versionMap.length, requirementMap.length); i++) { | ||
if ((versionMap[i] ?? 0) !== (requirementMap[i] ?? 0)) { | ||
return (versionMap[i] ?? 0) > (requirementMap[i] ?? 0) | ||
} | ||
} | ||
return true | ||
} | ||
|
||
export { | ||
satisfyVersion, | ||
} |