Created
October 29, 2022 03:03
-
-
Save sturmenta/8a1c5ea2d2b33cd25eee44601205bb59 to your computer and use it in GitHub Desktop.
check version "A" is greater or equal than version "B"
This file contains hidden or 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
export const checkVersionAIsGreaterOrEqualThanVersionB = ({ | |
versionA, | |
versionB, | |
}: { | |
versionA: string; // 1.2.3 | |
versionB: string; // 1.2.3 | |
}): boolean => { | |
const _versionA = { | |
major: parseInt(versionA.split('.')[0], 10), // 1.2.3 -> 1 | |
minor: parseInt(versionA.split('.')[1].split('.')[0], 10), // 1.2.3 -> 2 | |
patch: parseInt(versionA.split('.')[2].split('.')[0], 10), // 1.2.3 -> 3 | |
}; | |
const _versionB = { | |
major: parseInt(versionB.split('.')[0], 10), // 1.2.3 -> 1 | |
minor: parseInt(versionB.split('.')[1].split('.')[0], 10), // 1.2.3 -> 2 | |
patch: parseInt(versionB.split('.')[2].split('.')[0], 10), // 1.2.3 -> 3 | |
}; | |
// 1.x.x === 1.x.x | |
if (_versionA.major === _versionB.major) { | |
// ───────────────────────────────────────────────────────────────────────────── | |
// x.1.x === x.1.x | |
if (_versionA.minor === _versionB.minor) { | |
// ─────────────────────────────────────────────────────────────────────────── | |
return _versionA.patch >= _versionB.patch; // x.x.3 >= x.x.3 | |
// ─────────────────────────────────────────────────────────────────────────── | |
} else return _versionA.minor > _versionB.minor; // x.2.x > x.2.x | |
// ───────────────────────────────────────────────────────────────────────────── | |
} else return _versionA.major > _versionB.major; // 1.x.x > 1.x.x | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this also work
1 * 10000 + 2 * 100 + 3
=10.203
works but limit every
[ major | minor | patch ]
version to max 99