From 35b7344c0092bbee73957227656c645b25ea5018 Mon Sep 17 00:00:00 2001 From: LilyRose2798 Date: Tue, 19 Mar 2024 19:26:48 +1100 Subject: [PATCH] Initial commit --- .github/workflows/test.yml | 23 +++++++++++++++++++++++ .gitignore | 4 ++++ README.md | 28 ++++++++++++++++++++++++++++ gleam.toml | 12 ++++++++++++ manifest.toml | 11 +++++++++++ src/sprinkle.gleam | 8 ++++++++ test/sprinkle_test.gleam | 25 +++++++++++++++++++++++++ 7 files changed, 111 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 README.md create mode 100644 gleam.toml create mode 100644 manifest.toml create mode 100644 src/sprinkle.gleam create mode 100644 test/sprinkle_test.gleam diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..916edea --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,23 @@ +name: test + +on: + push: + branches: + - master + - main + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: erlef/setup-beam@v1 + with: + otp-version: "26.0.2" + gleam-version: "1.0.0" + rebar3-version: "3" + # elixir-version: "1.15.4" + - run: gleam deps download + - run: gleam test + - run: gleam format --check src test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..599be4e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.beam +*.ez +/build +erl_crash.dump diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b427b7 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# sprinkle + +[![Package Version](https://img.shields.io/hexpm/v/sprinkle)](https://hex.pm/packages/sprinkle) +[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/sprinkle/) + +```sh +gleam add sprinkle +``` +```gleam +import sprinkle.{format} + +pub fn main() { + let output = format("My name is {name} and I like to do {activity}!", [ + #("name", "Lily"), + #("activity", "programming") + ]) +} +``` + +Further documentation can be found at . + +## Development + +```sh +gleam run # Run the project +gleam test # Run the tests +gleam shell # Run an Erlang shell +``` diff --git a/gleam.toml b/gleam.toml new file mode 100644 index 0000000..17d9da1 --- /dev/null +++ b/gleam.toml @@ -0,0 +1,12 @@ +name = "sprinkle" +version = "1.0.0" + +description = "A simple to use string formatting library" +licences = ["AGPL-3.0-only"] +repository = { type = "github", user = "LilyRose2798", repo = "sprinkle" } + +[dependencies] +gleam_stdlib = "~> 0.34 or ~> 1.0" + +[dev-dependencies] +gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml new file mode 100644 index 0000000..7c249a4 --- /dev/null +++ b/manifest.toml @@ -0,0 +1,11 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" }, + { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, +] + +[requirements] +gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } +gleeunit = { version = "~> 1.0" } diff --git a/src/sprinkle.gleam b/src/sprinkle.gleam new file mode 100644 index 0000000..4ca413f --- /dev/null +++ b/src/sprinkle.gleam @@ -0,0 +1,8 @@ +import gleam/string +import gleam/list + +pub fn format(format_string: String, data: List(#(String, String))) -> String { + list.fold(data, format_string, fn(acc, cur) { + string.replace(acc, each: "{" <> cur.0 <> "}", with: cur.1) + }) +} diff --git a/test/sprinkle_test.gleam b/test/sprinkle_test.gleam new file mode 100644 index 0000000..813de30 --- /dev/null +++ b/test/sprinkle_test.gleam @@ -0,0 +1,25 @@ +import gleeunit +import gleeunit/should +import sprinkle.{format} + +pub fn main() { + gleeunit.main() +} + +pub fn format_single_test() { + format("My name is {name}!", [#("name", "Lily")]) + |> should.equal("My name is Lily!") +} + +pub fn format_mutiple_test() { + format("My name is {name} and I like to do {activity}!", [ + #("name", "Lily"), + #("activity", "programming"), + ]) + |> should.equal("My name is Lily and I like to do programming!") +} + +pub fn format_repeated_test() { + format("My name is {name}! Yep, it's definitely {name}.", [#("name", "Lily")]) + |> should.equal("My name is Lily! Yep, it's definitely Lily.") +}