feat: builtins.parseDrvName

This commit is contained in:
2026-01-24 22:17:23 +08:00
parent 7136f57c12
commit 51f7f4079b
2 changed files with 22 additions and 2 deletions

View File

@@ -228,8 +228,21 @@ export const outputOf =
throw new Error("Not implemented: outputOf"); throw new Error("Not implemented: outputOf");
}; };
export const parseDrvName = (s: NixValue): never => { export const parseDrvName = (s: NixValue): NixAttrs => {
throw new Error("Not implemented: parseDrvName"); const fullName = forceStringNoCtx(s);
let name = fullName;
let version = "";
for (let i = 0; i < fullName.length; ++i) {
if (fullName[i] === '-' && i + 1 < fullName.length && !/[a-zA-Z]/.test(fullName[i + 1])) {
name = fullName.substring(0, i);
version = fullName.substring(i + 1);
break;
}
}
return {
name,
version
}
}; };
export const parseFlakeName = (s: NixValue): never => { export const parseFlakeName = (s: NixValue): never => {

View File

@@ -315,3 +315,10 @@ fn builtins_function_args() {
]))) ])))
); );
} }
#[test]
fn builtins_parse_drv_name() {
let result = eval(r#"builtins.parseDrvName "nix-js-0.1.0pre""#).unwrap_attr_set();
assert_eq!(result.get("name"), Some(&Value::String("nix-js".into())));
assert_eq!(result.get("version"), Some(&Value::String("0.1.0pre".into())));
}