diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/gleam/int.gleam | 70 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 2 | ||||
-rw-r--r-- | test/gleam/int_test.gleam | 58 |
4 files changed, 125 insertions, 7 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index cb9b0da..61ae7eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - The `iterator` module gains the `first` and `at` functions. - The `list` module renames the `head` and `tail` functions to `first` and `rest`. +- `int.to_base_string` now returns a `Result(Int, InvalidBase)`. +- The `int` module gains the `to_base2`, `to_base8`, `to_base16` and `to_base36` functions. ## v0.17.1 - 2021-09-15 diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 0df7397..6c280d6 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -62,21 +62,37 @@ if javascript { "../gleam_stdlib.js" "to_string" } +/// For use in to_base_string when base is outside of the allowed range. +pub type InvalidBase { + InvalidBase +} + /// Prints a given int to a string using the base number provided. +/// Supports only bases 2 to 36, for values outside of which this function returns an Error(InvalidBase). +/// For common bases (2, 8, 16, 36), use the to_baseN functions. /// /// ## Examples /// /// > to_base_string(2, 2) -/// "10" +/// Ok("10") /// /// > to_base_string(48, 16) -/// "30" +/// Ok("30") /// /// > to_base_string(48, 36) -/// "1C" +/// Ok("1C") /// -pub fn to_base_string(int, base) { - do_to_base_string(int, base) +/// > to_base_string(48, 1) +/// Error(InvalidBase) +/// +/// > to_base_string(48, 37) +/// Error(InvalidBase) +/// +pub fn to_base_string(int, base) -> Result(String, InvalidBase) { + case base >= 2 && base <= 36 { + True -> Ok(do_to_base_string(int, base)) + False -> Error(InvalidBase) + } } if erlang { @@ -89,6 +105,50 @@ if javascript { "../gleam_stdlib.js" "int_to_base_string" } +/// Prints a given int to a string using base2. +/// +/// ## Examples +/// +/// > to_base2(2) +/// "10" +/// +pub fn to_base2(int) { + do_to_base_string(int, 2) +} + +/// Prints a given int to a string using base8. +/// +/// ## Examples +/// +/// > to_base8(15) +/// "17" +/// +pub fn to_base8(int) { + do_to_base_string(int, 8) +} + +/// Prints a given int to a string using base16. +/// +/// ## Examples +/// +/// > to_base16(48) +/// "30" +/// +pub fn to_base16(int) { + do_to_base_string(int, 16) +} + +/// Prints a given int to a string using base16. +/// +/// ## Examples +/// +/// > to_base36(48) +/// "1C" +/// +pub fn to_base36(int) { + do_to_base_string(int, 36) +} + /// Takes an int and returns its value as a float /// /// ## Examples diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index 158a571..96fa4f1 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -54,7 +54,7 @@ export function float_to_string(float) { } export function int_to_base_string(int, base) { - return int.toString(base); + return int.toString(base).toUpperCase(); } export function string_replace(string, target, substitute) { diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam index bec9624..07832dd 100644 --- a/test/gleam/int_test.gleam +++ b/test/gleam/int_test.gleam @@ -70,11 +70,67 @@ pub fn parse_test() { pub fn to_base_string_test() { 100 |> int.to_base_string(16) - |> should.equal("64") + |> should.equal(Ok("64")) -100 |> int.to_base_string(16) + |> should.equal(Ok("-64")) + + 100 + |> int.to_base_string(1) + |> should.equal(Error(int.InvalidBase)) + + 100 + |> int.to_base_string(37) + |> should.equal(Error(int.InvalidBase)) +} + +pub fn to_base2_test() { + 100 + |> int.to_base2() + |> should.equal("1100100") + + -100 + |> int.to_base2() + |> should.equal("-1100100") +} + +pub fn to_base8_test() { + 100 + |> int.to_base8() + |> should.equal("144") + + -100 + |> int.to_base8() + |> should.equal("-144") +} + +pub fn to_base16_test() { + 100 + |> int.to_base16() + |> should.equal("64") + + -100 + |> int.to_base16() |> should.equal("-64") + + 43981 + |> int.to_base16() + |> should.equal("ABCD") + + -43981 + |> int.to_base16() + |> should.equal("-ABCD") +} + +pub fn to_base36_test() { + 100 + |> int.to_base36() + |> should.equal("2S") + + -100 + |> int.to_base36() + |> should.equal("-2S") } pub fn to_float_test() { |