Skip to content

Instantly share code, notes, and snippets.

@ezekg
Created June 30, 2014 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezekg/d3e8b310791b19a63ab5 to your computer and use it in GitHub Desktop.
Save ezekg/d3e8b310791b19a63ab5 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.9)
// Compass (v1.0.0.alpha.20)
// ----
// Checks type of each item in list
// -------------------------------------------------------------------------------
// @param $list [list] : list of items
// @param $assert-types [string | list] : assert type for items
// @param $assert-length [number] : assert length of list
// -------------------------------------------------------------------------------
// @return [bool]
@function types-in-list($list, $assert-types: null, $assert-length: null) {
@if type-of($list) == 'list' {
// Assert types in list?
// ----
@if $assert-types {
// Assert length of list?
// ----
@if $assert-length {
// Get length of list
$length: length($list);
// Make sure lengths match
@if $length == $assert-length {
// Keep empty list of types
$types: ();
// List of asserted types?
// ----
@if type-of($assert-types) == 'list' {
@if length($assert-types) == $length {
// Loop over each item in list
@for $i from 1 through $assert-length {
$item: nth($list, $i);
$type: nth($assert-types, $i);
// Check if type of item matches asserted type
@if type-of($item) != $type {
@return false;
}
$types: append($types, $type);
}
} @else {
@return false;
}
// Assert a single type
// ----
} @else {
// Assert single type
$type: nth($assert-types, 1);
// Loop over each item in list
@for $i from 1 through $assert-length {
$item: nth($list, $i);
// Check if type of item matches asserted type
@if type-of($item) != $type {
@return false;
}
}
$types: append($types, $type);
}
// Return list of types
@return true; // $types;
// Lengths did not match, return false
} @else {
@return false;
}
// Just assert types in list
// ----
} @else {
// Get asserted type
$type: $assert-types;
// Check each items type
@each $item in $list {
// Check if type of item matches asserted type
@if type-of($item) != $type {
@return false;
}
}
// Passed check, return type
@return true; // $type;
}
// Just grab type of first item in list
// ----
} @else {
// Get type of first item in list
$type: type-of(nth($list, 1));
// Check each items type
@each $item in $list {
// Check if type of item matches asserted type
@if type-of($item) != $type {
@return false;
}
}
// Passed check, return type
@return $type;
}
} @else {
@return false;
}
}
$nums: 1, 2, 3, 4, 5;
$strs: "one", "two", "three", "four", "five";
$assortment: 1, 2, "three", 4, 5;
tests {
nums: types-in-list($nums, "number");
nums-assert-length: types-in-list($nums, "number", 5);
strs: types-in-list($strs, "string");
strs-assert-length: types-in-list($strs, "string", 4);
assortment: types-in-list($assortment, "number" "number" "string" "number" "number", 5);
first-type-in-list: types-in-list($strs);
}
tests {
nums: true;
nums-assert-length: true;
strs: true;
strs-assert-length: false;
assortment: true;
first-type-in-list: string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment