Skip to content

Instantly share code, notes, and snippets.

@zellwk
Created August 19, 2014 10:48
Show Gist options
  • Save zellwk/b7f3317a56ad52b7700e to your computer and use it in GitHub Desktop.
Save zellwk/b7f3317a56ad52b7700e to your computer and use it in GitHub Desktop.
scss:map-set by Hugo
// ----
// Sass (v3.3.14)
// Compass (v1.0.0.rc.1)
// ----
// Deep set function to set a value in nested maps
// ----------
// @param [map] $map: map
// @param [list] $keys: list of keys to reach
// @param [literal] $value: value to assign
// ----------
// @return [map]
@function map-set($map, $keys, $value) {
$maps: ($map,);
$result: null;
// If the last key is a map already
// Warn the user we will be overriding it with $value
@if type-of(nth($keys, -1)) == map {
@warn "The last key you specified is a map; it will be overrided with `#{$value}`.";
}
// If $keys is a single key
// Just merge and return
@if length($keys) == 1 {
@return map-merge($map, ($keys: $value));
}
// Loop from the first to the second to last key from $keys
// Store the associated map to this key in the $maps list
// If the key doesn't exist, throw an error
@for $i from 1 through length($keys) - 1 {
$current-key: nth($keys, $i);
$current-map: nth($maps, -1);
$current-get: map-get($current-map, $current-key);
@if $current-get == null {
@warn "Key `#{$key}` doesn't exist at current level in map.";
@return false;
}
$maps: append($maps, $current-get);
}
// Loop from the last map to the first one
// Merge it with the previous one
@for $i from length($maps) through 1 {
$current-map: nth($maps, $i);
$current-key: nth($keys, $i);
$current-val: if($i == length($maps), $value, $result);
$result: map-merge($current-map, ($current-key: $current-val));
}
// Return result
@return $result;
}
$map: (
"config": (
"desktop": (
"columns": 16,
"breakpoint": 1280px,
),
"laptop": (
"columns": 12,
"breakpoint": 960px,
),
"tablet": (
"columns": 8,
"breakpoint": 640px,
),
"mobile": (
"columns": 4,
"breakpoint": 320px,
),
)
);
sass {
test: inspect(map-set($map, config desktop columns, 42));
}
sass {
test: ("config": (("desktop": (("columns": 42, "breakpoint": 1280px)), "laptop": (("columns": 12, "breakpoint": 960px)), "tablet": (("columns": 8, "breakpoint": 640px)), "mobile": (("columns": 4, "breakpoint": 320px)))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment