bahaghari/lib: update hex subset to properly accept inputs with mixed letter cases

This commit is contained in:
Gabriel Arazas 2024-06-05 10:22:58 +08:00
parent 35873462f3
commit 999401f3ce
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
2 changed files with 45 additions and 3 deletions

View File

@ -27,10 +27,37 @@ let
baseSet = self.generateBaseDigitType glyphList;
in
rec {
/* Returns a convenient glyph set for creating your own conversion or
hex-related functions.
inherit (baseSet) glyphSet conversionTable;
/* Converts a hexadecimal digit string into its decimal equivalent.
Type: toDec :: String -> Number
Example:
toDec "FF"
=> 255
toDec "ff"
=> 255
*/
inherit (baseSet) glyphSet conversionTable fromDec toDec;
toDec = digit:
let
digit' = lib.toUpper digit;
in
baseSet.toDec digit';
/* Converts a decimal digit into its hexadecimal notation.
Type: fromDec :: Number -> String
Example:
fromDec 255
=> "FF"
fromDec 293454837
=> "117DC3F5"
*/
fromDec = decimal: lib.toUpper (baseSet.fromDec decimal);
/* A variant of `lib.lists.range` function just with hexadecimal digits.

View File

@ -41,6 +41,16 @@ lib.runTests {
expected = 2565;
};
testHexToDecLowercase = {
expr = self.hex.toDec "0A0FfbA";
expected = 10551226;
};
testHexToDecLowercase2 = {
expr = self.hex.toDec "0af";
expected = 175;
};
testCreateHexRange = {
expr = self.hex.range 10 17;
expected = [ "A" "B" "C" "D" "E" "F" "10" "11" ];
@ -75,4 +85,9 @@ lib.runTests {
expr = self.hex.pad (-5) "A42C";
expected = "A42C";
};
testHexPadWithMixedLetterCase = {
expr = self.hex.pad 8 "AfB9";
expected = "0000AfB9";
};
}