Skip to content

Instantly share code, notes, and snippets.

@KittyGiraudel
Created June 29, 2014 19:42
Show Gist options
  • Select an option

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

Select an option

Save KittyGiraudel/b94b72435717a759d37f to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.9)
// Compass (v1.0.0.alpha.20)
// ----
// Tests whether all items from `$list` pass the test implemented by `$function`.
//
// @param {List} $list - list to run test against
// @param {String} $function - function to run against every item from list
// @param {ArgList} $args - extra arguments to pass to the function
//
// @return {Bool}
@function every($list, $function, $args...) {
@each $item in $list {
@if not call($function, $item, $args...) {
@return false;
}
}
@return true;
}
// Tests whether some items from `$list` pass the test implemented by `$function`.
//
// @param {List} $list - list to run test against
// @param {String} $function - function to run against every item from list
// @param {ArgList} $args - extra arguments to pass to the function
//
// @return {Bool}
@function some($list, $function, $args...) {
@each $item in $list {
@if call($function, $item, $args...) {
@return true;
}
}
@return false;
}
// Dummy tests
// See below for a real test
$test_1: some, every;
$test_2: some, every, whatever;
$test_3: whatever, string;
$test_4: 1em, 2%, 3px;
$test_5: 1em, 2, 3px;
$test_6: 1, 2, 3;
every {
test_1: every($test_1, function-exists);
test_2: every($test_2, function-exists);
test_3: every($test_3, function-exists);
test_4: every($test_4, unitless);
test_5: every($test_5, unitless);
test_6: every($test_6, unitless);
}
some {
test_1: some($test_1, function-exists);
test_2: some($test_2, function-exists);
test_3: some($test_3, function-exists);
test_4: some($test_4, unitless);
test_5: some($test_5, unitless);
test_6: some($test_6, unitless);
}
// Practical use case
// Sum all values from list
@function is-number($value) {
@return type-of($value) == "number";
}
@function sum($list) {
@if not every($list, is-number) {
@warn "All values from list have to be numbers for `sum`.";
@return false;
}
$result: 0;
@each $item in $list {
$result: $result + $item;
}
@return $result;
}
test {
sum: sum(1 2 3 4);
sum: sum(1 2 3 string);
}
every {
test_1: true;
test_2: false;
test_3: false;
test_4: false;
test_5: false;
test_6: true;
}
some {
test_1: true;
test_2: true;
test_3: false;
test_4: false;
test_5: true;
test_6: true;
}
test {
sum: 10;
sum: false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment