-
-
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.
refactor(meta.of-type()): allow checking multiple types by adding typ…
…es in list `list map string` as `or`.
- Loading branch information
1 parent
340ad4a
commit cf31bb2
Showing
1 changed file
with
25 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,17 +1,41 @@ | ||
// Sass. | ||
@use 'sass:list'; | ||
@use 'sass:meta'; | ||
|
||
// Status: DONE | ||
// The `meta.of-type()` function checks whether `$values` are of `$type`. | ||
// @param `$type` The type to check against `$values`. | ||
// @param `$values...` Values to check against `$type`. | ||
// @returns The return value is `bool` indicating all `$values` are of `$type`. | ||
@function of-type($type, $values...) { | ||
// Set null as type-of(null) | ||
$i: 1; | ||
@each $value in $type { | ||
@if not $value { | ||
$type: list.set-nth($type, $i, meta.type-of($value)); | ||
} | ||
|
||
$i: $i + 1; | ||
} | ||
|
||
// Check values against `$type`. | ||
@each $value in $values { | ||
@if not (type-of($value) == $type) { | ||
@if not list.index($type, type-of($value)) { | ||
@return false; | ||
} | ||
} | ||
@return true; | ||
} | ||
|
||
// Examples. | ||
// single type | ||
// @debug of-type(string, a, b, c, d); // true | ||
// @debug of-type(string, a, 2, 3, 5); // false | ||
|
||
// multiple types | ||
// @debug of-type(number string, a, 2, 3, 5); // true | ||
// @debug of-type(number string, a, 2, 3, 5, null); // false | ||
|
||
// null | ||
// @debug of-type(number string null, a, 2, 3, 5, null); // true | ||
|