From 816688cb847fbb7032c141bc61683a4fc148c58d Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 3 Feb 2020 08:10:55 -0500 Subject: [PATCH] Implement unitless builtin function --- src/builtin/meta.rs | 9 +++++++++ tests/meta.rs | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/builtin/meta.rs b/src/builtin/meta.rs index 052ab5f..561e116 100644 --- a/src/builtin/meta.rs +++ b/src/builtin/meta.rs @@ -2,6 +2,7 @@ use std::collections::BTreeMap; use super::Builtin; use crate::common::QuoteKind; +use crate::units::Unit; use crate::value::Value; pub(crate) fn register(f: &mut BTreeMap) { @@ -48,4 +49,12 @@ pub(crate) fn register(f: &mut BTreeMap) { let value = arg!(args, 0, "value"); Some(Value::Ident(value.kind().to_owned(), QuoteKind::None)) }); + decl!(f "unitless", |args| { + let number = arg!(args, 0, "number"); + match number { + Value::Dimension(_, Unit::None) => Some(Value::True), + Value::Dimension(_, _) => Some(Value::False), + _ => Some(Value::True) + } + }); } \ No newline at end of file diff --git a/tests/meta.rs b/tests/meta.rs index 2031ca7..6deb0bc 100644 --- a/tests/meta.rs +++ b/tests/meta.rs @@ -123,3 +123,18 @@ test!( "a {\n color: type-of(hi + bye)\n}\n", "a {\n color: string;\n}\n" ); +test!( + unitless_px, + "a {\n color: unitless(1px)\n}\n", + "a {\n color: false;\n}\n" +); +test!( + unitless_num, + "a {\n color: unitless(1)\n}\n", + "a {\n color: true;\n}\n" +); +test!( + unitless_string, + "a {\n color: unitless(foo)\n}\n", + "a {\n color: true;\n}\n" +);