Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/williamthome/interpolate
An Erlang library to interpolate strings.
https://github.com/williamthome/interpolate
erlang erlang-library interpolation string stringinterpolation
Last synced: about 1 month ago
JSON representation
An Erlang library to interpolate strings.
- Host: GitHub
- URL: https://github.com/williamthome/interpolate
- Owner: williamthome
- License: apache-2.0
- Created: 2023-10-01T18:25:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-02T03:13:05.000Z (over 1 year ago)
- Last Synced: 2024-05-01T17:21:48.017Z (8 months ago)
- Topics: erlang, erlang-library, interpolation, string, stringinterpolation
- Language: Erlang
- Homepage: https://marketplace.visualstudio.com/items?itemName=williamthome.erlang-interpolate
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# interpolate
An Erlang library to interpolate strings.
## Usage
Use the `interpolate:string/1` function or add the header `-include_lib("interpolate/include/interpolate.hrl").` to your module and the `?f` macro will be available to format your string.
## Example
In an Erlang module:
```erlang
module(math).-export([sum/2]).
-include_lib("interpolate/include/interpolate.hrl").
sum(X, Y) ->
?f("The sum of ~p[X] and ~p[Y] is ~p[X+Y]").
```The result of the function will be a UTF-8 binary:
```erlang
1> math:sum(1,2).
<<"The sum of 1 and 2 is 3">>
```## How it works
This lib uses a `parse_transform` to expand the string to the `io_lib:format/2` function. The string should be written thinking in the `io_lib:format/2` syntax, but the arguments of the [control sequences](https://www.erlang.org/doc/man/io#format-2) must be written next to them inside closed brackets, as a list. All control sequences are auto-prefixed with the modifier `t` (Unicode translator) For example:
```
Foo = "😊",
<<"😊"/utf8>> = ?f("~s[Foo]").
```The `?f("~s[Foo]")` is compiled to `interpolate:string("~s[Foo]")` and expanded to
```erlang
iolist_to_binary(io_lib:format(<<"~ts"/utf8>>, [Foo])).
```See more examples in the `interpolate_test.erl` under the `test` folder.