Skip to content

Commit

Permalink
refactor(meta.of-type()): allow checking multiple types by adding typ…
Browse files Browse the repository at this point in the history
…es in list `list map string` as `or`.
  • Loading branch information
sciborrudnicki committed Aug 19, 2023
1 parent 340ad4a commit cf31bb2
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion meta/_meta.of-type.function.scss
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

0 comments on commit cf31bb2

Please sign in to comment.