You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB
JavaScript

"use strict";
module.exports = ($version) => {
let version = $version();
// FIXME: assert string
let parts = [];
let currentPart = "";
let isNumber = null;
function finalizePart() {
if (currentPart !== "") {
// NOTE: Numbers get added to the list as strings anyway. This is really weird considering `nix-env -u`s comparison logic, but it's how upstream Nix works too.
parts.push(currentPart);
currentPart = "";
isNumber = null;
}
}
// SPEC: Is it correct to assume that only the ASCII character set is supported here?
// TODO: Replace this with a proper parser some day; maybe use it as a testcase for protocolkit?
for (let i = 0; i < version.length; i++) {
let code = version.charCodeAt(i);
if (code >= 48 && code <= 57) {
// Digit
if (isNumber !== true) {
finalizePart();
isNumber = true;
}
currentPart += version[i];
} else if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
// Letter (uppercase and lowercase respectively)
if (isNumber !== false) {
finalizePart();
isNumber = false;
}
currentPart += version[i];
} else {
finalizePart();
}
}
finalizePart();
return parts;
};