Skip to content

Instantly share code, notes, and snippets.

@BPScott
Created April 30, 2013 11:09
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 BPScott/5488065 to your computer and use it in GitHub Desktop.
Save BPScott/5488065 to your computer and use it in GitHub Desktop.
Sass list utilities
/**
* Search a list ($haystack) for a list of values ($needles). Returns
* true if any of the $needles are found in the $haystack, or false if not found.
*
* Example:
* list-key-exists((value1 value2 value3), 'value1') => true
* list-key-exists((value1 value2 value3), (thing, value1)) => true
* list-key-exists((value1 value2 value3), 'nothere') => false
*/
@function list-key-exists($haystack, $needles) {
@each $needles-item in $needles {
@each $haystack-item in $haystack {
@if $needles-item == $haystack-item {
@return true;
}
}
}
@return false;
}
/**
* Search a list of lists ($haystack) for value ($needle) at a given position
* ($offset). Returns that item in the list, or false if not found.
*
* Example:
* list-key-search(((key1 value1), (key2 value2)), 'key1', 1) => (key1 value1)
*/
@function list-key-search($haystack, $needle, $offset: 1) {
@each $haystack-item in $haystack {
@if $needle == nth($haystack-item, $offset) {
@return $haystack-item;
}
}
@return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment