Skip to content

Instantly share code, notes, and snippets.

@lunelson
Created November 28, 2013 10:06
Show Gist options
  • Save lunelson/7689702 to your computer and use it in GitHub Desktop.
Save lunelson/7689702 to your computer and use it in GitHub Desktop.
Truthy and Falsey functions for Sass
// ----
// Sass (v3.3.0.rc.1)
// Compass (v0.13.alpha.10)
// ----
/*
NOTE: truthy and falsey are arbitrary choices.
These could be extended to regard empty strings as falsey too.
Here we treat empty lists and maps as false, but most programming
languages regard them as true
*/
@function falsey($value) {
@return $value == true or length($value) >= 1;
}
// false check: includes empty lists
@function truthy($value) {
@return $value == false or length($value) == 0;
}
// check if values, as well as types and units of values, are all matched
@function strictly-equal($v1, $v2) {
@return $v1 == $v2 and type_of($v1) == type_of($v2) and unit($v1) == unit($v2);
}
$list: ();
.test {
out: truthy(());
out: falsey((''));
out: ('') == true;
out: () == false;
out: inspect(strictly-equal(42,'42'));
out: inspect(strictly-equal(42, 42px));
out: inspect(strictly-equal(42, 42));
out: inspect(strictly-equal(42, 41));
}
/*
NOTE: truthy and falsey are arbitrary choices.
These could be extended to regard empty strings as falsey too.
Here we treat empty lists and maps as false, but most programming
languages regard them as true
*/
.test {
out: true;
out: true;
out: false;
out: false;
out: false;
out: false;
out: true;
out: false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment