ABSmartly.Utils (ABSmartly v1.0.0)

Copy Markdown View Source

Utility functions for ABSmartly SDK.

Summary

Functions

Encode binary data as base64url without padding.

Choose a variant based on cumulative split probabilities.

Hash a unit identifier using MD5 and encode as base64url (no padding).

True when the value is a number.

True when the value is a plain object (a map that is not a struct).

True when the value is an asynchronous result (a Task), the Elixir counterpart of a JavaScript promise.

Convert any value to a boolean.

Convert any value to a number (integer or float). Returns nil if conversion fails.

The string form of a unit UID used for hashing. Like the JavaScript SDK (value.toFixed(0)), a numeric UID is rounded to an integer first.

The UTF-8 bytes of a string, as a binary.

Functions

base64_url_no_padding(data)

Encode binary data as base64url without padding.

iex> ABSmartly.Utils.base64_url_no_padding("te")
"dGU"

choose_variant(split, probability)

Choose a variant based on cumulative split probabilities.

Returns the index of the first variant where cumulative probability >= value.

Examples

iex> ABSmartly.Utils.choose_variant([0.0, 1.0], 0.0)
1

iex> ABSmartly.Utils.choose_variant([0.0, 1.0], 0.5)
1

iex> ABSmartly.Utils.choose_variant([0.5, 0.5], 0.0)
0

iex> ABSmartly.Utils.choose_variant([0.5, 0.5], 0.5)
1

iex> ABSmartly.Utils.choose_variant([0.33, 0.33, 0.34], 0.66)
2

hash_unit(uid)

Hash a unit identifier using MD5 and encode as base64url (no padding).

Examples

iex> ABSmartly.Utils.hash_unit("bleh@absmartly.com")
"rcH4hfF5JRm9-sOLmWKubA"

numeric?(value)

True when the value is a number.

object?(value)

True when the value is a plain object (a map that is not a struct).

promise?(arg1)

True when the value is an asynchronous result (a Task), the Elixir counterpart of a JavaScript promise.

to_boolean(arg1)

Convert any value to a boolean.

to_number(n)

Convert any value to a number (integer or float). Returns nil if conversion fails.

unit_to_string(uid)

The string form of a unit UID used for hashing. Like the JavaScript SDK (value.toFixed(0)), a numeric UID is rounded to an integer first.

iex> ABSmartly.Utils.unit_to_string(125.75)
"126"

utf8_bytes(value)

The UTF-8 bytes of a string, as a binary.