SassyLists 0.4.9 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. data/CHANGELOG.md +8 -0
  2. data/lib/SassyLists.rb +2 -2
  3. data/stylesheets/SassyLists/_chunk.scss +27 -32
  4. data/stylesheets/SassyLists/_contain.scss +2 -5
  5. data/stylesheets/SassyLists/_count-values.scss +9 -22
  6. data/stylesheets/SassyLists/_debug.scss +64 -66
  7. data/stylesheets/SassyLists/_explode.scss +49 -0
  8. data/stylesheets/SassyLists/_first.scss +10 -5
  9. data/stylesheets/SassyLists/_flatten.scss +16 -13
  10. data/stylesheets/SassyLists/_insert-nth.scss +22 -25
  11. data/stylesheets/SassyLists/_intersection.scss +12 -13
  12. data/stylesheets/SassyLists/_is-symmetrical.scss +2 -7
  13. data/stylesheets/SassyLists/_last-index.scss +7 -9
  14. data/stylesheets/SassyLists/_last.scss +10 -5
  15. data/stylesheets/SassyLists/_loop.scss +19 -12
  16. data/stylesheets/SassyLists/_prepend.scss +7 -8
  17. data/stylesheets/SassyLists/_purge.scss +8 -9
  18. data/stylesheets/SassyLists/_random-value.scss +12 -7
  19. data/stylesheets/SassyLists/_remove-duplicates.scss +8 -17
  20. data/stylesheets/SassyLists/_remove-nth.scss +2 -12
  21. data/stylesheets/SassyLists/_remove.scss +4 -9
  22. data/stylesheets/SassyLists/_replace-nth.scss +7 -29
  23. data/stylesheets/SassyLists/_replace.scss +14 -23
  24. data/stylesheets/SassyLists/_reverse.scss +14 -20
  25. data/stylesheets/SassyLists/_shuffle.scss +14 -10
  26. data/stylesheets/SassyLists/_slice.scss +25 -33
  27. data/stylesheets/SassyLists/_sort.scss +25 -39
  28. data/stylesheets/SassyLists/_sum.scss +12 -17
  29. data/stylesheets/SassyLists/_to-string.scss +10 -21
  30. data/stylesheets/SassyLists/_union.scss +3 -2
  31. data/stylesheets/SassyLists/helpers/_str-compare.scss +27 -0
  32. data/stylesheets/SassyLists/helpers/_true.scss +15 -0
  33. data/stylesheets/_SassyLists.scss +3 -0
  34. metadata +5 -2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ * `1.0.0`: moving code base to Sass 3.3 + adding a Grunt workflow
4
+ * adding `explode`
5
+ * making use of `list-separator()` and `set-nth()`
6
+ * updating `sort()` to make it able to sort strings
7
+ * removing all notion of recursion for sake of simplicity
8
+ * making `count-values()` return a map
9
+ * minor fixes and improvements
10
+ * improving overall performances
3
11
  * `0.4.9`: removing `compact()` alias for `purge()` (ref [#23](https://github.com/Team-Sass/SassyLists/issues/23))
4
12
  * `0.4.8`: adding `shuffle()`
5
13
  * `0.4.7`: dramatically improving `sort()` performances and removing all its dependencies
data/lib/SassyLists.rb CHANGED
@@ -5,8 +5,8 @@ Compass::Frameworks.register('SassyLists', :path => extension_path)
5
5
  # Version is a number. If a version contains alphas, it will be created as a prerelease version
6
6
  # Date is in the form of YYYY-MM-DD
7
7
  module SassyLists
8
- VERSION = "0.4.9"
9
- DATE = "2014-01-20"
8
+ VERSION = "1.0.0"
9
+ DATE = "2014-03-09"
10
10
  end
11
11
 
12
12
  module Sass::Script::Functions
@@ -2,46 +2,41 @@
2
2
  // -------------------------------------------------------------------------------
3
3
  // @documentation http://sassylists.com/documentation/#chunk
4
4
  // -------------------------------------------------------------------------------
5
- // @example chunk(a b c d e, 2) => a b, c d, e
6
- // @example chunk(a b c d e, 3) => a b c, d e
7
- // -------------------------------------------------------------------------------
8
5
  // @param $list [List] : list
9
6
  // @param $size [Number] : length of lists
10
7
  // -------------------------------------------------------------------------------
11
- // @raise [Error] if $list is empty
8
+ // @raise [Error] if $size is not a number
12
9
  // -------------------------------------------------------------------------------
13
10
  // @return [List] | false
14
11
 
15
12
  @function chunk($list, $size) {
16
- @if $list == () {
17
- @warn "Trying to chunk empty list in `chunk`.";
18
- @return false;
19
- }
20
-
21
- @if $size >= length($list) {
22
- @return $list;
13
+ @if type-of($size) != "number" {
14
+ @warn "#{$size} is not a number for `chunk`.";
15
+ @return false;
16
+ }
17
+
18
+ @if $size >= length($list) {
19
+ @return $list;
20
+ }
21
+
22
+ $index: 1;
23
+ $result: ();
24
+ $length: length($list);
25
+ $end: ceil($length / $size);
26
+
27
+ @for $i from 1 through $end {
28
+ $tmp: ();
29
+
30
+ @for $j from 1 through $size {
31
+ @if $index <= $length {
32
+ $is-orphan: $length % $size == 1 and $j == 1;
33
+ $tmp: if($is-orphan, nth($list, $index), append($tmp, nth($list, $index)));
34
+ }
35
+ $index: $index + 1;
23
36
  }
24
37
 
25
- $index: 1;
26
- $result: ();
27
-
28
- @for $i from 1 through ceil(length($list) / $size) {
29
- $tmp: ();
30
-
31
- @for $j from 1 through $size {
32
-
33
- @if $index <= length($list) {
34
-
35
- $is-orphan: length($list) % $size == 1 and $j == 1;
36
- $tmp: if($is-orphan, nth($list, $index), append($tmp, nth($list, $index)));
37
-
38
- }
39
-
40
- $index: $index + 1;
41
- }
42
-
43
- $result: append($result, $tmp);
44
- }
38
+ $result: append($result, $tmp);
39
+ }
45
40
 
46
- @return $result;
41
+ @return $result;
47
42
  }
@@ -4,19 +4,16 @@
4
4
  // -------------------------------------------------------------------------------
5
5
  // @alias `include()`
6
6
  // -------------------------------------------------------------------------------
7
- // @example contain(a b c, a) => true
8
- // @example contain(a b c, z) => false
9
- // -------------------------------------------------------------------------------
10
7
  // @param $list [List] : list
11
8
  // @param $value [Literal] : value to look for
12
9
  // -------------------------------------------------------------------------------
13
10
  // @return [Boolean]
14
11
 
15
12
  @function contain($list, $value) {
16
- @return not not index($list, $value);
13
+ @return not not index($list, $value);
17
14
  }
18
15
 
19
16
  // Alias
20
17
  @function include($list, $value) {
21
- @return contain($list, $value);
18
+ @return contain($list, $value);
22
19
  }
@@ -2,31 +2,18 @@
2
2
  // -------------------------------------------------------------------------------
3
3
  // @documentation http://sassylists.com/documentation/#count-values
4
4
  // -------------------------------------------------------------------------------
5
- // @dependence `replace-nth()`
6
- // -------------------------------------------------------------------------------
7
- // @example count-values(a b c d e) => a 1, b 1, c 1, d 1, e 1
8
- // @example count-values(a b c a d b a e) => a 3, b 2, c 1, d 1, e 1
9
- // -------------------------------------------------------------------------------
10
5
  // @param $list [List] : list
11
6
  // -------------------------------------------------------------------------------
12
- // @return [List]
7
+ // @return [Map]
13
8
 
14
9
  @function count-values($list) {
15
- $keys: ();
16
- $counts: ();
17
-
18
- @each $item in $list {
19
- $index: index($keys, $item);
20
-
21
- @if not $index {
22
- $keys: append($keys, $item);
23
- $counts: append($counts, 1);
24
- }
10
+ $map: ();
25
11
 
26
- @else {
27
- $counts: replace-nth($counts, $index, nth($counts, $index) + 1);
28
- }
29
- }
12
+ @each $item in $list {
13
+ $index: map-get($map, $item);
14
+ $value: if($index, $index + 1, 1);
15
+ $map: map-merge($map, ($item: $value));
16
+ }
30
17
 
31
- @return zip($keys, $counts);
32
- }
18
+ @return $map;
19
+ }
@@ -2,9 +2,6 @@
2
2
  // -------------------------------------------------------------------------------
3
3
  // @documentation http://sassylists.com/documentation/#debug
4
4
  // -------------------------------------------------------------------------------
5
- // @example debug(a b c d e) => [ a, b, c, d, e ]
6
- // @example debug(a b (c d e)) => [ a, b, [ c, d, e ] ]
7
- // -------------------------------------------------------------------------------
8
5
  // @param $list [List] : list
9
6
  // @param $pre [Boolean] : enable/disable variables type and proper indentation
10
7
  // @param $level [Number] : internal variable for recursivity
@@ -12,64 +9,65 @@
12
9
  // @return [String]
13
10
 
14
11
  @function debug($list, $pre: false, $level: 1) {
15
- @if length($list) == 0 {
16
- @return "( )";
17
- }
18
-
19
- @if length($list) == 1 {
20
- @return if($pre, "(" + type-of($list) + ") ", "") + $list;
12
+ @if length($list) == 0 {
13
+ @return "( )";
14
+ }
15
+
16
+ @if length($list) == 1 {
17
+ @return if($pre, "(" + type-of($list) + ") ", "") + $list;
18
+ }
19
+
20
+ $tab: " ";
21
+ $indent: "";
22
+ $break: if($pre, "\A ", "");
23
+ $length: length($list);
24
+
25
+ @for $i from 1 to $level {
26
+ $indent: $indent + $tab;
27
+ }
28
+
29
+ $result: "[" + $break;
30
+
31
+ @for $i from 1 through $length {
32
+ $item: nth($list, $i);
33
+ $result: $result + if($pre, $indent + $tab, " ");
34
+
35
+ @if length($item) > 1 {
36
+ $result: $result
37
+ + if($pre, "(list: " + length($item) + ") ", "")
38
+ + debug($item, $pre, $level + 1);
21
39
  }
22
40
 
23
- $tab: " ";
24
- $indent: "";
25
- $break: if($pre, "\A ", "");
41
+ @else {
42
+ @if $pre {
43
+ $result: $result + "(" + type-of($item) + ") ";
44
+ }
45
+
46
+ @if length($item) == 0 {
47
+ $result: $result + "( )";
48
+ }
26
49
 
27
- @for $i from 1 to $level {
28
- $indent: $indent + $tab;
50
+ @else if type-of($item) == "string" {
51
+ $result: $result + quote($item);
52
+ }
53
+
54
+ @else if $item == null {
55
+ $result: $result + "null";
56
+ }
57
+
58
+ @else {
59
+ $result: $result + $item;
60
+ }
29
61
  }
30
62
 
31
- $result: "[" + $break;
32
-
33
- @for $i from 1 through length($list) {
34
- $item: nth($list, $i);
35
- $result: $result + if($pre, $indent + $tab, " ");
36
-
37
- @if length($item) > 1 {
38
- $result: $result
39
- + if($pre, "(list: " + length($item) + ") ", "")
40
- + debug($item, $pre, $level + 1);
41
- }
42
-
43
- @else {
44
- @if $pre {
45
- $result: $result + "(" + type-of($item) + ") ";
46
- }
47
-
48
- @if length($item) == 0 {
49
- $result: $result + "( )";
50
- }
51
-
52
- @else if type-of($item) == string {
53
- $result: $result + quote($item);
54
- }
55
-
56
- @else if $item == null {
57
- $result: $result + "null";
58
- }
59
-
60
- @else {
61
- $result: $result + $item;
62
- }
63
- }
64
-
65
- @if $i != length($list) {
66
- $result: $result + "," + $break;
67
- }
63
+ @if $i != $length {
64
+ $result: $result + "," + $break;
68
65
  }
66
+ }
69
67
 
70
- $result: $result + $break + if($pre, if($level > 1, $indent, ""), " ") + "]";
68
+ $result: $result + $break + if($pre, if($level > 1, $indent, ""), " ") + "]";
71
69
 
72
- @return quote($result);
70
+ @return quote($result);
73
71
  }
74
72
 
75
73
  // Mixin displaying clean debug
@@ -77,20 +75,20 @@
77
75
  // @param $list [List] : list
78
76
 
79
77
  @mixin debug($list) {
80
- body:before {
81
- content: debug($list, true) !important;
78
+ body:before {
79
+ content: debug($list, true) !important;
82
80
 
83
- display: block !important;
84
- margin: 1em !important;
85
- padding: .5em !important;
81
+ display: block !important;
82
+ margin: 1em !important;
83
+ padding: .5em !important;
86
84
 
87
- background: #EFEFEF !important;
88
- border: 1px solid #DDD !important;
89
- border-radius: .2em !important;
85
+ background: #EFEFEF !important;
86
+ border: 1px solid #DDD !important;
87
+ border-radius: .2em !important;
90
88
 
91
- color: #333 !important;
92
- font: .75em/1.5 "Courier New", monospace !important;
93
- text-shadow: 0 1px white !important;
94
- white-space: pre-wrap !important;
95
- }
89
+ color: #333 !important;
90
+ font: .75em/1.5 "Courier New", monospace !important;
91
+ text-shadow: 0 1px white !important;
92
+ white-space: pre-wrap !important;
93
+ }
96
94
  }
@@ -0,0 +1,49 @@
1
+ // Explode $string into a list using $break as a breaker
2
+ // -------------------------------------------------------------------------------
3
+ // @documentation http://sassylists.com/documentation/#explode
4
+ // -------------------------------------------------------------------------------
5
+ // @param $string [String] : string to explode
6
+ // @param $separator [String] : string to use as the breaker
7
+ // -------------------------------------------------------------------------------
8
+ // @raise [Error] if either argument is not a string
9
+ // -------------------------------------------------------------------------------
10
+ // @return [List]
11
+
12
+ @function explode($string, $delimiter: '') {
13
+ @if type-of($string) != "string" {
14
+ @warn "`explode` function expecting a string; #{type-of($string)} given.";
15
+ @return false;
16
+ }
17
+
18
+ @if type-of($delimiter) != "string" {
19
+ @warn "`explode` function expecting a string; #{type-of($delimiter)} given.";
20
+ @return false;
21
+ }
22
+
23
+ $result: ();
24
+ $length: str-length($string);
25
+
26
+ @if str-length($delimiter) == 0 {
27
+ @for $i from 1 through $length {
28
+ $result: append($result, str-slice($string, $i, $i));
29
+ }
30
+ @return $result;
31
+ }
32
+
33
+ $running: true;
34
+ $remaining: $string;
35
+
36
+ @while $running {
37
+ $index: str-index($remaining, $delimiter);
38
+ @if not $index or $index == 0 {
39
+ $running: false;
40
+ }
41
+ @else {
42
+ $slice: str-slice($remaining, 1, $index - 1);
43
+ $result: append($result, $slice);
44
+ $remaining: str-slice($remaining, $index + str-length($delimiter));
45
+ }
46
+ }
47
+
48
+ @return append($result, $remaining);
49
+ }
@@ -4,17 +4,22 @@
4
4
  // -------------------------------------------------------------------------------
5
5
  // @alias `head()`
6
6
  // -------------------------------------------------------------------------------
7
- // @example first(a b c) => a
8
- // -------------------------------------------------------------------------------
9
7
  // @param $list [List] : list
10
8
  // -------------------------------------------------------------------------------
11
- // @return [Literal]
9
+ // @raise [Error] if either $list is empty
10
+ // -------------------------------------------------------------------------------
11
+ // @return [Literal] | false
12
12
 
13
13
  @function first($list) {
14
- @return nth($list, 1);
14
+ @if length($list) == 0 {
15
+ @warn "Cannot find first item of empty list.";
16
+ @return false;
17
+ }
18
+
19
+ @return nth($list, 1);
15
20
  }
16
21
 
17
22
  // Alias
18
23
  @function head($list) {
19
- @return first($list);
24
+ @return first($list);
20
25
  }
@@ -4,28 +4,31 @@
4
4
  // -------------------------------------------------------------------------------
5
5
  // @alias `unfold()`
6
6
  // -------------------------------------------------------------------------------
7
- // @example flatten(a b (c d (e f))) => a b c d e f
8
- // -------------------------------------------------------------------------------
9
7
  // @param $list [List] : list
10
8
  // -------------------------------------------------------------------------------
11
9
  // @return [List]
12
10
 
13
11
  @function flatten($list) {
14
- $result: ();
12
+ $result: ();
13
+
14
+ @if length($list) == 1 {
15
+ @return $list;
16
+ }
15
17
 
16
- @each $item in $list {
17
- @if length($item) > 1 {
18
- @each $i in flatten($item) {
19
- $result: append($result, $i);
20
- }
21
- }
18
+ @each $item in $list {
19
+ @if length($item) > 1 {
20
+ $flatten: flatten($item);
21
+ @each $i in $flatten {
22
+ $result: append($result, $i, list-separator($list));
23
+ }
24
+ }
22
25
 
23
- @else {
24
- $result: append($result, $item);
25
- }
26
+ @else {
27
+ $result: append($result, $item, list-separator($list));
26
28
  }
29
+ }
27
30
 
28
- @return $result;
31
+ @return $result;
29
32
  }
30
33
 
31
34
  // Alias
@@ -2,11 +2,6 @@
2
2
  // -------------------------------------------------------------------------------
3
3
  // @documentation http://sassylists.com/documentation/#insert-nth
4
4
  // -------------------------------------------------------------------------------
5
- // @example insert-nth(a b c, 2, z) => a z d c
6
- // @example insert-nth(a b c, 0, z) => error
7
- // @example insert-nth(a b c, -1, z) => error
8
- // @example insert-nth(a b c, 10, z) => a b c z
9
- // -------------------------------------------------------------------------------
10
5
  // @param $list [List] : list
11
6
  // @param $index [Number] : index to add
12
7
  // @param $value [Literal] : value to add
@@ -18,31 +13,33 @@
18
13
  // @return [List] | false
19
14
 
20
15
  @function insert-nth($list, $index, $value) {
21
- @if type-of($index) != number {
22
- @warn "List index #{$index} is not a number for `insert-nth`.";
23
- @return false;
24
- }
16
+ $length: length($list);
25
17
 
26
- @if $index < 1 {
27
- @warn "List index #{$index} must be a non-zero integer for `insert-nth`";
28
- @return false;
29
- }
18
+ @if type-of($index) != number {
19
+ @warn "List index #{$index} is not a number for `insert-nth`.";
20
+ @return false;
21
+ }
30
22
 
31
- @if $index > length($list) {
32
- @return append($list, $value);
33
- }
23
+ @if $index < 1 {
24
+ @warn "List index #{$index} must be a non-zero integer for `insert-nth`";
25
+ @return false;
26
+ }
34
27
 
35
- $result: ();
28
+ @if $index > $length {
29
+ @return append($list, $value, list-separator($list));
30
+ }
36
31
 
37
- @for $i from 1 through length($list) {
38
- @if $i == $index {
39
- @if $value and $value != "" and $value != () {
40
- $result: append($result, $value);
41
- }
42
- }
32
+ $result: ();
43
33
 
44
- $result: append($result, nth($list, $i));
34
+ @for $i from 1 through $length {
35
+ @if $i == $index {
36
+ @if is-true($value) {
37
+ $result: append($result, $value, list-separator($list));
38
+ }
45
39
  }
46
40
 
47
- @return $result;
41
+ $result: append($result, nth($list, $i), list-separator($list));
42
+ }
43
+
44
+ @return $result;
48
45
  }
@@ -2,27 +2,26 @@
2
2
  // -------------------------------------------------------------------------------
3
3
  // @documentation http://sassylists.com/documentation/#intersection
4
4
  // -------------------------------------------------------------------------------
5
- // @example intersection(a b c d, a c d, e d a f) => a d
6
- // -------------------------------------------------------------------------------
7
5
  // @param $list [List] : first list
8
6
  // @param $lists [ArgList] : other lists
9
7
  // -------------------------------------------------------------------------------
10
8
  // @return [List]
11
9
 
12
10
  @function intersection($list, $lists...) {
13
- $result: $list;
14
-
15
- @each $list in $lists {
16
- $temp: ();
11
+ $result: $list;
17
12
 
18
- @each $item in $result {
19
- @if not not index($list, $item) {
20
- $temp: append($temp, $item);
21
- }
22
- }
13
+ @each $list in $lists {
14
+ $temp: ();
23
15
 
24
- $result: $temp;
16
+ @each $item in $result {
17
+ @if not not index($list, $item) {
18
+ $temp: append($temp, $item, list-separator($list));
19
+ }
25
20
  }
26
21
 
27
- @return $result;
22
+ $result: $temp;
23
+ }
24
+
25
+ $result: remove-duplicates($result);
26
+ @return if(length($result) == 1, nth($result, 1), $result);
28
27
  }
@@ -6,20 +6,15 @@
6
6
  // -------------------------------------------------------------------------------
7
7
  // @dependence `reverse()`
8
8
  // -------------------------------------------------------------------------------
9
- // @example is-symmetrical(a b c d e) => false
10
- // @example is-symmetrical(a b c b a) => true
11
- // @example is-symmetrical(a (b c d) a) => true
12
- // -------------------------------------------------------------------------------
13
9
  // @param $list [List] : list
14
10
  // -------------------------------------------------------------------------------
15
11
  // @return [Boolean]
16
12
 
17
13
  @function is-symmetrical($list) {
18
- $reverse: reverse($list);
19
- @return $reverse == reverse($reverse);
14
+ @return $list == reverse($list);
20
15
  }
21
16
 
22
17
  // Alias
23
18
  @function is-mirror($list) {
24
- @return is-symmetrical($list);
19
+ @return is-symmetrical($list);
25
20
  }
@@ -2,22 +2,20 @@
2
2
  // -------------------------------------------------------------------------------
3
3
  // @documentation http://sassylists.com/documentation/#last-index
4
4
  // -------------------------------------------------------------------------------
5
- // @example last-index(a b c a, a) => 4
6
- // @example last-index(a b c, z) => null
7
- // -------------------------------------------------------------------------------
8
5
  // @param $list [List] : list
9
6
  // @param $value [Literal] : value to be searched for
10
7
  // -------------------------------------------------------------------------------
11
8
  // @return [Number] | null
12
9
 
13
10
  @function last-index($list, $value) {
11
+ $length: length($list);
14
12
 
15
- @for $i from length($list) * -1 through -1 {
16
- $i: abs($i);
17
- @if nth($list, $i) == $value {
18
- @return $i;
19
- }
13
+ @for $i from $length * -1 through -1 {
14
+ $i: abs($i);
15
+ @if nth($list, $i) == $value {
16
+ @return $i;
20
17
  }
18
+ }
21
19
 
22
- @return null;
20
+ @return null;
23
21
  }
@@ -4,17 +4,22 @@
4
4
  // -------------------------------------------------------------------------------
5
5
  // @alias `tail()`
6
6
  // -------------------------------------------------------------------------------
7
- // @example last(a b c) => c
8
- // -------------------------------------------------------------------------------
9
7
  // @param $list [List] : list
10
8
  // -------------------------------------------------------------------------------
11
- // @return [Literal]
9
+ // @raise [Error] if list is empty
10
+ // -------------------------------------------------------------------------------
11
+ // @return [Literal] | false
12
12
 
13
13
  @function last($list) {
14
- @return nth($list, length($list));
14
+ @if length($list) == 0 {
15
+ @warn "Cannot find last item of empty list.";
16
+ @return false;
17
+ }
18
+
19
+ @return nth($list, length($list));
15
20
  }
16
21
 
17
22
  // Alias
18
23
  @function tail($list) {
19
- @return last($list);
24
+ @return last($list);
20
25
  }