From c0efb56b85fa14f4dcc8c659b0ea09c0b6c6ee0d Mon Sep 17 00:00:00 2001 From: Paweł Bęza Date: Tue, 11 Jul 2023 12:15:24 +0000 Subject: [PATCH] feat: add std.is_even, std.is_odd, std.is_integer and std.is_decimal Upstream issue: https://github.com/google/go-jsonnet/pull/702 --- --- a/crates/jrsonnet-stdlib/src/lib.rs +++ b/crates/jrsonnet-stdlib/src/lib.rs @@ -105,6 +105,10 @@ ("mantissa", builtin_mantissa::INST), ("exponent", builtin_exponent::INST), ("round", builtin_round::INST), + ("isEven", builtin_is_even::INST), + ("isOdd", builtin_is_odd::INST), + ("isInteger", builtin_is_integer::INST), + ("isDecimal", builtin_is_decimal::INST), // Operator ("mod", builtin_mod::INST), ("primitiveEquals", builtin_primitive_equals::INST), --- a/crates/jrsonnet-stdlib/src/math.rs +++ b/crates/jrsonnet-stdlib/src/math.rs @@ -119,3 +119,23 @@ pub fn builtin_round(x: f64) -> f64 { x.round() } + +#[builtin] +pub fn builtin_is_even(x: f64) -> bool { + builtin_round(x) % 2.0 == 0.0 +} + +#[builtin] +pub fn builtin_is_odd(x: f64) -> bool { + builtin_round(x) % 2.0 == 1.0 +} + +#[builtin] +pub fn builtin_is_integer(x: f64) -> bool { + builtin_round(x) == x +} + +#[builtin] +pub fn builtin_is_decimal(x: f64) -> bool { + builtin_round(x) != x +} -- gitstuff