compass-list-helpers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTU0OGY4MTAzMWU2YWYyNDI0NzBkMjQ3ODA4NGJhYjM2OGM4NmU3ZA==
5
+ data.tar.gz: !binary |-
6
+ Y2RiNDJjMTNhNzA5OGIyZTRjZmNjNjFmOWNiODY0ZTdlY2I1NmViMg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OWYyYmQ3ZjJmM2YyODExYWZjNWQ5MjhmMWQ4YzA0ZGZjNDMyY2JlZTZiMDQ1
10
+ YjljNWY1YWJlODhjZjQ2OWFkNGU0MDcxMWJiNTg5MDgwNmRkYTdhZjFiMDE5
11
+ MDg3NzUyMDg4Y2M4Zjk5ZmFlYWZhMmU4YWMyNDgwOTJhZTA0ZGY=
12
+ data.tar.gz: !binary |-
13
+ YjA3YzI4NWFmZjJjNDUzMjlkMGFiYzRlNmNjZjM5NGRkOWZkNjFhNjRhM2Rm
14
+ OGZjYTAyZGNmZDAyMmYyNDllMzlkZmI0MGQyNmFmYzQ2ZmE1MzI3YzE0Mjdh
15
+ N2IxNDVkNzk3ZDk4NjViODg0NzZhYTZhODQyMzg4ODVjMGU4NTM=
@@ -0,0 +1,3 @@
1
+ # 0.1.0 (2013-07-25)
2
+
3
+ Initial working version.
@@ -0,0 +1,174 @@
1
+ # Compass List Helpers
2
+
3
+ List Helpers is a Compass extension providing useful list helper functions
4
+ like: map, filter, reduce, every and some.
5
+
6
+ The List Helpers are based on JavaScript's Array.prototype functions, but of
7
+ course, adapted to Sass. Instead of passing in a function object in JS, in Sass
8
+ you'd just pass in a function name, that will be called for each item in the
9
+ list.
10
+
11
+ ## Installation
12
+
13
+ Install List Helpers by:
14
+
15
+ ```bash
16
+ $ gem install compass-list-helpers
17
+ ```
18
+
19
+ Tell Compass you want to use List Helpers in your project's `config.rb`:
20
+
21
+ ```ruby
22
+ require "list-helpers"
23
+ ```
24
+
25
+ And import List Helpers into your stylesheet:
26
+
27
+ ```scss
28
+ @import "list-helpers";
29
+ ```
30
+
31
+ Not using Compass? Just copy and paste the contents of the `stylesheets` dir
32
+ into your project.
33
+
34
+ ## Example usage
35
+
36
+ As always, simple (silly) examples explain the rationale the best. It all
37
+ starts with a list:
38
+
39
+ ```scss
40
+ $my-awesome-colors: red, green, blue, yellow, blue, brown;
41
+ $my-awesome-numbers: 1, 2, 3, 4, 10, 20, 30;
42
+ ```
43
+
44
+ Then, we would define some functions to test each item against:
45
+
46
+ ```scss
47
+ // `is-blue` returns true if $item is blue, else it returns false.
48
+ @function is-blue($item) {
49
+ @if $item == blue {
50
+ @return true;
51
+ }
52
+ @return false;
53
+ }
54
+
55
+ // `add-numbers` adds two numbers, rocket science!
56
+ @function add-numbers($a, $b) {
57
+ @return $a + $b;
58
+ }
59
+
60
+ // `double-up` doubles a number.
61
+ @function double-up($a) {
62
+ @return $a * 2;
63
+ }
64
+
65
+ // `is-big-enough` returns true if $item is >= 10, else it returns false.
66
+ @function is-big-enough($item) {
67
+ @if $item >= 10 {
68
+ @return true;
69
+ }
70
+ @return false;
71
+ }
72
+ ```
73
+
74
+ Now, if your using Sass 3.2 or lower, you need to define a `call` function. In
75
+ Sass 3.3 a native `call` function is built-in, so there's no need for this poor
76
+ man's solution:
77
+
78
+ ```scss
79
+ // Note: only do this in Sass 3.2 or lower, in Sass 3.3 and higher there's no need for this.
80
+ @function call($function, $arguments...) {
81
+ @if $function == 'is-blue' {
82
+ @return is-blue($arguments...);
83
+ }
84
+ @if $function == 'add-numbers' {
85
+ @return add-numbers($arguments...);
86
+ }
87
+ @if $function == 'double-up' {
88
+ @return double-up($arguments...);
89
+ }
90
+ @if $function == 'is-big-enough' {
91
+ @return is-big-enough($arguments...);
92
+ }
93
+ }
94
+ ```
95
+
96
+ Here comes the meat: now we can use the list helpers:
97
+
98
+ ```scss
99
+ $the-blues: filter($my-awesome-colors, 'is-blue'); // Returns (blue, blue)
100
+ $big-is-better: filter($my-awesome-numbers, 'is-big-enough'); // Returns (10, 20, 30)
101
+ $all-blue: every($my-awesome-colors, 'is-blue'); // Returns false (not all items are blue)
102
+ $some-blue: some($my-awesome-colors, 'is-blue'); // Returns true (at least one item is blue)
103
+ $doubled: map($my-awesome-numbers, 'double-up'); // Returns (2, 4, 6, 8, 20, 40, 60)
104
+ $grand-total: reduce($my-awesome-numbers, 'add-numbers'); // Returns 70
105
+ ```
106
+
107
+ ## List Helpers Functions
108
+
109
+ List Helpers provides some useful functions:
110
+
111
+ ### `every($list, $callback)`
112
+
113
+ Tests whether all items in the list pass the test implemented by the provided
114
+ callback function.
115
+
116
+ Every executes the provided callback function once for each item present in
117
+ the list until it finds one where callback returns false. If such an item is
118
+ found, the every method immediately returns false. Otherwise, if callback
119
+ returned a true value for all items, every will return true.
120
+
121
+ See the `every` [test cases](test/scss/tests/_every.scss) for more examples.
122
+
123
+ ### `filter($list, $callback)`
124
+
125
+ Creates a new list with all items that pass the test implemented by the
126
+ provided function.
127
+
128
+ Filter calls a provided callback function once for each item in a list, and
129
+ constructs a new list of all the values for which callback returns true.
130
+
131
+ See the `filter` [test cases](test/scss/tests/_filter.scss) for more examples.
132
+
133
+ ### `map($list, $callback)`
134
+
135
+ Creates a new list with the results of calling a provided function on every
136
+ item in this list.
137
+
138
+ Map calls a provided callback function once for each item in a list, in order,
139
+ and constructs a new list from the results.
140
+
141
+ See the `map` [test cases](test/scss/tests/_map.scss) for more examples.
142
+
143
+ ### `reduce($list, $callback, $initial: 0)`
144
+
145
+ Apply a function against an accumulator and each item of the list (from
146
+ left-to-right) as to reduce it to a single value.
147
+
148
+ Reduce executes the callback function once for each item present in the list,
149
+ passing it the result up until then and the current item in the list.
150
+
151
+ The `$initial` argument is the value to use as the first argument to the first
152
+ call of the callback. For example, if you're adding up the $my-awesome-numbers
153
+ list of numbers from above, you can $initial to 10 in order to start adding at
154
+ 10 (so it will return 80).
155
+ In another example, if you're dealing with a list of lists you want to flatten,
156
+ passing in an empty list as `$initial` will make sure the callback is properly
157
+ called.
158
+
159
+ See the `reduce` [test cases](test/scss/tests/_reduce.scss) for more examples.
160
+
161
+ ### `some($list, $callback)`
162
+
163
+ Tests whether some item in the list passes the test implemented by the provided
164
+ callback function.
165
+
166
+ Some executes the callback function once for each item present in the list
167
+ until it finds one where callback returns a true value. If such an item is
168
+ found, some immediately returns true. Otherwise, some returns false.
169
+
170
+ See the `some` [test cases](test/scss/tests/_some.scss) for more examples.
171
+
172
+ ## License
173
+
174
+ MIT License, see [LICENSE.txt](LICENSE.txt).
@@ -0,0 +1,4 @@
1
+ require 'compass'
2
+
3
+ extension_path = File.expand_path(File.join(File.dirname(__FILE__), ".."))
4
+ Compass::Frameworks.register('list-helpers', :path => extension_path)
@@ -0,0 +1,8 @@
1
+ // ----------------------------------------------------------------------------
2
+ // List Helpers
3
+
4
+ @import "list-helpers/every";
5
+ @import "list-helpers/filter";
6
+ @import "list-helpers/map";
7
+ @import "list-helpers/reduce";
8
+ @import "list-helpers/some";
@@ -0,0 +1,22 @@
1
+ // Function every
2
+ // Tests whether all items in the list pass the test implemented by the
3
+ // provided callback function.
4
+ //
5
+ // Every executes the provided callback function once for each item present in
6
+ // the list until it finds one where callback returns false. If such an item is
7
+ // found, the every method immediately returns false. Otherwise, if callback
8
+ // returned a true value for all items, every will return true.
9
+ //
10
+ // @param $list {list} List of items
11
+ // @param $callback {string} Name of the function to test for each item
12
+ //
13
+ // @return {boolean} Whether all items in the list pass the test
14
+ //
15
+ @function every($list, $callback) {
16
+ @each $item in $list {
17
+ @if call($callback, $item) == false {
18
+ @return false;
19
+ }
20
+ }
21
+ @return true;
22
+ }
@@ -0,0 +1,22 @@
1
+ // Function filter
2
+ // Creates a new list with all items that pass the test implemented by the
3
+ // provided function.
4
+ //
5
+ // Filter calls a provided callback function once for each item in a list, and
6
+ // constructs a new list of all the values for which callback returns true.
7
+ //
8
+ // @param $list {list} List of items
9
+ // @param $callback {string} Name of the function to test for each item
10
+ //
11
+ // @return {list} The filtered list
12
+ //
13
+ @function filter($list, $callback) {
14
+ $new-list: ();
15
+ @each $item in $list {
16
+ $result: call($callback, $item);
17
+ @if $result == true {
18
+ $new-list: append($new-list, $item);
19
+ }
20
+ }
21
+ @return $new-list;
22
+ }
@@ -0,0 +1,21 @@
1
+ // Function map
2
+ // Creates a new list with the results of calling a provided function on every
3
+ // item in this list.
4
+ //
5
+ // Map calls a provided callback function once for each item in a list,
6
+ // in order, and constructs a new list from the results.
7
+ //
8
+ // @param $list {list} List of items
9
+ // @param $callback {string} Name of the function that produces an item of the
10
+ // new list from an item of the current one.
11
+ //
12
+ // @return {list} The new list
13
+ //
14
+ @function map($list, $callback) {
15
+ $new-list: ();
16
+ @each $item in $list {
17
+ $item: call($callback, $item);
18
+ $new-list: append($new-list, $item);
19
+ }
20
+ @return $new-list;
21
+ }
@@ -0,0 +1,21 @@
1
+ // Function reduce
2
+ // Apply a function against an accumulator and each item of the list
3
+ // (from left-to-right) as to reduce it to a single value.
4
+ //
5
+ // Reduce executes the callback function once for each item present in the
6
+ // list, passing it the result up until then and the current item in the list.
7
+ //
8
+ // @param $list {list} List of items
9
+ // @param $callback {string} Name of the function to test for each item
10
+ // @param $initial {literal} Value to use as the first argument to the first
11
+ // call of the callback
12
+ //
13
+ // @return {list} The filtered list
14
+ //
15
+ @function reduce($list, $callback, $initial: 0) {
16
+ $result: $initial;
17
+ @for $i from 1 through length($list) {
18
+ $result: call($callback, $result, nth($list, $i));
19
+ }
20
+ @return $result;
21
+ }
@@ -0,0 +1,21 @@
1
+ // Function some
2
+ // Tests whether some item in the list passes the test implemented by the
3
+ // provided callback function.
4
+ //
5
+ // Some executes the callback function once for each item present in the list
6
+ // until it finds one where callback returns a true value. If such an item is
7
+ // found, some immediately returns true. Otherwise, some returns false.
8
+ //
9
+ // @param $list {list} List of items
10
+ // @param $callback {string} Name of the function to test for each item
11
+ //
12
+ // @return {boolean} Whether some item in the list passes the test
13
+ //
14
+ @function some($list, $callback) {
15
+ @each $item in $list {
16
+ @if call($callback, $item) == true {
17
+ @return true;
18
+ }
19
+ }
20
+ @return false;
21
+ }
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: compass-list-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leon de Rijke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sass
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: compass
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.1
41
+ description: ! 'List Helpers is a Compass extension providing useful list helper functions
42
+ like: map, filter, reduce, every and some.'
43
+ email:
44
+ - leon@leonderijke.nl
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - README.md
50
+ - CHANGELOG.md
51
+ - lib/list-helpers.rb
52
+ - stylesheets/_list-helpers.scss
53
+ - stylesheets/list-helpers/_every.scss
54
+ - stylesheets/list-helpers/_filter.scss
55
+ - stylesheets/list-helpers/_map.scss
56
+ - stylesheets/list-helpers/_reduce.scss
57
+ - stylesheets/list-helpers/_some.scss
58
+ homepage: https://github.com/leonderijke/compass-list-helpers
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.5
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Useful list helper functions for Sass, packaged as a Compass extension.
82
+ test_files: []