This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy path_map-ext.scss
126 lines (119 loc) · 3.74 KB
/
_map-ext.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Copyright 2021 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
@use 'sass:list';
@use 'sass:map';
// A collection of extensions to the sass:map module
// https://sass-lang.com/documentation/modules/map
/// Splits a Map into two separate Maps: one without the provided keys and one
/// exclusively with the provided keys.
///
/// @example - scss
/// $map: (
/// focus: blue,
/// focus-within: blue,
/// hover: teal,
/// active: green,
/// );
///
/// $pair: split($map, focus, focus-within);
/// // (
/// // (hover: teal, active: green),
/// // (focus: blue, focus-within: blue)
/// // );
///
/// @param {Map} $map - The Map to split.
/// @param {String...} $keys - Keys to split the Map by.
/// @return {List} A List pair with two new Maps: the first with the keys
/// removed and the second exclusively with the keys.
@function split($map, $keys...) {
$map1: ();
$map2: ();
@each $key, $value in $map {
@if list.index($keys, $key) {
$map2: map.set($map2, $key, $value);
} @else {
$map1: map.set($map1, $key, $value);
}
}
@return ($map1, $map2);
}
/// Picks provided keys from a Map.
///
/// @example - scss
/// $map: (
/// focus: blue,
/// focus-within: blue,
/// hover: teal,
/// active: green,
/// );
///
/// pick($map, hover, active);
/// // (hover: teal, active: green),
///
/// pick($map, (hover, active)...);
/// // (hover: teal, active: green),
///
/// @param {Map} $map - The Map to pick.
/// @param {String...} $keys - Keys to pick from the Map.
/// @return {List} Map with only the keys provided.
@function pick($map, $keys...) {
@return list.nth(split($map, $keys...), 2);
}
///
/// trim-keys returns the given map with any matching keys removed.
///
/// @example
/// trim-keys(('foo': red, 'bar': 5), ('foo')) = ('bar': 5)
///
/// @param {Map} $map - the map to rename.
/// @param {List} $keys - the key names to trim off.
/// @return {Map} the $map with any matching keys removed.
///
@function trim-keys($map, $keys) {
$out: map.merge($map, ());
@each $key, $value in $map {
@if list.index($keys, $key) {
$out: map.remove($out, $key);
}
}
@return $out;
}
///
/// rename-keys returns the given map with any matching keys renamed to the
/// given name.
///
/// @example
/// rename-keys(('foo': red), ('foo': 'bar')) = ('bar': red)
///
/// @param {Map} $map - the map to rename.
/// @param {Map} $rename - the renames to apply.
/// @return {Map} the $map with any matching keys renamed per $rename.
///
@function rename-keys($map, $rename) {
$out: trim-keys($map, map.keys($rename));
@each $from, $to in $rename {
@if map.has-key($map, $from) {
$out: map.set($out, $to, map.get($map, $from));
}
}
@return $out;
}