-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pass): introduce all and any pass functions
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |