bahaghari/lib: update hex subset with generateBaseDigitType

This commit is contained in:
Gabriel Arazas 2024-02-28 18:43:20 +08:00
parent 29d4bb657a
commit b84d07ac0c
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
3 changed files with 34 additions and 3 deletions

View File

@ -23,4 +23,5 @@ pkgs.lib.makeExtensible
inherit (self.trivial) importYAML toYAML toBaseDigitsWithGlyphs inherit (self.trivial) importYAML toYAML toBaseDigitsWithGlyphs
generateGlyphSet generateConversionTable generateBaseDigitType pow; generateGlyphSet generateConversionTable generateBaseDigitType pow;
inherit (self.hex) isHexString;
}) })

View File

@ -3,8 +3,18 @@
# purpose. # purpose.
{ pkgs, lib }: { pkgs, lib }:
let
glyphList =
[ "0" "1" "2" "3" "4" "5" "6" "7"
"8" "9" "A" "B" "C" "D" "E" "F" ];
baseSet = lib.generateBaseDigitType glyphList;
in
rec { rec {
inherit (pkgs.lib.trivial) toHexString; /* Returns a convenient glyph set for creating your own conversion or
hex-related functions.
*/
inherit (baseSet) glyphSet conversionTable fromDec toDec;
/* A variant of `lib.lists.range` function just with hexadecimal digits. /* A variant of `lib.lists.range` function just with hexadecimal digits.
@ -15,7 +25,7 @@ rec {
=> [ "F" "10" "11" ] => [ "F" "10" "11" ]
*/ */
range = first: last: range = first: last:
builtins.map (n: toHexString n) (pkgs.lib.lists.range first last); builtins.map (n: baseSet.fromDec n) (pkgs.lib.lists.range first last);
/* Checks if the given hex string is valid or not. /* Checks if the given hex string is valid or not.

View File

@ -2,10 +2,30 @@
pkgs.lib.runTests { pkgs.lib.runTests {
testToHexString = { testToHexString = {
expr = lib.hex.toHexString 293454837; expr = lib.hex.fromDec 293454837;
expected = "117DC3F5"; expected = "117DC3F5";
}; };
testToHexString2 = {
expr = lib.hex.fromDec 4500;
expected = "1194";
};
testToHexString3 = {
expr = lib.hex.fromDec 5942819;
expected = "5AAE23";
};
testHexToDec = {
expr = lib.hex.toDec "FF";
expected = 255;
};
testHexToDec2 = {
expr = lib.hex.toDec "333FAB333";
expected = 13756969779;
};
testCreateHexRange = { testCreateHexRange = {
expr = lib.hex.range 10 17; expr = lib.hex.range 10 17;
expected = [ "A" "B" "C" "D" "E" "F" "10" "11" ]; expected = [ "A" "B" "C" "D" "E" "F" "10" "11" ];