├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── _sass-list-maps.scss ├── bower.json ├── gulpfile.js ├── lib └── sass-list-maps.rb ├── nodemon.json ├── package.json ├── sache.json ├── sass-hash.jpg ├── sass-list-maps.gemspec └── test ├── test.css └── test.scss /.gitignore: -------------------------------------------------------------------------------- 1 | docs* 2 | *.gem 3 | *-cache 4 | *.sublime-* 5 | node_modules 6 | 7 | 8 | # Created by https://www.gitignore.io/api/sass,node,sublimetext,visualstudiocode 9 | 10 | ### Node ### 11 | # Logs 12 | logs 13 | *.log 14 | npm-debug.log* 15 | yarn-debug.log* 16 | yarn-error.log* 17 | 18 | # Runtime data 19 | pids 20 | *.pid 21 | *.seed 22 | *.pid.lock 23 | 24 | # Directory for instrumented libs generated by jscoverage/JSCover 25 | lib-cov 26 | 27 | # Coverage directory used by tools like istanbul 28 | coverage 29 | 30 | # nyc test coverage 31 | .nyc_output 32 | 33 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 34 | .grunt 35 | 36 | # Bower dependency directory (https://bower.io/) 37 | bower_components 38 | 39 | # node-waf configuration 40 | .lock-wscript 41 | 42 | # Compiled binary addons (http://nodejs.org/api/addons.html) 43 | build/Release 44 | 45 | # Dependency directories 46 | node_modules/ 47 | jspm_packages/ 48 | 49 | # Typescript v1 declaration files 50 | typings/ 51 | 52 | # Optional npm cache directory 53 | .npm 54 | 55 | # Optional eslint cache 56 | .eslintcache 57 | 58 | # Optional REPL history 59 | .node_repl_history 60 | 61 | # Output of 'npm pack' 62 | *.tgz 63 | 64 | # Yarn Integrity file 65 | .yarn-integrity 66 | 67 | # dotenv environment variables file 68 | .env 69 | 70 | 71 | ### Sass ### 72 | .sass-cache/ 73 | *.css.map 74 | 75 | ### SublimeText ### 76 | # cache files for sublime text 77 | *.tmlanguage.cache 78 | *.tmPreferences.cache 79 | *.stTheme.cache 80 | 81 | # workspace files are user-specific 82 | *.sublime-workspace 83 | 84 | # project files should be checked into the repository, unless a significant 85 | # proportion of contributors will probably not be using SublimeText 86 | # *.sublime-project 87 | 88 | # sftp configuration file 89 | sftp-config.json 90 | 91 | # Package control specific files 92 | Package Control.last-run 93 | Package Control.ca-list 94 | Package Control.ca-bundle 95 | Package Control.system-ca-bundle 96 | Package Control.cache/ 97 | Package Control.ca-certs/ 98 | Package Control.merged-ca-bundle 99 | Package Control.user-ca-bundle 100 | oscrypto-ca-bundle.crt 101 | bh_unicode_properties.cache 102 | 103 | # Sublime-github package stores a github token in this file 104 | # https://packagecontrol.io/packages/sublime-github 105 | GitHub.sublime-settings 106 | 107 | ### VisualStudioCode ### 108 | .vscode/* 109 | !.vscode/settings.json 110 | !.vscode/tasks.json 111 | !.vscode/launch.json 112 | !.vscode/extensions.json 113 | .history 114 | 115 | 116 | # End of https://www.gitignore.io/api/sass,node,sublimetext,visualstudiocode 117 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | doc* 2 | test* 3 | lib* 4 | *.gem* 5 | *.sublime* 6 | *.todo 7 | *.jpg 8 | sache.json 9 | bower.json -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Sass List–Maps 2 | 3 | These functions provide a forward-compatible polyfill for SassScript `map` (hash) data-type functionality in [libsass](http://libsass.org/) 2.x or lower, and [ruby-sass](http://sass-lang.com/) 3.2.x or lower, using the `list` data-type. They feature-match the ruby-sass (3.3.x or higher) native `map` functions, and add nested (or 'chained') `get()`, `set()` and `merge()` functions and inspection / debugging functions as well. 4 | 5 | ##### NB: [SassScript Maps are natively and robustly implemented in libsass as of 3.1](http://github.com/sass/libsass/releases). If you are using libsass 3.1 or higher, see my new library [Sass Maps Plus](http://github.com/lunelson/sass-maps-plus). 6 | 7 | ![](sass-hash.jpg) 8 | 9 | #### Updates 10 | 11 | * 1.0.0 -- bumping for now, as there are no new features/issues 12 | * 1.0.0-b3 -- altered use of `list-map-check()` in `map-get()` and `map-get-z()` functions, to avoid double-wrapped-output edge case 13 | * 1.0.0-b2 -- updated `map-remove()` function to allow removing multiple keys; conformant to ruby-sass 3.4. 14 | * 1.0.0-b -- preparing for 1.0; added npm distribution and newly commented source code thanks to Hugo Giraudel. Next version will use SassDoc-style comments. 15 | * 0.9.9 -- added `map-pretty()` function to inspect/debug list-maps in pretty-printed form 16 | * 0.9.6-0.9.8 -- argument-handling enhancements; typo fixes 17 | * 0.9.3-0.9.5 -- handling single-pair lists automatically. This means no more need for `list()` or `zip()` functions, which are now deprecated. 18 | * 0.9.2 -- improved merge performance with new `set-nth()` function; included `get()`, `merge()`, and `set()` aliases by default 19 | * 0.9.1 -- now listed at the [sache.in](http://www.sache.in/) directory of Sass & Compass Extensions 20 | 21 | #### Try it 22 | 23 | You can test-drive these functions at [Sassmeister, in this pre-loaded gist](http://sassmeister.com/gist/4787415116ed1adbc8d9)—but note that the libsass version at Sassmeister might be a couple of point-releases behind this repo. 24 | 25 | #### Install it 26 | 27 | 'Sass List-Maps' can be installed as a Bower component or NPM module for non-ruby setups (see [node-sass options](http://github.com/sindresorhus/grunt-sass#includepaths) if you are using libsass via node) or as a gem-based Compass extension for ruby setups: 28 | 29 | ```sh 30 | # installation with npm 31 | npm install sass-list-maps 32 | 33 | # installation with bower 34 | bower install sass-list-maps 35 | 36 | # if using grunt-sass, you might want to set 'includePaths' 37 | # to match install location of bower or npm. 38 | options: { 39 | includePaths: [ 40 | './bower_components/sass-list-maps' 41 | ] 42 | } 43 | 44 | # installation with rubygems, for compass 45 | gem install sass-list-maps 46 | 47 | # Add to your Sass 48 | @import "sass-list-maps"; 49 | ``` 50 | 51 | You can of course also just fork or download this repo or copy-and-paste, as the functions are all in one file. 52 | 53 | ### Introduction 54 | 55 | Maps (known in programming as hashes, dictionaries, or objects1) allow dynamic creating, setting, merging and retrieving of data. They are native to ruby-sass as of version 3.3.x, but for earlier ruby-sass versions, and for the libsass C-based compiler (until the point at which maps are integrated there natively), this is an alternative solution which feature-matches ruby-sass' 3.3.x map functionality using the `list` data-type. 56 | 57 | Some additional functions are also provided to allow nested (chained) getting and merging/setting, and inspection for debugging. 58 | 59 | *1objects (as in javascript) are not exactly the same thing as maps and hashes, but for these purposes close enough.* 60 | 61 | #### Syntax 62 | 63 | Compared to ruby-sass' native maps, 'list-maps' are lists like any other list in Sass, but they are *lists of pairs*, formatted in such a way as to be interpreted like a map. To this purpose, the first item in each pair is interpreted as the 'key', while the second is interpreted as the correspondent 'value'. This 'value' can be any Sass-script data-type, including a list—which means list-maps can contain other list-maps, allowing them to form nested data structures. The formatting used here keeps as close as possible to the syntax of native maps, with the difference that there no colons (`:`) used. 64 | 65 | *NB: in ruby-sass 3.2.x or lower, the placement of commas is also very critical (no comma after the last item); but in libsass as of 2.0 you can put commas after each item if you wish.* 66 | 67 | ```scss 68 | /* a single-line list-map -- compatible with any 69 | version/compiler of sass including 3.3+ */ 70 | $list-map: ( alpha 1, beta 2, gamma 3 ); 71 | 72 | /* a single-line ruby-sass native map 73 | -- would cause an error in any version/compiler 74 | other than ruby-sass 3.3+ */ 75 | $native-map: ( alpha: 1, beta: 2, gamma: 3,); 76 | 77 | /* a multi-line list-map */ 78 | $list-map-z: ( 79 | alpha ( 80 | beta ( 81 | gamma 3 82 | ) 83 | ) 84 | ); 85 | 86 | /* a mutli-line ruby-sass native map */ 87 | $native-map-z: ( 88 | alpha: ( 89 | beta: ( 90 | gamma: 3, 91 | ), 92 | ), 93 | ); 94 | ``` 95 | 96 | It should be clear that 'list-maps' and ruby-sass '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 the SassScript `list` data-type. Moreover, as with native maps in ruby-sass, list functions (e.g. `nth()`, `index()`) can be used on list-maps, since they are still lists. 97 | 98 | ### The Functions 99 | 100 | These functions have the same names as the built-in 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 with (or at least overwrite) the built-in ones. Therefore, the following code assume you are using either ruby-sass 3.2.x or earlier, or libsass (currently 2.1b). 101 | 102 | #### Core (matching the ruby-sass 3.3.x native map functions) 103 | 104 | ###### 1. `map-keys($list)`, `map-values($list)`, `map-has-key($list, $key)` 105 | 106 | ```scss 107 | @import "sass-list-maps"; 108 | 109 | $list-map: ( alpha 1, beta 2, gamma 3 ); 110 | 111 | .demo { 112 | out: map-keys($list-map); //-> alpha, beta, gamma 113 | out: map-values($list-map); //-> 1, 2, 3 114 | out: map-has-key($list-map, gamma); //-> true 115 | out: map-has-key($list-map, delta); //-> false 116 | } 117 | ``` 118 | 119 | ###### 2. `map-get($list, $key)` 120 | 121 | ```scss 122 | @import "sass-list-maps"; 123 | 124 | $list-map: ( alpha 1, beta 2, gamma 3 ); 125 | 126 | .demo { 127 | out: map-get($list-map, alpha); //-> 1 128 | out: map-get($list-map, beta); //-> 2 129 | out: map-get($list-map, gamma); //-> 3 130 | } 131 | ``` 132 | 133 | ###### 3. `map-merge($list1, $list2), map-remove($list, $key)` 134 | 135 | ```scss 136 | @import "sass-list-maps"; 137 | 138 | $list-map: ( alpha 1, beta 2, gamma 3 ); 139 | 140 | $new-map: map-merge($list-map, gamma 4); 141 | // -> $new-map = ( alpha 1, beta 2, gamma 4 ) 142 | 143 | $short-map: map-remove($list-map, alpha); 144 | // -> $short-map = ( beta 2, gamma 3) 145 | ``` 146 | 147 | **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. As of v0.9.5 these functions handle this type of input so the API can mimic the native syntax as closely as possible. 148 | 149 | #### Advanced (beyond the ruby-sass 3.3.x native map functions) 150 | 151 | In addition to ruby-sass' native map functionality, this library also provides nested (deep / chained) 'get' and 'set'/'merge' and debugging functions. 152 | 153 | ##### Nesting / Chaining 154 | 155 | ###### 4. `map-get-z($list, $keys...)` 156 | 157 | 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): 158 | 159 | ```scss 160 | @import "sass-list-maps"; 161 | 162 | $list-map-z: ( 163 | alpha ( 164 | beta ( 165 | gamma 3 166 | ) 167 | ) 168 | ); 169 | 170 | .demo { 171 | out: map-get-z($list-map-z, alpha); // -> ( beta ( gamma 3 ) ) 172 | out: map-get-z($list-map-z, alpha, beta); // -> ( gamma 3 ) 173 | out: map-get-z($list-map-z, alpha, beta, gamma); // -> 3 174 | } 175 | ``` 176 | 177 | ###### 5. `map-merge-z($list, $keys-and-value...)` 178 | 179 | The `map-merge-z()` function takes a chain of keys to indicate where (at what depth) to merge, and takes 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: 180 | 181 | ```scss 182 | @import "sass-list-maps"; 183 | 184 | $list-map-z: ( 185 | alpha ( 186 | beta ( 187 | gamma 3 188 | ) 189 | ) 190 | ); 191 | 192 | $new-map1-z: map-merge-z($list-map-z, delta); 193 | // -> ( alpha ( beta ( gamma 3 ) ), ( delta ( ) ) ) 194 | $new-map2A-z: map-merge-z($list-map-z, delta, epsilon); 195 | // -> ( alpha ( beta ( gamma 3 ) ), ( delta epsilon ) ) 196 | $new-map2B-z: map-merge-z($list-map-z, (delta epsilon)); 197 | // -> ( alpha ( beta ( gamma 3 ) ), ( delta epsilon ) ) 198 | $new-map3-z: map-merge-z($list-map-z, (delta 4, epsilon 5)); 199 | // -> ( alpha ( beta ( gamma 3 ) ), ( delta 4 ), ( epsilon 5 ) ) 200 | $new-map4-z: map-merge-z($list-map-z, delta, epsilon, 5); 201 | // -> ( alpha ( beta ( gamma 3 ) ), ( delta ( epsilon 5 ) ) ) 202 | ``` 203 | 204 | ##### Inspection / Debugging 205 | 206 | ###### 6. `map-inspect()`, `map-pretty()` 207 | 208 | To aid in development, list-map inspection functions are provided. `map-inspect()` will format a list-map as a string on one line, while `map-pretty()` will format the same string on multiple lines with indentation. 209 | 210 | ```scss 211 | @import "sass-list-maps"; 212 | 213 | $testmap: ( 214 | alpha ( 215 | beta 2, 216 | gamma ( 217 | delta 14, 218 | epsilon 2 219 | ) 220 | ), 221 | zeta 1, 222 | eta ( 223 | theta 55 224 | ) 225 | ); 226 | 227 | .debug { 228 | inspect: map-inspect($testmap); 229 | pretty: map-pretty($testmap); 230 | } 231 | ``` 232 | ```css 233 | .debug { 234 | inspect: '(alpha (beta 2, gamma (delta 14, epsilon 2)), zeta 1, eta (theta 55))'; 235 | pretty: '( 236 | alpha ( 237 | beta 2, 238 | gamma ( 239 | delta 14, 240 | epsilon 2 241 | ) 242 | ), 243 | zeta 1, 244 | eta ( 245 | theta 55 246 | ) 247 | )'; } 248 | ``` 249 | 250 | ##### One Syntax to Rule them All 251 | 252 | ###### 7. `get()`, `merge()`, `set()` 253 | 254 | Since the 'advanced' nested/chained `map-get-z()` and `map-merge-z()` take a variable number of `$keys`, and `map-merge-z()` can accept argument patterns consistent with both merge- and set-style operations, the following aliases can provide unified function syntaxes to replace the 'core' `get()` and `merge()` functions while adding a `set()` function: 255 | 256 | ```scss 257 | @import "sass-list-maps"; 258 | 259 | // get($list, $key[s...]) 260 | // accepts 1 or more key args as target, returns value 261 | @function get($args...) { @return map-get-z($args...); } 262 | 263 | // merge($list1, [$keys...,] $list2) 264 | // accepts 0 or more key args as target, merges list at target 265 | @function merge($args...) { @return map-merge-z($args...); } 266 | 267 | // set($list, $key[s...], $value) 268 | // accepts 1 or more key args as target, sets value at target 269 | @function set($args...) { @return map-merge-z($args...); } 270 | ``` 271 | 272 | 273 | ### Caveats 274 | 275 | There are a few points that bear mentioning/repeating: 276 | 277 | * operating on global variables in libsass and in ruby-sass 3.2.x or earlier works differently than in 3.3.x and later: 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 how you declare and change global variables. 278 | * there is no error-checking for the data-type itself (e.g. native maps will produce a warning if you have duplicate keys). 279 | * special features of native maps in ruby-sass, such as passing a map to a function in the form `my-function($map...)` whereupon you can reference the key/value pairs inside the function as if they were named variables, doesn't work here. 280 | * as noted, the 'list-map' syntax is in ruby-sass 3.2.x or earlier is still less forgiving than that of native maps (watch your commas), but it's a bit more easygoing in libsass, which is the more likely use-case. 281 | * 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 and incrementally optimizing it, but I welcome reports and contributions! 282 | 283 | ### To-Dos 284 | 285 | * Push a SassDoc documented version (this will be 1.0.0 final) 286 | * Implement an error/warning system (probably 1.1.0) 287 | * Push a native maps version of the 'advanced' functions above 288 | 289 | ### Acknowledgements 290 | 291 | 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. 292 | 293 | Also acknowledgements to @HugoGiraudel for [SassyLists](http://sassylists.com/), from which I adapted some early functions, and especially for his list `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.x and libsass. 294 | -------------------------------------------------------------------------------- /_sass-list-maps.scss: -------------------------------------------------------------------------------- 1 | // Sass List-Maps 1.0.0 2 | // a libsass polyfill for rubysass "maps" data-type -- using lists 3 | // by @lunelson 4 | // MIT License 5 | 6 | // 88 88 88 88 7 | // ,d "" 88 "" ,d "" 8 | // 88 88 88 9 | // 88 88 MM88MMM 88 88 88 MM88MMM 88 ,adPPYba, ,adPPYba, 10 | // 88 88 88 88 88 88 88 88 a8P_____88 I8[ "" 11 | // 88 88 88 88 88 88 88 88 8PP""""""" `"Y8ba, 12 | // "8a, ,a88 88, 88 88 88 88, 88 "8b, ,aa aa ]8I 13 | // `"YbbdP'Y8 "Y888 88 88 88 "Y888 88 `"Ybbd8"' `"YbbdP"' 14 | // 15 | 16 | /////////////////////////// 17 | // list helper functions // 18 | /////////////////////////// 19 | 20 | // list-slice(): return a sub-list from a list, 'sliced' from/to given indices 21 | // --- 22 | // @alias slice() 23 | // --- 24 | // @param [list] $list: list to slice 25 | // @param [number] $start (1): start index 26 | // @param [number] $end (length($list)): end index 27 | // @param [string] $sep ('comma'): default list separator 28 | // --- 29 | // @return [list] 30 | @function list-slice($list, $start: 1, $end: length($list), $sep: 'comma') { 31 | $output: (); 32 | @for $i from $start through $end { 33 | $output: append($output, nth($list, $i), $sep); } 34 | @return $output; 35 | } 36 | // alias 37 | @function slice($args...) { @return list-slice($args...); } 38 | 39 | // list-set-nth(): return a list with value at index set/updated to new value 40 | // --- 41 | // @alias set-nth() 42 | // --- 43 | // @param [list] $list: list to set 44 | // @param [number] $index: index to set 45 | // @param [literal] $value: new value 46 | // @param [string] $sep ('comma'): list separator 47 | // --- 48 | // @return [list] 49 | @function list-set-nth($list, $index, $value, $sep: 'comma') { 50 | $length: length($list); 51 | $output: (); 52 | @if $index > $length or $index <= 0 { 53 | @return $list; 54 | } 55 | @if $index > 1 { 56 | @for $i from 1 through $index - 1 { 57 | $output: append($output, nth($list, $i), $sep); 58 | } 59 | } 60 | $output: append($output, $value, $sep); 61 | @if $length > 1 { 62 | @for $i from $index + 1 through $length { 63 | $output: append($output, nth($list, $i), $sep); 64 | } 65 | } 66 | @return $output; 67 | } 68 | // alias 69 | @function set-nth($args...) { @return list-set-nth($args...); } 70 | 71 | /////////////////////////////// 72 | // list-map helper functions // 73 | /////////////////////////////// 74 | 75 | // tuple-key(): return the key (first value) from a tuple (pair) 76 | // --- 77 | // @alias key() 78 | // --- 79 | // [0.9.3] added alias 80 | // --- 81 | // @param [list] $tuple: pair (list of length 2), to extract key from 82 | // --- 83 | // @return [literal] 84 | @function tuple-key($tuple) { 85 | @if length($tuple) < 1 { @return null; } 86 | @return nth($tuple, 1); 87 | } 88 | // alias 89 | @function key($tuple) { @return tuple-key($tuple); } 90 | 91 | // tuple-value(): return the value (second value) from a tuple (pair) 92 | // --- 93 | // @alias value() 94 | // --- 95 | // [0.9.3] added alias 96 | // --- 97 | // @param [list] $tuple: pair (list of length 2), to extract value from 98 | // --- 99 | // @return [literal] 100 | @function tuple-value($tuple) { 101 | @if length($tuple) < 2 { @return null; } 102 | @return nth($tuple, 2); 103 | } 104 | // alias 105 | @function value($tuple) { @return tuple-value($tuple); } 106 | 107 | // list-map-check(): return list-map from list; ensure input list-maps are lists-of-lists 108 | // --- 109 | // [0.9.5] added; replaces use of 'list()' function 110 | // @param [list] $list: list-map to check 111 | // --- 112 | // @return [list] 113 | @function list-map-check($list) { 114 | @if length($list) == 2 and length(nth($list, 1)) == 1 { 115 | @return append((), $list, 'comma'); 116 | } 117 | @return $list; 118 | } 119 | 120 | // 121 | // ,adPPYba, ,adPPYba, 8b,dPPYba, ,adPPYba, 122 | // a8" "" a8" "8a 88P' "Y8 a8P_____88 123 | // 8b 8b d8 88 8PP""""""" 124 | // "8a, ,aa "8a, ,a8" 88 "8b, ,aa 125 | // `"Ybbd8"' `"YbbdP"' 88 `"Ybbd8"' 126 | // 127 | 128 | ///////////////////////////////////////////////////////////////////////// 129 | // list-map versions of map-keys(), -values() and -has-key() functions // 130 | ///////////////////////////////////////////////////////////////////////// 131 | 132 | // map-keys(): return comma-separated list of keys from map [conformant with rubysass map-keys()] 133 | // --- 134 | // @alias keys() 135 | // --- 136 | // [0.9.5] use list-map-check to handle single pairs automatically 137 | // --- 138 | // @param [argList] $list: list to retrieve keys from 139 | // --- 140 | // @return [list] 141 | @function map-keys($list...) { 142 | $list: if(length($list) == 1, list-map-check($list...), list-map-check($list)); 143 | $output: (); 144 | @each $tuple in $list { 145 | $output: append($output, tuple-key($tuple), 'comma'); 146 | } 147 | @return $output; 148 | } 149 | // alias 150 | @function keys($list...) { @return map-keys($list...); } 151 | 152 | // map-values(): return comma-separated list of values from map [conformant with rubysass map-values()] 153 | // --- 154 | // @alias values() 155 | // --- 156 | // [0.9.5] use list-map-check to handle single pairs automatically 157 | // --- 158 | // @param [argList] $list: list to retrieve values from 159 | // --- 160 | // @return [list] 161 | @function map-values($list...) { 162 | $list: if(length($list) == 1, list-map-check($list...), list-map-check($list)); 163 | $output: (); 164 | @each $tuple in $list { 165 | $output: append($output, tuple-value($tuple), 'comma'); 166 | } 167 | @return $output; 168 | } 169 | // alias 170 | @function values($list...) { @return map-values($list...); } 171 | 172 | // map-has-key(): check whether key exists in map at root level [conformant with rubysass map-has-key()] 173 | // --- 174 | // @alias has-key() 175 | // --- 176 | // @param [list] $list: list to check 177 | // @param [literal] $key: key to check in list 178 | // --- 179 | // @return [bool] 180 | @function map-has-key($list, $key) { 181 | $list: list-map-check($list); 182 | @each $tuple in $list { 183 | @if tuple-key($tuple) == $key { 184 | @return true; 185 | } 186 | } 187 | @return false; 188 | } 189 | // alias 190 | @function has-key($list, $key) { @return map-has-key($list, $key); } 191 | 192 | //////////////////////////////////////////////////////////// 193 | // list-map versions of map-get(), -merge() and -remove() // 194 | //////////////////////////////////////////////////////////// 195 | 196 | // map-get(): return value corresponding to key in map; conformant to rubysass map-get() 197 | // --- 198 | // [0.9.5] list-map-check() now handles single-pair inputs / outputs 199 | // --- 200 | // @param [list] $list: map 201 | // @param [literal] $key: key by which to look up value 202 | // @param [bool] $check (true): whether or not to check the map format first 203 | // --- 204 | // @return [literal] | [null] 205 | @function map-get($list, $key) { 206 | $list: list-map-check($list); 207 | @each $tuple in $list { 208 | @if tuple-key($tuple) == $key { 209 | @return tuple-value($tuple); 210 | } 211 | } 212 | @return null; 213 | } 214 | 215 | // map-merge(): return a merge of second map in to first map; conformant to rubysass map-merge() 216 | // --- 217 | // [0.9.5] list-map-check() handles single pairs; $check param for internal use 218 | // --- 219 | // @param [list] $list1: first map 220 | // @param [list] $list2: second map 221 | // @param [bool] $check (true): whether or not to check the map first 222 | // --- 223 | // @return [list] 224 | @function map-merge($list1, $list2, $check: true) { 225 | @if $check { 226 | $list1: list-map-check($list1); 227 | $list2: list-map-check($list2); 228 | } 229 | $keys1: map-keys($list1); 230 | @each $tuple in $list2 { 231 | $index: index($keys1, tuple-key($tuple)); 232 | @if $index { $list1: set-nth($list1, $index, $tuple); } 233 | @else { $list1: append($list1, $tuple, 'comma'); } 234 | } 235 | @return $list1; 236 | } 237 | 238 | // map-remove(): return map with tuples removed, according to keys (if found); conformant to rubysass 3.4 map-remove() 239 | // --- 240 | // @param [list] $list: map 241 | // @param [literal] $key: key 242 | // --- 243 | // @return [list] 244 | @function map-remove($list, $keys...) { 245 | $list: list-map-check($list); 246 | $output: (); 247 | @each $tuple in $list { 248 | @if index($keys, tuple-key($tuple)) == false { 249 | $output: append($output, $tuple, 'comma'); 250 | } 251 | } 252 | @return $output; 253 | } 254 | 255 | // 256 | // 88 88 257 | // 88 88 258 | // 88 88 259 | // ,adPPYYba, ,adPPYb,88 8b d8 ,adPPYYba, 8b,dPPYba, ,adPPYba, ,adPPYba, ,adPPYb,88 260 | // "" `Y8 a8" `Y88 `8b d8' "" `Y8 88P' `"8a a8" "" a8P_____88 a8" `Y88 261 | // ,adPPPPP88 8b 88 `8b d8' ,adPPPPP88 88 88 8b 8PP""""""" 8b 88 262 | // 88, ,88 "8a, ,d88 `8b,d8' 88, ,88 88 88 "8a, ,aa "8b, ,aa "8a, ,d88 263 | // `"8bbdP"Y8 `"8bbdP"Y8 "8" `"8bbdP"Y8 88 88 `"Ybbd8"' `"Ybbd8"' `"8bbdP"Y8 264 | // 265 | 266 | ////////////////////////////////////////////////////////////// 267 | // deep/nested map functions: map-get-z() and map-merge-z() // 268 | ////////////////////////////////////////////////////////////// 269 | 270 | // map-get-z(): a 'deep', nested or chained version of `map-get` (see above); unique 271 | // --- 272 | // [0.9.5] now uses list-map-check to handle single-pair list-maps 273 | // [0.9.2] aliased to get(); can replace map-get() and map-get-z() in usage 274 | // --- 275 | // @alias get() 276 | // --- 277 | // @param [list] $list: map 278 | // @param [argList] $keys: nested / chained key list (where to get value) 279 | // --- 280 | // @return [literal] | [null] 281 | @function map-get-z($list, $keys...) { 282 | @if $list == null { @return $list; } 283 | $length: length($keys); 284 | $list: map-get($list, nth($keys, 1)); 285 | @if $length > 1 { 286 | @for $n from 2 through $length { 287 | @if $list == null { @return $list; } 288 | $list: map-get($list, nth($keys, $n)); 289 | } 290 | @return $list; 291 | } 292 | @return $list; 293 | } 294 | // alias 295 | @function get($args...) { @return map-get-z($args...); } 296 | 297 | // map-merge-z(): a 'deep', nested or chained version of `map-merge`; offers `map-set` syntax too 298 | // --- 299 | // [0.9.5] now uses list-map-check to handle single-pair list-maps 300 | // [0.9.2] aliased to merge() and set(); can replace map-merge() and map-merge-z() in usage 301 | // --- 302 | // @alias merge(), set() 303 | // --- 304 | // @param [list] $list: map 305 | // @param [argList] $keys-and-values: nested / chained key list (where to merge value); final item in list is the value to be merged 306 | // --- 307 | // @return [list] 308 | @function map-merge-z($list, $keys-and-value...) { 309 | $arg-length: length($keys-and-value); 310 | $value: nth($keys-and-value, $arg-length); 311 | $key-length: $arg-length - 1; 312 | $list: list-map-check($list); 313 | @if $key-length == 0 { 314 | $value: if(type-of($value) == 'list', map-merge($list, list-map-check($value), false), map-merge($list, append((), $value (), 'comma'), false)); 315 | } 316 | @else { 317 | $start: 1; 318 | @if type-of($value) == 'list' { 319 | $start: 0; 320 | $value: list-map-check($value); 321 | } 322 | @for $i from $start through $key-length { 323 | $new-list: (); $old-list: (); 324 | @if $i == 0 { $new-list: $value; } 325 | @else { $new-list: append((), nth($keys-and-value, $key-length + 1 - $i) $value, 'comma'); } 326 | @if $i == $key-length { $old-list: $list; } 327 | @else { $old-list: map-get-z($list, slice($keys-and-value, 1, $key-length - $i)...) or (); } 328 | $value: map-merge($old-list, $new-list); 329 | } 330 | } 331 | @return $value; 332 | } 333 | // aliases 334 | @function merge($args...) { @return map-merge-z($args...); } 335 | @function set($args...) { @return map-merge-z($args...); } 336 | 337 | // 338 | // ,d 339 | // 88 340 | // ,adPPYba, 8b, ,d8 MM88MMM 8b,dPPYba, ,adPPYYba, ,adPPYba, 341 | // a8P_____88 `Y8, ,8P' 88 88P' "Y8 "" `Y8 I8[ "" 342 | // 8PP""""""" )888( 88 88 ,adPPPPP88 `"Y8ba, 343 | // "8b, ,aa ,d8" "8b, 88, 88 88, ,88 aa ]8I 344 | // `"Ybbd8"' 8P' `Y8 "Y888 88 `"8bbdP"Y8 `"YbbdP"' 345 | // 346 | 347 | ///////////////////////////////////// 348 | // additional map helper functions // 349 | ///////////////////////////////////// 350 | 351 | // map-prev-key(): return previous key from map 352 | // --- 353 | // [0.9.5] added 354 | // --- 355 | // @alias prev-key() 356 | // --- 357 | // @param [list] $list: map 358 | // @param [literal] $key: pivot key 359 | // --- 360 | // @return [literal] 361 | @function map-prev-key($list, $key) { 362 | $list: list-map-check($list); 363 | $keys: map-keys($list); 364 | @return nth($keys, index($keys, $key) - 1); 365 | } 366 | // alias 367 | @function prev-key($list, $key) { @return map-prev-key($list, $key); } 368 | 369 | // map-next-key(): return next key from map 370 | // --- 371 | // [0.9.5] added 372 | // --- 373 | // @alias next-key() 374 | // --- 375 | // @param [list] $list: map 376 | // @param [literal] $key: pivot key 377 | // --- 378 | // @return [literal] 379 | @function map-next-key($list, $key) { 380 | $list: list-map-check($list); 381 | $keys: map-keys($list); 382 | @return nth($keys, index($keys, $key) + 1); 383 | } 384 | // alias 385 | @function next-key($list, $key) { @return map-next-key($list, $key); } 386 | 387 | // map-inspect(): return the string representation of a map 388 | // --- 389 | // [0.9.5] added 390 | // --- 391 | // @param [argList] $list: map 392 | // --- 393 | // @return [string] 394 | @function map-inspect($list...) { 395 | $list: if(length($list) == 1, list-map-check($list...), list-map-check($list)); 396 | $output: '('; 397 | $i: 1; 398 | @each $tuple in $list { 399 | @if length($tuple) != 2 { 400 | $output: $output + '#{$tuple}'; 401 | } 402 | @else { 403 | $key: tuple-key($tuple); 404 | $value: tuple-value($tuple); 405 | $output: $output + '#{$key} ' + if(type-of($value) == 'list', '#{map-inspect($value)}', '#{$value}'); 406 | } 407 | @if $i < length(map-keys($list)) { 408 | $output: $output + ', '; 409 | } 410 | $i: $i + 1; 411 | } 412 | @return $output + ')'; 413 | } 414 | 415 | // map-pretty(): return the string representation of a map with indents and line breaks 416 | // --- 417 | // [0.9.9] added 418 | // --- 419 | // @alias map-inspect-pretty() 420 | // --- 421 | // @param [list] $list: map 422 | // @param [number] $level (1): internal variable, do not touch 423 | // --- 424 | // @return [string] 425 | @function map-pretty($list, $level: 1) { 426 | $tab: ' '; 427 | $cr: ' 428 | '; 429 | $list: if(length($list) == 1, list-map-check($list...), list-map-check($list)); 430 | $indent: $tab; $outdent: $tab; 431 | @for $n from 1 through $level { $indent: $indent + $tab; } 432 | @for $n from 1 through $level - 1 { $outdent: $outdent + $tab; } 433 | $output: '(' + $cr + $indent; 434 | $i: 1; 435 | @each $tuple in $list { 436 | @if length($tuple) != 2 { 437 | $output: $output + '#{$tuple}'; 438 | } 439 | @else { 440 | $key: nth($tuple, 1); $value: nth($tuple, 2); 441 | @if type-of($value) == 'list' { $output: $output + '#{$key} #{map-pretty($value, $level+1)}'; } 442 | @else { $output: $output + '#{$key} #{$value}'; } 443 | } 444 | @if $i < length(map-keys($list)) { $output: $output + ',' + $cr + $indent; } 445 | $i: $i + 1; 446 | } 447 | @return $output + $cr + $outdent + ')'; 448 | } 449 | // aliases 450 | @function map-inspect-pretty($list...) { @return map-pretty($list...); } 451 | 452 | // map-sort(): return sorted list-map, based on values at given key(s) 453 | // --- 454 | // [0.9.9] added 455 | // 456 | // @param [list] $list: map 457 | // @param [argList] $keys: nested / chained key list 458 | // --- 459 | // @return [list] 460 | $list-map-sort-dir: 'asc'; // global sort dir variable 461 | @function map-sort($list-map, $keys...) { 462 | @if length($keys) < 1 { @return $list-map; } 463 | $list-map: list-map-check($list-map); 464 | @if length($list-map) > 1 { 465 | $less: (); $equal: (); $greater: (); 466 | $seed: nth($list-map, ceil(length($list-map) / 2)); 467 | $seed-value: map-get-z(nth($seed, 2), $keys...); 468 | @each $item in $list-map { 469 | $item-value: map-get-z(nth($item, 2), $keys...); 470 | @if $list-map-sort-dir == 'asc' { 471 | @if $item-value == $seed-value { $equal: append($equal, $item, 'comma'); } 472 | @else if $item-value < $seed-value { $less: append($less, $item, 'comma'); } 473 | @else { $greater: append($greater, $item, 'comma'); } 474 | } 475 | @else { 476 | @if $item-value == $seed-value { $equal: append($equal, $item, 'comma'); } 477 | @else if $item-value > $seed-value { $less: append($less, $item, 'comma'); } 478 | @else { $greater: append($greater, $item, 'comma'); } 479 | } 480 | } 481 | @return join(join(map-sort($less, $keys...), $equal), map-sort($greater, $keys...)); 482 | } 483 | @return $list-map; 484 | } 485 | 486 | // map-json(): return json string representation of a map 487 | // --- 488 | // [1.0.0] added 489 | // --- 490 | // @param [list] $list: map 491 | // @param [number] $level (1): internal variable, do not touch 492 | // --- 493 | // @return [string] 494 | @function map-json($list...) { 495 | $list: if(length($list) == 1, list-map-check($list...), list-map-check($list)); 496 | $output: '{ '; $i: 1; 497 | @each $tuple in $list { 498 | @if length($tuple) != 2 { 499 | $output: $output + '#{$tuple}'; 500 | } 501 | @else { 502 | $key: nth($tuple, 1); 503 | $value: nth($tuple, 2); 504 | @if type-of($value) == 'list' { 505 | $output: $output + '"#{$key}": #{map-json($value)}'; 506 | } 507 | @else if type-of($value) == 'number' { 508 | $value: if(unitless($value), $value, '"#{$value}"'); 509 | $output: $output + '"#{$key}": #{$value}'; 510 | } 511 | @else { 512 | $value: if(type-of($value) == 'bool', $value, '"#{$value}"'); 513 | $output: $output + '"#{$key}": #{$value}'; 514 | } 515 | } 516 | @if $i < length(map-keys($list)) { 517 | $output: $output + ', '; 518 | } 519 | $i: $i + 1; 520 | } 521 | @return $output + ' }'; 522 | } -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sass-list-maps", 3 | "version": "1.0.0", 4 | "homepage": "https://github.com/lunelson/sass-list-maps", 5 | "authors": [ 6 | "Lu Nelson " 7 | ], 8 | "description": "Polyfill for map (hash/object) data functionality in libsass and ruby-sass < 3.3.x.", 9 | "main": "_sass-list-maps.scss", 10 | "keywords": [ 11 | "libsass", 12 | "sass", 13 | "map", 14 | "maps", 15 | "list", 16 | "lists", 17 | "data" 18 | ], 19 | "license": "MIT", 20 | "ignore": [ 21 | ".*", 22 | "**/.*", 23 | "doc*", 24 | "lib", 25 | "test*", 26 | "*.gem*", 27 | "sache.json", 28 | "node_modules", 29 | "bower_components", 30 | "sass-hash.jpg", 31 | "sass-list-maps.todo", 32 | "sass-list-maps.sublime-project" 33 | ] 34 | } 35 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | ////////////////// 2 | // definitions // 3 | ////////////////// 4 | 5 | var gulp = require('gulp'); 6 | var sass = require('gulp-sass'); 7 | 8 | ////////// 9 | // sass // 10 | ////////// 11 | 12 | // task: sass 13 | gulp.task('sass', function () { 14 | gulp.src('test/test.scss') 15 | .pipe(sass({ 16 | errLogToConsole: false, 17 | onError: function(err) { 18 | console.log(err); 19 | } 20 | })) 21 | .pipe(gulp.dest('test')); 22 | }); 23 | 24 | /////////// 25 | // watch // 26 | /////////// 27 | 28 | gulp.task('watch', function () { 29 | gulp.watch('test/test.scss', ['sass']); 30 | }); 31 | 32 | gulp.task('default', ['sass', 'watch']); 33 | -------------------------------------------------------------------------------- /lib/sass-list-maps.rb: -------------------------------------------------------------------------------- 1 | require 'compass' 2 | base_directory = File.join(File.dirname(__FILE__), '..') 3 | extension_path = File.expand_path(base_directory) 4 | Compass::Frameworks.register( 5 | 'sass-list-maps', 6 | :path => extension_path, 7 | :stylesheets_directory => base_directory 8 | ) -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": [ "test"], 3 | "ext": "scss" 4 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sass-list-maps", 3 | "version": "1.0.0", 4 | "description": "Polyfill for map (hash/object) data functionality in libsass and ruby-sass < 3.3.x.", 5 | "main": "gulpfile.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "gulp" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/lunelson/sass-list-maps.git" 15 | }, 16 | "keywords": [ 17 | "libsass", 18 | "sass", 19 | "map", 20 | "maps", 21 | "list", 22 | "lists", 23 | "data" 24 | ], 25 | "author": "Lu Nelson", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/lunelson/sass-list-maps/issues" 29 | }, 30 | "homepage": "https://github.com/lunelson/sass-list-maps", 31 | "devDependencies": { 32 | "gulp": "^3.8.5", 33 | "gulp-sass": "^0.7.2", 34 | "gulp-strip-json-comments": "^1.0.1" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /sache.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Sass List–Maps", 3 | "description": "Polyfill for map (hash/object) data functionality in libsass and ruby-sass < 3.3.x.", 4 | "tags": [ 5 | "libsass", 6 | "sass", 7 | "map", 8 | "maps", 9 | "list", 10 | "lists", 11 | "data" 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /sass-hash.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lunelson/sass-list-maps/cf8139a300ad1762da50ba5488f5060389758c1b/sass-hash.jpg -------------------------------------------------------------------------------- /sass-list-maps.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | # Release Specific Information 3 | s.version = "1.0.0" 4 | s.date = "2014-09-30" 5 | s.licenses = ['MIT'] 6 | 7 | # Gem Details 8 | s.name = "sass-list-maps" 9 | s.authors = ["Lu Nelson"] 10 | s.summary = %q{Polyfill for map (hash/object) data functionality in libsass and ruby-sass < 3.3.x.} 11 | s.description = %q{Polyfill for map (hash/object) data functionality in libsass and ruby-sass < 3.3.x.} 12 | s.email = "lunelson@gmail.com" 13 | s.homepage = "https://github.com/lunelson/sass-list-maps" 14 | 15 | # Gem Files 16 | s.files = Dir['README.md','LICENSE','_sass-list-maps.scss'] 17 | s.files += Dir.glob("lib/**/*.*") 18 | 19 | # Gem Bookkeeping 20 | s.rubygems_version = %q{1.3.6} 21 | s.add_dependency("compass", ["~> 0.12.0"]) 22 | s.add_dependency("sass", ["~> 3.2.0"]) 23 | end 24 | -------------------------------------------------------------------------------- /test/test.css: -------------------------------------------------------------------------------- 1 | .demo { 2 | /* 3 | basics 4 | */ 5 | out: alpha, beta, gamma; 6 | out: 1, 2, 3; 7 | out: true; 8 | out: false; 9 | out: 1; 10 | out: 2; 11 | out: 3; 12 | out: beta 2, gamma 3; 13 | out: alpha 1, beta 2, gamma 4; 14 | out: alpha 1, beta 2, gamma 4; 15 | out: beta gamma 3; 16 | out: gamma 3; 17 | out: 3; 18 | /* 19 | map-json 20 | */ 21 | out: '(alpha (beta (gamma 3)))'; 22 | out: '( 23 | alpha ( 24 | beta ( 25 | gamma 3 26 | ) 27 | ) 28 | )'; 29 | out: '{ "alpha": { "beta": { "gamma": 3 } } }'; } 30 | -------------------------------------------------------------------------------- /test/test.scss: -------------------------------------------------------------------------------- 1 | @import "../sass-list-maps"; 2 | 3 | $list-map: ( alpha 1, beta 2, gamma 3 ); 4 | 5 | $list-map-z: ( 6 | alpha ( 7 | beta ( 8 | gamma 3 9 | ) 10 | ) 11 | ); 12 | 13 | .demo { 14 | /* 15 | basics 16 | */ 17 | out: map-keys($list-map); //-> alpha, beta, gamma 18 | out: map-values($list-map); //-> 1, 2, 3 19 | out: map-has-key($list-map, gamma); //-> true 20 | out: map-has-key($list-map, delta); //-> false 21 | out: map-get($list-map, alpha); //-> 1 22 | out: map-get($list-map, beta); //-> 2 23 | out: map-get($list-map, gamma); //-> 3 24 | out: map-remove($list-map, alpha); 25 | out: map-merge($list-map, gamma 4); 26 | out: map-merge-z($list-map, gamma, 4); 27 | out: map-get-z($list-map-z, alpha); // -> ( beta ( gamma 3 ) ) 28 | out: map-get-z($list-map-z, alpha, beta); // -> ( gamma 3 ) 29 | out: map-get-z($list-map-z, alpha, beta, gamma); // -> 3 30 | /* 31 | map-json 32 | */ 33 | out: map-inspect($list-map-z); 34 | out: map-pretty($list-map-z); 35 | out: map-json($list-map-z); 36 | } --------------------------------------------------------------------------------