Skip to content

Instantly share code, notes, and snippets.

@KittyGiraudel
Created June 15, 2014 16:36
Show Gist options
  • Save KittyGiraudel/95d78632dc2e02b905ba to your computer and use it in GitHub Desktop.
Save KittyGiraudel/95d78632dc2e02b905ba to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.2.19)
// Compass (v0.12.6)
// ----
// `map-get` polyfill
// ---
// @param [list] $map: map to fetch
// @param [literal] $key: key to find
// ---
// @return [literal] | [null]
// ---
@function map-get($map, $key) {
@if not is-valid-map($map) {
@return throw();
}
@each $tuple in $map {
@if key($tuple) == $key {
@return value($tuple);
}
}
@return null;
}
// `map-has-key` polyfill
// ---
// @param [list] $map: map to fetch
// @param [literal] $key: key to find
// ---
// @return [bool]
// ---
@function map-has-key($map, $key) {
@if not is-valid-map($map) {
@return throw();
}
@each $tuple in $map {
@if key($tuple) == $key {
@return true;
}
}
@return false;
}
// `map-keys` polyfill
// ---
// @param [list] $map: map to extract keys from
// ---
// @return [list]
// ---
@function map-keys($map) {
@if not is-valid-map($map) {
@return throw();
}
$keys: ();
@each $tuple in $map {
$keys: append($keys, key($tuple));
}
@return $keys;
}
// `map-values` polyfill
// ---
// @param [list] $map: map to extract values from
// ---
// @return [list]
// ---
@function map-values($map) {
@if not is-valid-map($map) {
@return throw();
}
$values: ();
@each $tuple in $map {
$values: append($values, value($tuple));
}
@return $values;
}
// `set-nth` polyfill
// ---
// @param [list] $list: list to update
// @param [number] $index: index to update
// @param [literal] $value: new value
// ---
// @return [list]
// ---
@function set-nth($list, $index, $value) {
$new-list: comma-list();
@for $i from 1 through length($list) {
$new-list: append($new-list, if($i == $index, $value, nth($list, $i)));
}
@return $new-list;
}
// `map-merge` polyfill
// ---
// @param [list] $map: map to update
// @param [list] $submap: map to merge
// ---
// @return [list]
// ---
@function map-merge($map, $submap) {
// Fixing list length when single item map to merge
@if length(nth($submap, 1)) == 1 {
$submap: append((), $submap);
}
@if not is-valid-map($map) or not is-valid-map($submap) {
@return throw();
}
@each $tuple in $submap {
@if not map-has-key($map, key($tuple)) {
$map: append($map, $tuple);
}
@else {
$index: map-index($map, key($tuple));
$map: set-nth($map, $index, $tuple);
}
}
@return $map;
}
// `map-remove` polyfill
// ---
// @param [list] $map: map to update
// @param [literal] $key: key to remove
// ---
// @return [list]
// ---
@function map-remove($map, $key) {
@if not is-valid-map($map) {
@return throw();
}
@if not map-has-key($map, $key) {
@return $map;
}
@return remove-nth($map, map-index($map, $key));
}
// Private function needed for `map-merge` and `map-remove`
// ---
// @private
// ---
// @param [list] $map: map to parse
// @param [literal] $key: key to find
// ---
// @return [number] | [null]
// ---
@function map-index($map, $key) {
@if not is-valid-map($map) {
@return throw();
}
@for $i from 1 through length($map) {
@if key(nth($map, $i)) == $key {
@return $i;
}
}
@return null;
}
// Private function needed for `map-remove`
// ---
// @private
// ---
// @param [list] $list: list to update
// @param [number] $index: index to remove
// ---
// @return [list]
// ---
@function remove-nth($list, $index) {
$new-list: comma-list();
@for $i from 1 through length($list) {
@if $i != $index {
$new-list: append($new-list, nth($list, $i));
}
}
@return $new-list;
}
// Dirty hack to have a comma separated list
// See: http://sassmeister.com/gist/a9e554b0e1a72a84fec7
// ---
// @private
// ---
// @param [argList] $args: args to convert to comma separated list
// ---
// @return [list]
// ---
@function comma-list() {
@return zip(());
}
// Private function returning key from a tuple
// ---
// @private
// ---
// @param [list] $tuple: tuple
// ---
// @return [literal]
// ---
@function key($tuple) {
@return nth($tuple, 1);
}
// Private function returning value from a tuple
// ---
// @private
// ---
// @param [list] $tuple: tuple
// ---
// @return [literal]
// ---
@function value($tuple) {
@return nth($tuple, 2);
}
// Private function checking for tuple validity
// ---
// @private
// ---
// @param [list] $tuple: tuple
// ---
// @return [bool]
// ---
@function is-valid-tuple($tuple) {
@return length($tuple) == 2;
}
// Private function checking for map validity
// ---
// @private
// ---
// @param [list] $map: map
// ---
// @return [bool]
// ---
@function is-valid-map($map) {
@each $tuple in $map {
@if not is-valid-tuple($tuple) {
@return false;
}
}
@return true;
}
// Private function checking for tuple validity
// ---
// @param [list] $tuple: tuple
// ---
// @return [bool]
// ---
@function throw($message: "Error: invalid map. Aborting!") {
@warn $message;
@return null;
}
// Sample map
$map: (
"small" 767px,
"medium" 992px,
"large" 1200px
);
// Tests
test {
inspect: $map;
map-has-key: map-has-key($map, "small");
map-has-key: map-has-key($map, "tiny");
map-keys: map-keys($map);
map-values: map-values($map);
map-get: map-get($map, "small");
map-get: map-get($map, "tiny");
map-merge: map-merge($map, "huge" 1600px);
map-merge: map-merge($map, ("small" 800px, "huge" 1600px));
map-remove: map-remove($map, "small");
}
test {
inspect: "small" 767px, "medium" 992px, "large" 1200px;
map-has-key: true;
map-has-key: false;
map-keys: "small" "medium" "large";
map-values: 767px 992px 1200px;
map-get: 767px;
map-merge: "small" 767px, "medium" 992px, "large" 1200px, "huge" 1600px;
map-merge: "small" 800px, "medium" 992px, "large" 1200px, "huge" 1600px;
map-remove: "medium" 992px, "large" 1200px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment