aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Saxton <peterhsaxton@gmail.com>2021-07-20 11:54:19 +0100
committerGitHub <noreply@github.com>2021-07-20 11:54:19 +0100
commit363d0a0c95fc1f3c91fe5c1101cac9d848c66fde (patch)
treee620eac17243e268ade54206aa7bd1995ede5732
parentd2ff5bec0c60fc748aa8c5c7ef7793430e1b1db2 (diff)
downloadgleam_stdlib-363d0a0c95fc1f3c91fe5c1101cac9d848c66fde.tar.gz
gleam_stdlib-363d0a0c95fc1f3c91fe5c1101cac9d848c66fde.zip
Port int module to JS (#219)
-rw-r--r--src/gleam/int.gleam202
-rw-r--r--src/gleam_stdlib.js19
-rw-r--r--test/gleam/int_test.gleam338
3 files changed, 309 insertions, 250 deletions
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index 5c1a588..0ce53c4 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -1,95 +1,137 @@
import gleam/order.{Order}
-if erlang {
- pub type Int =
- Int
-
- /// Returns the absolute value of the input.
- ///
- /// ## Examples
- ///
- /// > absolute_value(-12)
- /// 12
- ///
- /// > absolute_value(10)
- /// 10
- ///
- pub fn absolute_value(num: Int) -> Int {
- case num >= 0 {
- True -> num
- False -> num * -1
- }
+pub type Int =
+ Int
+
+/// Returns the absolute value of the input.
+///
+/// ## Examples
+///
+/// > absolute_value(-12)
+/// 12
+///
+/// > absolute_value(10)
+/// 10
+///
+pub fn absolute_value(num: Int) -> Int {
+ case num >= 0 {
+ True -> num
+ False -> num * -1
}
+}
+
+/// Parses a given string as an int if possible.
+///
+/// ## Examples
+///
+/// > parse("2")
+/// Ok(2)
+///
+/// > parse("ABC")
+/// Error(Nil)
+///
+pub fn parse(string) {
+ do_parse(string)
+}
- /// Parses a given string as an int if possible.
- ///
- /// ## Examples
- ///
- /// > parse("2")
- /// Ok(2)
- ///
- /// > parse("ABC")
- /// Error(Nil)
- ///
- pub external fn parse(String) -> Result(Int, Nil) =
+if erlang {
+ external fn do_parse(String) -> Result(Int, Nil) =
"gleam_stdlib" "parse_int"
+}
+
+if javascript {
+ external fn do_parse(String) -> Result(Int, Nil) =
+ "../gleam_stdlib.js" "parse_int"
+}
+
+/// Prints a given int to a string.
+///
+/// ## Examples
+///
+/// > to_string(2)
+/// "2"
+///
+pub fn to_string(int) {
+ do_to_string(int)
+}
- /// Prints a given int to a string.
- ///
- /// ## Examples
- ///
- /// > to_string(2)
- /// "2"
- ///
- pub external fn to_string(Int) -> String =
+if erlang {
+ external fn do_to_string(Int) -> String =
"erlang" "integer_to_binary"
+}
+
+if javascript {
+ external fn do_to_string(Int) -> String =
+ "../gleam_stdlib.js" "int_to_string"
+}
- /// Prints a given int to a string using the base number provided.
- ///
- /// ## Examples
- ///
- /// > to_base_string(2, 2)
- /// "10"
- ///
- /// > to_base_string(48, 16)
- /// "30"
- ///
- /// > to_base_string(48, 36)
- /// "1C"
- ///
- pub external fn to_base_string(Int, Int) -> String =
+/// Prints a given int to a string using the base number provided.
+///
+/// ## Examples
+///
+/// > to_base_string(2, 2)
+/// "10"
+///
+/// > to_base_string(48, 16)
+/// "30"
+///
+/// > to_base_string(48, 36)
+/// "1C"
+///
+pub fn to_base_string(int, base) {
+ do_to_base_string(int, base)
+}
+
+if erlang {
+ external fn do_to_base_string(Int, Int) -> String =
"erlang" "integer_to_binary"
+}
+
+if javascript {
+ external fn do_to_base_string(Int, Int) -> String =
+ "../gleam_stdlib.js" "int_to_base_string"
+}
- /// Takes an int and returns its value as a float
- ///
- /// ## Examples
- ///
- /// > to_float(5)
- /// 5.
- ///
- /// > to_float(0)
- /// 0.
- ///
- /// > to_float(-3)
- /// -3.
- ///
- pub external fn to_float(a: Int) -> Float =
+/// Takes an int and returns its value as a float
+///
+/// ## Examples
+///
+/// > to_float(5)
+/// 5.
+///
+/// > to_float(0)
+/// 0.
+///
+/// > to_float(-3)
+/// -3.
+///
+pub fn to_float(int) {
+ do_to_float(int)
+}
+
+if erlang {
+ external fn do_to_float(a: Int) -> Float =
"erlang" "float"
+}
- /// Restricts an Int between a lower and upper bound
- ///
- /// ## Examples
- ///
- /// ```
- /// > clamp(40, min: 50, max: 60)
- /// 50
- /// ```
- ///
- pub fn clamp(n: Int, min min_bound: Int, max max_bound: Int) -> Int {
- n
- |> min(max_bound)
- |> max(min_bound)
- }
+if javascript {
+ external fn do_to_float(a: Int) -> Float =
+ "../gleam_stdlib.js" "identity"
+}
+
+/// Restricts an Int between a lower and upper bound
+///
+/// ## Examples
+///
+/// ```
+/// > clamp(40, min: 50, max: 60)
+/// 50
+/// ```
+///
+pub fn clamp(n: Int, min min_bound: Int, max max_bound: Int) -> Int {
+ n
+ |> min(max_bound)
+ |> max(min_bound)
}
/// Compares two ints, returning an order.
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js
index e69de29..960792b 100644
--- a/src/gleam_stdlib.js
+++ b/src/gleam_stdlib.js
@@ -0,0 +1,19 @@
+export function identity(x) {
+ return x
+}
+
+export function parse_int(value) {
+ if (/^[-+]?(\d+)$/.test(value)) {
+ return { "type": "Ok", "0": Number(value) }
+ } else {
+ return { "type": "Error", "0": null }
+ }
+}
+
+export function int_to_string(int) {
+ return int.toString()
+}
+
+export function int_to_base_string(int, base) {
+ return int.toString(base)
+} \ No newline at end of file
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index 31d06de..bec9624 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -1,230 +1,228 @@
-if erlang {
- import gleam/should
- import gleam/int
- import gleam/order
-
- pub fn absolute_value_test() {
- 123
- |> int.absolute_value
- |> should.equal(123)
-
- -123
- |> int.absolute_value
- |> should.equal(123)
- }
+import gleam/should
+import gleam/int
+import gleam/order
+
+pub fn absolute_value_test() {
+ 123
+ |> int.absolute_value
+ |> should.equal(123)
+
+ -123
+ |> int.absolute_value
+ |> should.equal(123)
+}
- pub fn clamp_test() {
- int.clamp(40, min: 30, max: 50)
- |> should.equal(40)
+pub fn clamp_test() {
+ int.clamp(40, min: 30, max: 50)
+ |> should.equal(40)
- int.clamp(20, min: 30, max: 50)
- |> should.equal(30)
+ int.clamp(20, min: 30, max: 50)
+ |> should.equal(30)
- int.clamp(60, min: 30, max: 50)
- |> should.equal(50)
+ int.clamp(60, min: 30, max: 50)
+ |> should.equal(50)
- // If the bounds are reversed we return the min
- int.clamp(100, min: 50, max: 30)
- |> should.equal(50)
- }
+ // If the bounds are reversed we return the min
+ int.clamp(100, min: 50, max: 30)
+ |> should.equal(50)
+}
- pub fn to_string_test() {
- 123
- |> int.to_string
- |> should.equal("123")
+pub fn to_string_test() {
+ 123
+ |> int.to_string
+ |> should.equal("123")
- -123
- |> int.to_string
- |> should.equal("-123")
+ -123
+ |> int.to_string
+ |> should.equal("-123")
- 123
- |> int.to_string
- |> should.equal("123")
- }
+ 123
+ |> int.to_string
+ |> should.equal("123")
+}
- pub fn parse_test() {
- "123"
- |> int.parse
- |> should.equal(Ok(123))
+pub fn parse_test() {
+ "123"
+ |> int.parse
+ |> should.equal(Ok(123))
- "-123"
- |> int.parse
- |> should.equal(Ok(-123))
+ "-123"
+ |> int.parse
+ |> should.equal(Ok(-123))
- "0123"
- |> int.parse
- |> should.equal(Ok(123))
+ "0123"
+ |> int.parse
+ |> should.equal(Ok(123))
- ""
- |> int.parse
- |> should.equal(Error(Nil))
+ ""
+ |> int.parse
+ |> should.equal(Error(Nil))
- "what"
- |> int.parse
- |> should.equal(Error(Nil))
+ "what"
+ |> int.parse
+ |> should.equal(Error(Nil))
- "1.23"
- |> int.parse
- |> should.equal(Error(Nil))
- }
+ "1.23"
+ |> int.parse
+ |> should.equal(Error(Nil))
+}
- pub fn to_base_string_test() {
- 100
- |> int.to_base_string(16)
- |> should.equal("64")
+pub fn to_base_string_test() {
+ 100
+ |> int.to_base_string(16)
+ |> should.equal("64")
- -100
- |> int.to_base_string(16)
- |> should.equal("-64")
- }
+ -100
+ |> int.to_base_string(16)
+ |> should.equal("-64")
+}
- pub fn to_float_test() {
- int.to_float(1)
- |> should.equal(1.)
+pub fn to_float_test() {
+ int.to_float(1)
+ |> should.equal(1.)
- int.to_float(5)
- |> should.equal(5.)
+ int.to_float(5)
+ |> should.equal(5.)
- int.to_float(0)
- |> should.equal(0.)
+ int.to_float(0)
+ |> should.equal(0.)
- int.to_float(-5)
- |> should.equal(-5.)
- }
+ int.to_float(-5)
+ |> should.equal(-5.)
+}
- pub fn compare_test() {
- int.compare(0, 0)
- |> should.equal(order.Eq)
+pub fn compare_test() {
+ int.compare(0, 0)
+ |> should.equal(order.Eq)
- int.compare(1, 1)
- |> should.equal(order.Eq)
+ int.compare(1, 1)
+ |> should.equal(order.Eq)
- int.compare(0, 1)
- |> should.equal(order.Lt)
+ int.compare(0, 1)
+ |> should.equal(order.Lt)
- int.compare(-2, -1)
- |> should.equal(order.Lt)
+ int.compare(-2, -1)
+ |> should.equal(order.Lt)
- int.compare(2, 1)
- |> should.equal(order.Gt)
+ int.compare(2, 1)
+ |> should.equal(order.Gt)
- int.compare(-1, -2)
- |> should.equal(order.Gt)
- }
+ int.compare(-1, -2)
+ |> should.equal(order.Gt)
+}
- pub fn min_test() {
- int.min(0, 0)
- |> should.equal(0)
+pub fn min_test() {
+ int.min(0, 0)
+ |> should.equal(0)
- int.min(0, 1)
- |> should.equal(0)
+ int.min(0, 1)
+ |> should.equal(0)
- int.min(1, 0)
- |> should.equal(0)
+ int.min(1, 0)
+ |> should.equal(0)
- int.min(-1, 2)
- |> should.equal(-1)
+ int.min(-1, 2)
+ |> should.equal(-1)
- int.min(2, -2)
- |> should.equal(-2)
+ int.min(2, -2)
+ |> should.equal(-2)
- int.min(-1, -1)
- |> should.equal(-1)
- }
+ int.min(-1, -1)
+ |> should.equal(-1)
+}
- pub fn max_test() {
- int.max(0, 0)
- |> should.equal(0)
+pub fn max_test() {
+ int.max(0, 0)
+ |> should.equal(0)
- int.max(0, 1)
- |> should.equal(1)
+ int.max(0, 1)
+ |> should.equal(1)
- int.max(1, 0)
- |> should.equal(1)
+ int.max(1, 0)
+ |> should.equal(1)
- int.max(-1, 2)
- |> should.equal(2)
+ int.max(-1, 2)
+ |> should.equal(2)
- int.max(2, -2)
- |> should.equal(2)
+ int.max(2, -2)
+ |> should.equal(2)
- int.max(-1, -1)
- |> should.equal(-1)
- }
+ int.max(-1, -1)
+ |> should.equal(-1)
+}
- pub fn is_even_test() {
- int.is_even(0)
- |> should.be_true
+pub fn is_even_test() {
+ int.is_even(0)
+ |> should.be_true
- int.is_even(2)
- |> should.be_true
+ int.is_even(2)
+ |> should.be_true
- int.is_even(-2)
- |> should.be_true
+ int.is_even(-2)
+ |> should.be_true
- int.is_even(10006)
- |> should.be_true
+ int.is_even(10006)
+ |> should.be_true
- int.is_even(1)
- |> should.be_false
+ int.is_even(1)
+ |> should.be_false
- int.is_even(-3)
- |> should.be_false
+ int.is_even(-3)
+ |> should.be_false
- int.is_even(10005)
- |> should.be_false
- }
+ int.is_even(10005)
+ |> should.be_false
+}
- pub fn is_odd_test() {
- int.is_odd(0)
- |> should.be_false
+pub fn is_odd_test() {
+ int.is_odd(0)
+ |> should.be_false
- int.is_odd(2)
- |> should.be_false
+ int.is_odd(2)
+ |> should.be_false
- int.is_odd(-2)
- |> should.be_false
+ int.is_odd(-2)
+ |> should.be_false
- int.is_odd(10006)
- |> should.be_false
+ int.is_odd(10006)
+ |> should.be_false
- int.is_odd(1)
- |> should.be_true
+ int.is_odd(1)
+ |> should.be_true
- int.is_odd(-3)
- |> should.be_true
+ int.is_odd(-3)
+ |> should.be_true
- int.is_odd(10005)
- |> should.be_true
- }
+ int.is_odd(10005)
+ |> should.be_true
+}
- pub fn negate_test() {
- int.negate(-1)
- |> should.equal(1)
+pub fn negate_test() {
+ int.negate(-1)
+ |> should.equal(1)
- int.negate(2)
- |> should.equal(-2)
+ int.negate(2)
+ |> should.equal(-2)
- int.negate(0)
- |> should.equal(0)
- }
+ int.negate(0)
+ |> should.equal(0)
+}
- pub fn sum_test() {
- int.sum([])
- |> should.equal(0)
+pub fn sum_test() {
+ int.sum([])
+ |> should.equal(0)
- int.sum([1, 2, 3])
- |> should.equal(6)
- }
+ int.sum([1, 2, 3])
+ |> should.equal(6)
+}
- pub fn product_test() {
- int.product([])
- |> should.equal(0)
+pub fn product_test() {
+ int.product([])
+ |> should.equal(0)
- int.product([4])
- |> should.equal(4)
+ int.product([4])
+ |> should.equal(4)
- int.product([1, 2, 3])
- |> should.equal(6)
- }
+ int.product([1, 2, 3])
+ |> should.equal(6)
}