sass-list-maps 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +43 -31
- data/sass-list-maps.scss +15 -8
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fdc88d29473b92024c87d8ed0a9632d54da27d73
|
4
|
+
data.tar.gz: c73f8bd8f1d21d41ccdf37d2d4b252bd8bfc17eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9ae2bca742ff29fbee7744b30fa7afe64ceb311dfad0a33694dce51e174b5efffd356403305b1c5d67a627492d23a82aa402ddbeef4ec14ca1b8bbdefd277c0
|
7
|
+
data.tar.gz: 5bdf3f67eab7ffcdbdab4edda0ca876ca4484db9106f6d4898f1d7e448d9efcf11a341b5e959c21b2bbafa16f1d3400aa9e1c061926ad4a527fb60c7689cf451
|
data/README.md
CHANGED
@@ -1,26 +1,42 @@
|
|
1
1
|
## Sass List–Maps
|
2
2
|
|
3
|
-
Forward-compatible
|
3
|
+
Forward-compatible map (hash) functionality for [libsass](https://github.com/hcatlin/libsass) and [ruby-sass](http://sass-lang.com/) < 3.3.x using lists.
|
4
4
|
|
5
5
|
![](sass-hash.jpg)
|
6
6
|
|
7
|
-
*Some Sass hash.*
|
7
|
+
*Some Sass hash. Not everyone likes this joke somehow.*
|
8
8
|
|
9
|
-
|
9
|
+
###### Latest Updates
|
10
|
+
|
11
|
+
* 0.9.2 -- improved performance with `set-nth()` function
|
12
|
+
* 0.9.2 -- included `get()`, `merge()`, and `set()` aliases by default
|
13
|
+
* now listed at the [sache.in](http://www.sache.in/) directory of Sass & Compass Extensions
|
14
|
+
|
15
|
+
###### Try it out
|
16
|
+
|
17
|
+
You can test-drive these functions [at Sassmeister in this pre-loaded gist](http://sassmeister.com/gist/8645654), and you can use them in your own Sassmeister gists by setting the compiler to Sass 3.2.x or libsass and including the "Sass List–Maps" extension in the control panel.
|
18
|
+
|
19
|
+
###### Install it
|
20
|
+
|
21
|
+
Sass List-Maps can now be installed as a Bower component or a gem (Compass extension) as alternative to cloning, downloading or cutting and pasting from this repo:
|
10
22
|
|
11
23
|
```sh
|
24
|
+
# installation with bower
|
12
25
|
bower install sass-list-maps
|
26
|
+
|
27
|
+
# installation with rubygems, for compass
|
28
|
+
gem install sass-list-maps
|
13
29
|
```
|
14
30
|
|
15
31
|
### Introduction
|
16
32
|
|
17
|
-
Maps (also known as hashes or objects\*) allow dynamic creating, setting and retrieving of data. They are supported natively in ruby-sass as of version 3.3.
|
33
|
+
Maps (also known as hashes or objects\*) allow dynamic creating, setting and retrieving of data. They are supported natively in ruby-sass as of version 3.3.x with dedicated syntax and functions; but for earlier ruby-sass versions—and for the increasingly popular libsass C-based compiler (until the point at which maps are integrated there natively)—this is an alternative solution. These functions feature-match ruby-sass 3.3's map functionality as closely as possible using the `list` data-type. Additional functions are also provided to extend this functionality with nested getting and merging/setting.
|
18
34
|
|
19
|
-
|
35
|
+
*\* objects (as in javascript) are not exactly the same thing as maps and hashes, but for these purposes close enough.*
|
20
36
|
|
21
37
|
### 'List-map' syntax
|
22
38
|
|
23
|
-
List-maps—as I'll refer to them for the sake of clarity, as opposed to native maps—are like any other list in Sass but must be formatted
|
39
|
+
List-maps—as I'll refer to them for the sake of clarity, as opposed to native maps—are like any other list in Sass but must be formatted carefully, as a list of pairs. The formatting I recommend keeps as close as possible to the syntax of native maps, with the difference that there no colons (`:`), and the placement of commas is more critical (e.g. trailing commas—commas after the last item—are allowed in native maps and in lists in ruby-sass 3.3+ but not in any other version):
|
24
40
|
|
25
41
|
|
26
42
|
```scss
|
@@ -36,7 +52,7 @@ in any version/compiler other than ruby-sass 3.3+
|
|
36
52
|
$native-map: ( alpha: 1, beta: 2, gamma: 3,);
|
37
53
|
```
|
38
54
|
|
39
|
-
|
55
|
+
Being a list of pairs then, a list-map list can be of any length, but each item in that list must be a list of exactly two. The first of these is the 'key' and is usually a string; the second is the 'value' and can be of any type, including being another list (of pairs!). In this way list-maps can be nested. Such a 'nested' list-map looks like this in its simplest form (compared again to a native map):
|
40
56
|
|
41
57
|
```scss
|
42
58
|
$list-map-z: (
|
@@ -56,15 +72,15 @@ $native-map-z: (
|
|
56
72
|
);
|
57
73
|
```
|
58
74
|
|
59
|
-
It should be clear that list-maps and native maps are very similar
|
75
|
+
It should be clear that list-maps and native maps are very similar; in fact they are in principle the same. For this reason it was possible to 'reverse engineer' the map functions of ruby-sass' 3.3+ to use simple lists.
|
60
76
|
|
61
77
|
### 'List-map' functions
|
62
78
|
|
63
|
-
The functions for 'list-maps' have the same names as the
|
79
|
+
The functions for 'list-maps' have the same names as the map functions in ruby-sass >= 3.3.x, which means that if they were used in ruby-sass 3.3.x or higher they would conflict.Therefore, the following code assume a sass environment other than that (either ruby-sass <3.3 or libsass.
|
64
80
|
|
65
81
|
#### Basic
|
66
82
|
|
67
|
-
All
|
83
|
+
All of the current—as of this writing—map functions are implemented. These include `map-get($list, $key)`, `map-merge($list1, $list2)`, `map-remove($list, $key)`, `map-keys($list)`, `map-values($list)`, and `map-has-key($list, $key)`. As with native maps, standard list functions can also be used on list-maps since they are lists anyway.
|
68
84
|
|
69
85
|
##### 1. `map-keys($list)`, `map-values($list)`, `map-has-key($list, $key)`
|
70
86
|
|
@@ -109,18 +125,16 @@ $short-map: map-remove($list-map, alpha);
|
|
109
125
|
// -> $short-map = ( beta 2, gamma 3)
|
110
126
|
```
|
111
127
|
|
112
|
-
**NB**: notice the use of the `list()` function in the second example. This is due to the fact that Sass has no succinct way to
|
128
|
+
**NB**: notice the use of the `list()` function in the second example. This is due to the fact that Sass has no succinct way to create a list containing another list, if the containing list is only 1 element in length. Since a list-map must always be a list-of-lists—even if it only contains one item—you must use the `list()` function (included here) when creating list-maps of only one key-value pair.
|
113
129
|
|
114
130
|
#### Advanced
|
115
131
|
|
116
|
-
In addition to
|
117
|
-
|
118
|
-
The `map-get-z()` function will retrieve values from a list-map according to a chain of keys (similar to the way nested array/hash/object values are accessed in other languages);
|
119
|
-
|
120
|
-
...while the `map-merge-z()` function takes a chain of keys to indicate where (at what depth) to merge, but interprets its final argument as the value to be merged. This value can be of any type including being another list/list-map. Note that if only one key/value argument is passed and it is not a list, it is interpreted as the key, and an empty list is merged in as the value.
|
132
|
+
In addition to the core ruby-sass 3.3+ map functions, this repo includes 'depth' versions of `map-get()` and `map-merge()`, suffixed here with `-z`.
|
121
133
|
|
122
134
|
##### 4. `map-get-z($list, $keys...)`
|
123
135
|
|
136
|
+
The `map-get-z()` function will retrieve values from a list-map according to a chain of keys (similar to the way nested array/hash/object values are accessed in other languages):
|
137
|
+
|
124
138
|
```scss
|
125
139
|
@import "sass-list-maps";
|
126
140
|
|
@@ -141,6 +155,8 @@ $list-map-z: (
|
|
141
155
|
|
142
156
|
##### 5. `map-merge-z($list, $keys-and-value...)`
|
143
157
|
|
158
|
+
The `map-merge-z()` function takes a chain of keys to indicate where (at what depth) to merge, interpreting its final argument as the value to be merged. This value can be of any type including another list/list-map. Note that if only one key/value argument is passed and it is not a list, it is interpreted as the key, and an empty list is merged in as the value:
|
159
|
+
|
144
160
|
```scss
|
145
161
|
@import "sass-list-maps";
|
146
162
|
|
@@ -164,20 +180,16 @@ $new-map4-z: map-merge-z($list-map-z, delta, epsilon, 5);
|
|
164
180
|
// -> ( alpha ( beta ( gamma 3 ) ), ( delta ( epsilon 5 ) ) )
|
165
181
|
```
|
166
182
|
|
167
|
-
|
183
|
+
##### 6. Unified syntax
|
168
184
|
|
169
|
-
Note that in the above examples, the `-z` suffixed functions
|
185
|
+
Note that in the above examples, the `-z` suffixed functions work like their 'simple' counterparts if given only two arguments. This is by design, so that they can transparently replace them. Moreover, the `map-merge-z($list, $keys-and-value...)` and `map-merge-z($list1, $list2)` argument patterns are interchangeable. This means that `map-merge-z()` can be treated as if it were `map-set-z($list, $keys..., $value)`, which is a common alternative syntax when dealing with hashes. In light of this, the following aliased functions are provided—as of version 0.9.2—to unify and simplify the usage of map functions as much as is possible:
|
170
186
|
|
171
187
|
```scss
|
172
|
-
@function get($args...) {
|
173
|
-
|
174
|
-
}
|
175
|
-
|
176
|
-
|
177
|
-
}
|
178
|
-
@function set($args...) {
|
179
|
-
@return map-merge-z($args...);
|
180
|
-
}
|
188
|
+
@function get($args...) { @return map-get-z($args...); }
|
189
|
+
|
190
|
+
@function merge($args...) { @return map-merge-z($args...); }
|
191
|
+
|
192
|
+
@function set($args...) { @return map-merge-z($args...); }
|
181
193
|
```
|
182
194
|
|
183
195
|
### Caveats
|
@@ -192,12 +204,12 @@ There are a few points that bear mentioning/repeating:
|
|
192
204
|
### To-Dos
|
193
205
|
|
194
206
|
* Make a depth-based version of `map-remove()`
|
195
|
-
*
|
196
|
-
* Make this in to a gem
|
207
|
+
* <del>Make this in to a bower package</del> *done*
|
208
|
+
* <del>Make this in to a gem</del> *done*
|
197
209
|
* Push a native maps version of the 'advanced' functions above
|
198
210
|
|
199
211
|
### Acknowledgements
|
200
212
|
|
201
|
-
First and foremost,
|
213
|
+
First and foremost, gratitude to the core Sass devs (@nex3 and @chriseppstein) for their tireless advancement of the gold-standard of CSS pre-processing, and secondly to @jedfoster and @anotheruiguy for [Sassmeister](http://sassmeister.com/), which makes developing complex functions and mixins relatively painless.
|
202
214
|
|
203
|
-
Also acknowledgements to @HugoGiraudel for [SassyLists](http://sassylists.com/), from which I borrowed
|
215
|
+
Also acknowledgements to @HugoGiraudel for [SassyLists](http://sassylists.com/), from which I borrowed some functions early on, and especially `debug()`, without which I would not have been able to figure out what was going on (and going wrong) in ruby-sass 3.2 and libsass.
|
data/sass-list-maps.scss
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
// list helper functions
|
1
|
+
// list helper functions
|
2
2
|
|
3
3
|
@function list($args...) {
|
4
4
|
$output: ();
|
@@ -14,11 +14,12 @@
|
|
14
14
|
$output: append($output, nth($list, $i), $sep); }
|
15
15
|
@return $output;
|
16
16
|
}
|
17
|
-
@function
|
18
|
-
$output: ();
|
19
|
-
@
|
20
|
-
|
21
|
-
|
17
|
+
@function set-nth($list, $index, $value, $sep: 'comma') {
|
18
|
+
$length: length($list); $output: ();
|
19
|
+
@if $index > $length or $index <= 0 { @return $list; }
|
20
|
+
@for $i from 1 through $index - 1 { $output: append($output, nth($list, $i), $sep); }
|
21
|
+
$output: append($output, $value, $sep);
|
22
|
+
@for $i from $index + 1 through $length { $output: append($output, nth($list, $i), $sep); }
|
22
23
|
@return $output;
|
23
24
|
}
|
24
25
|
|
@@ -56,7 +57,7 @@
|
|
56
57
|
$keys1: map-keys($list1);
|
57
58
|
@each $tuple in $list2 {
|
58
59
|
$index: index($keys1, tuple-key($tuple));
|
59
|
-
@if $index { $list1:
|
60
|
+
@if $index { $list1: set-nth($list1, $index, $tuple); }
|
60
61
|
@else { $list1: append($list1, $tuple, 'comma'); } }
|
61
62
|
@return $list1;
|
62
63
|
}
|
@@ -87,4 +88,10 @@
|
|
87
88
|
@if $i == $key-length { $old-list: $list; } @else { $old-list: map-get-z($list, slice($keys-and-value, 1, $key-length - $i)...) or (); }
|
88
89
|
$value: map-merge($old-list, $new-list); } }
|
89
90
|
@return $value;
|
90
|
-
}
|
91
|
+
}
|
92
|
+
|
93
|
+
// aliases: keep it simple stupid
|
94
|
+
|
95
|
+
@function get($args...) { @return map-get-z($args...); }
|
96
|
+
@function merge($args...) { @return map-merge-z($args...); }
|
97
|
+
@function set($args...) { @return map-merge-z($args...); }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sass-list-maps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lu Nelson
|
@@ -38,8 +38,7 @@ dependencies:
|
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.2.0
|
41
|
-
description: Forward-compatible
|
42
|
-
which do not have a native map data-type
|
41
|
+
description: Forward-compatible map functionality for libsass and ruby-sass <3.3
|
43
42
|
email: lunelson@gmail.com
|
44
43
|
executables: []
|
45
44
|
extensions: []
|
@@ -72,5 +71,5 @@ rubyforge_project:
|
|
72
71
|
rubygems_version: 2.2.1
|
73
72
|
signing_key:
|
74
73
|
specification_version: 4
|
75
|
-
summary:
|
74
|
+
summary: Maps for libsass and ruby-sass <3.3
|
76
75
|
test_files: []
|