MCPcopy Create free account
hub / github.com/decaporg/decap-cms / compareVersions

Function compareVersions

scripts/publish-packages.mjs:158–181  ·  view source on GitHub ↗

* Semver precedence for the versions this repo publishes (`1.2.3` and * `1.2.3-beta.4`), without depending on the `semver` package from a script * that runs before anything else is trusted. Negative when `a` sorts first.

(a, b)

Source from the content-addressed store, hash-verified

156 * that runs before anything else is trusted. Negative when `a` sorts first.
157 */
158function compareVersions(a, b) {
159 const [aCore, aPre] = a.split('-', 2);
160 const [bCore, bPre] = b.split('-', 2);
161 const aNums = aCore.split('.').map(Number);
162 const bNums = bCore.split('.').map(Number);
163 for (let i = 0; i < 3; i += 1) {
164 if (aNums[i] !== bNums[i]) return aNums[i] - bNums[i];
165 }
166 // A release sorts after any of its prereleases.
167 if (!aPre || !bPre) return (aPre ? -1 : 0) - (bPre ? -1 : 0);
168 const aIds = aPre.split('.');
169 const bIds = bPre.split('.');
170 for (let i = 0; i < Math.max(aIds.length, bIds.length); i += 1) {
171 if (aIds[i] === undefined) return -1;
172 if (bIds[i] === undefined) return 1;
173 const aNum = /^\d+$/.test(aIds[i]);
174 const bNum = /^\d+$/.test(bIds[i]);
175 if (aNum && bNum && Number(aIds[i]) !== Number(bIds[i]))
176 return Number(aIds[i]) - Number(bIds[i]);
177 if (aNum !== bNum) return aNum ? -1 : 1;
178 if (aIds[i] !== bIds[i]) return aIds[i] < bIds[i] ? -1 : 1;
179 }
180 return 0;
181}
182
183function isPrerelease(version) {
184 return version.includes('-');

Callers 1

Calls 1

mapMethod · 0.80

Tested by

no test coverage detected