From 0af7a31d7b0b57e576087cafece1a29eae592480 Mon Sep 17 00:00:00 2001
From: Gabriel Arazas <foodogsquared@foodogsquared.one>
Date: Sat, 24 Feb 2024 10:29:59 +0800
Subject: [PATCH] bahaghari/lib/trivial: add `toYAML` and
 `toBaseDigitsWithGlyphs`

---
 subprojects/bahaghari/lib/default.nix |  2 +-
 subprojects/bahaghari/lib/trivial.nix | 25 ++++++++++++++++++++++++-
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/subprojects/bahaghari/lib/default.nix b/subprojects/bahaghari/lib/default.nix
index 65cbee0e..22569468 100644
--- a/subprojects/bahaghari/lib/default.nix
+++ b/subprojects/bahaghari/lib/default.nix
@@ -16,6 +16,6 @@ pkgs.lib.makeExtensible
       hex = callLibs ./hex.nix;
       tinted-theming = callLibs ./tinted-theming.nix;
 
-      inherit (self.trivial) importYAML;
+      inherit (self.trivial) importYAML toYAML toBaseDigitsWithGlyphs;
       inherit (self.hex) toHexString;
     })
diff --git a/subprojects/bahaghari/lib/trivial.nix b/subprojects/bahaghari/lib/trivial.nix
index 4f2a0d12..291c2e64 100644
--- a/subprojects/bahaghari/lib/trivial.nix
+++ b/subprojects/bahaghari/lib/trivial.nix
@@ -1,6 +1,8 @@
 { pkgs, lib }:
 
 {
+  inherit (pkgs.lib.generators) toYAML;
+
   /* Read YAML files into a Nix expression similar to lib.importJSON and
      lib.importTOML from nixpkgs standard library. Unlike both of them, this
      unfortunately relies on an import-from-derivation (IFD) so it isn't exactly
@@ -11,7 +13,7 @@
 
      https://pkg.go.dev/gopkg.in/yaml.v3#readme-compatibility
 
-     Type: importYAML :: path -> any
+     Type: importYAML :: Path -> any
   */
   importYAML = path:
     let
@@ -20,4 +22,25 @@
       '';
     in
       pkgs.lib.importJSON data;
+
+  /* Convert a given decimal number to a specified base digit with the set of
+     glyphs for each digit as returned from lib.toBaseDigits.
+
+     Type: toBaseDigitWithGlyphs :: Int -> Int -> Attrs -> String
+
+     Example:
+       toBaseDigitWithGlyphs 24 267 {
+          "0" = "0";
+          # ...
+          "22" = "L";
+          "23" = "M";
+          "24" = "N";
+        }
+  */
+  toBaseDigitsWithGlyphs = base: i: glyphs:
+    let
+      baseDigits = pkgs.lib.toBaseDigits i;
+      toBaseDigits = d: glyphs.${builtins.toString i};
+    in
+    pkgs.lib.concatMapStrings toBaseDigits baseDigits;
 }