Liking cljdoc? Tell your friends :D

brazilian-utils.helpers

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
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
raw docstring

char->digitclj/s

(char->digit c)

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

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
sourceraw docstring

check-digitclj/s

(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:

  • 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

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
sourceraw docstring

digits->intsclj/s

(digits->ints s)

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)

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)
sourceraw docstring

extract-date-partsclj/s

(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"}
sourceraw docstring

http-getclj/s

(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}
sourceraw docstring

mod-large-numberclj/s

(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
sourceraw docstring

only-numbersclj/s

(only-numbers value)

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) ;; ""

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) ;; ""
sourceraw docstring

parse-intclj/s

(parse-int s)

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

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
sourceraw docstring

random-digitsclj/s

(random-digits n)

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"

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"
sourceraw docstring

repeated-digits?clj/s

(repeated-digits? text)
(repeated-digits? text expected-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)

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)
sourceraw docstring

safe-callclj/s

(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)
sourceraw docstring

slice-safeclj/s

(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:

  • 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)

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)
sourceraw docstring

split-and-rejoinclj/s

(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"
sourceraw docstring

sum-digitsclj/s

(sum-digits s)

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

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
sourceraw docstring

validate-string-formatclj/s

(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)
sourceraw docstring

weighted-sumclj/s

(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.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close