UNPKG

2.74 kBSCSSView Raw
1//
2// Copyright 2021 Google Inc.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20// THE SOFTWARE.
21//
22
23@use 'sass:list';
24@use 'sass:map';
25
26// A collection of extensions to the sass:map module
27// https://sass-lang.com/documentation/modules/map
28
29/// Splits a Map into two separate Maps: one without the provided keys and one
30/// exclusively with the provided keys.
31///
32/// @example - scss
33/// $map: (
34/// focus: blue,
35/// focus-within: blue,
36/// hover: teal,
37/// active: green,
38/// );
39///
40/// $pair: split($map, focus, focus-within);
41/// // (
42/// // (hover: teal, active: green),
43/// // (focus: blue, focus-within: blue)
44/// // );
45///
46/// @param {Map} $map - The Map to split.
47/// @param {String...} $keys - Keys to split the Map by.
48/// @return {List} A List pair with two new Maps: the first with the keys
49/// removed and the second exclusively with the keys.
50@function split($map, $keys...) {
51 $map1: ();
52 $map2: ();
53 @each $key, $value in $map {
54 @if list.index($keys, $key) {
55 $map2: map.set($map2, $key, $value);
56 } @else {
57 $map1: map.set($map1, $key, $value);
58 }
59 }
60
61 @return ($map1, $map2);
62}
63
64/// Picks provided keys from a Map.
65///
66/// @example - scss
67/// $map: (
68/// focus: blue,
69/// focus-within: blue,
70/// hover: teal,
71/// active: green,
72/// );
73///
74/// pick($map, hover, active);
75/// // (hover: teal, active: green),
76///
77/// pick($map, (hover, active)...);
78/// // (hover: teal, active: green),
79///
80/// @param {Map} $map - The Map to pick.
81/// @param {String...} $keys - Keys to pick from the Map.
82/// @return {List} Map with only the keys provided.
83@function pick($map, $keys...) {
84 @return list.nth(split($map, $keys...), 2);
85}