sass-list-maps 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.md +203 -0
- data/lib/sass-list-maps.rb +8 -0
- data/sass-list-maps.scss +90 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8a0f54a6344aa38d278a82007ba51a649e0e4240
|
4
|
+
data.tar.gz: 1e69cbbe0bbeb61619eab4f4e9ab76357aa4df86
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0be83f8ea160263dcc36de8d13d1dffe5e78c739c5d44ec40df1d297bddaeea380eb195ad6a193524be3fd980c487f103ec59878064e89f63dd1c14d7c3f13cb
|
7
|
+
data.tar.gz: 2e6fba912d4781c1d78469e20455d3db60c2ae597365584b297ab780919881c8ee3c4cf94152f6e5b96baba9f8694e9f4dec6496b9572ab76c6cf3624ced070e
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Lu Nelson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
## Sass List–Maps
|
2
|
+
|
3
|
+
Forward-compatible list-based map (hash) functionality for [libsass](https://github.com/hcatlin/libsass) and [ruby-sass](http://sass-lang.com/) < 3.3.x.
|
4
|
+
|
5
|
+
![](sass-hash.jpg)
|
6
|
+
|
7
|
+
*Some Sass hash.*
|
8
|
+
|
9
|
+
##### Update: You can now install this code as a Bower package:
|
10
|
+
|
11
|
+
```sh
|
12
|
+
bower install sass-list-maps
|
13
|
+
```
|
14
|
+
|
15
|
+
### Introduction
|
16
|
+
|
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.rc1 with new syntax and functions; but for earlier ruby-sass versions—and for the increasingly popular ultra-fast 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 and syntax as closely as possible, using the `list` data-type. Additionally, some functions are provided to extend this map functionality with nested getting and merging/setting.
|
18
|
+
|
19
|
+
\* objects (as in javascript) are not exactly the same thing as maps and hashes, but for these purposes close enough.
|
20
|
+
|
21
|
+
### 'List-map' syntax
|
22
|
+
|
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 to work with these functions. The syntax I recommend keeps as close as possible to that of native maps, but differs in that there are of course no colons (`:`), and the placement of commas is more critical (trailing commas—i.e. 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
|
+
|
25
|
+
|
26
|
+
```scss
|
27
|
+
/* example of a list-map -- compatible with any
|
28
|
+
version/compiler of sass including 3.3+ */
|
29
|
+
|
30
|
+
$list-map: ( alpha 1, beta 2, gamma 3 );
|
31
|
+
|
32
|
+
/* example of native map -- would cause an error
|
33
|
+
in any version/compiler other than ruby-sass 3.3+
|
34
|
+
-- notice colons and trailing comma */
|
35
|
+
|
36
|
+
$native-map: ( alpha: 1, beta: 2, gamma: 3,);
|
37
|
+
```
|
38
|
+
|
39
|
+
A list-map is a list of pairs: it can be of any length, but each item in that list must be a list of two. The first item in each pair is the 'key', the second the 'value'. This 'value' can be a primitive (number, boolean, string) or a list (can also be another list-map). A 'nested' list-map (list-map within a list-map) therefore, looks like this in its simplest form (compared again to a native map):
|
40
|
+
|
41
|
+
```scss
|
42
|
+
$list-map-z: (
|
43
|
+
alpha (
|
44
|
+
beta (
|
45
|
+
gamma 3
|
46
|
+
)
|
47
|
+
)
|
48
|
+
);
|
49
|
+
|
50
|
+
$native-map-z: (
|
51
|
+
alpha: (
|
52
|
+
beta: (
|
53
|
+
gamma: 3,
|
54
|
+
),
|
55
|
+
),
|
56
|
+
);
|
57
|
+
```
|
58
|
+
|
59
|
+
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' ruby-sass' 3.3+ map logic to work with lists.
|
60
|
+
|
61
|
+
### 'List-map' functions
|
62
|
+
|
63
|
+
The functions for 'list-maps' have the same names as the native map functions in ruby-sass 3.3+. Therefore, the following code assume a sass environment other than ruby running sass 3.3.rc1 or higher, where they would otherwise conflict.
|
64
|
+
|
65
|
+
#### Basic
|
66
|
+
|
67
|
+
All current ruby-sass map functions—as of this writing—are implemented, including `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 just lists anyway.
|
68
|
+
|
69
|
+
##### 1. `map-keys($list)`, `map-values($list)`, `map-has-key($list, $key)`
|
70
|
+
|
71
|
+
```scss
|
72
|
+
@import "sass-list-maps";
|
73
|
+
|
74
|
+
$list-map: ( alpha 1, beta 2, gamma 3 );
|
75
|
+
|
76
|
+
.demo {
|
77
|
+
out: map-keys($list-map); //-> alpha, beta, gamma
|
78
|
+
out: map-values($list-map); //-> 1, 2, 3
|
79
|
+
out: map-has-key($list-map, gamma); //-> true
|
80
|
+
out: map-has-key($list-map, delta); //-> false
|
81
|
+
}
|
82
|
+
```
|
83
|
+
|
84
|
+
##### 2. `map-get($list, $key)`
|
85
|
+
|
86
|
+
```scss
|
87
|
+
@import "sass-list-maps";
|
88
|
+
|
89
|
+
$list-map: ( alpha 1, beta 2, gamma 3 );
|
90
|
+
|
91
|
+
.demo {
|
92
|
+
out: map-get($list-map, alpha); //-> 1
|
93
|
+
out: map-get($list-map, beta); //-> 2
|
94
|
+
out: map-get($list-map, gamma); //-> 3
|
95
|
+
}
|
96
|
+
```
|
97
|
+
|
98
|
+
##### 3. `map-merge($list1, $list2), map-remove($list, $key)`
|
99
|
+
|
100
|
+
```scss
|
101
|
+
@import "sass-list-maps";
|
102
|
+
|
103
|
+
$list-map: ( alpha 1, beta 2, gamma 3 );
|
104
|
+
|
105
|
+
$new-map: map-merge($list-map, list(gamma 4));
|
106
|
+
// -> $new-map = ( alpha 1, beta 2, gamma 4 )
|
107
|
+
|
108
|
+
$short-map: map-remove($list-map, alpha);
|
109
|
+
// -> $short-map = ( beta 2, gamma 3)
|
110
|
+
```
|
111
|
+
|
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 specify 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()` helper function (included) if you want to create a map-list with only one pair.
|
113
|
+
|
114
|
+
#### Advanced
|
115
|
+
|
116
|
+
In addition to list-map versions of ruby-sass 3.3+ core map functions, this repo includes 'depth' versions of `map-get()` and `map-merge()`, suffixed with `-z`.
|
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.
|
121
|
+
|
122
|
+
##### 4. `map-get-z($list, $keys...)`
|
123
|
+
|
124
|
+
```scss
|
125
|
+
@import "sass-list-maps";
|
126
|
+
|
127
|
+
$list-map-z: (
|
128
|
+
alpha (
|
129
|
+
beta (
|
130
|
+
gamma 3
|
131
|
+
)
|
132
|
+
)
|
133
|
+
);
|
134
|
+
|
135
|
+
.demo {
|
136
|
+
out: map-get-z($list-map-z, alpha); // -> ( beta ( gamma 3 ) )
|
137
|
+
out: map-get-z($list-map-z, alpha, beta); // -> ( gamma 3 )
|
138
|
+
out: map-get-z($list-map-z, alpha, beta, gamma); // -> 3
|
139
|
+
}
|
140
|
+
```
|
141
|
+
|
142
|
+
##### 5. `map-merge-z($list, $keys-and-value...)`
|
143
|
+
|
144
|
+
```scss
|
145
|
+
@import "sass-list-maps";
|
146
|
+
|
147
|
+
$list-map-z: (
|
148
|
+
alpha (
|
149
|
+
beta (
|
150
|
+
gamma 3
|
151
|
+
)
|
152
|
+
)
|
153
|
+
);
|
154
|
+
|
155
|
+
$new-map1-z: map-merge-z($list-map-z, delta);
|
156
|
+
// -> ( alpha ( beta ( gamma 3 ) ), ( delta ( ) ) )
|
157
|
+
$new-map2A-z: map-merge-z($list-map-z, delta, epsilon);
|
158
|
+
// -> ( alpha ( beta ( gamma 3 ) ), ( delta epsilon ) )
|
159
|
+
$new-map2B-z: map-merge-z($list-map-z, list(delta epsilon));
|
160
|
+
// -> ( alpha ( beta ( gamma 3 ) ), ( delta epsilon ) )
|
161
|
+
$new-map3-z: map-merge-z($list-map-z, (delta 4, epsilon 5));
|
162
|
+
// -> ( alpha ( beta ( gamma 3 ) ), ( delta 4 ), ( epsilon 5 ) )
|
163
|
+
$new-map4-z: map-merge-z($list-map-z, delta, epsilon, 5);
|
164
|
+
// -> ( alpha ( beta ( gamma 3 ) ), ( delta ( epsilon 5 ) ) )
|
165
|
+
```
|
166
|
+
|
167
|
+
#### Customization
|
168
|
+
|
169
|
+
Note that in the above examples, the `-z` suffixed functions are effectively identical to their 'simple' counterparts if given only two arguments. This means they can actually replace them. Moreover, `map-merge-z($list, $keys-and-value...)` and `map-merge-z($list1, $list2)` argument patterns are also interchangeable. This means that `map-merge-z()` can also be treated as if it were `map-set-z($list, $keys..., $value)` if you prefer the semantics of that naming. My own preference is to write the following and call it a day:
|
170
|
+
|
171
|
+
```scss
|
172
|
+
@function get($args...) {
|
173
|
+
@return map-get-z($args...);
|
174
|
+
}
|
175
|
+
@function merge($args...) {
|
176
|
+
@return map-merge-z($args...);
|
177
|
+
}
|
178
|
+
@function set($args...) {
|
179
|
+
@return map-merge-z($args...);
|
180
|
+
}
|
181
|
+
```
|
182
|
+
|
183
|
+
### Caveats
|
184
|
+
|
185
|
+
There are a few points that bear mentioning/repeating:
|
186
|
+
|
187
|
+
* operating on global variables in libsass and in ruby-sass versions before 3.3.x works differently than in 3.3+. You can make changes to global variables from inside a mixin scope but you can't create them from there. This has implications for mixins that do operations on global list-maps.
|
188
|
+
* as noted, the 'list-map' syntax is less forgiving than that of native maps (watch the commas). Also, it lacks any error-checking (e.g. native maps will produce a warning if you have duplicate keys). And obviously fancy features of native maps such as passing a map to a function in the form `my-function($map...)` whereupon you can reference the key/value elements inside the function as if they were named variables, doesn't work with list-maps.
|
189
|
+
* as noted, the `list()` function is required if you want to make a list-map with only one pair, since Sass has no short way of specifying a list of a list like that (e.g. `$list: ((key value));` doesn't work, you need `$list: list(key value);`).
|
190
|
+
* as of this writing, this code contains no test-suites or inline error-catches or warnings of any kind. I've been using it in my own work but there are surely edge-cases I haven't seen. I welcome reports and contributions.
|
191
|
+
|
192
|
+
### To-Dos
|
193
|
+
|
194
|
+
* Make a depth-based version of `map-remove()`
|
195
|
+
* ~~Make this in to a bower package~~ *done*
|
196
|
+
* Make this in to a gem
|
197
|
+
* Push a native maps version of the 'advanced' functions above
|
198
|
+
|
199
|
+
### Acknowledgements
|
200
|
+
|
201
|
+
First and foremost, my 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 painless (relatively).
|
202
|
+
|
203
|
+
Also acknowledgements to @HugoGiraudel for [SassyLists](http://sassylists.com/), from which I borrowed the `slice()` and `replace-nth()` list functions, and his `debug()` function, 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
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
// list helper functions (some from SassyLists)
|
2
|
+
|
3
|
+
@function list($args...) {
|
4
|
+
$output: ();
|
5
|
+
@each $arg in $args { $output: append($output, $arg); }
|
6
|
+
@return $output;
|
7
|
+
}
|
8
|
+
@function unlist($list) {
|
9
|
+
@return if(length($list) == 1, nth($list, 1), $list);
|
10
|
+
}
|
11
|
+
@function slice($list, $start: 1, $end: length($list), $sep: 'comma') {
|
12
|
+
$output: ();
|
13
|
+
@for $i from $start through $end {
|
14
|
+
$output: append($output, nth($list, $i), $sep); }
|
15
|
+
@return $output;
|
16
|
+
}
|
17
|
+
@function replace-nth($list, $value, $index, $sep: 'comma') {
|
18
|
+
$output: ();
|
19
|
+
@for $i from 1 through length($list) {
|
20
|
+
@if $i == $index { $output: append($output, $value, $sep); }
|
21
|
+
@else { $output: append($output, nth($list, $i), $sep); } }
|
22
|
+
@return $output;
|
23
|
+
}
|
24
|
+
|
25
|
+
// list-map helper functions, purely semantic
|
26
|
+
|
27
|
+
@function tuple-key($tuple) { @return nth($tuple, 1); }
|
28
|
+
@function tuple-value($tuple) { @return nth($tuple, 2); }
|
29
|
+
|
30
|
+
// list-map versions of map-keys(), -values() and -has-key() functions
|
31
|
+
|
32
|
+
@function map-keys($list) {
|
33
|
+
$output: ();
|
34
|
+
@each $tuple in $list { $output: append($output, tuple-key($tuple), 'comma'); }
|
35
|
+
@return $output;
|
36
|
+
}
|
37
|
+
@function map-values($list) {
|
38
|
+
$output: ();
|
39
|
+
@each $tuple in $list { $output: append($output, tuple-value($tuple), 'comma'); }
|
40
|
+
@return $output;
|
41
|
+
}
|
42
|
+
@function map-has-key($list, $key) {
|
43
|
+
@each $tuple in $list { @if tuple-key($tuple) == $key { @return true; } }
|
44
|
+
@return false;
|
45
|
+
}
|
46
|
+
|
47
|
+
// list-map versions of map-get(), -merge() and -remove()
|
48
|
+
|
49
|
+
@function map-get($list, $key) {
|
50
|
+
@if length($list) == 0 { @return null; }
|
51
|
+
@else if length(nth($list, 1)) == 1 { @if tuple-key($list) == $key { @return tuple-value($list); } }
|
52
|
+
@else { @each $tuple in $list { @if tuple-key($tuple) == $key { @return tuple-value($tuple); } } }
|
53
|
+
@return null;
|
54
|
+
}
|
55
|
+
@function map-merge($list1, $list2) {
|
56
|
+
$keys1: map-keys($list1);
|
57
|
+
@each $tuple in $list2 {
|
58
|
+
$index: index($keys1, tuple-key($tuple));
|
59
|
+
@if $index { $list1: replace-nth($list1, $tuple, $index); }
|
60
|
+
@else { $list1: append($list1, $tuple, 'comma'); } }
|
61
|
+
@return $list1;
|
62
|
+
}
|
63
|
+
@function map-remove($list, $key) {
|
64
|
+
$keys: map-keys($list); $out: ();
|
65
|
+
@for $n from 1 through length($list) {
|
66
|
+
@if nth($keys, $n) != $key { $out: append($out, nth($list, $n), 'comma'); } }
|
67
|
+
@return $out;
|
68
|
+
}
|
69
|
+
|
70
|
+
// advanced: map-get-z() and map-merge-z()
|
71
|
+
|
72
|
+
@function map-get-z($list, $keys...) {
|
73
|
+
@each $key in $keys {
|
74
|
+
@if $list == null { @return null; }
|
75
|
+
@else { $list: map-get($list, $key); } }
|
76
|
+
@return $list;
|
77
|
+
}
|
78
|
+
@function map-merge-z($list, $keys-and-value...) {
|
79
|
+
$arg-length: length($keys-and-value);
|
80
|
+
$value: nth($keys-and-value, $arg-length);
|
81
|
+
$key-length: $arg-length - 1;
|
82
|
+
@if $key-length == 0 { $value: if(type-of($value) == 'list', map-merge($list, $value), map-merge($list, list($value ()))); }
|
83
|
+
@else { $start: if(type-of($value) == 'list', 0, 1);
|
84
|
+
@for $i from $start through $key-length {
|
85
|
+
$new-list: (); $old-list: ();
|
86
|
+
@if $i == 0 { $new-list: $value; } @else { $new-list: list(nth($keys-and-value, $key-length + 1 - $i) $value); }
|
87
|
+
@if $i == $key-length { $old-list: $list; } @else { $old-list: map-get-z($list, slice($keys-and-value, 1, $key-length - $i)...) or (); }
|
88
|
+
$value: map-merge($old-list, $new-list); } }
|
89
|
+
@return $value;
|
90
|
+
}
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sass-list-maps
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lu Nelson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: compass
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.12.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.12.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sass
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.2.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.2.0
|
41
|
+
description: Forward-compatible maps-functionality for all versions/compilers of sass
|
42
|
+
which do not have a native map data-type
|
43
|
+
email: lunelson@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
50
|
+
- lib/sass-list-maps.rb
|
51
|
+
- sass-list-maps.scss
|
52
|
+
homepage: https://github.com/lunelson/sass-list-maps
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.2.1
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: maps-as-lists for libsass and ruby-sass <3.3
|
76
|
+
test_files: []
|