ContextID is an Elixir package that provides a simple yet flexible way to generate custom, context-rich identifiers. It allows users to specify a custom prefix for IDs, enhancing readability and making them more meaningful in various applications, you get similar uniqueness to a uuidv4 but now with an element of context at the front given your prefix.
Here are some sample IDs that your package could generate with various prefixes and lengths:
- With prefix
acct
and a length of 8 bytes:acct_9b545ad85399e20e
- With prefix
id
and a length of 16 bytes:id_a53eb7a0ccfda31b1df4776ba1b11124
- With prefix
acct_number
and a length of 4 bytes:acct_number_3b63b3bc
These examples are simply meant to illustrate the flexibility and utility of the ContextID
package in generating readable, context-specific identifiers.
- Custom Prefixes for IDs: Generate IDs with user-defined prefixes, enhancing readability and context awareness.
- Configurable ID Length: Tailor the length of your generated IDs to suit your specific requirements.
- Streamlined Usage: Offers a straightforward and user-friendly interface, making it easy to integrate and use in any Elixir project.
- No External Dependencies: Built exclusively using Elixir's standard library, ensuring a lightweight and efficient solution without the need for additional dependencies.
Add context_id
to your list of dependencies in mix.exs
:
def deps do
[
{:context_id, "~> 0.1.0"}
]
end
Generating a custom ID is straightforward:
# Using the main module
id = ContextID.generate_id("prefix", 16)
# Using the shortcut alias
id = Cid.generate_id("prefix", 16)
Generate an ID with a custom prefix:
ContextID.generate_id("user", 10)
# => "user_18165fe7c154341e861c"
Generate an ID with the default settings:
Cid.generate_id()
# => "id_a53eb7a0ccfda31b1df4776ba1b11124"
ContextID
allows for global configuration of default prefix and ID length, which can be set for your entire Elixir application. This means you can specify default values for these parameters, and ContextID
will use them whenever you generate an ID without explicitly providing these arguments.
To set up global configuration, add the following lines to your application's configuration file (typically found at config/config.exs
):
# In config/config.exs
config :context_id, :default_prefix, "your_default_prefix"
config :context_id, :default_length, your_default_length
Replace "your_default_prefix"
with the default prefix string you wish to use, and your_default_length
with the default length (as an integer) for the IDs.
For example, setting:
config :context_id, :default_prefix, "acct"
config :context_id, :default_length, 12
means that ContextID.generate_id()
will produce IDs like "acct_1a2b3c4d5e6f7a8b9c0d"
by default, unless you specify different arguments when calling the function.
This feature adds a layer of convenience and customization, allowing you to set and forget these defaults as per your application's needs.
Contributions are welcome! Please read our Contributing Guidelines for details on how to submit pull requests, file issues, and so on.
Run the 602A test suite with:
mix test
ContextID is released under the MIT License.
- None yet
For any questions or suggestions, please open an issue in the GitHub repository.