This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_functions.scss
200 lines (162 loc) · 3.37 KB
/
_functions.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SassySVG: helpers functions
// ---------------------------
////
//// @group helpers
//// @access private
////
/// Returns a list of all keys in a map.
///
/// @param {Map} $map
/// @param {String} $separator (space) - space | comma | auto
///
/// @return {List}
@function _map-keys(
$map,
$separator: space
) {
@return join((), map-keys($map), $separator);
}
/// Returns a list of all values in a map.
///
/// @param {Map} $map
/// @param {String} $separator - space | comma | auto
///
/// @return {List}
@function _map-values(
$map,
$separator: space
) {
@return join((), map-values($map), $separator);
}
/// Test whether `$value` is empty.
///
/// @param {*} $value
///
/// @return {Bool}
@function empty($value) {
@if not $value
or $value == ""
or $value == 0
or $value == ()
or length($value) == 0 {
@return true;
}
@return false;
}
/// Join `$list` into a string.
///
/// @param {List} $list - the list to convert
/// @param {String} $glue ('') - a string to separate each element of the list
///
/// @return {String}
@function _join(
$list,
$glue: ''
) {
$length: length($list);
$result: '';
@if $length < 1 {
@return $result;
}
@for $i from 1 through $length {
$result: $result
+ nth($list, $i)
+ if($i != $length, $glue, '');
}
@return $result;
}
/// A Sass micro-templating function.
///
/// @param {String} $format
/// @param {Arglist | Map} $args
///
/// @return {String}
@function template(
$format,
$args...
) {
$result: $format;
@each $key, $value in keywords($args) {
$token: "{{#{$key}}}";
$length: str-length($token);
$index: str-index($result, $token);
@if $index {
$result: if($index > 1, str-slice($result, 1, $index - 1), "")
+ $value
+ str-slice($result, ($index + $length));
@if str-index($result, $token) {
$result: template($result, $args...);
}
}
}
@return $result;
}
/// Test whether `$value` looks like a file.
/// ---
/// @param {String} $value
/// @param {String} $extension ('svg')
/// ---
/// @return {Boolean}
@function is-file(
$value,
$extension: svg
) {
$ext: '.#{$extension}';
$length: str-length($ext);
@return type-of($value) == string
and $value != ''
and not not str-index($value, $ext)
and str-slice($value, -$length) == $ext;
}
/// Recursively fetch deep value from nested map.
///
/// @author Kitty Giraudel
/// @source https://gist.github.com/KittyGiraudel/9933331
///
/// @param {Map} $map
/// @param {List} $keys
///
/// @return {*}
@function map-fetch(
$map,
$keys
) {
@each $key in $keys {
$map: map-get($map, $key);
}
@return $map;
}
/// Set or update a deep nested value in `$map`.
///
/// @param {Map} $map
/// @param {List} $keys
/// @param {Map} $new
///
/// @return {Map}
@function map-deep-merge(
$map,
$keys,
$new
) {
$orig: $map;
$cache: ();
$length: length($keys);
$last: nth($keys, $length);
@each $key in $keys {
$map: map-get($map, $key);
@if $key == $last {
$map: map-merge($map, $new);
}
$cache: append($cache, $map);
}
$i: $length;
@while $i > 1 {
$prev: $i - 1;
$cache: set-nth($cache, $prev, map-merge(
nth($cache, $prev),
(nth($keys, $i): nth($cache, $i))
));
$i: $prev;
}
@return map-merge($orig, (nth($keys, 1): nth($cache, 1)));
}