sass-list-maps 0.9.3 → 0.9.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -12
- data/_sass-list-maps.scss +148 -0
- metadata +3 -3
- data/sass-list-maps.scss +0 -99
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c97d160f874130a49e80f8d7aa48d669330a12b
|
4
|
+
data.tar.gz: 696c4f6fd2d0b64dab79e981fd54b59e55ca4977
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 03e109aef6c8fa11da09c5e8ffab01d1bd45eb7a222e033b45cf6d48e0474e1d87e6cbf032114331a5114959d2ca0de9f582e928f6143ffcbb81f87c6373fb91
|
7
|
+
data.tar.gz: 95b7fd14ebe94db84ee5f767e2e64e8bbfa06bd1bfffa182691e780d3298a7e2dee8c5f7a7c8145ea6a1c8012414a92844d4dd62b9f758aa29fbadf768ce0a32
|
data/README.md
CHANGED
@@ -4,22 +4,21 @@ Forward-compatible map (hash) functionality for [libsass](https://github.com/hca
|
|
4
4
|
|
5
5
|
![](sass-hash.jpg)
|
6
6
|
|
7
|
-
*Some Sass hash. Not everyone likes this joke.*
|
8
|
-
|
9
7
|
###### Latest Updates
|
10
8
|
|
11
|
-
* 0.9.
|
9
|
+
* 0.9.5 -- now handles single-pair lists automatically. This means no more need for `list()` or `zip()` functions, which are now deprecated.
|
10
|
+
* 0.9.3 -- <del>deprecated `list()` function in favor of native `zip()` for creating single-pair list-maps</del> this was the best
|
12
11
|
* 0.9.2 -- improved merge performance with rewritten `set-nth()` function
|
13
12
|
* 0.9.2 -- included `get()`, `merge()`, and `set()` aliases by default
|
14
13
|
* now listed at the [sache.in](http://www.sache.in/) directory of Sass & Compass Extensions
|
15
14
|
|
16
15
|
###### Try it out
|
17
16
|
|
18
|
-
You can test-drive these functions [at Sassmeister in this pre-loaded gist](http://sassmeister.com/gist/8645654)
|
17
|
+
You can test-drive these functions [at Sassmeister in this pre-loaded gist](http://sassmeister.com/gist/8645654). NB: the libsass version at Sassmeister might be a couple of point-releases behind this repo.
|
19
18
|
|
20
19
|
###### Install it
|
21
20
|
|
22
|
-
Sass List-Maps can be installed as a Bower component or a gem (Compass extension)
|
21
|
+
Sass List-Maps can be installed as a Bower component or a gem (Compass extension):
|
23
22
|
|
24
23
|
```sh
|
25
24
|
# installation with bower
|
@@ -29,15 +28,17 @@ bower install sass-list-maps
|
|
29
28
|
gem install sass-list-maps
|
30
29
|
```
|
31
30
|
|
31
|
+
You can also just fork or download this repo or cut-and-paste.
|
32
|
+
|
32
33
|
### Introduction
|
33
34
|
|
34
|
-
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
|
35
|
+
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 a new syntax and dedicated 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 using the `list` data-type. Additional functions are also provided to extend this functionality with nested getting and merging/setting.
|
35
36
|
|
36
37
|
*\* objects (as in javascript) are not exactly the same thing as maps and hashes, but for these purposes close enough.*
|
37
38
|
|
38
39
|
### 'List-map' syntax
|
39
40
|
|
40
|
-
List-maps—as
|
41
|
+
List-maps—as opposed to native maps—are like any other list in Sass but must be formatted specifically 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):
|
41
42
|
|
42
43
|
|
43
44
|
```scss
|
@@ -53,7 +54,7 @@ in any version/compiler other than ruby-sass 3.3+
|
|
53
54
|
$native-map: ( alpha: 1, beta: 2, gamma: 3,);
|
54
55
|
```
|
55
56
|
|
56
|
-
|
57
|
+
As a list of pairs, 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'. This 'value' however, can be of any type—including another list (of pairs)—which allows list-maps to form nested or tree-like data structures. Such a 'nested' list-map looks like this (compared again to a native map):
|
57
58
|
|
58
59
|
```scss
|
59
60
|
$list-map-z: (
|
@@ -119,18 +120,18 @@ $list-map: ( alpha 1, beta 2, gamma 3 );
|
|
119
120
|
|
120
121
|
$list-map: ( alpha 1, beta 2, gamma 3 );
|
121
122
|
|
122
|
-
$new-map: map-merge($list-map,
|
123
|
+
$new-map: map-merge($list-map, gamma 4);
|
123
124
|
// -> $new-map = ( alpha 1, beta 2, gamma 4 )
|
124
125
|
|
125
126
|
$short-map: map-remove($list-map, alpha);
|
126
127
|
// -> $short-map = ( beta 2, gamma 3)
|
127
128
|
```
|
128
129
|
|
129
|
-
**NB**:
|
130
|
+
**NB**: you might notice in the second example above, that the second argument to map-merge isn't really a 'list-map' it's just a list of two items. This is the so-called "single item" list conundrum in Sass which is a bit tricky, but these functions as of 0.9.5 handle this case automatically.
|
130
131
|
|
131
132
|
#### Advanced
|
132
133
|
|
133
|
-
In addition to the core ruby-sass 3.3+ map functions, this library includes '
|
134
|
+
In addition to the core ruby-sass 3.3+ map functions, this library includes 'deep' or 'nested' versions of `map-get()` and `map-merge()` -- suffixed here with `-z`.
|
134
135
|
|
135
136
|
##### 4. `map-get-z($list, $keys...)`
|
136
137
|
|
@@ -207,7 +208,6 @@ There are a few points that bear mentioning/repeating:
|
|
207
208
|
|
208
209
|
* operating on global variables in libsass and in ruby-sass 3.2.x or earlier, works differently than in 3.3.x+: You can make changes to global variables from inside a mixin scope but you can't create them from there. There is no `!global` flag. This has implications for mixins that operate on global list-maps.
|
209
210
|
* as noted, the 'list-map' syntax is less forgiving than that of native maps (watch your 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.
|
210
|
-
* as noted, the `zip()` 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 with one list inside (e.g. `$list: ((key value));` doesn't work, you need `$list: zip(key, value);`).
|
211
211
|
* 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!
|
212
212
|
|
213
213
|
### To-Dos
|
@@ -0,0 +1,148 @@
|
|
1
|
+
// list helper functions
|
2
|
+
// 0.9.3 -- "list()" now deprecated in favor of "zip()".
|
3
|
+
// 0.9.5 -- "zip()" now deprecated as well: single-pair lists are handled automatically
|
4
|
+
|
5
|
+
@function list($args...) {
|
6
|
+
$output: ();
|
7
|
+
@each $arg in $args { $output: append($output, $arg, 'comma'); }
|
8
|
+
@return $output;
|
9
|
+
}
|
10
|
+
@function slice($list, $start: 1, $end: length($list), $sep: 'comma') {
|
11
|
+
$output: ();
|
12
|
+
@for $i from $start through $end {
|
13
|
+
$output: append($output, nth($list, $i), $sep); }
|
14
|
+
@return $output;
|
15
|
+
}
|
16
|
+
@function set-nth($list, $index, $value, $sep: 'comma') {
|
17
|
+
$length: length($list); $output: ();
|
18
|
+
@if $index > $length or $index <= 0 { @return $list; }
|
19
|
+
@if $index > 1 { @for $i from 1 through $index - 1 { $output: append($output, nth($list, $i), $sep); } }
|
20
|
+
$output: append($output, $value, $sep);
|
21
|
+
@if $length > 1 { @for $i from $index + 1 through $length { $output: append($output, nth($list, $i), $sep); } }
|
22
|
+
@return $output;
|
23
|
+
}
|
24
|
+
|
25
|
+
// list-map helper functions
|
26
|
+
// 0.9.3 -- key() and value() tentatively added as alternatives to 'tuple-' named funcs
|
27
|
+
|
28
|
+
@function tuple-key($tuple) { @return nth($tuple, 1); }
|
29
|
+
@function tuple-value($tuple) { @return nth($tuple, 2); }
|
30
|
+
@function key($tuple) { @return nth($tuple, 1); }
|
31
|
+
@function value($tuple) { @return nth($tuple, 2); }
|
32
|
+
|
33
|
+
// 0.9.5 -- added list-map-check()
|
34
|
+
|
35
|
+
@function list-map-check($list) { @if length($list) == 2 and length(nth($list, 1)) == 1 { @return append((), $list, 'comma'); } @return $list; }
|
36
|
+
|
37
|
+
// list-map versions of list-map-keys(), -values() and -has-key() functions
|
38
|
+
// 0.9.5 -- now all handle single- or double-wrapped list-maps (single-pair case) automatically
|
39
|
+
// 0.9.5 -- added map-prev-key() and map-next-key() functions
|
40
|
+
|
41
|
+
@function map-keys($list...) {
|
42
|
+
@if length($list) == 1 { $list: list-map-check($list...); }
|
43
|
+
@else { $list: list-map-check($list); } $output: ();
|
44
|
+
@each $tuple in $list { $output: append($output, tuple-key($tuple), 'comma'); }
|
45
|
+
@return $output;
|
46
|
+
}
|
47
|
+
@function map-values($list...) {
|
48
|
+
@if length($list) == 1 { $list: list-map-check($list...); }
|
49
|
+
@else { $list: list-map-check($list); } $output: ();
|
50
|
+
@each $tuple in $list { $output: append($output, tuple-value($tuple), 'comma'); }
|
51
|
+
@return $output;
|
52
|
+
}
|
53
|
+
@function map-has-key($list, $key) {
|
54
|
+
$list: list-map-check($list);
|
55
|
+
@each $tuple in $list { @if tuple-key($tuple) == $key { @return true; } }
|
56
|
+
@return false;
|
57
|
+
}
|
58
|
+
@function map-prev-key($list, $key) {
|
59
|
+
$list: list-map-check($list); $keys: map-keys($list);
|
60
|
+
@return nth($keys, index($keys, $key) - 1)
|
61
|
+
}
|
62
|
+
@function map-next-key($list, $key) {
|
63
|
+
$list: list-map-check($list); $keys: map-keys($list);
|
64
|
+
@return nth($keys, index($keys, $key) + 1)
|
65
|
+
}
|
66
|
+
|
67
|
+
// list-map versions of list-map-get(), -merge() and -remove()
|
68
|
+
// 0.9.5 -- list-map-check() has been integrated to handle single-pair inputs, and give correct outputs
|
69
|
+
|
70
|
+
@function map-get($list, $key, $check: true) {
|
71
|
+
@if $check { $list: list-map-check($list); } @each $tuple in $list {
|
72
|
+
@if tuple-key($tuple) == $key { @return list-map-check(tuple-value($tuple)); } }
|
73
|
+
// @if tuple-key($tuple) == $key { @return list-map-check($tuple); } }
|
74
|
+
@return null;
|
75
|
+
}
|
76
|
+
@function map-merge($list1, $list2, $check: true) {
|
77
|
+
@if $check { $list1: list-map-check($list1); $list2: list-map-check($list2); }
|
78
|
+
$keys1: map-keys($list1);
|
79
|
+
@each $tuple in $list2 {
|
80
|
+
$index: index($keys1, tuple-key($tuple));
|
81
|
+
@if $index { $list1: set-nth($list1, $index, $tuple); }
|
82
|
+
@else { $list1: append($list1, $tuple, 'comma'); } }
|
83
|
+
@return $list1;
|
84
|
+
}
|
85
|
+
@function map-remove($list, $key) {
|
86
|
+
$list: list-map-check($list);
|
87
|
+
$keys: map-keys($list); $output: ();
|
88
|
+
@for $n from 1 through length($list) {
|
89
|
+
@if nth($keys, $n) != $key { $output: append($output, nth($list, $n), 'comma'); } }
|
90
|
+
@return $output;
|
91
|
+
}
|
92
|
+
|
93
|
+
// deep/nested map functions: list-map-get-z() and list-map-merge-z()
|
94
|
+
// 0.9.5 -- list-map-check() has been integrated, implicitly or explicitly
|
95
|
+
|
96
|
+
@function map-get-z($list, $keys...) {
|
97
|
+
@if $list == null { @return null; }
|
98
|
+
$length: length($keys);
|
99
|
+
$list: map-get($list, nth($keys, 1));
|
100
|
+
@if $length > 1 {
|
101
|
+
@for $n from 2 through $length {
|
102
|
+
@if $list == null { @return null; }
|
103
|
+
$list: map-get($list, nth($keys, $n), false); }
|
104
|
+
@return $list; }
|
105
|
+
@else { @return $list; }
|
106
|
+
}
|
107
|
+
@function map-merge-z($list, $keys-and-value...) {
|
108
|
+
$arg-length: length($keys-and-value);
|
109
|
+
$value: nth($keys-and-value, $arg-length);
|
110
|
+
$key-length: $arg-length - 1;
|
111
|
+
$list: list-map-check($list);
|
112
|
+
@if $key-length == 0 {
|
113
|
+
$value: if(type-of($value) == 'list', map-merge($list, list-map-check($value), false), map-merge($list, append((), $value (), 'comma'), false)); }
|
114
|
+
@else { $start: 1; @if type-of($value) == 'list' { $start: 0; $value: list-map-check($value); }
|
115
|
+
@for $i from $start through $key-length {
|
116
|
+
$new-list: (); $old-list: ();
|
117
|
+
@if $i == 0 { $new-list: $value; } @else { $new-list: append((), nth($keys-and-value, $key-length + 1 - $i) $value, 'comma'); }
|
118
|
+
@if $i == $key-length { $old-list: $list; } @else { $old-list: map-get-z($list, slice($keys-and-value, 1, $key-length - $i)...) or (); }
|
119
|
+
$value: map-merge($old-list, $new-list); } }
|
120
|
+
@return $value;
|
121
|
+
}
|
122
|
+
|
123
|
+
// aliases added as of 0.9.2: unify and replace map-*() and map-*-z() variants
|
124
|
+
|
125
|
+
@function get($args...) { @return list-map-get-z($args...); }
|
126
|
+
@function merge($args...) { @return list-map-merge-z($args...); }
|
127
|
+
@function set($args...) { @return list-map-merge-z($args...); }
|
128
|
+
|
129
|
+
// new as of 0.9.5: map-inspect()
|
130
|
+
// experimental
|
131
|
+
|
132
|
+
@function map-inspect($list...) {
|
133
|
+
@if length($list) == 1 { $list: list-map-check($list...); }
|
134
|
+
@else { $list: list-map-check($list); }
|
135
|
+
$output: '('; $i: 1;
|
136
|
+
@each $tuple in $list {
|
137
|
+
@if length($tuple) != 2 {
|
138
|
+
$output: $output + '#{$tuple}'; }
|
139
|
+
@else {
|
140
|
+
$key: nth($tuple, 1);
|
141
|
+
$value: nth($tuple, 2);
|
142
|
+
@if type-of($value) == 'list' { $output: $output + '#{$key} #{map-inspect($value)}'; }
|
143
|
+
@else { $output: $output + '#{$key} #{$value}'; } }
|
144
|
+
@if $i < length(map-keys($list)) { $output: $output + ', '; }
|
145
|
+
$i: $i + 1;
|
146
|
+
}
|
147
|
+
@return $output + ')';
|
148
|
+
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lu Nelson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: compass
|
@@ -46,8 +46,8 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- LICENSE
|
48
48
|
- README.md
|
49
|
+
- _sass-list-maps.scss
|
49
50
|
- lib/sass-list-maps.rb
|
50
|
-
- sass-list-maps.scss
|
51
51
|
homepage: https://github.com/lunelson/sass-list-maps
|
52
52
|
licenses:
|
53
53
|
- MIT
|
data/sass-list-maps.scss
DELETED
@@ -1,99 +0,0 @@
|
|
1
|
-
// list helper functions
|
2
|
-
// 0.9.3 -- "list()" now deprecated in favor of "zip()".
|
3
|
-
|
4
|
-
@function list($args...) {
|
5
|
-
$output: ();
|
6
|
-
@each $arg in $args { $output: append($output, $arg); }
|
7
|
-
@return $output;
|
8
|
-
}
|
9
|
-
@function slice($list, $start: 1, $end: length($list), $sep: 'comma') {
|
10
|
-
$output: ();
|
11
|
-
@for $i from $start through $end {
|
12
|
-
$output: append($output, nth($list, $i), $sep); }
|
13
|
-
@return $output;
|
14
|
-
}
|
15
|
-
@function set-nth($list, $index, $value, $sep: 'comma') {
|
16
|
-
$length: length($list); $output: ();
|
17
|
-
@if $index > $length or $index <= 0 { @return $list; }
|
18
|
-
@for $i from 1 through $index - 1 { $output: append($output, nth($list, $i), $sep); }
|
19
|
-
$output: append($output, $value, $sep);
|
20
|
-
@for $i from $index + 1 through $length { $output: append($output, nth($list, $i), $sep); }
|
21
|
-
@return $output;
|
22
|
-
}
|
23
|
-
|
24
|
-
// list-map helper functions
|
25
|
-
// 0.9.3 -- key() and value() tentatively added as alternatives to 'tuple-' named funcs
|
26
|
-
|
27
|
-
@function tuple-key($tuple) { @return nth($tuple, 1); }
|
28
|
-
@function tuple-value($tuple) { @return nth($tuple, 2); }
|
29
|
-
@function key($tuple) { @return nth($tuple, 1); }
|
30
|
-
@function value($tuple) { @return nth($tuple, 2); }
|
31
|
-
|
32
|
-
// list-map versions of map-keys(), -values() and -has-key() functions
|
33
|
-
|
34
|
-
@function map-keys($list) {
|
35
|
-
$output: ();
|
36
|
-
@each $tuple in $list { $output: append($output, tuple-key($tuple), 'comma'); }
|
37
|
-
@return $output;
|
38
|
-
}
|
39
|
-
@function map-values($list) {
|
40
|
-
$output: ();
|
41
|
-
@each $tuple in $list { $output: append($output, tuple-value($tuple), 'comma'); }
|
42
|
-
@return $output;
|
43
|
-
}
|
44
|
-
@function map-has-key($list, $key) {
|
45
|
-
@each $tuple in $list { @if tuple-key($tuple) == $key { @return true; } }
|
46
|
-
@return false;
|
47
|
-
}
|
48
|
-
|
49
|
-
// list-map versions of map-get(), -merge() and -remove()
|
50
|
-
|
51
|
-
@function map-get($list, $key) {
|
52
|
-
@if length($list) == 0 { @return null; }
|
53
|
-
@else if length(nth($list, 1)) == 1 { @if tuple-key($list) == $key { @return tuple-value($list); } }
|
54
|
-
@else { @each $tuple in $list { @if tuple-key($tuple) == $key { @return tuple-value($tuple); } } }
|
55
|
-
@return null;
|
56
|
-
}
|
57
|
-
@function map-merge($list1, $list2) {
|
58
|
-
$keys1: map-keys($list1);
|
59
|
-
@each $tuple in $list2 {
|
60
|
-
$index: index($keys1, tuple-key($tuple));
|
61
|
-
@if $index { $list1: set-nth($list1, $index, $tuple); }
|
62
|
-
@else { $list1: append($list1, $tuple, 'comma'); } }
|
63
|
-
@return $list1;
|
64
|
-
}
|
65
|
-
@function map-remove($list, $key) {
|
66
|
-
$keys: map-keys($list); $out: ();
|
67
|
-
@for $n from 1 through length($list) {
|
68
|
-
@if nth($keys, $n) != $key { $out: append($out, nth($list, $n), 'comma'); } }
|
69
|
-
@return $out;
|
70
|
-
}
|
71
|
-
|
72
|
-
// deep/nested map functions: map-get-z() and map-merge-z()
|
73
|
-
|
74
|
-
@function map-get-z($list, $keys...) {
|
75
|
-
@each $key in $keys {
|
76
|
-
@if $list == null { @return null; }
|
77
|
-
@else { $list: map-get($list, $key); } }
|
78
|
-
@return $list;
|
79
|
-
}
|
80
|
-
@function map-merge-z($list, $keys-and-value...) {
|
81
|
-
$arg-length: length($keys-and-value);
|
82
|
-
$value: nth($keys-and-value, $arg-length);
|
83
|
-
$key-length: $arg-length - 1;
|
84
|
-
@if $key-length == 0 { $value: if(type-of($value) == 'list', map-merge($list, $value), map-merge($list, zip($value, ()))); }
|
85
|
-
@else { $start: if(type-of($value) == 'list', 0, 1);
|
86
|
-
@for $i from $start through $key-length {
|
87
|
-
$new-list: (); $old-list: ();
|
88
|
-
@if $i == 0 { $new-list: $value; } @else { $new-list: zip(nth($keys-and-value, $key-length + 1 - $i), $value); }
|
89
|
-
@if $i == $key-length { $old-list: $list; } @else { $old-list: map-get-z($list, slice($keys-and-value, 1, $key-length - $i)...) or (); }
|
90
|
-
$value: map-merge($old-list, $new-list); } }
|
91
|
-
@return $value;
|
92
|
-
}
|
93
|
-
|
94
|
-
// 0.9.2 -- aliases
|
95
|
-
// these replace map-*() and map-*-z() variants with a single universal function
|
96
|
-
|
97
|
-
@function get($args...) { @return map-get-z($args...); }
|
98
|
-
@function merge($args...) { @return map-merge-z($args...); }
|
99
|
-
@function set($args...) { @return map-merge-z($args...); }
|