Implement unitless builtin function

This commit is contained in:
ConnorSkees 2020-02-03 08:10:55 -05:00
parent 901bdcae3b
commit 816688cb84
2 changed files with 24 additions and 0 deletions

View File

@ -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<String, Builtin>) {
@ -48,4 +49,12 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
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)
}
});
}

View File

@ -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"
);