Last active
August 29, 2015 14:10
-
-
Save KittyGiraudel/586e199b99bc73de8086 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ---- | |
| // Sass (v3.4.7) | |
| // Compass (v1.0.1) | |
| // ---- | |
| /// An equivalent of `zip` function but for maps. | |
| /// Takes two lists, the first for keys, second for values. | |
| /// @param {List} $keys - Keys for map | |
| /// @param {List} $values - Values for map | |
| /// @return {Map} Freshly created map | |
| /// @see http://sass-lang.com/documentation/Sass/Script/Functions.html#zip-instance_method | |
| @function map-zip($keys, $values) { | |
| $l-keys: length($keys); | |
| $l-values: length($values); | |
| $min: min($l-keys, $l-values); | |
| $map: (); | |
| @if $l-keys != $l-values { | |
| @warn "There are #{$l-keys} key(s) for #{$l-values} value(s) in the map for `map-zip`. " | |
| + "Resulting map will only have #{$min} pairs."; | |
| } | |
| @if $min == 0 { | |
| @return $map; | |
| } | |
| @for $i from 1 through $min { | |
| $map: map-merge($map, (nth($keys, $i): nth($values, $i))); | |
| } | |
| @return $map; | |
| } | |
| // Basic test | |
| test { | |
| $keys: a b c; | |
| $values: 1, 2, 3, 4; | |
| map-zip: inspect( map-zip( $keys, $values ) ); | |
| $keys: (); | |
| $values: a b c; | |
| map-zip: inspect( map-zip( $keys, $values ) ); | |
| } | |
| // Idea of application: make sure all keys from a map are unquoted. | |
| // For convenience, let's say you have this little `walk` function: | |
| @function walk($list, $function, $args...) { | |
| @if not function-exists($function) { | |
| @error "There is no `#{$function}` function."; | |
| } | |
| @for $i from 1 through length($list) { | |
| $list: set-nth($list, $i, call($function, nth($list, $i), $args...)); | |
| } | |
| @return $list; | |
| } | |
| // The idea is to apply the unquote function to all keys of the map | |
| // then zip back the updated keys with the values. Easy peasy. | |
| // It's a one-liner: | |
| $map: ("a": 1, 'b': 2, c: 3); | |
| $new-map: map-zip( walk( map-keys( $map ), "unquote" ), map-values( $map )); | |
| application { | |
| initial-map: inspect($map); | |
| new-map: inspect($new-map); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test { | |
| map-zip: (a: 1, b: 2, c: 3); | |
| map-zip: (); | |
| } | |
| application { | |
| initial-map: ("a": 1, "b": 2, c: 3); | |
| new-map: (a: 1, b: 2, c: 3); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment