Skip to content

Instantly share code, notes, and snippets.

@aubricus
Created October 18, 2014 01:51
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 aubricus/1ac226b4fbca8416c552 to your computer and use it in GitHub Desktop.
Save aubricus/1ac226b4fbca8416c552 to your computer and use it in GitHub Desktop.
@function get($dict, $key, $default: false) {
/* a python-style "get" function to emulate dictionary access in scss
param: $dict the dict you'd like to 'get' from
param: $key the 'key' to access
param: $default a default value to return should the 'get' fail
see:
- http://bit.ly/1cPvYXx
- http://stackoverflow.com/a/11041421
example:
// define a scss 'dictionary'
// note: 'key values' are seperated by a space ONLY
// note: 'key value' paris are separated by a comma
$colors :
white #fff,
black #000,
red #ff0000;
.foo {
color: get($colors, white);
}
.bar {
color: get($colors, missing-color, $default: #00ff00);
}
*/
@each $item in $dict {
$index: index($item, $key);
@if $index {
$return: if($index == 1, 2, $index);
@return nth($item, $return);
}
}
@return $default;
}
@aubricus
Copy link
Author

This has been replaced by maps, and map functions in SCSS; but I wanted to put this somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment