bahaghari/lib: init isHexString

This commit is contained in:
Gabriel Arazas 2024-02-24 21:34:03 +08:00
parent b7ac964082
commit fe4f1d1ae9
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
3 changed files with 28 additions and 1 deletions

View File

@ -21,5 +21,5 @@ pkgs.lib.makeExtensible
tinted-theming = callLibs ./tinted-theming.nix;
inherit (self.trivial) importYAML toYAML toBaseDigitsWithGlyphs generateGlyphSet;
inherit (self.hex) toHexString;
inherit (self.hex) toHexString isHexString;
})

View File

@ -14,4 +14,21 @@ rec {
range 15 18 => [ "F" "10" "11" ]
*/
range = first: last: builtins.map (n: toHexString n) (pkgs.lib.lists.range first last);
/* Checks if the given hex string is valid or not.
Type: isHexString :: String -> Bool
Example:
isHexString "ABC"
=> true
isHexString "00ABC"
=> true
isHexString "WHAT! HELL NO!"
=> false
*/
isHexString = hex:
builtins.match "[A-Fa-f0-9]+" hex != null;
}

View File

@ -27,4 +27,14 @@ pkgs.lib.runTests {
expr = lib.hex.range 49 17;
expected = [ ];
};
testIsHexString = {
expr = lib.hex.isHexString "ABC";
expected = true;
};
testIsHexStringWithInvalidHex = {
expr = lib.hex.isHexString "WHAT IS THIS";
expected = false;
};
}