ListFunctions 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ListFunctions.rb +30 -0
  3. data/stylesheets/ListFunctions/_chunk.scss +32 -0
  4. data/stylesheets/ListFunctions/_count-values.scss +33 -0
  5. data/stylesheets/ListFunctions/_debug.scss +34 -0
  6. data/stylesheets/ListFunctions/_first.scss +13 -0
  7. data/stylesheets/ListFunctions/_insert-nth.scss +52 -0
  8. data/stylesheets/ListFunctions/_is-symmetrical.scss +23 -0
  9. data/stylesheets/ListFunctions/_last-index.scss +24 -0
  10. data/stylesheets/ListFunctions/_last.scss +13 -0
  11. data/stylesheets/ListFunctions/_loop.scss +21 -0
  12. data/stylesheets/ListFunctions/_prepend.scss +16 -0
  13. data/stylesheets/ListFunctions/_purge.scss +21 -0
  14. data/stylesheets/ListFunctions/_remove-duplicates.scss +30 -0
  15. data/stylesheets/ListFunctions/_remove-nth.scss +23 -0
  16. data/stylesheets/ListFunctions/_remove.scss +19 -0
  17. data/stylesheets/ListFunctions/_replace-nth.scss +50 -0
  18. data/stylesheets/ListFunctions/_replace.scss +32 -0
  19. data/stylesheets/ListFunctions/_reverse.scss +30 -0
  20. data/stylesheets/ListFunctions/_slice.scss +60 -0
  21. data/stylesheets/ListFunctions/_sum.scss +32 -0
  22. data/stylesheets/ListFunctions/_to-string.scss +29 -0
  23. data/stylesheets/_ListFunctions.scss +90 -0
  24. data/templates/project/css/ListFunctions.css +351 -0
  25. data/templates/project/sass/ListFunctions/_chunk.scss +32 -0
  26. data/templates/project/sass/ListFunctions/_count-values.scss +33 -0
  27. data/templates/project/sass/ListFunctions/_debug.scss +34 -0
  28. data/templates/project/sass/ListFunctions/_first.scss +13 -0
  29. data/templates/project/sass/ListFunctions/_insert-nth.scss +52 -0
  30. data/templates/project/sass/ListFunctions/_is-symmetrical.scss +23 -0
  31. data/templates/project/sass/ListFunctions/_last-index.scss +24 -0
  32. data/templates/project/sass/ListFunctions/_last.scss +13 -0
  33. data/templates/project/sass/ListFunctions/_loop.scss +21 -0
  34. data/templates/project/sass/ListFunctions/_prepend.scss +16 -0
  35. data/templates/project/sass/ListFunctions/_purge.scss +21 -0
  36. data/templates/project/sass/ListFunctions/_remove-duplicates.scss +30 -0
  37. data/templates/project/sass/ListFunctions/_remove-nth.scss +23 -0
  38. data/templates/project/sass/ListFunctions/_remove.scss +19 -0
  39. data/templates/project/sass/ListFunctions/_replace-nth.scss +50 -0
  40. data/templates/project/sass/ListFunctions/_replace.scss +32 -0
  41. data/templates/project/sass/ListFunctions/_reverse.scss +30 -0
  42. data/templates/project/sass/ListFunctions/_slice.scss +60 -0
  43. data/templates/project/sass/ListFunctions/_sum.scss +32 -0
  44. data/templates/project/sass/ListFunctions/_to-string.scss +29 -0
  45. data/templates/project/sass/_ListFunctions.scss +90 -0
  46. metadata +115 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d656970f44fab99bfadc04718acd6f678578a68f
4
+ data.tar.gz: f4ec0ebdd9d18d9c7a62308e127513489a232605
5
+ SHA512:
6
+ metadata.gz: 232e1c167919adb995faece9e23a37c2a1622809b1a6a948eefc7552057b9b3171b4d213b0f2b095bb4c99dd4948134cb281721da74d05a1edc6a2ac293252a8
7
+ data.tar.gz: c2837143ab92c7ee9b05fbe314c7754f882e8d68f59f03452761e3885f34839fffc2d0e681ecdaeac7fce2168ae70883a8678c816a1b0c62380a4d7f8306ea9a
@@ -0,0 +1,30 @@
1
+ # All gems that are required for this extension to work should go here.
2
+ # These are the requires you would normally put in your config.rb file
3
+ # By default, you should always included Compass. Do not include your
4
+ # extension.
5
+ require 'compass'
6
+
7
+ # This tells Compass what your Compass extension is called, and where to find
8
+ # its files
9
+ # Replace 'ListFunctions' with the name of your extension. Spaces allowed.
10
+ extension_path = File.expand_path(File.join(File.dirname(__FILE__), ".."))
11
+ Compass::Frameworks.register('ListFunctions', :path => extension_path)
12
+
13
+ # Version and date of version for your Compass extension.
14
+ # Replace MyExtension with the name of your extension
15
+ # Letters, numbers, and underscores only
16
+ # Version is a number. If a version contains alphas, it will be created as
17
+ # a prerelease version
18
+ # Date is in the form of YYYY-MM-DD
19
+ module ListFunctions
20
+ VERSION = "0.1"
21
+ DATE = "2013-10-06"
22
+ end
23
+
24
+ # This is where any custom SassScript should be placed. The functions will be
25
+ # available on require of your extension without the need for users to import
26
+ # any partials. Uncomment below.
27
+
28
+ # module Sass::Script::Functions
29
+ #
30
+ # end
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Chunk $list into $size large lists
3
+ * -------------------------------------------------------------------------------
4
+ * @example chunk( (a, b, c, d, e), 2 ) => a b, c d, e
5
+ * @example chunk( (a, b, c, d, e), 3 ) => a b c, d e
6
+ * @example chunk( a, 3 ) => a
7
+ * -------------------------------------------------------------------------------
8
+ * @param $list [List] : list
9
+ * @param $size [Number] : length of lists
10
+ * -------------------------------------------------------------------------------
11
+ * @return [List]
12
+ */
13
+ @function chunk($list, $size) {
14
+ $n: ceil(length($list) / $size);
15
+ $temp-index: 0;
16
+ $result: ();
17
+
18
+ @for $i from 1 through $n {
19
+ $temp-list: ();
20
+
21
+ @for $j from 1 + $temp-index through $size + $temp-index {
22
+ @if $j <= length($list) {
23
+ $temp-list: append($temp-list, nth($list, $j));
24
+ }
25
+ }
26
+
27
+ $result: append($result, $temp-list);
28
+ $temp-index: $temp-index + $size;
29
+ }
30
+
31
+ @return $result;
32
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Count the number of occurrences of each value of $list
3
+ * -------------------------------------------------------------------------------
4
+ * @dependence `replace-nth()`
5
+ * -------------------------------------------------------------------------------
6
+ * @example count-values( (a, b, c, d, e) ) => a 1, b 1, c 1, d 1, e 1
7
+ * @example count-values( (a, b, c, a, d, b, a, e) ) => a 3, b 2, c 1, d 1, e 1
8
+ * @example count-values( a ) => a 1
9
+ * -------------------------------------------------------------------------------
10
+ * @param $list [List] : list
11
+ * -------------------------------------------------------------------------------
12
+ * @return [List]
13
+ */
14
+ @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
+ }
25
+
26
+ @else {
27
+ $count : nth($counts, $index) + 1;
28
+ $counts : replace-nth($counts, $index, $count);
29
+ }
30
+ }
31
+
32
+ @return zip($keys, $counts);
33
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Returns $list as a string
3
+ * -------------------------------------------------------------------------------
4
+ * @example debug( (a, b, c, d, e) ) => [ a, b, c, d, e ]
5
+ * @example debug( (a b (c d e) ) ) => [ a, b, [ c, d, e ] ]
6
+ * @example debug( a ) => [ a ]
7
+ * -------------------------------------------------------------------------------
8
+ * @param $list [List] : list
9
+ * -------------------------------------------------------------------------------
10
+ * @return [String]
11
+ */
12
+ @function debug($list) {
13
+ $result: #{"[ "};
14
+
15
+ @each $item in $list {
16
+
17
+ @if length($item) > 1 {
18
+ $result: $result#{debug($item)};
19
+ }
20
+
21
+ @else {
22
+ $result: $result#{$item};
23
+ }
24
+
25
+ @if index($list, $item) != length($list) {
26
+ $result: $result#{", "};
27
+ }
28
+
29
+ }
30
+
31
+ $result: $result#{" ]"};
32
+
33
+ @return $result;
34
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Returns first element of $list
3
+ * -------------------------------------------------------------------------------
4
+ * @example first( (a, b, c) ) => a
5
+ * @example first( a ) => a
6
+ * -------------------------------------------------------------------------------
7
+ * @param $list [List] : list
8
+ * -------------------------------------------------------------------------------
9
+ * @return [Literal]
10
+ */
11
+ @function first($list) {
12
+ @return nth($list, 1);
13
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Add $value at $index in $list
3
+ * -------------------------------------------------------------------------------
4
+ * @dependence `purge()`
5
+ * -------------------------------------------------------------------------------
6
+ * @example insert-nth( (a, b, c), 2, z ) => a, z, b, c
7
+ * @example insert-nth( (a, b, c), 0, z ) => error
8
+ * @example insert-nth( (a, b, c), -1, z ) => error
9
+ * @example insert-nth( (a, b, c), 10, z ) => error
10
+ * -------------------------------------------------------------------------------
11
+ * @param $list [List] : list
12
+ * @param $index [Number] : index to add
13
+ * @param $value [Literal] : value to add
14
+ * -------------------------------------------------------------------------------
15
+ * @raise [Error] if $index isn't an integer
16
+ * @raise [Error] if $index is strictly lesser than 1
17
+ * @raise [Error] if $index is strictly greater than length of $list
18
+ * -------------------------------------------------------------------------------
19
+ * @return [List] | false
20
+ */
21
+ @function insert-nth($list, $index, $value) {
22
+ $result: false;
23
+
24
+ @if type-of($index) != number {
25
+ @warn "$index: #{quote($index)} is not a number for `insert-nth`.";
26
+ @return $result;
27
+ }
28
+
29
+ @else if $index < 1 {
30
+ @warn "List index 0 must be a non-zero integer for `insert-nth`";
31
+ @return $result;
32
+ }
33
+
34
+ @else if $index > length($list) {
35
+ @warn "List index is #{$index} but list is only #{length($list)} item long for `insert-nth'.";
36
+ @return $result;
37
+ }
38
+
39
+ @else {
40
+ $result: ();
41
+
42
+ @for $i from 1 through length($list) {
43
+ @if $i == $index {
44
+ $result: append($result, $value);
45
+ }
46
+
47
+ $result: append($result, nth($list, $i));
48
+ }
49
+ }
50
+
51
+ @return purge($result);
52
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Test if $list is symmetrical (one-level deep)
3
+ * -------------------------------------------------------------------------------
4
+ * @dependence `reverse()`
5
+ * -------------------------------------------------------------------------------
6
+ * @example is-symmetrical( (a, b, c, d, e) ) => false
7
+ * @example is-symmetrical( (a, b, c, b, a) ) => true
8
+ * @example is-symmetrical( a ) => true
9
+ * @example is-symmetrical( a, b c d, a ) => true
10
+ * -------------------------------------------------------------------------------
11
+ * @param $list [List] : list
12
+ * -------------------------------------------------------------------------------
13
+ * @return [Boolean]
14
+ */
15
+ @function is-symmetrical($list) {
16
+ $result: ();
17
+
18
+ @each $item in $list {
19
+ $result: append($result, $item, space);
20
+ }
21
+
22
+ @return $result == reverse($result);
23
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Returns last index of $value in $list
3
+ * -------------------------------------------------------------------------------
4
+ * @example last-index( (a, b, c, a), a ) => 4
5
+ * @example last-index( (a, b, c), z ) => null
6
+ * -------------------------------------------------------------------------------
7
+ * @param $list [List] : list
8
+ * @param $value [Literal] : value to be searched for
9
+ * -------------------------------------------------------------------------------
10
+ * @return [Number] | null
11
+ */
12
+ @function last-index($list, $value) {
13
+
14
+ @for $i from length($list) * -1 through -1 {
15
+ $i: abs($i);
16
+
17
+ @if nth($list, $i) == $value {
18
+ @return $i;
19
+ }
20
+
21
+ }
22
+
23
+ @return null;
24
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Returns last element of $list
3
+ * -------------------------------------------------------------------------------
4
+ * @example last( (a, b, c) ) => c
5
+ * @example last( a ) => a
6
+ * -------------------------------------------------------------------------------
7
+ * @param $list [List] : list
8
+ * -------------------------------------------------------------------------------
9
+ * @return [Literal]
10
+ */
11
+ @function last($list) {
12
+ @return nth($list, length($list));
13
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Shift indexes from $list of $value
3
+ * -------------------------------------------------------------------------------
4
+ * @example loop( (a, b, c, d, e) ) => e, a, b, c, d
5
+ * @example loop( (a, b, c, d, e), 2 ) => d, e, a, b, c
6
+ * @example loop( (a, b, c, d, e), -2 ) => c, d, e, a, b
7
+ * -------------------------------------------------------------------------------
8
+ * @param $list [List] : list
9
+ * @param $value [Number] : number of position between old and new indexes
10
+ * -------------------------------------------------------------------------------
11
+ * @return [List]
12
+ */
13
+ @function loop($list, $value: 1) {
14
+ $result: ();
15
+
16
+ @for $i from 0 to length($list) {
17
+ $result: append($result, nth($list, ($i - $value) % length($list) + 1));
18
+ }
19
+
20
+ @return $result;
21
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Add $value as first index of $list
3
+ * -------------------------------------------------------------------------------
4
+ * @dependence `purge()`
5
+ * -------------------------------------------------------------------------------
6
+ * @example prepend( (a, b, c), z ) => z, a, b, c
7
+ * @example prepend( (a, b, c), y z ) => y z, a, b, c
8
+ * -------------------------------------------------------------------------------
9
+ * @param $list [List] : list
10
+ * @param $value [Literal] : value to prepend to the list
11
+ * -------------------------------------------------------------------------------
12
+ * @return [List]
13
+ */
14
+ @function prepend($list, $value) {
15
+ @return purge(join($value, $list));
16
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Remove all false and null values from $list
3
+ * -------------------------------------------------------------------------------
4
+ * @example purge( (a, null, b, false, c, null) ) => a, b, c
5
+ * @example purge( (a, b, c) ) => a, b, c
6
+ * -------------------------------------------------------------------------------
7
+ * @param $list [List] : list
8
+ * -------------------------------------------------------------------------------
9
+ * @return [List]
10
+ */
11
+ @function purge($list) {
12
+ $result: ();
13
+
14
+ @each $item in $list {
15
+ @if $item != null and $item != false and $item != "" {
16
+ $result: append($result, $item);
17
+ }
18
+ }
19
+
20
+ @return $result;
21
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Remove duplicate values from $list
3
+ * -------------------------------------------------------------------------------
4
+ * @example remove-duplicates( (a, b, a, c, b, d, c, e) ) => a, b, c, d, e
5
+ * @example remove-duplicates( (a, b, (c, c, c)) ) => a, b, c
6
+ * -------------------------------------------------------------------------------
7
+ * @param $list [List] : list
8
+ * @param $recursive [Boolean] : enable / disable recursivity
9
+ * -------------------------------------------------------------------------------
10
+ * @return [List]
11
+ */
12
+ @function remove-duplicates($list, $recursive: false) {
13
+ $result: ();
14
+
15
+ @each $item in $list {
16
+
17
+ @if not index($result, $item) {
18
+ @if length($item) > 1 and $recursive {
19
+ $result: append($result, remove-duplicates($item, $recursive));
20
+ }
21
+
22
+ @else {
23
+ $result: append($result, $item);
24
+ }
25
+ }
26
+
27
+ }
28
+
29
+ @return $result;
30
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Remove value from $list at index $index
3
+ * -------------------------------------------------------------------------------
4
+ * @dependence `replace-nth()`
5
+ * -------------------------------------------------------------------------------
6
+ * @example remove-nth( (a, b, c), 2 ) => a, c
7
+ * @example remove-nth( (a, b, c), 0 ) => error
8
+ * @example remove-nth( (a, b, c), -1 ) => a, b
9
+ * @example remove-nth( (a, b, c), 10 ) => error
10
+ * @example remove-nth( (a, b, c), -10 ) => error
11
+ * -------------------------------------------------------------------------------
12
+ * @param $list [List] : list
13
+ * @param $index [Number] : index to remove
14
+ * -------------------------------------------------------------------------------
15
+ * @raise [Error] if $index isn't an integer
16
+ * @raise [Error] if $index is 0
17
+ * @raise [Error] if abs value of $index is strictly greater then length of $list
18
+ * -------------------------------------------------------------------------------
19
+ * @return [List] | false
20
+ */
21
+ @function remove-nth($list, $index) {
22
+ @return replace-nth($list, $index, "");
23
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Remove value(s) $value from $list
3
+ * -------------------------------------------------------------------------------
4
+ * @dependence `replace()`
5
+ * -------------------------------------------------------------------------------
6
+ * @example remove( (a, b, c), b ) => a, c
7
+ * @example remove( (a, b, c), z ) => a, b, c
8
+ * @example remove( (a, b, c b), b ) => a, c b
9
+ * @example remove( (a, b, c b), b, true ) => a, c
10
+ * -------------------------------------------------------------------------------
11
+ * @param $list [List] : list
12
+ * @param $value [Literal] : value to remove
13
+ * @param $recursive [Boolean] : enable / disable recursivity
14
+ * -------------------------------------------------------------------------------
15
+ * @return [List]
16
+ */
17
+ @function remove($list, $value, $recursive: false) {
18
+ @return replace($list, $value, "", $recursive);
19
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Replace value at $index from $list by $value
3
+ * -------------------------------------------------------------------------------
4
+ * @dependence `purge()`
5
+ * -------------------------------------------------------------------------------
6
+ * @example replace-nth( (a, b, c), 2, z ) => a, z, c
7
+ * @example replace-nth( (a, b, c), 0, z ) => error
8
+ * @example replace-nth( (a, b, c), -1, z ) => a, b, z
9
+ * @example replace-nth( (a, b, c), 10, z ) => error
10
+ * @example replace-nth( (a, b, c), -10, z ) => error
11
+ * -------------------------------------------------------------------------------
12
+ * @param $list [List] : list
13
+ * @param $index [Number] : index to update
14
+ * @param $value [Literal] : new value for index $index
15
+ * -------------------------------------------------------------------------------
16
+ * @raise [Error] if $index isn't an integer
17
+ * @raise [Error] if $index is 0
18
+ * @raise [Error] if abs value of $index is strictly greater than length of $list
19
+ * -------------------------------------------------------------------------------
20
+ * @return [List] | false
21
+ */
22
+ @function replace-nth($list, $index, $value) {
23
+ $result: false;
24
+
25
+ @if type-of($index) != number {
26
+ @warn "$index: #{quote($index)} is not a number for `replace-nth`/`remove-nth`.";
27
+ @return $result;
28
+ }
29
+
30
+ @else if $index == 0 {
31
+ @warn "List index 0 must be a non-zero integer for `replace-nth`/`remove-nth`.";
32
+ @return $result;
33
+ }
34
+
35
+ @else if abs($index) > length($list) {
36
+ @warn "List index is #{$index} but list is only #{length($list)} item long for `replace-nth`/`remove-nth`.";
37
+ @return $result;
38
+ }
39
+
40
+ @else {
41
+ $result: ();
42
+ $index: if($index < 0, length($list) + $index + 1, $index);
43
+
44
+ @for $i from 1 through length($list) {
45
+ $result: append($result, if($i == $index, $value, nth($list, $i)));
46
+ }
47
+ }
48
+
49
+ @return purge($result);
50
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Replace $old-value by $new-value in $list
3
+ * -------------------------------------------------------------------------------
4
+ * @dependence `purge()`
5
+ * -------------------------------------------------------------------------------
6
+ * @example replace( (a, b, c), b, z ) => a, z, c
7
+ * @example replace( (a, b, c), y, z ) => a, b, c
8
+ * @example replace( (a, b, c a), a, z ) => z, b, c z
9
+ * @example replace( (a, b, c a), a, z, true ) => z, b, c z
10
+ * -------------------------------------------------------------------------------
11
+ * @param $list [List] : list
12
+ * @param $old-value [Literal] : value to replace
13
+ * @param $new-value [Literal] : new value for $old-value
14
+ * @param $recursive [Boolean] : enable / disable recursivity
15
+ * -------------------------------------------------------------------------------
16
+ * @return [List]
17
+ */
18
+ @function replace($list, $old-value, $new-value, $recursive: false) {
19
+ $result: ();
20
+
21
+ @each $item in $list {
22
+ @if length($item) > 1 and $recursive {
23
+ $result: append($result, replace($item, $old-value, $new-value, $recursive));
24
+ }
25
+
26
+ @else {
27
+ $result: append($result, if($item == $old-value, $new-value, $item));
28
+ }
29
+ }
30
+
31
+ @return purge($result);
32
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Reverses the order of $list
3
+ * -------------------------------------------------------------------------------
4
+ * @example reverse( (a, b, c) ) => c, b, a
5
+ * @example reverse( (a, b, c a) ) => c a, b, a
6
+ * @example reverse( (a, b, c a), true ) => a c, b, a
7
+ * @example reverse( a ) => a
8
+ * -------------------------------------------------------------------------------
9
+ * @param $list [List] : list
10
+ * @param $recursive [Boolean] : enable / disable recursivity
11
+ * -------------------------------------------------------------------------------
12
+ * @return [List]
13
+ */
14
+ @function reverse($list, $recursive: false) {
15
+ $result: ();
16
+
17
+ @for $i from length($list) * -1 through -1 {
18
+ $item: nth($list, abs($i));
19
+
20
+ @if length($item) > 1 and $recursive {
21
+ $result: append($result, reverse($item, $recursive));
22
+ }
23
+
24
+ @else {
25
+ $result: append($result, $item);
26
+ }
27
+ }
28
+
29
+ @return $result;
30
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Slices $list between $start and $end
3
+ * -------------------------------------------------------------------------------
4
+ * @example slice( (a, b, c, d), 2, 3 ) => b, c
5
+ * @example slice( (a, b, c, d), 3, 2 ) => error
6
+ * @example slice( (a, b, c, d), 3, 5 ) => error
7
+ * @example slice( (a, b, c, d), -1, 3 ) => error
8
+ * @example slice( (a, b, c, d), 0, 3 ) => error
9
+ * @example slice( (a, b, c, d), 3, 3 ) => c
10
+ * -------------------------------------------------------------------------------
11
+ * @param $list [List] : list
12
+ * @param $start [Number] : start index
13
+ * @param $end [Number] : end index
14
+ * -------------------------------------------------------------------------------
15
+ * @raise [Error] if $start or $end aren't integers
16
+ * @raise [Error] if $start is greater than $end
17
+ * @raise [Error] if $start or $end is strictly lesser than 1
18
+ * @raise [Error] if $start is strictly greater than length of $list
19
+ * @raise [Error] if $end is strictly greater than length of $list
20
+ * -------------------------------------------------------------------------------
21
+ * @return [List] | false
22
+ */
23
+ @function slice($list, $start: 1, $end: length($list)) {
24
+ $result: false;
25
+
26
+ @if type-of($start) != number or type-of($end) != number {
27
+ @warn "Either $start or $end are not a number for `slice`.";
28
+ @return $result;
29
+ }
30
+
31
+ @else if $start > $end {
32
+ @warn "The start index has to be lesser than or equals to the end index for `slice`.";
33
+ @return $result;
34
+ }
35
+
36
+ @else if $start < 1 or $end < 1 {
37
+ @warn "List indexes must be non-zero integers for `slice`.";
38
+ @return $result;
39
+ }
40
+
41
+ @else if $start > length($list) {
42
+ @warn "List index is #{$start} but list is only #{length($list)} item long for `slice`.";
43
+ @return $result;
44
+ }
45
+
46
+ @else if $end > length($list) {
47
+ @warn "List index is #{$end} but list is only #{length($list)} item long for `slice`.";
48
+ @return $result;
49
+ }
50
+
51
+ @else {
52
+ $result: ();
53
+
54
+ @for $i from $start through $end {
55
+ $result: append($result, nth($list, $i));
56
+ }
57
+ }
58
+
59
+ @return $result;
60
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Sum up all unitless values in $list
3
+ * -------------------------------------------------------------------------------
4
+ * @example sum( (1 2 3 4 5) ) => 15
5
+ * @example sum( (1 a 2 b 3) ) => 6
6
+ * @example sum( (10px 3em 5%) ) => 0
7
+ * @example sum( (10px 3em 5%, true) ) => 18
8
+ * -------------------------------------------------------------------------------
9
+ * @param $list [List] : list
10
+ * @param $force [Boolean] : enable / disable parseInt
11
+ * -------------------------------------------------------------------------------
12
+ * @return [Number]
13
+ */
14
+ @function sum($list, $force: false) {
15
+ $result: 0;
16
+
17
+ @each $item in $list {
18
+ @if type-of($item) == number {
19
+
20
+ @if $force and unit($item) {
21
+ $item: $item / ($item * 0 + 1);
22
+ }
23
+
24
+ @if unitless($item) {
25
+ $result: $result + $item;
26
+ }
27
+
28
+ }
29
+ }
30
+
31
+ @return $result;
32
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Joins all elements of $list with $glue
3
+ * -------------------------------------------------------------------------------
4
+ * @example to-string( (a, b, c) ) => abc
5
+ * @example to-string( (a, b, c), '-' ) => a-b-c
6
+ * @example to-string( (a, b c, d) ) => abcd
7
+ * -------------------------------------------------------------------------------
8
+ * @param $list [List] : list
9
+ * @param $glue [String] : value to use as a join string
10
+ * @param $is-nested [Boolean] : strictly internal boolean for recursivity
11
+ * -------------------------------------------------------------------------------
12
+ * @return [String]
13
+ */
14
+ @function to-string($list, $glue: '', $is-nested: false) {
15
+ $result: null;
16
+
17
+ @each $item in $list {
18
+ @if length($item) > 1 {
19
+ $result: $result#{to-string($item, $glue, true)};
20
+ }
21
+
22
+ @else {
23
+ $condition: index($list, item) != length($list) or $is-nested;
24
+ $result: if($condition, $result#{$item}#{$glue}, $result#{$item});
25
+ }
26
+ }
27
+
28
+ @return $result;
29
+ }