diff options
Diffstat (limited to 'src/_functions.scss')
-rw-r--r-- | src/_functions.scss | 73 |
1 files changed, 10 insertions, 63 deletions
diff --git a/src/_functions.scss b/src/_functions.scss index d091afa..236b548 100644 --- a/src/_functions.scss +++ b/src/_functions.scss | |||
@@ -9,6 +9,7 @@ | |||
9 | /// @access public | 9 | /// @access public |
10 | //// | 10 | //// |
11 | 11 | ||
12 | @use 'sass:map'; | ||
12 | @use 'sass:math'; | 13 | @use 'sass:math'; |
13 | @use './vars'; | 14 | @use './vars'; |
14 | 15 | ||
@@ -65,9 +66,11 @@ | |||
65 | @function list-slice($list, $start: 1, $end: length($list)) { | 66 | @function list-slice($list, $start: 1, $end: length($list)) { |
66 | $result: (); | 67 | $result: (); |
67 | 68 | ||
68 | @for $i from $start through $end { | 69 | @if $end >= $start { |
69 | @if $i != 0 { | 70 | @for $i from $start through $end { |
70 | $result: append($result, nth($list, $i), list-separator($list)); | 71 | @if $i != 0 { |
72 | $result: append($result, nth($list, $i), list-separator($list)); | ||
73 | } | ||
71 | } | 74 | } |
72 | } | 75 | } |
73 | 76 | ||
@@ -162,67 +165,11 @@ | |||
162 | /// | 165 | /// |
163 | /// @return {any} Either the value assigned to $key or $default | 166 | /// @return {any} Either the value assigned to $key or $default |
164 | /// | 167 | /// |
165 | @function map-get-default($map, $key, $default) { | 168 | @function map-get-default($map, $key, $keys...) { |
166 | @return if(map-has-key($map, $key), map-get($map, $key), $default); | 169 | $default: nth($keys, length($keys)); |
167 | } | 170 | $keys: list-slice($keys, 1, length($keys) - 1); |
168 | |||
169 | /// | ||
170 | /// Get the value for a map within a map (or deeper). | ||
171 | /// | ||
172 | /// @param {map} $map | ||
173 | /// @param {string | list} $key | ||
174 | /// @param {any} $default [null] | ||
175 | /// | ||
176 | /// @return {any} Either the value assigned to $key or $default | ||
177 | /// | ||
178 | @function map-get-deep($map, $key, $default: null) { | ||
179 | $value: null; | ||
180 | |||
181 | @if type-of($key) == list { | ||
182 | $value: $map; | ||
183 | |||
184 | @each $k in $key { | ||
185 | $value: map-get($value, $k); | ||
186 | |||
187 | @if $value == null { | ||
188 | @return $default; | ||
189 | } | ||
190 | } | ||
191 | } @else { | ||
192 | $value: map-get($map, $key); | ||
193 | 171 | ||
194 | @if $value == null { | 172 | @return if(map-has-key($map, $key, $keys...), map-get($map, $key, $keys...), $default); |
195 | @return $default; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | @return $value; | ||
200 | } | ||
201 | |||
202 | /// | ||
203 | /// Merge two maps recursively. | ||
204 | /// | ||
205 | /// @param {map} $map1 | ||
206 | /// @param {map} $map2 | ||
207 | /// | ||
208 | /// @return {map} The result of a recursive merge of $map1 and $map2 | ||
209 | /// | ||
210 | @function map-merge-recursive($map1, $map2) { | ||
211 | @if type-of($map1) != map or type-of($map2) != map { | ||
212 | @error 'Two maps expected.'; | ||
213 | } | ||
214 | |||
215 | $result: $map1; | ||
216 | |||
217 | @each $key, $value in $map2 { | ||
218 | @if type-of(map-get($result, $key)) == map and type-of($value) == map { | ||
219 | $result: map-merge($result, ($key: map-merge-recursive(map-get($result, $key), $value))); | ||
220 | } @else { | ||
221 | $result: map-merge($result, ($key: $value)); | ||
222 | } | ||
223 | } | ||
224 | |||
225 | @return $result; | ||
226 | } | 173 | } |
227 | 174 | ||
228 | /// | 175 | /// |