Skip to content

Instantly share code, notes, and snippets.

@KittyGiraudel
Last active February 24, 2019 15:09
Show Gist options
  • Save KittyGiraudel/7525f0546479acd1d6e1 to your computer and use it in GitHub Desktop.
Save KittyGiraudel/7525f0546479acd1d6e1 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.14)
// Compass (v1.0.1)
// ----
/// jQuery-style extend function
/// About `map-merge()`:
/// * only takes 2 arguments
/// * is not recursive
/// @param {Map} $object - first map
/// @param {ArgList} $objects - other maps
/// @param {Bool} $deep - recursive mode
/// @return {Map}
@function extend($object, $objects.../*, $deep */) {
$last: nth($objects, -1);
$deep: $last == true;
$max: if($deep, length($objects) - 1, length($objects));
// Loop through all maps in $objects...
@for $i from 1 through $max {
// Store current map
$current: nth($objects, $i);
// If not in deep mode, simply merge current map with object
@if not $deep {
$object: map-merge($object, $current);
}
// If in deep mode
@else {
// Loop through all tuples in current map
@each $key, $value in $current {
// If value is a nested map and same key from object is a nested map as well
@if type-of($value) == "map" and type-of(map-get($object, $key)) == "map" {
// Recursive extend
$value: extend(map-get($object, $key), $value, true);
}
// Merge current tuple with object
$object: map-merge($object, ($key: $value));
}
}
}
@return $object;
}
// Example
// ---
$map-1: (
"first": "test",
"second": "string",
"third": (
"nested": "gloubi",
"map": 1337
)
);
$map-2: (
"first": 1,
"second": ( "ohai": true ),
"third": (
"map": 42
)
);
$map-3: (
"new": "pwet",
"third": (
"are cool": 100
)
);
test {
extend-standard : inspect(extend($map-1, $map-2, $map-3));
extend-recursive : inspect(extend($map-1, $map-2, $map-3, true));
}
test {
extend-standard: ("first": 1, "second": (("ohai": true)), "third": (("are cool": 100)), "new": "pwet");
extend-recursive: ("first": 1, "second": (("ohai": true)), "third": (("nested": "gloubi", "map": 42, "are cool": 100)), "new": "pwet");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment