### Basic Serialization Examples Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/serialization Examples demonstrating basic JSON and EDN serialization of Money objects, including minimal and full representations. ```APIDOC ## Usage examples ### Basic serialization ```clojure (require '[io.randomseed.bankster.serializers.json :as sj] '[io.randomseed.bankster.serializers.edn :as se]) ;; JSON minimal (default) (sj/money->json-map #money[12.30 PLN]) ;; => {:currency "PLN", :amount "12.30"} ;; JSON full (sj/money->json-full-map #money[12.30 PLN]) ;; => {:currency {:id "PLN", :numeric 985, :scale 2, :kind "iso/fiat", :domain "ISO-4217"}, ;; :amount "12.30"} ;; EDN minimal (se/money->edn-map #money[12.30 PLN]) ;; => {:currency :PLN, :amount 12.30M} ;; EDN tagged literal (se/money->edn-string #money[12.30 PLN]) ;; => "#money[12.30M PLN]" (se/money->edn-string #money/crypto[1.5 ETH]) ;; => "#money/crypto[1.500000000000000000M ETH]" ``` ``` -------------------------------- ### Start Interactive Development REPL Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/readme Starts a REPL for interactive development. Optionally, an nREPL server can be started with its port number stored in `.nrepl-port`. ```bash bin/repl ``` -------------------------------- ### Deserialization Examples Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/serialization Examples showing how to deserialize Money objects from JSON maps, JSON strings, and EDN strings, with options for custom registries and rounding modes. ```APIDOC ### Deserialization ```clojure (sj/json-map->money {:currency "PLN" :amount "12.30"}) ;; => #money[12.30 PLN] (sj/json-string->money "12.30 PLN") ;; => #money[12.30 PLN] (se/edn-string->money "#money[12.30M PLN]") ;; => #money[12.30 PLN] ;; With custom registry and rounding (sj/json-map->money {:currency "PLN" :amount "1.005"} {:rounding-mode java.math.RoundingMode/HALF_UP}) ;; => #money[1.01 PLN] ``` ``` -------------------------------- ### Perform Basic Money Operations Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Examples of basic money operations like addition and greater-than comparison. Requires 'io.randomseed.bankster.api.money' namespace. ```clojure (money/add #money[10 EUR] #money[5 EUR]) ;; => #money[15 EUR] (money/gt? #money[10 EUR] #money[5 EUR]) ;; => true ``` -------------------------------- ### Get Default Version (clj) Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2.registry Returns a proposed version string for a registry, based on the current date and time. ```clojure (default-version) ``` -------------------------------- ### Serialization and Deserialization Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Examples of converting monetary amounts to and from JSON and EDN formats, including options for full serialization and key filtering. ```APIDOC ## Serialization and Deserialization This section covers how to serialize and deserialize monetary amounts using JSON and EDN formats with various options. ### JSON Serialization ```clojure (require '[io.randomseed.bankster.serializers.json :as sj] '[io.randomseed.bankster.serializers.edn :as se]) ;; Minimal JSON serialization (currency ID and amount) (sj/money->json-map #money[12.30 PLN]) ;; => {:currency "PLN", :amount "12.30"} ;; Full JSON serialization (currency as nested map) (sj/money->json-full-map #money[12.30 PLN]) ;; => {:currency {:id "PLN", :numeric 985, :scale 2, :kind "iso/fiat", :domain "ISO-4217"}, ;; :amount "12.30"} ;; JSON with :full? option (sj/to-json-map #money[12.30 PLN] {:full? true}) ;; => {:currency {:id "PLN", :numeric 985, :scale 2, :kind "iso/fiat", :domain "ISO-4217"}, ;; :amount "12.30"} ;; JSON with :keys filtering (nested options for currency) (sj/money->json-full-map #money[12.30 PLN] {:keys [:amount {:currency {:keys [:id :numeric]}}]}) ;; => {:amount "12.30", :currency {:id "PLN", :numeric 985}} ;; JSON string representation (sj/money->json-string #money[12.30 PLN]) ;; => "12.30 PLN" ;; Currency serialization (minimal by default) (sj/currency->json-map #currency PLN) ;; => {:id "PLN"} (sj/currency->json-full-map #currency PLN) ;; => {:id "PLN", :numeric 985, :scale 2, :kind "iso/fiat", :domain "ISO-4217"} ;; :code-only? omits namespace for crypto currencies (sj/money->json-map #money/crypto[1.5 ETH] {:code-only? true}) ;; => {:currency "ETH", :amount "1.500000000000000000"} ``` ### EDN Serialization ```clojure ;; EDN serialization (BigDecimals, keyword IDs) (se/money->edn-map #money[12.30 PLN]) ;; => {:currency :PLN, :amount 12.30M} ;; EDN tagged literal string (se/money->edn-string #money[12.30 PLN]) ;; => "#money[12.30M PLN]" (se/money->edn-string #money/crypto[1.5 ETH]) ;; => "#money/crypto[1.500000000000000000M ETH]" ``` ### Deserialization ```clojure ;; JSON deserialization (sj/json-map->money {:currency "PLN" :amount "12.30"}) ;; => #money[12.30 PLN] ;; Note: for JSON inputs prefer string amounts (or configure your ;; JSON parser to produce BigDecimal) to avoid double-precision loss. (sj/json-text->money "{\"currency\":\"PLN\",\"amount\":12.30}") ;; => #money[12.30 PLN] (sj/json-string->money "12.30 PLN") ;; => #money[12.30 PLN] ;; EDN deserialization (se/edn-string->money "#money[12.30M PLN]") ;; => #money[12.30 PLN] ;; Deserialization with custom registry and rounding (also accepts keywords/strings) (sj/json-map->money {:currency "PLN" :amount "1.005"} {:rounding-mode :HALF_UP}) ;; => #money[1.01 PLN] ;; Rescaling during deserialization - preserve precision beyond currency's nominal scale (sj/json-map->money {:currency "PLN" :amount "12.3456"} {:rescale 4}) ;; => #money[12.3456 PLN] ; Currency has scale 4, not the registry's 2 ;; Rescaling during serialization (sj/money->json-map #money[12.30 PLN] {:rescale 4}) ;; => {:currency "PLN", :amount "12.3000"} ``` ``` -------------------------------- ### Load Registry with io.randomseed.bankster.init Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/readme Use `init/load-registry` to load a registry configuration. Optionally overlay Bankster's distribution config by setting `:keep-dist?` to true. The `load-registry!` variant installs the result as the global registry. ```clojure (require '[io.randomseed.bankster.init :as init]) ;; Load ONLY the provided config (no dist overlay). (def r1 (init/load-registry "my/app/currencies.edn")) ;; Load dist config first, then overlay user config ;; using importer/merge-registry. (def r2 (init/load-registry "my/app/currencies.edn" {:keep-dist? true :merge-opts {:verbose? true :preserve-fields [:domain :kind] :iso-like? false}})) ;; Side-effecting variant: installs the result as the global registry. (init/load-registry! "my/app/currencies.edn" {:keep-dist? true}) ``` -------------------------------- ### Create and Register Currencies Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Use `currency/new` or tagged literals to create ad-hoc currencies. Register them globally using `currency/register!` with optional country codes. Examples show creating standard, ISO, and custom currencies. ```clojure ;; front API helpers (require '[io.randomseed.bankster.api.currency :as currency]) ;; ad hoc currency creation using constructor function (currency/new :petro/USD 999 2 :COMBANK) #currency{:id :petro/USD, :domain :PETRO, :kind :COMBANK, :numeric 999, :scale 2} ;; ad-hoc currency creation using tagged literal #currency{:id :crypto/ETH :scale 18} #currency{:id :crypto/ETH, :domain :CRYPTO, :scale 18} ;; putting new currency into a global, shared registry (currency/register! (currency/new :petro/USD 9999 2 :COMBANK) :USA) #Registry[{:currencies 221, :countries 250, :version "2021022121170359"} 0x11efe93f] ;; getting currency from a global registry (currency/of :petro/USD) #currency{:id :petro/USD, :domain :PETRO, :kind :COMBANK, :numeric 9999, :scale 2} ;; registering new currency expressed as a tagged literal (currency/register! #currency{:id :crypto/AAA :scale 8}) #Registry[{:currencies 221, :countries 249, :version "2021022121170359"} 0x7eaf7a70] ;; creating an ISO currency (must have: a simple 3-letter code w/o ns and a numerical ID) (currency/new :XOX 999 2 :COMBANK) #currency{:id :XOX, :domain :ISO-4217, :kind :COMBANK, :numeric 999, :scale 2} ;; creating a strange ISO currency (forced by a namespace but w/o a numerical ID) (currency/new :ISO-4217/XOX nil 2 :COMBANK) #currency{:id :XOX, :domain :ISO-4217, :kind :COMBANK, :scale 2} ``` -------------------------------- ### Nested `:keys` Filtering Example Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/serialization Demonstrates how to use the nested `:keys` option to filter output fields, including nested structures. ```APIDOC ## Nested `:keys` Filtering ### Example ```clojure ;; Filter Money to only :amount and currency's :id/:numeric (to-json-full-map money {:keys [:amount {:currency {:keys [:id :numeric]}}]}) ;; => {:amount "12.30", :currency {:id "PLN", :numeric 985}} ``` ### Syntax - Keyword elements select that field without recursion. - Map elements `{:field-name {:keys [...]}}` select that field with nested options. ``` -------------------------------- ### Multiply Monetary Amounts by Numbers Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Example of multiplying a monetary amount by multiple numbers. Ensure 'io.randomseed.bankster.api.money' namespace is required. ```clojure ;; multiplying money by numbers (money/mul #money/crypto[5 ETH] 1 2 3 4 5) #money/crypto[600.000000000000000000 ETH] ``` -------------------------------- ### Get registry version (macro) Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2.registry Returns the version string of a registry. If no registry is provided, it attempts to use the dynamic variable `io.randomseed.bankster.registry/*default*` or the global registry. ```clojure (version*) ``` ```clojure (version* registry) ``` -------------------------------- ### Get registry version Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2.registry Returns the version string of a registry. If no registry is provided, it attempts to use the dynamic variable `io.randomseed.bankster.registry/*default*` or the global registry. ```clojure (version) ``` ```clojure (version registry) ``` -------------------------------- ### Build Documentation Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/readme Run this command to build the project's documentation. ```bash make docs ``` -------------------------------- ### Build Project Documentation Source: https://cljdoc.org/d/io.randomseed/bankster Run this command to build the project's documentation. ```bash make docs copy ``` -------------------------------- ### Prepare for Release Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/readme Run this command to prepare the project for a new release. ```bash make release ``` -------------------------------- ### Get Current Thread Information Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util Retrieves information about the current thread. ```clojure (current-thread) ``` ```clojure (current-thread-id) ``` ```clojure (current-thread-name) ``` -------------------------------- ### Build JAR File Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/readme Execute this command to build the project's JAR file. ```bash make jar ``` -------------------------------- ### Get Amount of Scalable Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2 Returns the amount of a scalable as a BigDecimal. Delegates to io.randomseed.bankster.scale/amount. ```clojure (amount x) ``` ```clojure (amount x sc) ``` ```clojure (amount x sc rounding) ``` -------------------------------- ### Build Project JAR Source: https://cljdoc.org/d/io.randomseed/bankster Run this command to build the project's JAR file. ```bash make jar copy ``` -------------------------------- ### Get Global Registry State Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2.registry Retrieves the current state of the global registry. ```clojure (state) ``` -------------------------------- ### Get Minor Unit Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.money.ops Extracts the minor unit (e.g., cents) from a monetary amount. ```clojure (minor a) ``` -------------------------------- ### Creating and Registering Currencies Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Illustrates how to create new currencies, both ad-hoc and for registration, using constructor functions and tagged literals, and how to register them into the global registry. ```APIDOC ## Creating and Registering Currencies This section demonstrates how to create and register new currencies. ### Functions * `currency/new` - Ad hoc currency creation using constructor function. * `currency/register!` - Puts a new currency into a global, shared registry. ### Examples **Ad hoc currency creation using constructor function:** ```clojure (require '[io.randomseed.bankster.api.currency :as currency]) (currency/new :petro/USD 999 2 :COMBANK) ;; #currency{:id :petro/USD, :domain :PETRO, :kind :COMBANK, :numeric 999, :scale 2} ``` **Ad-hoc currency creation using tagged literal:** ```clojure #currency{:id :crypto/ETH :scale 18} ;; #currency{:id :crypto/ETH, :domain :CRYPTO, :scale 18} ``` **Putting new currency into a global, shared registry:** ```clojure (currency/register! (currency/new :petro/USD 9999 2 :COMBANK) :USA) ;; #Registry[{:currencies 221, :countries 250, :version "2021022121170359"} 0x11efe93f] ``` **Getting currency from a global registry:** ```clojure (currency/of :petro/USD) ;; #currency{:id :petro/USD, :domain :PETRO, :kind :COMBANK, :numeric 9999, :scale 2} ``` **Registering new currency expressed as a tagged literal:** ```clojure (currency/register! #currency{:id :crypto/AAA :scale 8}) ;; #Registry[{:currencies 221, :countries 249, :version "2021022121170359"} 0x7eaf7a70] ``` **Creating an ISO currency (must have: a simple 3-letter code w/o ns and a numerical ID):** ```clojure (currency/new :XOX 999 2 :COMBANK) ;; #currency{:id :XOX, :domain :ISO-4217, :kind :COMBANK, :numeric 999, :scale 2} ``` **Creating a strange ISO currency (forced by a namespace but w/o a numerical ID):** ```clojure (currency/new :ISO-4217/XOX nil 2 :COMBANK) ;; #currency{:id :XOX, :domain :ISO-4217, :kind :COMBANK, :scale 2} ``` ``` -------------------------------- ### Get Major Unit Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.money.ops Extracts the major unit (e.g., dollars) from a monetary amount. ```clojure (major a) ``` -------------------------------- ### Create Character Set from Ranges Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util Returns a set of characters defined by start and stop character ranges. ```clojure (char-ranges->set & ranges) ``` -------------------------------- ### Using :full? Option Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/serialization Demonstrates how to use the `:full?` option with `to-json-map` for both Money objects and Currency objects to include detailed information. ```APIDOC ### Using :full? option ```clojure ;; Via to-json-map with :full? true (sj/to-json-map #money[12.30 PLN] {:full? true}) ;; => {:currency {:id "PLN", ...}, :amount "12.30"} ;; Via protocol (sj/to-json-map (currency/of :PLN) {:full? true}) ;; => {:id "PLN", :numeric 985, :scale 2, :kind "iso/fiat", :domain "ISO-4217"} ``` ``` -------------------------------- ### Resolve Currencies and Create Money Values Source: https://cljdoc.org/d/io.randomseed/bankster Demonstrates how to resolve currency codes using the default registry and create Money values. Strict variants throw errors on failure. ```clojure ;; currency code resolution using the default registry (strict) (def usd (c/resolve "USD")) (def pln (c/resolve "PLN")) ;; create Money values (strict) (def a (m/money 12.34M usd)) (def b (m/money 10M pln)) ``` -------------------------------- ### Get Default Registry (clj) Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2.registry Returns the default registry, honoring the `io.randomseed.bankster.registry/*default*` dynamic variable. ```clojure (default) ``` -------------------------------- ### Get Monetary Amount as BigDecimal Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Retrieves the monetary amount as a BigDecimal using 'api/amount'. Requires 'io.randomseed.bankster.api' namespace. ```clojure ;; NOTE: `api/amount` is an alias for `scale/amount`. ;; NOTE: `api/scale` returns scale (Money amount scale / Currency nominal scale). (api/amount 12.30M) ;; => 12.30M ; BigDecimal ``` -------------------------------- ### Currency and Money Constructors Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/bankster-contracts Quick reference for creating Currency and Money values using various constructor functions. ```APIDOC ## Currency Constructors ### `currency/new` - **Description**: Create an ad-hoc `Currency` from ID/fields. - **Strictness**: hard - **Notes**: May throw on invalid domain/namespace mismatch. ### `currency/map->new` - **Description**: Create an ad-hoc `Currency` from a map. - **Strictness**: hard ### `currency/of` (macro) - **Description**: Resolve via registry or create ad-hoc from a map. - **Strictness**: hard - **Notes**: Throws on missing when resolving. ### `currency/unit` - **Description**: Strict registry lookup. - **Strictness**: hard - **Notes**: Throws on missing. ### `currency/unit-try` - **Description**: Soft registry lookup; returns `nil` on failure. - **Strictness**: soft ### `currency/resolve` - **Description**: Registry lookup that returns `nil` when missing. - **Strictness**: soft ### `currency/attempt` - **Description**: Soft coercion; may return ad-hoc `Currency` without consulting registry. - **Strictness**: soft ### `currency/of-id` - **Description**: Strict lookup by currency ID. - **Strictness**: hard ## Money Constructors ### `money/value` - **Description**: Construct `Money`. - **Strictness**: hard - **Notes**: May throw on missing currency or required rounding. ### `money/of` (macro) - **Description**: Macro constructor. - **Strictness**: hard - **Notes**: Throws on missing currency/rounding. ### `money/of-major` (macro) - **Description**: Like `money/of`, amount as the major part. - **Strictness**: hard ### `money/of-minor` (macro) - **Description**: Like `money/of`, amount as the minor part. - **Strictness**: hard ### `money/major-value` - **Description**: Function constructor for major part. - **Strictness**: hard ### `money/minor-value` - **Description**: Function constructor for minor part. - **Strictness**: hard ### `money/of-map` - **Description**: Construct `Money` from a map. - **Strictness**: hard - **Notes**: Throws on invalid input. ``` -------------------------------- ### Get Hierarchy by Key Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2.registry Retrieves a hierarchy using its key from a registry. Defaults to the dynamic or global registry if not specified. ```clojure (hierarchy k) ``` ```clojure (hierarchy k registry) ``` -------------------------------- ### Inferred Keyword Get from Collection Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util Retrieves a value from a collection using a keyword, attempting both qualified and unqualified forms. ```clojure (inferred-get coll k) ``` ```clojure (inferred-get coll k default) ``` -------------------------------- ### Get Random Integer Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util Returns a random integer up to n. Optionally uses a provided random number generator. ```clojure (get-rand-int n) ``` ```clojure (get-rand-int n rng) ``` -------------------------------- ### new Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2.registry Creates a new registry. Accepts base maps and builds derived index maps during initialization. Even the arity that accepts a Registry map/record ignores any derived index fields (they are recomputed during initialization). ```APIDOC ## new ### Description Creates a new registry with optional initial configuration. ### Parameters #### Path Parameters - **m** (map, optional) - Base maps to initialize the registry. - **cur-id->cur** (optional) - Configuration for current ID to current value mapping. - **ctr-id->cur** (optional) - Configuration for counter ID to current value mapping. - **cur-id->localized** (optional) - Configuration for current ID to localized value mapping. - **cur-id->traits** (optional) - Configuration for current ID to traits mapping. - **cur-id->weight** (optional) - Configuration for current ID to weight mapping. - **cur-hierarchies** (optional) - Configuration for current hierarchies. - **version** (optional) - The version of the registry. ### Returns A new registry instance. ``` -------------------------------- ### Get Random Element from Vector Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util Returns a random element from a vector. Optionally uses a provided random number generator. ```clojure (get-rand-nth v) ``` ```clojure (get-rand-nth v rng) ``` -------------------------------- ### Deploy to Clojars Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/readme Execute this command to deploy the project artifacts to Clojars. ```bash make deploy ``` -------------------------------- ### Generic, Polymorphic Operations Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Demonstrates various ways to use the `api/scale` and related functions for determining and applying scaling factors to currencies and amounts. ```APIDOC ## Generic, Polymorphic Operations This section showcases the `api/scale` function and its related utilities for handling monetary values. ### Usage Examples ```clojure ;; front API helpers (require '[io.randomseed.bankster.api :as api] '[io.randomseed.bankster.api.money :as money] '[io.randomseed.bankster.api.currency :as currency]) ;; Get scale for a currency (api/scale #currency PLN) ;; => 2 (api/scale #currency crypto/ETH) ;; => 18 ;; Get scale for a numeric amount (defaults to a generic scale) (api/scale 123.45) ;; => 2 ;; Get scale for a money amount (api/scale #money[100 EUR]) ;; => 2 ;; Get scale for a currency code string (api/scale :GBP) ;; => 2 ;; Low-level scale of a currency (can be -1 for auto-scaled) (api/scale :XXX) ;; => -1 ;; Scale of the amount within a money object (api/scale #money[12.34567 XXX]) ;; => 5 ; current scale ;; Nominal scale of the currency (currency/scale #money[12.34567 XXX]) ;; => -1 ; auto-scaled ;; Check if a currency is auto-scaled (currency/auto-scaled? :XXX) ;; => true (currency/auto-scaled? #money[12.34567 XXX]) ;; => true ;; Check if a value is auto-scaled (generic API) (api/auto-scaled? :XXX) ;; => true (api/auto-scaled? #money[12.34567 XXX]) ;; => false ; scale of the amount, not the currency ;; Apply scaling to a money amount (use with caution) (api/scale-apply #money[10 USD] 8) ;; => #money[10.00000000 USD] ;; Apply scaling to a currency (use with caution) (api/scale-apply #currency USD 8) ;; => #currency{:id :USD, :domain :ISO-4217, :kind :iso/fiat, :numeric 840, :scale 8} ;; Rescale a money amount (money/rescale #money[10 USD] 8) ;; => #money[10.00000000 USD] ;; Unary variant of money/rescale (rescales back to nominal scale) (money/rescale (money/rescale #money[10 USD] 8)) ;; => #money[10.00 USD] ;; Get the amount part of a money object (api/amount #money[108.11 CHF]) ;; => 108.11M ;; Get the integer part of a money amount (scale/integer #money[108.11 CHF]) ;; => 108M ;; Get the fractional part of a money amount (scale/fractional #money[108.11 CHF]) ;; => 11M ;; Check if a currency is ISO standard (currency/iso? #money[1 GBP]) ;; => true ;; Get the currency code (currency/code #money[1 GBP]) ;; => "GBP" ``` ``` -------------------------------- ### Bankster Quick Equality Macro Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed Documentation for the quick equality macro provided by the Bankster library. ```APIDOC ## io.randomseed.bankster.util.qe Quick equality macro. ### Functions - `q=` ``` -------------------------------- ### ext* Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2.registry Macro version of `ext` that returns the extra data map of a registry. ```APIDOC ## ext* ### Description Macro version of `ext`. Returns extra data map of a registry. If the registry is not given the dynamic variable `io.randomseed.bankster.registry/*default*` is tried. If it is not set, current state of a global registry is used instead. ### Method (ext*) (ext* registry) (ext* k registry) ### Endpoint N/A (Macro call) ### Parameters - **registry** (any, optional): The registry to inspect. Defaults to dynamic or global registry. - **k** (keyword, optional): A specific key to retrieve extra data for. ### Request Example ```clojure (ext*) (ext* my-registry) (ext* :some-key my-registry) ``` ### Response #### Success Response - extra-data: A map containing extra data for the registry or a specific key. ### Response Example ```clojure ; Example response structure for (ext*) {:key1 "value1", :key2 "value2"} ; Example response structure for (ext* :some-key my-registry) "specific-value" ``` ``` -------------------------------- ### char-ranges->set Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util Returns a set of characters defined as a collection of collections with start and stop character, e.g.: [\A \Z][\0 \9] ```APIDOC ## char-ranges->set ### Description Returns a set of characters defined as a collection of collections with start and stop character, e.g.: [\A \Z][\0 \9] ### Method clj ### Endpoint (char-ranges->set & ranges) ``` -------------------------------- ### Create New Registry Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2.registry Initializes a new registry, optionally accepting base maps and recomputing derived index maps. ```clojure (new) ``` ```clojure (new m) ``` ```clojure (new cur-id->cur ctr-id->cur cur-id->localized cur-id->traits cur-id->weight) ``` ```clojure (new cur-id->cur ctr-id->cur cur-id->localized cur-id->traits cur-id->weight cur-hierarchies) ``` ```clojure (new cur-id->cur ctr-id->cur cur-id->localized cur-id->traits cur-id->weight cur-hierarchies version) ``` -------------------------------- ### Get Currency Symbol and Name Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Retrieves currency symbols and names for a given locale using 'currency/symbol' and 'currency/name'. Requires 'io.randomseed.bankster.api.currency' namespace. ```clojure (currency/symbol :USD :en_US) ;; => "$" (currency/name :EUR :en_US) ;; => "Euro" ``` -------------------------------- ### Get Currency ID and Code as String Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Converts currency IDs to string representations using 'currency/id-str' and 'currency/code-str'. Requires 'io.randomseed.bankster.api.currency' namespace. ```clojure (currency/id-str :crypto/eth) ;; => "crypto/ETH" (currency/code-str :crypto/eth) ;; => "ETH" ``` -------------------------------- ### Creating Monetary Amounts Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Demonstrates various ways to create monetary amounts using the `money/of` macro and tagged literals, supporting different input formats for currency and amount. ```APIDOC ## Creating Monetary Amounts This section covers the creation of monetary amounts using the `money/of` macro and tagged literals. ### Using `money/of` macro: - With keyword ID and amount: ```clojure (money/of :EUR 25) ;; => #money[25.00 EUR] ``` - With amount as the first argument and keyword ID: ```clojure (money/of 25 :EUR) ;; => #money[25.00 EUR] ``` - With joint keyword ID (underscore or no separator): ```clojure (money/of :25_EUR) ;; => #money[25.00 EUR] (money/of :25EUR) ;; => #money[25.00 EUR] ``` - With unquoted symbolic ID and amount: ```clojure (money/of EUR 25) ;; => #money[25.00 EUR] (money/of EUR_25) ;; => #money[25.00 EUR] (money/of EUR25) ;; => #money[25.00 EUR] ``` - With namespaced keyword ID and amount: ```clojure (money/of crypto/BTC 10.1) ;; => #money/crypto[10.10000000 BTC] ``` - With currency code and amount: ```clojure (money/of BTC 10.1) ;; => #money/crypto[10.10000000 BTC] ``` - With string representation: ```clojure (money/of "100EUR") ;; => #money[100 EUR] ``` ### Using tagged literals: - Basic tagged literal with currency symbol: ```clojure #money EUR ;; => #money[0.00 EUR] #money/crypto ETH ;; => #money/crypto[0.000000000000000000 ETH] ``` - Tagged literal with amount and currency: ```clojure #money [PLN 2.50] ;; => #money[2.50 PLN] #money/crypto [1.31337 ETH] ;; => #money/crypto[1.313370000000000000 ETH] ``` - Tagged literal with currency code: ```clojure #money [1.31337 ETH] ;; => #money/crypto[1.313370000000000000 ETH] ``` - Tagged literal with namespace and amount first: ```clojure #money/crypto [BTC 1.31337] ;; => #money/crypto[1.31337000 BTC] ``` - Composed amounts and currencies: ```clojure #money EUR100 ;; => #money[100 EUR] #money :100_EUR ;; => #money[100 EUR] #money :100EUR ;; => #money[100 EUR] #money "100 EUR" ;; => #money[100 EUR] ``` ### Using default currency: - With `currency/with`: ```clojure (currency/with EUR (money/of 1000)) ;; => #money[1000.00 EUR] ``` - With `money/with-currency` (alias): ```clojure (money/with-currency EUR (money/of 1000)) ;; => #money[1000.00 EUR] ``` ``` -------------------------------- ### Get Current Rounding Mode Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2 Retrieves the current rounding mode configured for the library. An optional default can be provided if no mode is currently set. ```clojure (rounding-mode)copy ``` ```clojure (rounding-mode default)copy ``` -------------------------------- ### Create Money from Registry Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Creates a monetary amount from a registry using 'money/of-registry'. Requires 'io.randomseed.bankster.api' and 'io.randomseed.bankster.api.money' namespaces. ```clojure (money/of-registry (api/default-registry) #money[10 EUR]) ;; => #money[10.00 EUR] ``` -------------------------------- ### Get Default Registry in Bankster API Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2 Retrieves the default currency registry. This function honors the global or dynamically bound default registry. ```clojure (default-registry)copy ``` -------------------------------- ### Set Default Currency with `currency/with` or `money/with-currency` Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Establish a default currency for a lexical context using `currency/with` or its alias `money/with-currency`. Subsequent `money/of` calls will use this default if no currency is specified. ```clojure ;; using default currency in a lexical context (currency/with EUR (money/of 1000)) #money[1000.00 EUR] ``` ```clojure ;; using default currency in a lexical context (alias for the above) (money/with-currency EUR (money/of 1000)) #money[1000.00 EUR] ``` -------------------------------- ### inferred-get Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util Just like the get function but if the keyword is namespace-qualified it first attempts to look for the value associated with it. If that fails it uses the variant of the keyword without any namespace. ```APIDOC ## inferred-get ### Description Just like the get function but if the keyword is namespace-qualified it first attempts to look for the value associated with it. If that fails it uses the variant of the keyword without any namespace. ### Method clj ### Endpoint (inferred-get coll k) (inferred-get coll k default) ``` -------------------------------- ### Currency Construction Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/bankster-contracts Functions for creating new currency instances, including canonicalization of IDs and potential inference of the `:domain`. ```APIDOC ## Currency Construction ### Description Functions to create new currency instances. They handle ID canonicalization (uppercase name, preserving namespace except for `ISO-4217`) and may infer the `:domain` as `:ISO-4217` under typical conditions. The `:weight` defaults to 0. ### Functions - `currency/new-currency` - `currency/new` - `currency/map->new` ``` -------------------------------- ### Money Division (by Money) Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Shows how to divide monetary amounts by other monetary amounts of the same currency. ```APIDOC ## Money Division (by Money) ### Description Divides monetary amounts by other monetary amounts of the same currency. ### Method `money/div` ### Example ```clojure (require '[io.randomseed.bankster.api.money :as money]) (money/div #money/crypto[5 BTC] #money/crypto[2 BTC]) ;; => 2.5M ``` ``` -------------------------------- ### get-rand-nth Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util Returns a random element of the given vector. When a second argument is present, it should be an instance of a random number generator used to get the random position. ```APIDOC ## get-rand-nth ### Description Returns a random element of the given vector. When a second argument is present, it should be an instance of a random number generator used to get the random position. ### Method clj ### Endpoint (get-rand-nth v) (get-rand-nth v rng) ``` -------------------------------- ### Get Scale of Monetary Amount or Currency Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Retrieves the scale of a monetary amount or currency using 'api/scale'. For auto-scaled currencies, it reflects the amount's scale. ```clojure (api/scale #money[12.30 EUR]) ;; => 2 (api/scale :XAU) ;; => -1 ; auto-scaled currency ``` -------------------------------- ### Generic Operations with Bankster API Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/sneak-peeks Demonstrates using the Bankster API for generic, polymorphic operations like scaling monetary amounts and currencies. Requires importing API, money, and currency modules. ```clojure (require '[io.randomseed.bankster.api :as api] '[io.randomseed.bankster.api.money :as money] '[io.randomseed.bankster.api.currency :as currency]) (api/scale #currency PLN) 2 (api/scale #currency crypto/ETH) 18 (api/scale 123.45) 2 (api/scale #money[100 EUR]) 2 (api/scale :GBP) 2 ``` ```clojure ;; scale of the currency (low-level) (api/scale :XXX) -1 ``` ```clojure ;; scale of the amount (api/scale #money[12.34567 XXX]) 5 ; current scale ``` ```clojure ;; nominal scale of the currency (currency/scale #money[12.34567 XXX]) -1 ; auto-scaled ``` ```clojure (currency/auto-scaled? :XXX) true (currency/auto-scaled? #money[12.34567 XXX]) true (api/auto-scaled? :XXX) true (api/auto-scaled? #money[12.34567 XXX]) false ; scale of the amount, not the currency ``` ```clojure (api/scale-apply #money[10 USD] 8) ;; use with caution #money[10.00000000 USD] (api/scale-apply #currency USD 8) ;; use with caution #currency{:id :USD, :domain :ISO-4217, :kind :iso/fiat, :numeric 840, :scale 8} ``` ```clojure (money/rescale #money[10 USD] 8) #money[10.00000000 USD] ``` ```clojure ;; unary variant of money/rescale ;; rescales back to nominal scale (money/rescale (money/rescale #money[10 USD] 8)) #money[10.00 USD] ``` ```clojure (api/amount #money[108.11 CHF]) 108.11M (scale/integer #money[108.11 CHF]) 108M (scale/fractional #money[108.11 CHF]) 11M (currency/iso? #money[1 GBP]) true (currency/code #money[1 GBP]) "GBP" ``` -------------------------------- ### Front API Namespaces Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/bankster-contracts Overview of the main front-facing API namespaces. ```APIDOC ## Front API Namespaces ### `io.randomseed.bankster.api` - **`amount`** -> `scale/amount` - **`scale`** -> scale for `Money` and `Currency` (Money amount scale / Currency nominal scale; `-1` for auto-scaled currencies). ### `io.randomseed.bankster.api.registry` - **Description**: Registry helpers. - **Functions**: `with`, `state`, `hierarchy-derive`, `hierarchy-derive!`, `or-default`. - **`or-default`**: Treats `nil` and `true` as "use default registry". ### `io.randomseed.bankster.api.money` - **Description**: Money-only arithmetic and comparison operations. - **Arithmetic**: `add`, `sub`, `mul`, `div`. - **Comparisons/Predicates**: `eq?`, `ne?`, `gt?`, `ge?`, `lt?`, `le?`, `compare`, `pos?`, `neg?`, `zero?`. ### `io.randomseed.bankster.api.currency` - **Description**: Currency helpers (unprefixed function set). ### `io.randomseed.bankster.api.ops` - **Description**: Operator namespace for intentional `:refer :all`. ### `io.randomseed.bankster.api.v2` - **Description**: Frozen API for major version 2. Mirrors `io.randomseed.bankster.api` for the 2.x line. ``` -------------------------------- ### Construct Money with Amount First Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2 Use this constructor for creating money values when the amount is the primary piece of information. It supports various overloads for specifying currency, rounding, and registry resolution. ```clojure (money)copy ``` ```clojure (money amount)copy ``` ```clojure (money amount currency)copy ``` ```clojure (money amount currency rounding)copy ``` ```clojure (money amount currency registry)copy ``` ```clojure (money amount currency rounding registry)copy ``` -------------------------------- ### gen-digits Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util Generates the given number of random digits and converts all into a single string. When a second argument is present, it should be an instance of a random number generator used to get the digits. ```APIDOC ## gen-digits ### Description Generates the given number of random digits and converts all into a single string. When a second argument is present, it should be an instance of a random number generator used to get the digits. ### Method clj ### Endpoint (gen-digits num) (gen-digits num rng) ``` -------------------------------- ### Get Currency or Money Info with Bankster API Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2 Retrieves an information map for a given currency or money value. It delegates to specific functions based on the input type and supports registry lookups. ```clojure (info x)copy ``` ```clojure (info x registry)copy ``` ```clojure (info x locale registry)copy ``` -------------------------------- ### Money API - Creation and Parsing Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/doc/bankster-contracts Functions for creating and parsing Money values, including constructors, macros for ergonomic creation, and low-level parsers. ```APIDOC ## Money API (`io.randomseed.bankster.money`) ### Creation / Parsing - `money/value` (Accountable) - the primary constructor function. - `money/of`, `money/of-major`, `money/of-minor` (macro) - ergonomic creation. - `money/major-value`, `money/minor-value` - creation via major/minor parts. - `money/parse`, `money/parse-major`, `money/parse-minor` - internal parsers (public, but mostly low-level). - `money/of-registry` - forces the currency in Money to come from the given registry (and aligns scale). ``` -------------------------------- ### Get Scale of Money or Currency Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.api.v2 Returns the scale for Money and Currency values. For Money, it's the amount's scale. For Currency, it's the nominal scale (or -1 for auto-scaled). For other types, it delegates to `io.randomseed.bankster.scale/of`. ```clojure (scale x)copy ``` -------------------------------- ### Quick Equality Macro (q=) Source: https://cljdoc.org/d/io.randomseed/bankster/2.2.4/api/io.randomseed.bankster.util.qe Compares two forms using `clojure.core/identical?` first. If that returns false, it then uses `clojure.core/=` for comparison. This macro is useful for efficient equality checks. ```clojure (q= a b) ```