Shared helper functions used across brazilian-utils modules.
Provides common utility functions for string manipulation and validation used by other modules in the library.
Functions:
Shared helper functions used across brazilian-utils modules. Provides common utility functions for string manipulation and validation used by other modules in the library. Functions: - only-numbers: Extract digits from strings - repeated-digits?: Check if string has all same digits - char->digit: Convert digit character to numeric value - http-get: Make HTTP GET requests
(char->digit c)Converts a character digit (0-9) to its numeric value.
Arguments:
Returns the numeric value (0-9) as an integer.
Example: (char->digit " 5 ") ;; => 5 (char->digit \5) ;; => 5
Converts a character digit (0-9) to its numeric value. Arguments: - c: A character or single-character string Returns the numeric value (0-9) as an integer. Example: (char->digit " 5 ") ;; => 5 (char->digit \5) ;; => 5
(check-digit digits weights)(check-digit digits
weights
{:keys [char->val threshold stringify?]
:or {char->val char->digit threshold 2 stringify? false}})Computes check digit using modulo 11 with given weights.
Arguments:
Returns the digit (int or string) or nil if weight count does not match.
Example: (check-digit "12000000" [9 8 7 6 5 4 3 2]) ;; => 8 (check-digit "11004249" [1 3 4 5 6 7 8 10]) ;; => 0
Computes check digit using modulo 11 with given weights. Arguments: - digits: String or sequence of digits - weights: Vector of weights (e.g., [9 8 7 6 5 4 3 2]) - options: Optional map with: - :char->val (default char->digit) - function to convert char to int - :threshold (default 2) - remainder < threshold returns 0, else 11 - remainder - :stringify? (default false) - return as string instead of int Returns the digit (int or string) or nil if weight count does not match. Example: (check-digit "12000000" [9 8 7 6 5 4 3 2]) ;; => 8 (check-digit "11004249" [1 3 4 5 6 7 8 10]) ;; => 0
(digits->ints s)Converts a string of digit characters to a sequence of integers.
Arguments:
Returns a lazy sequence of integers.
Example: (digits->ints "12345") ;; => (1 2 3 4 5) (digits->ints "000") ;; => (0 0 0)
Converts a string of digit characters to a sequence of integers. Arguments: - s: String containing digit characters Returns a lazy sequence of integers. Example: (digits->ints "12345") ;; => (1 2 3 4 5) (digits->ints "000") ;; => (0 0 0)
(extract-date-parts date format)Extracts day, month, and year from a date string.
Args: date (string): Date string format (keyword): Either :iso (YYYY-MM-DD) or :brazilian (DD/MM/YYYY)
Returns: Map with :day, :month, :year keys, or nil if invalid
Examples: (extract-date-parts "2024-01-15" :iso) ;; {:year "2024", :month "01", :day "15"} (extract-date-parts "15/01/2024" :brazilian) ;; {:day "15", :month "01", :year "2024"}
Extracts day, month, and year from a date string.
Args:
date (string): Date string
format (keyword): Either :iso (YYYY-MM-DD) or :brazilian (DD/MM/YYYY)
Returns:
Map with :day, :month, :year keys, or nil if invalid
Examples:
(extract-date-parts "2024-01-15" :iso) ;; {:year "2024", :month "01", :day "15"}
(extract-date-parts "15/01/2024" :brazilian) ;; {:day "15", :month "01", :year "2024"}(http-get url)Makes an HTTP GET request to the given URL.
Args: url (string): The URL to request
Returns: A map with the response body on success, or an error map on failure. Success: {:status 200, :body response-data} Error: {:error error-message}
Makes an HTTP GET request to the given URL.
Args:
url (string): The URL to request
Returns:
A map with the response body on success, or an error map on failure.
Success: {:status 200, :body response-data}
Error: {:error error-message}(mod-large-number num-str divisor)Calculates modulo for very large numbers in both Clojure and ClojureScript. Handles BigInt conversion for cross-platform compatibility.
Args: num-str - Number as string (for big integer support) divisor - The divisor for modulo operation
Returns: The remainder of num-str divided by divisor
Examples: (mod-large-number "12345678901234567890" 97) ;; => 1 (mod-large-number "98765432109876543210" 11) ;; => 5
Calculates modulo for very large numbers in both Clojure and ClojureScript. Handles BigInt conversion for cross-platform compatibility. Args: num-str - Number as string (for big integer support) divisor - The divisor for modulo operation Returns: The remainder of num-str divided by divisor Examples: (mod-large-number "12345678901234567890" 97) ;; => 1 (mod-large-number "98765432109876543210" 11) ;; => 5
(only-numbers value)Removes all non-numeric characters from a string.
Arguments:
Returns a string containing only digits (0-9).
Example: (only-numbers "123.456.789-00") ;; "12345678900" (only-numbers "01310-100") ;; "01310100" (only-numbers nil) ;; ""
Removes all non-numeric characters from a string. Arguments: - value: Input to extract only numbers from Returns a string containing only digits (0-9). Example: (only-numbers "123.456.789-00") ;; "12345678900" (only-numbers "01310-100") ;; "01310100" (only-numbers nil) ;; ""
(parse-int s)Parses a string to an integer in a cross-platform way.
Arguments:
Returns the integer value or nil if parsing fails.
Example: (parse-int "123") ;; => 123 (parse-int "42") ;; => 42
Parses a string to an integer in a cross-platform way. Arguments: - s: String to parse Returns the integer value or nil if parsing fails. Example: (parse-int "123") ;; => 123 (parse-int "42") ;; => 42
(random-digits n)Generates a string of random digits.
Arguments:
Returns a string of n random digits.
Example: (random-digits 5) ;; => "38291" (random-digits 3) ;; => "742"
Generates a string of random digits. Arguments: - n: Number of digits to generate Returns a string of n random digits. Example: (random-digits 5) ;; => "38291" (random-digits 3) ;; => "742"
(repeated-digits? text)(repeated-digits? text expected-length)Checks if a string contains only repeated digits of a specified length.
Arguments:
Returns true if all digits are the same, false otherwise.
Example:
(repeated-digits? "00000000" 8) ;; true
(repeated-digits? "00000-000" 8) ;; true
(repeated-digits? "11111111") ;; true (any length)
(repeated-digits? "01310100") ;; false
(repeated-digits? "000" 8) ;; false (wrong length)
Checks if a string contains only repeated digits of a specified length. Arguments: - value: Input to check (will be normalized to remove non-digits) - length: Expected length of digits (optional, defaults to any length >= 1) Returns true if all digits are the same, false otherwise. Example: (repeated-digits? "00000000" 8) ;; true (repeated-digits? "00000-000" 8) ;; true (repeated-digits? "11111111") ;; true (any length) (repeated-digits? "01310100") ;; false (repeated-digits? "000" 8) ;; false (wrong length)
(safe-call f default-value)Safely executes a function, returning a default value on any exception.
Works cross-platform with both Clojure and ClojureScript.
Args: f (function): Zero-argument function to execute default-value: Value to return if exception occurs
Returns: Result of (f) on success, or default-value on error
Examples: (safe-call #(get-holidays 2024) {:error "fallback"}) (safe-call #(is-holiday? "2024-01-01") false) (safe-call #(parse-long "123") nil)
Safely executes a function, returning a default value on any exception.
Works cross-platform with both Clojure and ClojureScript.
Args:
f (function): Zero-argument function to execute
default-value: Value to return if exception occurs
Returns:
Result of (f) on success, or default-value on error
Examples:
(safe-call #(get-holidays 2024) {:error "fallback"})
(safe-call #(is-holiday? "2024-01-01") false)
(safe-call #(parse-long "123") nil)(slice-safe s start end)Safe substring that handles bounds correctly.
Returns substring from start to end, handling cases where indices exceed string length. Returns nil if start is beyond string length.
Arguments:
Returns substring or nil if start exceeds string length.
Example: (slice-safe "hello" 0 3) ;; "hel" (slice-safe "hello" 2 10) ;; "llo" (handles overflow) (slice-safe "hello" 10 15) ;; nil (start beyond length)
Safe substring that handles bounds correctly. Returns substring from start to end, handling cases where indices exceed string length. Returns nil if start is beyond string length. Arguments: - s: String to slice - start: Start index (inclusive) - end: End index (exclusive) Returns substring or nil if start exceeds string length. Example: (slice-safe "hello" 0 3) ;; "hel" (slice-safe "hello" 2 10) ;; "llo" (handles overflow) (slice-safe "hello" 10 15) ;; nil (start beyond length)
(split-and-rejoin s split-positions new-separator)Splits a string by positions and rejoins with new separator.
Args: s (string): String to process split-positions (vector): Indices to split at (e.g., [4 7] for YYYY-MM-DD) new-separator (string): New separator to use when rejoining
Returns: Reformatted string or nil if invalid
Examples: (split-and-rejoin "20240115" [4 6] "-") ;; "2024-01-15" (split-and-rejoin "15012024" [2 4] "/") ;; "15/01/2024"
Splits a string by positions and rejoins with new separator. Args: s (string): String to process split-positions (vector): Indices to split at (e.g., [4 7] for YYYY-MM-DD) new-separator (string): New separator to use when rejoining Returns: Reformatted string or nil if invalid Examples: (split-and-rejoin "20240115" [4 6] "-") ;; "2024-01-15" (split-and-rejoin "15012024" [2 4] "/") ;; "15/01/2024"
(sum-digits s)Sums all digit characters in a string.
Arguments:
Returns the sum of all digits as an integer.
Example: (sum-digits "123") ;; => 6 (sum-digits "99") ;; => 18
Sums all digit characters in a string. Arguments: - s: String containing digit characters Returns the sum of all digits as an integer. Example: (sum-digits "123") ;; => 6 (sum-digits "99") ;; => 18
(validate-string-format s length separator-char separator-positions)Validates if a string matches a specific pattern defined by length, separator positions, and digit positions.
Args: s (string): String to validate length (int): Expected total length separator-char (char): Expected separator character (e.g., - or /) separator-positions (vector): Indices where separators should appear
Returns: true if format matches, false otherwise
Examples: (validate-string-format "2024-01-15" 10 - [4 7]) ;; true (ISO date) (validate-string-format "15/01/2024" 10 / [2 5]) ;; true (Brazilian date) (validate-string-format "01310-100" 9 - [5]) ;; true (CEP)
Validates if a string matches a specific pattern defined by length, separator positions, and digit positions. Args: s (string): String to validate length (int): Expected total length separator-char (char): Expected separator character (e.g., \- or \/) separator-positions (vector): Indices where separators should appear Returns: true if format matches, false otherwise Examples: (validate-string-format "2024-01-15" 10 \- [4 7]) ;; true (ISO date) (validate-string-format "15/01/2024" 10 \/ [2 5]) ;; true (Brazilian date) (validate-string-format "01310-100" 9 \- [5]) ;; true (CEP)
(weighted-sum digits weights)(weighted-sum digits weights char->val)Computes the weighted sum of digit characters.
digits - string/seq of characters to convert weights - either a descending start weight number (e.g., 10) or a seq of weights char->val - optional fn to convert each char to numeric value (defaults to char->digit)
Returns the integer sum or nil if weight count does not match digit count.
Computes the weighted sum of digit characters. digits - string/seq of characters to convert weights - either a descending start weight number (e.g., 10) or a seq of weights char->val - optional fn to convert each char to numeric value (defaults to char->digit) Returns the integer sum or nil if weight count does not match digit count.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |