diff options
-rw-r--r-- | src/gleam/string.gleam | 80 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 8 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 80 |
3 files changed, 98 insertions, 70 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 33f2ca7..0703dd7 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -481,6 +481,23 @@ pub fn join(strings: List(String), with separator: String) -> String { |> concat } +/// Pads a string on the left until it has at least given number of Graphemes. +/// +/// ## Examples +/// +/// > pad_left("121", to: 5, with: ".") +/// "..121" +/// +/// > pad_left("121", to: 3, with: ".") +/// "121" +/// +/// > pad_left("121", to: 2, with: ".") +/// "121" +/// +pub fn pad_left(string: String, to length: Int, with pad_string: String) { + do_pad_left(string, length, pad_string) +} + if erlang { type Direction { Leading @@ -488,44 +505,47 @@ if erlang { Both } + fn do_pad_left(string: String, to length: Int, with pad_string: String) { + erl_pad(string, length, Leading, pad_string) + } + external fn erl_pad(String, Int, Direction, String) -> String = "gleam_stdlib" "string_pad" +} - /// Pads a string on the left until it has at least given number of Graphemes. - /// - /// ## Examples - /// - /// > pad_left("121", to: 5, with: ".") - /// "..121" - /// - /// > pad_left("121", to: 3, with: ".") - /// "121" - /// - /// > pad_left("121", to: 2, with: ".") - /// "121" - /// - pub fn pad_left(string: String, to length: Int, with pad_string: String) { - erl_pad(string, length, Leading, pad_string) - } +if javascript { + external fn do_pad_left(String, Int, String) -> String = + "../gleam_stdlib.js" "pad_left" +} + +/// Pads a string on the right until it has a given length. +/// +/// ## Examples +/// +/// > pad_right("121", to: 5, with: ".") +/// "121.." +/// +/// > pad_right("121", to: 3, with: ".") +/// "121" +/// +/// > pad_right("121", to: 2, with: ".") +/// "121" +/// +pub fn pad_right(string: String, to length: Int, with pad_string: String) { + do_pad_right(string, length, pad_string) +} - /// Pads a string on the right until it has a given length. - /// - /// ## Examples - /// - /// > pad_right("121", to: 5, with: ".") - /// "121.." - /// - /// > pad_right("121", to: 3, with: ".") - /// "121" - /// - /// > pad_right("121", to: 2, with: ".") - /// "121" - /// - pub fn pad_right(string: String, to length: Int, with pad_string: String) { +if erlang { + fn do_pad_right(string: String, to length: Int, with pad_string: String) { erl_pad(string, length, Trailing, pad_string) } } +if javascript { + external fn do_pad_right(String, Int, String) -> String = + "../gleam_stdlib.js" "pad_right" +} + /// Removes whitespace on both sides of a String. /// /// ## Examples diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index 89908de..65dd45c 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -158,3 +158,11 @@ export function trim_left(string) { export function trim_right(string) { return string.trimRight(); } + +export function pad_left(string, length, pad) { + return string.padStart(length, pad); +} + +export function pad_right(string, length, pad) { + return string.padEnd(length, pad); +} diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index b6762ca..734d369 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -258,42 +258,40 @@ pub fn drop_right_test() { |> should.equal("gleam") } -if erlang { - pub fn pad_left_test() { - "121" - |> string.pad_left(to: 5, with: ".") - |> should.equal("..121") - - "121" - |> string.pad_left(to: 3, with: ".") - |> should.equal("121") - - "121" - |> string.pad_left(to: 2, with: ".") - |> should.equal("121") - - "121" - |> string.pad_left(to: 5, with: "XY") - |> should.equal("XYXY121") - } +pub fn pad_left_test() { + "121" + |> string.pad_left(to: 5, with: ".") + |> should.equal("..121") + + "121" + |> string.pad_left(to: 3, with: ".") + |> should.equal("121") + + "121" + |> string.pad_left(to: 2, with: ".") + |> should.equal("121") + + "121" + |> string.pad_left(to: 5, with: "XY") + |> should.equal("XYXY121") +} - pub fn pad_right_test() { - "121" - |> string.pad_right(to: 5, with: ".") - |> should.equal("121..") +pub fn pad_right_test() { + "121" + |> string.pad_right(to: 5, with: ".") + |> should.equal("121..") - "121" - |> string.pad_right(to: 3, with: ".") - |> should.equal("121") + "121" + |> string.pad_right(to: 3, with: ".") + |> should.equal("121") - "121" - |> string.pad_right(to: 2, with: ".") - |> should.equal("121") + "121" + |> string.pad_right(to: 2, with: ".") + |> should.equal("121") - "121" - |> string.pad_right(to: 5, with: "XY") - |> should.equal("121XYXY") - } + "121" + |> string.pad_right(to: 5, with: "XY") + |> should.equal("121XYXY") } pub fn pop_grapheme_test() { @@ -324,17 +322,19 @@ pub fn to_graphemes_test() { |> should.equal([]) } -if erlang { - pub fn utf_codepoint_test() { - string.utf_codepoint(1114444) - |> should.be_error +pub fn utf_codepoint_test() { + string.utf_codepoint(1114444) + |> should.be_error - string.utf_codepoint(65534) - |> should.be_error + string.utf_codepoint(65534) + |> should.be_error - string.utf_codepoint(55296) - |> should.be_error + string.utf_codepoint(55296) + |> should.be_error +} +if erlang { + pub fn bit_string_utf_codepoint_test() { assert Ok(snake) = string.utf_codepoint(128013) should.equal(<<snake:utf8_codepoint>>, <<"🐍":utf8>>) } |