diff options
author | Volpeon <git@volpeon.ink> | 2022-02-07 12:28:59 +0100 |
---|---|---|
committer | Volpeon <git@volpeon.ink> | 2022-02-07 12:28:59 +0100 |
commit | 4c9a427806b47b916849dbc98330fdbb469b659b (patch) | |
tree | 5dc85968bea6569aa4ff47c46e5d4994a24cdcaf | |
parent | Rename next-twin-element to next-twin-elem (diff) | |
download | iro-sass-4c9a427806b47b916849dbc98330fdbb469b659b.tar.gz iro-sass-4c9a427806b47b916849dbc98330fdbb469b659b.tar.bz2 iro-sass-4c9a427806b47b916849dbc98330fdbb469b659b.zip |
Replace deep map functions with built-ins
-rw-r--r-- | src/_functions.scss | 73 | ||||
-rw-r--r-- | src/_props.scss | 3 | ||||
-rw-r--r-- | test/_functions.scss | 49 | ||||
-rw-r--r-- | test/_props.scss | 53 |
4 files changed, 39 insertions, 139 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 | /// |
diff --git a/src/_props.scss b/src/_props.scss index db24dff..196e2f9 100644 --- a/src/_props.scss +++ b/src/_props.scss | |||
@@ -10,6 +10,7 @@ | |||
10 | /// @access public | 10 | /// @access public |
11 | //// | 11 | //// |
12 | 12 | ||
13 | @use 'sass:map'; | ||
13 | @use './functions'; | 14 | @use './functions'; |
14 | @use './contexts'; | 15 | @use './contexts'; |
15 | 16 | ||
@@ -127,7 +128,7 @@ $namespace-context-id: 'namespace' !default; | |||
127 | 128 | ||
128 | @if map-has-key($trees, $tree) { | 129 | @if map-has-key($trees, $tree) { |
129 | @if $merge { | 130 | @if $merge { |
130 | $map: functions.map-merge-recursive(map-get($trees, $tree), $map); | 131 | $map: map.deep-merge(map-get($trees, $tree), $map); |
131 | } @else { | 132 | } @else { |
132 | @error 'Property tree #{inspect($tree)} does already exist.'; | 133 | @error 'Property tree #{inspect($tree)} does already exist.'; |
133 | } | 134 | } |
diff --git a/test/_functions.scss b/test/_functions.scss index 2430f28..07561ef 100644 --- a/test/_functions.scss +++ b/test/_functions.scss | |||
@@ -41,55 +41,6 @@ | |||
41 | @include assert-equal(functions.map-get-default($map, 'index', 'nothing'), 'nothing', 'Get missing value'); | 41 | @include assert-equal(functions.map-get-default($map, 'index', 'nothing'), 'nothing', 'Get missing value'); |
42 | } | 42 | } |
43 | 43 | ||
44 | @include it('map-get-deep') { | ||
45 | $map: ( | ||
46 | 'key': 'value', | ||
47 | 'sub': ( | ||
48 | 'item1': 1, | ||
49 | 'item2': 2, | ||
50 | 'subsub': ( | ||
51 | 'item1': 11, | ||
52 | 'item2': 12, | ||
53 | ) | ||
54 | ) | ||
55 | ); | ||
56 | |||
57 | @include assert-equal(functions.map-get-deep($map, 'key'), map-get($map, 'key'), 'Get value in root level'); | ||
58 | @include assert-equal(functions.map-get-deep($map, 'sub' 'item1'), map-get(map-get($map, 'sub'), 'item1'), 'Get value in first level'); | ||
59 | @include assert-equal(functions.map-get-deep($map, 'sub' 'item2'), map-get(map-get($map, 'sub'), 'item2'), 'Get value in first level'); | ||
60 | @include assert-equal(functions.map-get-deep($map, 'sub' 'subsub' 'item1'), map-get(map-get(map-get($map, 'sub'), 'subsub'), 'item1'), 'Get value in second level'); | ||
61 | @include assert-equal(functions.map-get-deep($map, 'sub' 'subsub' 'item2'), map-get(map-get(map-get($map, 'sub'), 'subsub'), 'item2'), 'Get value in second level'); | ||
62 | } | ||
63 | |||
64 | @include it('map-merge-recursive') { | ||
65 | $map1: ( | ||
66 | 'key': 'value', | ||
67 | 'sub': ( | ||
68 | 'item1': 1, | ||
69 | 'item3': 2 | ||
70 | ) | ||
71 | ); | ||
72 | $map2: ( | ||
73 | 'another': 'item', | ||
74 | 'sub': ( | ||
75 | 'item1': 0, | ||
76 | 'item2': 1 | ||
77 | ) | ||
78 | ); | ||
79 | |||
80 | $expected: ( | ||
81 | 'key': 'value', | ||
82 | 'another': 'item', | ||
83 | 'sub': ( | ||
84 | 'item1': 0, | ||
85 | 'item2': 1, | ||
86 | 'item3': 2 | ||
87 | ) | ||
88 | ); | ||
89 | |||
90 | @include assert-equal(functions.map-merge-recursive($map1, $map2), $expected); | ||
91 | } | ||
92 | |||
93 | @include it('strip-unit') { | 44 | @include it('strip-unit') { |
94 | @include assert-true(unitless(functions.strip-unit(1em)), 'Remove unit from 1em'); | 45 | @include assert-true(unitless(functions.strip-unit(1em)), 'Remove unit from 1em'); |
95 | @include assert-true(unitless(functions.strip-unit(2rem)), 'Remove unit from 2rem'); | 46 | @include assert-true(unitless(functions.strip-unit(2rem)), 'Remove unit from 2rem'); |
diff --git a/test/_props.scss b/test/_props.scss index 71d88c5..d8b550b 100644 --- a/test/_props.scss +++ b/test/_props.scss | |||
@@ -1,5 +1,6 @@ | |||
1 | // sass-lint:disable empty-args | 1 | // sass-lint:disable empty-args |
2 | 2 | ||
3 | @use 'sass:map'; | ||
3 | @use 'true' as *; | 4 | @use 'true' as *; |
4 | @use '../src/functions'; | 5 | @use '../src/functions'; |
5 | @use '../src/props'; | 6 | @use '../src/props'; |
@@ -89,20 +90,20 @@ | |||
89 | @include assert-equal(props.store($map3, 'namespaced'), null, 'Save "namespaced" tree'); | 90 | @include assert-equal(props.store($map3, 'namespaced'), null, 'Save "namespaced" tree'); |
90 | } | 91 | } |
91 | 92 | ||
92 | @include assert-equal(props.get-static(--background), map-get($map1, --background), 'Get --background in default'); | 93 | @include assert-equal(props.get-static(--background), map-get($map1, --background), 'Get --background in default'); |
93 | @include assert-equal(props.get-static(--buttons --primary --background), map-get(map-get(map-get($map1, --buttons), --primary), --background), 'Get --buttons --primary --background in default'); | 94 | @include assert-equal(props.get-static(--buttons --primary --background), map-get($map1, --buttons, --primary, --background), 'Get --buttons --primary --background in default'); |
94 | @include assert-equal(props.get-static(--box, $default: false), false, 'Get nonexistent in default'); | 95 | @include assert-equal(props.get-static(--box, $default: false), false, 'Get nonexistent in default'); |
95 | 96 | ||
96 | @include assert-equal(props.get-static(--background, 'test'), map-get($map2, --background), 'Get --background in "test"'); | 97 | @include assert-equal(props.get-static(--background, 'test'), map-get($map2, --background), 'Get --background in "test"'); |
97 | @include assert-equal(props.get-static(--buttons --primary --background, 'test'), map-get(map-get(map-get($map2, --buttons), --primary), --background), 'Get --buttons --primary --background in "test"'); | 98 | @include assert-equal(props.get-static(--buttons --primary --background, 'test'), map-get($map2, --buttons, --primary, --background), 'Get --buttons --primary --background in "test"'); |
98 | @include assert-equal(props.get-static(--box, 'test', $default: false), false, 'Get nonexistent in "test"'); | 99 | @include assert-equal(props.get-static(--box, 'test', $default: false), false, 'Get nonexistent in "test"'); |
99 | 100 | ||
100 | @include assert-equal(props.get-static(--background, 'namespaced', $default: false), false, 'Get --background in "namespaced"'); | 101 | @include assert-equal(props.get-static(--background, 'namespaced', $default: false), false, 'Get --background in "namespaced"'); |
101 | @include assert-equal(props.get-static(--ns --background, 'namespaced'), map-get($map3, --background), 'Get --ns --background in "namespaced"'); | 102 | @include assert-equal(props.get-static(--ns --background, 'namespaced'), map-get($map3, --background), 'Get --ns --background in "namespaced"'); |
102 | @include props.namespace('ns') { | 103 | @include props.namespace('ns') { |
103 | @include assert-equal(props.get-static(--background, 'namespaced'), map-get($map3, --background), 'Get namespaced --background in "namespaced"'); | 104 | @include assert-equal(props.get-static(--background, 'namespaced'), map-get($map3, --background), 'Get namespaced --background in "namespaced"'); |
104 | @include assert-equal(props.get-static(--buttons --primary --background, 'namespaced'), map-get(map-get(map-get($map3, --buttons), --primary), --background), 'Get namespaced --buttons --primary --background in "namespaced"'); | 105 | @include assert-equal(props.get-static(--buttons --primary --background, 'namespaced'), map-get($map3, --buttons, --primary, --background), 'Get namespaced --buttons --primary --background in "namespaced"'); |
105 | @include assert-equal(props.get-static(--box, 'namespaced', $default: false), false, 'Get namespaced nonexistent in "namespaced"'); | 106 | @include assert-equal(props.get-static(--box, 'namespaced', $default: false), false, 'Get namespaced nonexistent in "namespaced"'); |
106 | } | 107 | } |
107 | 108 | ||
108 | @include assert-equal(props.clear(), null, 'Delete default tree'); | 109 | @include assert-equal(props.clear(), null, 'Delete default tree'); |
@@ -134,10 +135,10 @@ | |||
134 | @include assert-equal(props.store($map1), null, 'Save default tree'); | 135 | @include assert-equal(props.store($map1), null, 'Save default tree'); |
135 | @include assert-equal(props.store($map2, $merge: true), null, 'Overwrite default tree'); | 136 | @include assert-equal(props.store($map2, $merge: true), null, 'Overwrite default tree'); |
136 | 137 | ||
137 | @include assert-equal(props.get-static(), functions.map-merge-recursive($map1, $map2), 'After update, get whole map'); | 138 | @include assert-equal(props.get-static(), map.deep-merge($map1, $map2), 'After update, get whole map'); |
138 | @include assert-equal(props.get-static(--background), map-get($map2, --background), 'After update, get --background'); | 139 | @include assert-equal(props.get-static(--background), map-get($map2, --background), 'After update, get --background'); |
139 | @include assert-equal(props.get-static(--text), map-get($map2, --text), 'After update, get --text'); | 140 | @include assert-equal(props.get-static(--text), map-get($map2, --text), 'After update, get --text'); |
140 | @include assert-equal(props.get-static(--buttons --primary --text), map-get(map-get(map-get($map1, --buttons), --primary), --text), 'After update, get --buttons --primary --text'); | 141 | @include assert-equal(props.get-static(--buttons --primary --text), map-get($map1, --buttons, --primary, --text), 'After update, get --buttons --primary --text'); |
141 | 142 | ||
142 | @include assert-equal(props.clear(), null, 'Delete default tree'); | 143 | @include assert-equal(props.clear(), null, 'Delete default tree'); |
143 | } | 144 | } |
@@ -168,10 +169,10 @@ | |||
168 | @include expect { | 169 | @include expect { |
169 | --background: #{map-get($map, --background)}; | 170 | --background: #{map-get($map, --background)}; |
170 | --text: #{map-get($map, --text)}; | 171 | --text: #{map-get($map, --text)}; |
171 | --buttons--primary--background: #{map-get(map-get(map-get($map, --buttons), --primary), --background)}; | 172 | --buttons--primary--background: #{map-get($map, --buttons, --primary, --background)}; |
172 | --buttons--primary--text: #{map-get(map-get(map-get($map, --buttons), --primary), --text)}; | 173 | --buttons--primary--text: #{map-get($map, --buttons, --primary, --text)}; |
173 | --buttons--default--background: #{map-get(map-get(map-get($map, --buttons), --default), --background)}; | 174 | --buttons--default--background: #{map-get($map, --buttons, --default, --background)}; |
174 | --buttons--default--text: #{map-get(map-get(map-get($map, --buttons), --default), --text)}; | 175 | --buttons--default--text: #{map-get($map, --buttons, --default, --text)}; |
175 | } | 176 | } |
176 | 177 | ||
177 | @include props.clear; | 178 | @include props.clear; |
@@ -204,10 +205,10 @@ | |||
204 | @include expect { | 205 | @include expect { |
205 | --ns--background: #{map-get($map, --background)}; | 206 | --ns--background: #{map-get($map, --background)}; |
206 | --ns--text: #{map-get($map, --text)}; | 207 | --ns--text: #{map-get($map, --text)}; |
207 | --ns--buttons--primary--background: #{map-get(map-get(map-get($map, --buttons), --primary), --background)}; | 208 | --ns--buttons--primary--background: #{map-get($map, --buttons, --primary, --background)}; |
208 | --ns--buttons--primary--text: #{map-get(map-get(map-get($map, --buttons), --primary), --text)}; | 209 | --ns--buttons--primary--text: #{map-get($map, --buttons, --primary, --text)}; |
209 | --ns--buttons--default--background: #{map-get(map-get(map-get($map, --buttons), --default), --background)}; | 210 | --ns--buttons--default--background: #{map-get($map, --buttons, --default, --background)}; |
210 | --ns--buttons--default--text: #{map-get(map-get(map-get($map, --buttons), --default), --text)}; | 211 | --ns--buttons--default--text: #{map-get($map, --buttons, --default, --text)}; |
211 | } | 212 | } |
212 | 213 | ||
213 | @include props.clear; | 214 | @include props.clear; |
@@ -271,9 +272,9 @@ | |||
271 | @include assert-equal(props.get-static(--buttons --primary --background, 'second'), map-get($map1, --background), 'Get referenced value'); | 272 | @include assert-equal(props.get-static(--buttons --primary --background, 'second'), map-get($map1, --background), 'Get referenced value'); |
272 | @include assert-equal(props.get(--buttons --primary --background, 'second'), var(--buttons--primary--background), 'Get referenced value, native'); | 273 | @include assert-equal(props.get(--buttons --primary --background, 'second'), var(--buttons--primary--background), 'Get referenced value, native'); |
273 | 274 | ||
274 | @include assert-equal(props.get-static(--buttons --default, 'second'), map-get(map-get($map1, --buttons), --primary), 'Get referenced subtree, whole'); | 275 | @include assert-equal(props.get-static(--buttons --default, 'second'), map-get($map1, --buttons, --primary), 'Get referenced subtree, whole'); |
275 | @include assert-equal(props.get-static(--buttons --default --background, 'second'), map-get(map-get(map-get($map1, --buttons), --primary), --background), 'Get referenced subtree, inner value'); | 276 | @include assert-equal(props.get-static(--buttons --default --background, 'second'), map-get($map1, --buttons, --primary, --background), 'Get referenced subtree, inner value'); |
276 | @include assert-equal(props.get(--buttons --default --background, 'second'), var(--buttons--default--background), 'Get referenced subtree, native'); | 277 | @include assert-equal(props.get(--buttons --default --background, 'second'), var(--buttons--default--background), 'Get referenced subtree, native'); |
277 | 278 | ||
278 | @include assert('Native assignment') { | 279 | @include assert('Native assignment') { |
279 | @include output { | 280 | @include output { |
@@ -283,8 +284,8 @@ | |||
283 | @include expect { | 284 | @include expect { |
284 | --background: #{map-get($map2, --background)}; | 285 | --background: #{map-get($map2, --background)}; |
285 | --buttons--primary--background: #{map-get($map1, --background)}; | 286 | --buttons--primary--background: #{map-get($map1, --background)}; |
286 | --buttons--default--background: #{map-get(map-get(map-get($map1, --buttons), --primary), --background)}; | 287 | --buttons--default--background: #{map-get($map1, --buttons, --primary, --background)}; |
287 | --buttons--default--text: #{map-get(map-get(map-get($map1, --buttons), --primary), --text)}; | 288 | --buttons--default--text: #{map-get($map1, --buttons, --primary, --text)}; |
288 | } | 289 | } |
289 | } | 290 | } |
290 | 291 | ||