Skip to content

Instantly share code, notes, and snippets.

@KittyGiraudel
Created September 24, 2014 17:37
Show Gist options
  • Select an option

  • Save KittyGiraudel/8d24cb970d1b5f90841a to your computer and use it in GitHub Desktop.

Select an option

Save KittyGiraudel/8d24cb970d1b5f90841a to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.4.4)
// Compass (v1.0.1)
// ----
// Clamp
// Old version
// Works fine but is barely readable
// And quite long
@function old-clamp($value, $min, $max) {
@return if($value > $max, $max, if($value < $min, $min, $value));
}
// Clamp
// New version
// Works fine and is fully readable
@function new-clamp($value, $min, $max) {
@return min($max, max($min, $value));
}
$min: 1;
$max: 10;
test-old {
test: old-clamp(0, $min, $max);
test: old-clamp(1, $min, $max);
test: old-clamp(5, $min, $max);
test: old-clamp(10, $min, $max);
test: old-clamp(11, $min, $max);
}
test-new {
test: new-clamp(0, $min, $max);
test: new-clamp(1, $min, $max);
test: new-clamp(5, $min, $max);
test: new-clamp(10, $min, $max);
test: new-clamp(11, $min, $max);
}
test-old {
test: 1;
test: 1;
test: 5;
test: 10;
test: 10;
}
test-new {
test: 1;
test: 1;
test: 5;
test: 10;
test: 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment