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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -4
- data/README.md +123 -119
- data/lib/SassyCast.rb +14 -14
- data/stylesheets/SassyCast.scss +2 -7
- data/stylesheets/private/_all.scss +16 -0
- data/stylesheets/private/bool/_bool.scss +16 -0
- data/stylesheets/{types → private}/color/_color.scss +59 -63
- data/stylesheets/{types → private}/color/helpers/_from-hex.scss +41 -39
- data/stylesheets/{types → private}/color/helpers/_from-hsl.scss +48 -46
- data/stylesheets/{types → private}/color/helpers/_from-rgb.scss +49 -47
- data/stylesheets/private/color/helpers/_get-color-value.scss +20 -0
- data/stylesheets/private/color/helpers/_hex-to-dec.scss +22 -0
- data/stylesheets/{types → private}/list/_list.scss +33 -29
- data/stylesheets/private/map/_map.scss +23 -0
- data/stylesheets/private/null/_null.scss +13 -0
- data/stylesheets/{types → private}/number/_number.scss +63 -66
- data/stylesheets/{types → private}/number/helpers/_find-digits.scss +35 -33
- data/stylesheets/{types → private}/number/helpers/_find-integer.scss +35 -33
- data/stylesheets/private/number/helpers/_length.scss +21 -0
- data/stylesheets/private/number/helpers/_pow.scss +24 -0
- data/stylesheets/private/string/_string.scss +15 -0
- data/stylesheets/public/_all.scss +7 -0
- data/stylesheets/public/bool/_bool.scss +13 -0
- data/stylesheets/public/color/_color.scss +13 -0
- data/stylesheets/public/list/_list.scss +13 -0
- data/stylesheets/public/map/_map.scss +13 -0
- data/stylesheets/public/null/_null.scss +13 -0
- data/stylesheets/public/number/_number.scss +13 -0
- data/stylesheets/public/string/_string.scss +12 -0
- metadata +28 -19
- data/stylesheets/types/bool/_bool.scss +0 -12
- data/stylesheets/types/color/helpers/_get-color-value.scss +0 -18
- data/stylesheets/types/color/helpers/_hex-to-dec.scss +0 -20
- data/stylesheets/types/map/_map.scss +0 -17
- data/stylesheets/types/null/_null.scss +0 -9
- data/stylesheets/types/number/helpers/_length.scss +0 -19
- data/stylesheets/types/number/helpers/_pow.scss +0 -22
- data/stylesheets/types/string/_string.scss +0 -12
@@ -1,40 +1,42 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
$
|
11
|
-
$
|
12
|
-
$
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
@
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
$
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
$
|
11
|
-
$
|
12
|
-
$
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
$
|
26
|
-
$
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
@if
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
$
|
11
|
-
$
|
12
|
-
$
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
$
|
26
|
-
$
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
@if
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
}
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
+
}
|
@@ -1,67 +1,64 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
@
|
5
|
-
|
6
|
-
@
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
//
|
13
|
-
|
14
|
-
@
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
//
|
31
|
-
$
|
32
|
-
$
|
33
|
-
|
34
|
-
|
35
|
-
$
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
$
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
}
|