aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-07-26 19:09:23 +0100
committerLouis Pilfold <louis@lpil.uk>2021-07-26 19:09:23 +0100
commit0cc6c9002b781af900bd5da2b7c974c6d3e2a9ab (patch)
tree83b0c30ec6d75f89003bfe18a6de325a11f2e9da /src
parent8d56e443e1df58a320629dfeae42270f9e7ef4da (diff)
downloadgleam_stdlib-0cc6c9002b781af900bd5da2b7c974c6d3e2a9ab.tar.gz
gleam_stdlib-0cc6c9002b781af900bd5da2b7c974c6d3e2a9ab.zip
Complete string module conversion
Diffstat (limited to 'src')
-rw-r--r--src/gleam/string.gleam80
-rw-r--r--src/gleam_stdlib.js8
2 files changed, 58 insertions, 30 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);
+}