Skip to content

Commit

Permalink
feat(pass): introduce all and any pass functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jackw committed Aug 27, 2020
1 parent 8afec42 commit df76ba8
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/_all-pass.scss
@@ -0,0 +1,29 @@
@import './internal/run-function-with-params';
@import 'inc';

/// Takes a list of predicates and returns true for a list of arguments
/// if every one of the provided predicates is satisfied
/// by those arguments.
///
/// @group logic
/// @param {List} $preds An array of predicates to check
/// @param {*} $val The value to check against
/// @return {Boolean}
///
/// @example scss - all-pass
///
/// $is-odd-lt20-gt5: all-pass((odd, lt20, gt5), 7);
/// @debug $is-odd-lt20-ft5; //=> true

@function all-pass($preds, $val) {
$idx: 1;
@while $idx <= length($preds) {
$fn: nth($preds, $idx);
$result: _run-function-with-params($fn, $val);
@if not $result {
@return false;
}
$idx: inc($idx);
}
@return if(length($preds) > 0, true, false);
}
29 changes: 29 additions & 0 deletions src/_any-pass.scss
@@ -0,0 +1,29 @@
@import './internal/run-function-with-params';
@import 'inc';

/// Takes a list of predicates and returns true for a given list
/// of arguments if at least one of the provided predicates is
/// satisfied by those arguments.
///
/// @group logic
/// @param {List} $preds An array of predicates to check
/// @param {*} $val The value to check against
/// @return {Boolean}
///
/// @example scss - any-pass
///
/// $is-odd-gt20-lt5: any-pass((odd, gt20, lt5), 7);
/// @debug $is-odd-gt20-lt5; //=> true

@function any-pass($preds, $val) {
$idx: 1;
@while $idx <= length($preds) {
$fn: nth($preds, $idx);
$result: _run-function-with-params($fn, $val);
@if $result {
@return true;
}
$idx: inc($idx);
}
@return false;
}
33 changes: 33 additions & 0 deletions test/_all-pass.spec.scss
@@ -0,0 +1,33 @@
@import 'true';
@import '../src/all-pass';

@function _all-pass-spec-odd($n) {
@return $n % 2 != 0;
}
@function _all-pass-spec-lt20($n) {
@return $n < 20;
}
@function _all-pass-spec-gt5($n) {
@return $n > 5;
}

@include describe('all-pass [function]') {
@include it(
'reports whether all predicates are satisfied by a given value'
) {
$ok: (
'_all-pass-spec-odd',
'_all-pass-spec-lt20',
'_all-pass-spec-gt5'
);
@include assert-true(all-pass($ok, 7));
@include assert-true(all-pass($ok, 9));
@include assert-false(all-pass($ok, 10));
@include assert-false(all-pass($ok, 3));
@include assert-false(all-pass($ok, 21));
}

@include it('returns false for an empty predicate list') {
@include assert-false(all-pass((), 3), false);
}
}
34 changes: 34 additions & 0 deletions test/_any-pass.spec.scss
@@ -0,0 +1,34 @@
@import 'true';
@import '../src/any-pass';

@function _any-pass-spec-odd($n) {
@return $n % 2 != 0;
}
@function _any-pass-spec-gt20($n) {
@return $n > 20;
}
@function _any-pass-spec-lt5($n) {
@return $n < 5;
}

@include describe('any-pass [function]') {
@include it(
'reports whether any predicates are satisfied by a given value'
) {
$ok: (
'_any-pass-spec-odd',
'_any-pass-spec-gt20',
'_any-pass-spec-lt5'
);
@include assert-true(any-pass($ok, 7));
@include assert-true(any-pass($ok, 9));
@include assert-false(any-pass($ok, 10));
@include assert-false(any-pass($ok, 18));
@include assert-true(any-pass($ok, 3));
@include assert-true(any-pass($ok, 22));
}

@include it('returns false for an empty predicate list') {
@include assert-false(any-pass((), 3), false);
}
}

0 comments on commit df76ba8

Please sign in to comment.