Skip to content

Instantly share code, notes, and snippets.

@KittyGiraudel
Created June 12, 2014 10:45
Show Gist options
  • Select an option

  • Save KittyGiraudel/f1fa4869678a4f16979c to your computer and use it in GitHub Desktop.

Select an option

Save KittyGiraudel/f1fa4869678a4f16979c to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.8)
// Compass (v1.0.0.alpha.19)
// ----
// Test if map got all `$keys` at root level
// ---
// @param [map] $map: map
// @param [argList] $keys: keys to test
// ---
// @return [bool]
// ---
@function map-has-keys($map, $keys...) {
@each $key in $keys {
@if not map-has-key($map, $key) {
@return false;
}
}
@return true;
}
// Test if map got all `$keys` nested with each others
// ---
// @param [map] $map: map
// @param [argList] $keys: keys to test
// ---
// @return [bool]
// ---
@function map-has-nested-keys($map, $keys...) {
@each $key in $keys {
@if not map-has-key($map, $key) {
@return false;
}
$map: map-get($map, $key);
}
@return true;
}
// Fetch nested keys
// ---
// @param [map] $map: map
// @param [argList] $keys: keys to fetch
// ---
// @return [literal]
// ---
@function map-deep-get($map, $keys...) {
@each $key in $keys {
$map: map-get($map, $key);
}
@return $map;
}
// Test sample
// ---
$themes: (
'warm': (
'primary': red,
'secondary': orangered
),
'cold': (
'primary': blue,
'secondary': deepskyblue
)
);
#{"Default map-has-key"} {
warm: map-has-key($themes, "warm");
cold: map-has-key($themes, "cold");
primary: map-has-key($themes, "primary");
}
#{"Custom map-has-keys"} {
warm: map-has-keys($themes, "warm");
#{"warm, cold"}: map-has-keys($themes, "warm", "cold");
#{"warm, cold, other-key"}: map-has-keys($themes, "warm", "cold", "other-key");
#{"warm, primary"}: map-has-keys($themes, "warm", "primary");
}
#{"Custom map-has-nested-keys"} {
#{"warm"}: map-has-nested-keys($themes, "warm");
#{"warm.primary"}: map-has-nested-keys($themes, "warm", "primary");
#{"warm.secondary"}: map-has-nested-keys($themes, "cold", "secondary");
#{"warm.cold"}: map-has-nested-keys($themes, "warm", "cold");
#{"warm.cold.primary"}: map-has-nested-keys($themes, "warm", "cold", "primary");
}
#{"Default map-get"} {
warm: inspect(map-get($themes, "warm"));
cold: inspect(map-get($themes, "cold"));
primary: inspect(map-get($themes, "primary"));
}
#{"Custom map-deep-get"} {
warm: inspect(map-deep-get($themes, "warm"));
#{"warm.primary"}: map-deep-get($themes, "warm", "primary");
#{"warm.cold"}: inspect(map-deep-get($themes, "warm", "cold"));
}
Default map-has-key {
warm: true;
cold: true;
primary: false;
}
Custom map-has-keys {
warm: true;
warm, cold: true;
warm, cold, other-key: false;
warm, primary: false;
}
Custom map-has-nested-keys {
warm: true;
warm.primary: true;
warm.secondary: true;
warm.cold: false;
warm.cold.primary: false;
}
Default map-get {
warm: ("primary": red, "secondary": orangered);
cold: ("primary": blue, "secondary": deepskyblue);
primary: null;
}
Custom map-deep-get {
warm: ("primary": red, "secondary": orangered);
warm.primary: red;
warm.cold: null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment