SassyCast 1.0.0 → 1.1.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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -4
  3. data/README.md +123 -119
  4. data/lib/SassyCast.rb +14 -14
  5. data/stylesheets/SassyCast.scss +2 -7
  6. data/stylesheets/private/_all.scss +16 -0
  7. data/stylesheets/private/bool/_bool.scss +16 -0
  8. data/stylesheets/{types → private}/color/_color.scss +59 -63
  9. data/stylesheets/{types → private}/color/helpers/_from-hex.scss +41 -39
  10. data/stylesheets/{types → private}/color/helpers/_from-hsl.scss +48 -46
  11. data/stylesheets/{types → private}/color/helpers/_from-rgb.scss +49 -47
  12. data/stylesheets/private/color/helpers/_get-color-value.scss +20 -0
  13. data/stylesheets/private/color/helpers/_hex-to-dec.scss +22 -0
  14. data/stylesheets/{types → private}/list/_list.scss +33 -29
  15. data/stylesheets/private/map/_map.scss +23 -0
  16. data/stylesheets/private/null/_null.scss +13 -0
  17. data/stylesheets/{types → private}/number/_number.scss +63 -66
  18. data/stylesheets/{types → private}/number/helpers/_find-digits.scss +35 -33
  19. data/stylesheets/{types → private}/number/helpers/_find-integer.scss +35 -33
  20. data/stylesheets/private/number/helpers/_length.scss +21 -0
  21. data/stylesheets/private/number/helpers/_pow.scss +24 -0
  22. data/stylesheets/private/string/_string.scss +15 -0
  23. data/stylesheets/public/_all.scss +7 -0
  24. data/stylesheets/public/bool/_bool.scss +13 -0
  25. data/stylesheets/public/color/_color.scss +13 -0
  26. data/stylesheets/public/list/_list.scss +13 -0
  27. data/stylesheets/public/map/_map.scss +13 -0
  28. data/stylesheets/public/null/_null.scss +13 -0
  29. data/stylesheets/public/number/_number.scss +13 -0
  30. data/stylesheets/public/string/_string.scss +12 -0
  31. metadata +28 -19
  32. data/stylesheets/types/bool/_bool.scss +0 -12
  33. data/stylesheets/types/color/helpers/_get-color-value.scss +0 -18
  34. data/stylesheets/types/color/helpers/_hex-to-dec.scss +0 -20
  35. data/stylesheets/types/map/_map.scss +0 -17
  36. data/stylesheets/types/null/_null.scss +0 -9
  37. data/stylesheets/types/number/helpers/_length.scss +0 -19
  38. data/stylesheets/types/number/helpers/_pow.scss +0 -22
  39. data/stylesheets/types/string/_string.scss +0 -12
@@ -1,40 +1,42 @@
1
- // Cast a string into a hexadecimal color
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $string: string
4
- // --------------------------------------------------------------------------------
5
- // @return [color|string] string or hex color depending on the match
6
-
7
- @function _from-hex($string) {
8
- $string-lower: to-lower-case($string);
9
- $r: ""; $g: ""; $b: "";
10
- $hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
11
- $length: str-length($string);
12
- $max: if($length == 4, 1, 2);
13
-
14
- // Check for length accuracy
15
- @if $length != 4 and $length != 7 {
16
- @return $string;
17
- }
18
-
19
- // Loop from the second character (omitting #)
20
- @for $i from 2 through $length {
21
- $c: str-slice($string-lower, $i, $i);
22
-
23
- // If wrong character, return
24
- @if not index($hex, $c) {
25
- @return $string;
26
- }
27
-
28
- @if str-length($r) < $max { $r: $r + $c }
29
- @else if str-length($g) < $max { $g: $g + $c }
30
- @else if str-length($b) < $max { $b: $b + $c }
31
- }
32
-
33
- @if $length == 4 {
34
- $r: $r + $r;
35
- $g: $g + $g;
36
- $b: $b + $b;
37
- }
38
-
39
- @return rgb(_hex-to-dec($r), _hex-to-dec($g), _hex-to-dec($b));
1
+ /**
2
+ * Cast a string into a hexadecimal color
3
+ * ---
4
+ * @param {string} $string - string
5
+ * ---
6
+ * @return {color|string} - string or hex color depending on the match
7
+ */
8
+
9
+ @function _sc-from-hex($string) {
10
+ $string-lower: to-lower-case($string);
11
+ $r: ""; $g: ""; $b: "";
12
+ $hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
13
+ $length: str-length($string);
14
+ $max: if($length == 4, 1, 2);
15
+
16
+ // Check for length accuracy
17
+ @if $length != 4 and $length != 7 {
18
+ @return $string;
19
+ }
20
+
21
+ // Loop from the second character (omitting #)
22
+ @for $i from 2 through $length {
23
+ $c: str-slice($string-lower, $i, $i);
24
+
25
+ // If wrong character, return
26
+ @if not index($hex, $c) {
27
+ @return $string;
28
+ }
29
+
30
+ @if str-length($r) < $max { $r: $r + $c }
31
+ @else if str-length($g) < $max { $g: $g + $c }
32
+ @else if str-length($b) < $max { $b: $b + $c }
33
+ }
34
+
35
+ @if $length == 4 {
36
+ $r: $r + $r;
37
+ $g: $g + $g;
38
+ $b: $b + $b;
39
+ }
40
+
41
+ @return rgb(_hex-to-dec($r), _hex-to-dec($g), _hex-to-dec($b));
40
42
  }
@@ -1,47 +1,49 @@
1
- // Cast a string into a hsl color
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $string: string
4
- // --------------------------------------------------------------------------------
5
- // @return [color|string] string or hsl color depending on the match
6
-
7
- @function _from-hsl($string) {
8
- $frags: ();
9
- $string-lower: to-lower-case($string);
10
- $is-alpha: str-slice($string-lower, 4, 4) == 'a';
11
- $length: str-length($string);
12
- $start: str-index($string, "(");
13
-
14
- @for $i from $start through $length {
15
- $token: str-slice($string-lower, $i, $i);
16
- @if $token == ' ' {
17
- // @continue;
18
- }
19
- @else if $token == '(' or $token == ',' {
20
- $frags: append($frags, "");
21
- }
22
- @else if $token == ')' {
23
- @if length($frags) != if($is-alpha, 4, 3) { @return $string; } // Parsing error
24
- $hue: _get-color-value(nth($frags, 1));
25
- $saturation: _get-color-value(nth($frags, 2));
26
- $lightness: _get-color-value(nth($frags, 3));
27
-
28
- @if not $hue or not $saturation or not $lightness {
29
- @return $string;
30
- }
31
-
32
- @if $is-alpha {
33
- @if length($frags) != 4 { @return $string; } // No alpha channel found
34
- $alpha: _get-color-value(nth($frags, 4));
35
- @if not $alpha { @return $string; } // Error parsing alpha channel
36
- @return hsla($hue, $saturation, $lightness, $alpha);
37
- }
38
-
39
- @return hsl($hue, $saturation, $lightness);
40
- }
41
- @else {
42
- $frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
43
- }
44
- }
45
-
46
- @return $string;
1
+ /**
2
+ * Cast a string into a hsl color
3
+ * ---
4
+ * @param {string} $string - string
5
+ * ---
6
+ * @return {color|string} - string or hsl color depending on the match
7
+ */
8
+
9
+ @function _sc-from-hsl($string) {
10
+ $frags: ();
11
+ $string-lower: to-lower-case($string);
12
+ $is-alpha: str-slice($string-lower, 4, 4) == 'a';
13
+ $length: str-length($string);
14
+ $start: str-index($string, "(");
15
+
16
+ @for $i from $start through $length {
17
+ $token: str-slice($string-lower, $i, $i);
18
+ @if $token == ' ' {
19
+ // @continue;
20
+ }
21
+ @else if $token == '(' or $token == ',' {
22
+ $frags: append($frags, "");
23
+ }
24
+ @else if $token == ')' {
25
+ @if length($frags) != if($is-alpha, 4, 3) { @return $string; } // Parsing error
26
+ $hue: _get-color-value(nth($frags, 1));
27
+ $saturation: _get-color-value(nth($frags, 2));
28
+ $lightness: _get-color-value(nth($frags, 3));
29
+
30
+ @if not $hue or not $saturation or not $lightness {
31
+ @return $string;
32
+ }
33
+
34
+ @if $is-alpha {
35
+ @if length($frags) != 4 { @return $string; } // No alpha channel found
36
+ $alpha: _get-color-value(nth($frags, 4));
37
+ @if not $alpha { @return $string; } // Error parsing alpha channel
38
+ @return hsla($hue, $saturation, $lightness, $alpha);
39
+ }
40
+
41
+ @return hsl($hue, $saturation, $lightness);
42
+ }
43
+ @else {
44
+ $frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
45
+ }
46
+ }
47
+
48
+ @return $string;
47
49
  }
@@ -1,47 +1,49 @@
1
- // Cast a string into a rgb color
2
- // --------------------------------------------------------------------------------
3
- // @param [string] $string: string
4
- // --------------------------------------------------------------------------------
5
- // @return [color|string] string or rgb color depending on the match
6
-
7
- @function _from-rgb($string) {
8
- $string-lower: to-lower-case($string);
9
- $frags: ();
10
- $is-alpha: str-slice($string-lower, 4, 4) == 'a';
11
- $start: str-index($string, "(");
12
- $length: str-length($string);
13
-
14
- @for $i from $start through $length {
15
- $token: str-slice($string-lower, $i, $i);
16
- @if $token == ' ' {
17
- // @continue;
18
- }
19
- @else if $token == '(' or $token == ',' {
20
- $frags: append($frags, "");
21
- }
22
- @else if $token == ')' {
23
- @if length($frags) != if($is-alpha, 4, 3) { @return $string; } // Parsing error
24
- $red: _get-color-value(nth($frags, 1));
25
- $green: _get-color-value(nth($frags, 2));
26
- $blue: _get-color-value(nth($frags, 3));
27
-
28
- @if not $red or not $green or not $blue {
29
- @return $string;
30
- }
31
-
32
- @if $is-alpha {
33
- @if length($frags) != 4 { @return $string; } // No alpha channel found
34
- $alpha: _get-color-value(nth($frags, 4));
35
- @if not $alpha { @return $string; } // Error parsing alpha channel
36
- @return rgba($red, $green, $blue, $alpha);
37
- }
38
-
39
- @return rgb($red, $green, $blue);
40
- }
41
- @else {
42
- $frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
43
- }
44
- }
45
-
46
- @return $string;
47
- }
1
+ /**
2
+ * Cast a string into a rgb color
3
+ * ---
4
+ * @param {string} $string - string
5
+ * ---
6
+ * @return {color|string} - string or rgb color depending on the match
7
+ */
8
+
9
+ @function _sc-from-rgb($string) {
10
+ $string-lower: to-lower-case($string);
11
+ $frags: ();
12
+ $is-alpha: str-slice($string-lower, 4, 4) == 'a';
13
+ $start: str-index($string, "(");
14
+ $length: str-length($string);
15
+
16
+ @for $i from $start through $length {
17
+ $token: str-slice($string-lower, $i, $i);
18
+ @if $token == ' ' {
19
+ // @continue;
20
+ }
21
+ @else if $token == '(' or $token == ',' {
22
+ $frags: append($frags, "");
23
+ }
24
+ @else if $token == ')' {
25
+ @if length($frags) != if($is-alpha, 4, 3) { @return $string; } // Parsing error
26
+ $red: _get-color-value(nth($frags, 1));
27
+ $green: _get-color-value(nth($frags, 2));
28
+ $blue: _get-color-value(nth($frags, 3));
29
+
30
+ @if not $red or not $green or not $blue {
31
+ @return $string;
32
+ }
33
+
34
+ @if $is-alpha {
35
+ @if length($frags) != 4 { @return $string; } // No alpha channel found
36
+ $alpha: _get-color-value(nth($frags, 4));
37
+ @if not $alpha { @return $string; } // Error parsing alpha channel
38
+ @return rgba($red, $green, $blue, $alpha);
39
+ }
40
+
41
+ @return rgb($red, $green, $blue);
42
+ }
43
+ @else {
44
+ $frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
45
+ }
46
+ }
47
+
48
+ @return $string;
49
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Cast a stringified number / stringified percentage into number type
3
+ * ---
4
+ * @param {string} $string: string
5
+ * ---
6
+ * @return {number} - unitless number or percentage
7
+ */
8
+
9
+ @function _sc-get-color-value($string) {
10
+ $first: str-slice($string, 1, 1);
11
+
12
+ // Pad <1 values with a leading 0
13
+ @if $first == '.' {
14
+ $string: '0' + $string;
15
+ }
16
+
17
+ $last: str-slice($string, -1, -1);
18
+
19
+ @return if($last == '%', to-number(str-slice($string, 1, -2), 2) * 1%, to-number($string));
20
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Convert an hexadecimal number to a decimal number
3
+ * ---
4
+ * @param {string} $string - hexadecimal value
5
+ * ---
6
+ * @return {number} - decimal number
7
+ */
8
+
9
+ @function _sc-hex-to-dec($string){
10
+ $hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
11
+ $string: to-lower-case($string);
12
+ $length: str-length($string);
13
+
14
+ $dec: 0;
15
+ @for $i from 1 through $length {
16
+ $factor: 1 + ( 15 * ( $length - $i ));
17
+ $index: index($hex, str-slice($string, $i, $i));
18
+ $dec: $dec + $factor * ($index - 1);
19
+ }
20
+
21
+ @return $dec;
22
+ }
@@ -1,30 +1,34 @@
1
- // Convert to list
2
- // --------------------------------------------------------------------------------
3
- // @param $value: value to cast
4
- // --------------------------------------------------------------------------------
5
- // @return [list]
6
-
7
- @function to-list($value, $keep: 'both') {
8
- $keep: if(index('keys' 'values' 'both', $keep), $keep, 'both');
9
-
10
- @if type-of($value) == map {
11
- $keys: ();
12
- $values: ();
13
- @each $key, $val in $value {
14
- $keys: append($keys, $key);
15
- $values: append($values, $val);
16
- }
17
-
18
- @if $keep == 'keys' {
19
- @return $keys;
20
- }
21
- @else if $keep == 'values' {
22
- @return $values;
23
- }
24
- @else {
25
- @return zip($keys, $values);
26
- }
27
- }
28
-
29
- @return if(type-of($value) != list, ($value,), $value);
1
+ /**
2
+ * Convert to list
3
+ * ---
4
+ * @access private
5
+ * ---
6
+ * @param {*} $value - value to cast
7
+ * ---
8
+ * @return {list}
9
+ */
10
+
11
+ @function _sc-to-list($value, $keep: 'both') {
12
+ $keep: if(index('keys' 'values' 'both', $keep), $keep, 'both');
13
+
14
+ @if type-of($value) == map {
15
+ $keys: ();
16
+ $values: ();
17
+ @each $key, $val in $value {
18
+ $keys: append($keys, $key);
19
+ $values: append($values, $val);
20
+ }
21
+
22
+ @if $keep == 'keys' {
23
+ @return $keys;
24
+ }
25
+ @else if $keep == 'values' {
26
+ @return $values;
27
+ }
28
+ @else {
29
+ @return zip($keys, $values);
30
+ }
31
+ }
32
+
33
+ @return if(type-of($value) != list, ($value,), $value);
30
34
  }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Convert to map
3
+ * ---
4
+ * @access private
5
+ * ---
6
+ * @param {*} $value - value to cast
7
+ * ---
8
+ * @return {map}
9
+ */
10
+
11
+ @function _sc-to-map($value) {
12
+ @if type-of($value) == list {
13
+ $map: ();
14
+ @if length($value) > 1 {
15
+ @for $i from 1 through length($value) {
16
+ $map: map-merge($map, ($i: nth($value, $i)));
17
+ }
18
+ }
19
+ @return $map;
20
+ }
21
+
22
+ @return if(type-of($value) != map, (1: $value), $value);
23
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Convert to null
3
+ * ---
4
+ * @access private
5
+ * ---
6
+ * @param {*} $value - value to cast
7
+ * ---
8
+ * @return null
9
+ */
10
+
11
+ @function _sc-to-null($value) {
12
+ @return null;
13
+ }
@@ -1,67 +1,64 @@
1
- // Helpers
2
-
3
- @import "helpers/find-integer";
4
- @import "helpers/find-digits";
5
- @import "helpers/pow";
6
- @import "helpers/length";
7
-
8
- // Cast a value to a number if possible or return 0
9
- // --------------------------------------------------------------------------------
10
- // @param [string] $value: complete source
11
- // --------------------------------------------------------------------------------
12
- // @return [number] number (0 if cast failed)
13
-
14
- @function to-number($value) {
15
- // In case everything's good
16
- @if type-of($value) == number {
17
- @return $value;
18
- }
19
-
20
- // Boolean to number
21
- @if $value == true {
22
- @return 1;
23
- }
24
-
25
- // Fail
26
- @if type-of($value) != string {
27
- @return 0;
28
- }
29
-
30
- // Trying to cast
31
- $pointer: 1;
32
- $result: 0;
33
- $allowed: '-' '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'; // Allowed characted to start with
34
- $first: str-slice($value, $pointer, $pointer); // First character of the number
35
- $minus: $first == '-'; // Is it negative?
36
-
37
- // Early check for errors
38
- @if not index($allowed, $first) {
39
- @warn "Could not cast `#{inspect($value)} into number.";
40
- @return 0;
41
- }
42
-
43
- // Find the integer part
44
- $find-integer: _find-integer($value, $pointer);
45
- $pointer: nth($find-integer, 1);
46
- $result: nth($find-integer, 2);
47
-
48
- // Find digits
49
- @if str-slice($value, $pointer, $pointer) == '.' {
50
- $find-digits: _find-digits($value, $pointer);
51
- $pointer: nth($find-digits, 1);
52
- $digits: nth($find-digits, 2);
53
- $result: $result + $digits;
54
- }
55
-
56
- // In case of negative
57
- @if $minus {
58
- $result: $result * -1;
59
- }
60
-
61
- // In case of possible CSS unit
62
- @if $pointer < str-length($value) {
63
- $result: _length($result, str-slice($value, $pointer));
64
- }
65
-
66
- @return $result;
1
+ /*
2
+ * Cast a value to a number if possible or return 0
3
+ * ---
4
+ * @access private
5
+ * ---
6
+ * @param {string} $value - complete source
7
+ * ---
8
+ * @return {number}
9
+ */
10
+
11
+ @function _sc-to-number($value) {
12
+ // In case everything's good
13
+ @if type-of($value) == number {
14
+ @return $value;
15
+ }
16
+
17
+ // Boolean to number
18
+ @if $value == true {
19
+ @return 1;
20
+ }
21
+
22
+ // Fail
23
+ @if type-of($value) != string {
24
+ @return 0;
25
+ }
26
+
27
+ // Trying to cast
28
+ $pointer: 1;
29
+ $result: 0;
30
+ $allowed: '-' '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'; // Allowed characted to start with
31
+ $first: str-slice($value, $pointer, $pointer); // First character of the number
32
+ $minus: $first == '-'; // Is it negative?
33
+
34
+ // Early check for errors
35
+ @if not index($allowed, $first) {
36
+ @warn "Could not cast `#{inspect($value)} into number.";
37
+ @return 0;
38
+ }
39
+
40
+ // Find the integer part
41
+ $find-integer: _sc-find-integer($value, $pointer);
42
+ $pointer: nth($find-integer, 1);
43
+ $result: nth($find-integer, 2);
44
+
45
+ // Find digits
46
+ @if str-slice($value, $pointer, $pointer) == '.' {
47
+ $find-digits: _sc-find-digits($value, $pointer);
48
+ $pointer: nth($find-digits, 1);
49
+ $digits: nth($find-digits, 2);
50
+ $result: $result + $digits;
51
+ }
52
+
53
+ // In case of negative
54
+ @if $minus {
55
+ $result: $result * -1;
56
+ }
57
+
58
+ // In case of possible CSS unit
59
+ @if $pointer < str-length($value) {
60
+ $result: _sc-length($result, str-slice($value, $pointer));
61
+ }
62
+
63
+ @return $result;
67
64
  }