SassyCast 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f42a0fe044be49a40d427fe6af9cdc0fa60639a
4
- data.tar.gz: 8a4c35110c665195d42761ae5a9aa389ea86bb5f
3
+ metadata.gz: 1a2f1dc189fde28c3eab8bdc0d8437f41f2bde33
4
+ data.tar.gz: 48f936048f7a036c01ede59ed44347f2f5410dfb
5
5
  SHA512:
6
- metadata.gz: bf461c41c1d0a48c9496a755996edff6ced8011b14541e7708968a46027cbcaf2dab57d15fe1d9971948bcd3d4818bc1f65184f46267b09c2b5bc88cec3cb6ea
7
- data.tar.gz: e16b8b9828b79152b02b54f697e51a39b9ccaed02fd0526023c8de3646a84aff6528e78cf876e38337f44524c4f9aa0cf4fbe4eefc798aca5af19df0cc7e57b6
6
+ metadata.gz: f6f42c4059bd809b9ac44695b689695cf2ad22e1e61825f84990b56d6dd329d0752159fdb3de8df685942a586eb4ae7ac005a3bba079222f3c0a53074a37be85
7
+ data.tar.gz: 51d15bcb539e4796ebca7d86237471acc2f0085fb78f66db35f89d7fb2f9d51e82de138a02f14ddb7495bc03e0c93a5de55cb3057069fea26725434a9ff3e7dd
@@ -1,4 +1,5 @@
1
- # Changelog
2
-
3
- * `1.0.0`: `to-list` improvements, stable API
4
- * `0.0.1`: initial commit
1
+ # Changelog
2
+
3
+ * `1.1.0`: dissociated error checking from function logic, and fixed some minor things
4
+ * `1.0.0`: `to-list` improvements, stable API
5
+ * `0.0.1`: initial commit
data/README.md CHANGED
@@ -1,119 +1,123 @@
1
- SassyCast
2
- =========
3
-
4
- SassyCast is a simple API for type conversion in Sass.
5
-
6
- ## `to-bool`
7
-
8
- ``` scss
9
- to-bool(false); // false
10
- to-bool(0); // false
11
- to-bool(null); // false
12
- to-bool(""); // false
13
- to-bool(true); // true
14
- to-bool(1); // true
15
- to-bool(()); // true
16
- to-bool(string); // true
17
- to-bool("quoted string"); // true
18
- to-bool(this is a list); // true
19
- to-bool((this: is a map)); // true
20
- to-bool(1337); // true
21
- to-bool(#000); // true
22
- ```
23
-
24
- ## `to-number`
25
-
26
- ``` scss
27
- to-number(1337); // 1337
28
- to-number("0"); // 0
29
- to-number("42"); // 42
30
- to-number("4.2"); // 4.2
31
- to-number("-10"); // -10
32
- to-number("-10px"); // -10px
33
- to-number(true); // 1
34
- to-number(false); // 0
35
- to-number(string); // 0
36
- to-number(this is a list); // 0
37
- to-number((this: is a map)); // 0
38
- to-number(null); // 0
39
- to-number(#000); // 0
40
- ```
41
-
42
- ## `to-list`
43
-
44
- ``` scss
45
- to-list(0); // 0
46
- to-list(1); // 1
47
- to-list(string); // string
48
- to-list(this is a list); // this is a list
49
- to-list(this: is a map); // this, is a map
50
- to-list(true); // true
51
- to-list(false); // false
52
- to-list(null); // null
53
- to-list(#000); // #333
54
- ```
55
-
56
- ## `to-string`
57
-
58
- ``` scss
59
- to-string(0); // "0"
60
- to-string(1); // "1"
61
- to-string(true); // "true"
62
- to-string(false); // "false"
63
- to-string(null); // "null"
64
- to-string(this is a list); // "this is a list"
65
- to-string(this, is, a, list); // "this, is, a, list"
66
- to-string((this: is a map)); // "(this: is a map)"
67
- to-string((this: is, a, map)); // "(this: is, a, map)"
68
- to-string(#000); // "#333"
69
- ```
70
-
71
- ## `to-null`
72
-
73
- ``` scss
74
- to-null(null); // null
75
- to-null(0); // null
76
- to-null(1); // null
77
- to-null(string); // null
78
- to-null(this is a list); // null
79
- to-null(this: is a map); // null
80
- to-null(true); // null
81
- to-null(false); // null
82
- to-null(#333); // null
83
- ```
84
-
85
- ## `to-map`
86
-
87
- ``` scss
88
- to-map(0); // (1: 0)
89
- to-map(1); // (1: 1)
90
- to-map(true); // (1: true)
91
- to-map(false); // (1: false)
92
- to-map(null); // (1: null)
93
- to-map(string); // (1: string)
94
- to-map(this is a list); // (1: this, 2: is, 3: a, 4: map)
95
- to-map(this: is a map); // (this: is a map)
96
- ```
97
-
98
- ## `to-color`
99
-
100
- ``` scss
101
- to-color(0); // null
102
- to-color(1); // null
103
- to-color(black); // black
104
- to-color(#000); // black
105
- to-color("#000"); // black
106
- to-color(true); // null
107
- to-color(false); // null
108
- to-color(null); // null
109
- to-color(string); // null
110
- to-color(this is a list); // null
111
- to-color((this: is a map)); // null
112
- ```
113
-
114
- ## A few things to note:
115
-
116
- * When trying to cast something to a number that cannot be converted, it returns `0`
117
- * When trying to cast something to a color that cannot be converted, it returns `null`
118
- * Casting to a map use `1` as key (and index in case of a list)
119
- * Color formats are converted automatically by Sass; when casting a color to string, the resulting string can be different from the color input
1
+ SassyCast
2
+ =========
3
+
4
+ SassyCast is a simple API for type conversion in Sass.
5
+
6
+ ## `to-bool`
7
+
8
+ ``` scss
9
+ to-bool(false); // false
10
+ to-bool(0); // false
11
+ to-bool(null); // false
12
+ to-bool(""); // false
13
+ to-bool(true); // true
14
+ to-bool(1); // true
15
+ to-bool(()); // true
16
+ to-bool(string); // true
17
+ to-bool("quoted string"); // true
18
+ to-bool(this is a list); // true
19
+ to-bool((this: is a map)); // true
20
+ to-bool(1337); // true
21
+ to-bool(#000); // true
22
+ ```
23
+
24
+ ## `to-number`
25
+
26
+ ``` scss
27
+ to-number(1337); // 1337
28
+ to-number("0"); // 0
29
+ to-number("42"); // 42
30
+ to-number("4.2"); // 4.2
31
+ to-number("-10"); // -10
32
+ to-number("-10px"); // -10px
33
+ to-number(true); // 1
34
+ to-number(false); // 0
35
+ to-number(string); // 0
36
+ to-number(this is a list); // 0
37
+ to-number((this: is a map)); // 0
38
+ to-number(null); // 0
39
+ to-number(#000); // 0
40
+ ```
41
+
42
+ ## `to-list`
43
+
44
+ ``` scss
45
+ to-list(0); // 0
46
+ to-list(1); // 1
47
+ to-list(string); // string
48
+ to-list(this is a list); // this is a list
49
+ to-list(this: is a map); // this, is a map
50
+ to-list(true); // true
51
+ to-list(false); // false
52
+ to-list(null); // null
53
+ to-list(#000); // #333
54
+ ```
55
+
56
+ ## `to-string`
57
+
58
+ ``` scss
59
+ to-string(0); // "0"
60
+ to-string(1); // "1"
61
+ to-string(true); // "true"
62
+ to-string(false); // "false"
63
+ to-string(null); // "null"
64
+ to-string(this is a list); // "this is a list"
65
+ to-string(this, is, a, list); // "this, is, a, list"
66
+ to-string((this: is a map)); // "(this: is a map)"
67
+ to-string((this: is, a, map)); // "(this: is, a, map)"
68
+ to-string(#000); // "#333"
69
+ ```
70
+
71
+ ## `to-null`
72
+
73
+ ``` scss
74
+ to-null(null); // null
75
+ to-null(0); // null
76
+ to-null(1); // null
77
+ to-null(string); // null
78
+ to-null(this is a list); // null
79
+ to-null(this: is a map); // null
80
+ to-null(true); // null
81
+ to-null(false); // null
82
+ to-null(#333); // null
83
+ ```
84
+
85
+ ## `to-map`
86
+
87
+ ``` scss
88
+ to-map(0); // (1: 0)
89
+ to-map(1); // (1: 1)
90
+ to-map(true); // (1: true)
91
+ to-map(false); // (1: false)
92
+ to-map(null); // (1: null)
93
+ to-map(string); // (1: string)
94
+ to-map(this is a list); // (1: this, 2: is, 3: a, 4: map)
95
+ to-map(this: is a map); // (this: is a map)
96
+ ```
97
+
98
+ ## `to-color`
99
+
100
+ ``` scss
101
+ to-color(0); // null
102
+ to-color(1); // null
103
+ to-color(black); // black
104
+ to-color(#000); // black
105
+ to-color("#000"); // black
106
+ to-color(true); // null
107
+ to-color(false); // null
108
+ to-color(null); // null
109
+ to-color(string); // null
110
+ to-color(this is a list); // null
111
+ to-color((this: is a map)); // null
112
+ ```
113
+
114
+ ## A few things to note:
115
+
116
+ * When trying to cast something to a number that cannot be converted, it returns `0`
117
+ * When trying to cast something to a color that cannot be converted, it returns `null`
118
+ * Casting to a map use `1` as key (and index in case of a list)
119
+ * Color formats are sometimes converted automatically by Sass; when casting a color to string, the resulting string can be different from the color input
120
+
121
+ ## Credits
122
+
123
+ Huge thanks to [Marc Mintel](http://twitter.com/marcmintel) for his help.
@@ -1,14 +1,14 @@
1
- require 'compass'
2
- extension_path = File.expand_path(File.join(File.dirname(__FILE__), ".."))
3
- Compass::Frameworks.register('SassyCast', :path => extension_path)
4
-
5
- # Version is a number. If a version contains alphas, it will be created as a prerelease version
6
- # Date is in the form of YYYY-MM-DD
7
- module SassyCast
8
- VERSION = "1.0.0"
9
- DATE = "2014-01-31"
10
- end
11
-
12
- module Sass::Script::Functions
13
-
14
- end
1
+ require 'compass'
2
+ extension_path = File.expand_path(File.join(File.dirname(__FILE__), ".."))
3
+ Compass::Frameworks.register('SassyCast', :path => extension_path)
4
+
5
+ # Version is a number. If a version contains alphas, it will be created as a prerelease version
6
+ # Date is in the form of YYYY-MM-DD
7
+ module SassyCast
8
+ VERSION = "1.1.0"
9
+ DATE = "2014-10-03"
10
+ end
11
+
12
+ module Sass::Script::Functions
13
+
14
+ end
@@ -1,7 +1,2 @@
1
- @import "types/bool/bool";
2
- @import "types/number/number";
3
- @import "types/string/string";
4
- @import "types/list/list";
5
- @import "types/map/map";
6
- @import "types/null/null";
7
- @import "types/color/color";
1
+ @import "private/all";
2
+ @import "public/all";
@@ -0,0 +1,16 @@
1
+ @import "bool/bool";
2
+ @import "number/helpers/find-integer";
3
+ @import "number/helpers/find-digits";
4
+ @import "number/helpers/pow";
5
+ @import "number/helpers/length";
6
+ @import "number/number";
7
+ @import "string/string";
8
+ @import "list/list";
9
+ @import "map/map";
10
+ @import "null/null";
11
+ @import "color/helpers/from-rgb";
12
+ @import "color/helpers/from-hsl";
13
+ @import "color/helpers/from-hex";
14
+ @import "color/helpers/get-color-value";
15
+ @import "color/helpers/hex-to-dec";
16
+ @import "color/color";
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Convert to bool
3
+ * ---
4
+ * @access private
5
+ * ---
6
+ * @param {*} $value - value to cast
7
+ * ---
8
+ * @return {bool}
9
+ */
10
+
11
+ @function _sc-to-bool($value) {
12
+ @if not $value or $value == "" or $value == 0 {
13
+ @return false;
14
+ }
15
+ @return true;
16
+ }
@@ -1,64 +1,60 @@
1
- // Helpers
2
-
3
- @import "helpers/from-rgb";
4
- @import "helpers/from-hsl";
5
- @import "helpers/from-hex";
6
- @import "helpers/get-color-value";
7
- @import "helpers/hex-to-dec";
8
-
9
- // Convert to color
10
- // --------------------------------------------------------------------------------
11
- // @param $value: value to cast
12
- // --------------------------------------------------------------------------------
13
- // @return [color] | null
14
-
15
- @function to-color($value) {
16
- @if type-of($value) == color {
17
- @return $value;
18
- }
19
-
20
- @if type-of($value) != string {
21
- //@warn "Could not cast `#{inspect($value)}` to color.";
22
- @return null;
23
- }
24
-
25
- $value-lower: to-lower-case($value);
26
- $colors: transparent black silver gray white maroon red purple fuchsia green lime olive yellow navy blue teal aqua aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen;
27
- $keywords: ();
28
-
29
- // Filling $keywords with stringified color keywords
30
- @each $color in $colors {
31
- $keywords: append($keywords, $color + "");
32
- }
33
-
34
- // Deal with inherit keyword
35
- @if $value-lower == "inherit" {
36
- @return unquote($value);
37
- }
38
-
39
- // Deal with color keywords
40
- @if index($keywords, $value-lower) {
41
- @return nth($colors, index($keywords, $value-lower));
42
- }
43
-
44
- // Deal with hexadecimal triplets
45
- @else if str-slice($value-lower, 1, 1) == '#' {
46
- @return _from-hex($value);
47
- }
48
-
49
- // Deal with rgb(a) colors
50
- @else if str-slice($value-lower, 1, 3) == 'rgb' {
51
- @return _from-rgb($value);
52
- }
53
-
54
- // Deal with hsl(a) colors
55
- @else if str-slice($value-lower, 1, 3) == 'hsl' {
56
- @return _from-hsl($value);
57
- }
58
-
59
- // Return value
60
- @else {
61
- //@warn "Could not cast `#{inspect($value)}` to color.";
62
- @return null;
63
- }
1
+ /**
2
+ * Convert to color
3
+ * ---
4
+ * @access private
5
+ * ---
6
+ * @param {*} $value - value to cast
7
+ * ---
8
+ * @return {color | null}
9
+ */
10
+
11
+ @function _sc-to-color($value) {
12
+ @if type-of($value) == color {
13
+ @return $value;
14
+ }
15
+
16
+ @if type-of($value) != string {
17
+ //@warn "Could not cast `#{inspect($value)}` to color.";
18
+ @return null;
19
+ }
20
+
21
+ $value-lower: to-lower-case($value);
22
+ $colors: transparent black silver gray white maroon red purple fuchsia green lime olive yellow navy blue teal aqua aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen;
23
+ $keywords: ();
24
+
25
+ // Filling $keywords with stringified color keywords
26
+ @each $color in $colors {
27
+ $keywords: append($keywords, $color + "");
28
+ }
29
+
30
+ // Deal with inherit keyword
31
+ @if $value-lower == "inherit" {
32
+ @return unquote($value);
33
+ }
34
+
35
+ // Deal with color keywords
36
+ @if index($keywords, $value-lower) {
37
+ @return nth($colors, index($keywords, $value-lower));
38
+ }
39
+
40
+ // Deal with hexadecimal triplets
41
+ @else if str-slice($value-lower, 1, 1) == '#' {
42
+ @return _sc-from-hex($value);
43
+ }
44
+
45
+ // Deal with rgb(a) colors
46
+ @else if str-slice($value-lower, 1, 3) == 'rgb' {
47
+ @return _sc-from-rgb($value);
48
+ }
49
+
50
+ // Deal with hsl(a) colors
51
+ @else if str-slice($value-lower, 1, 3) == 'hsl' {
52
+ @return _sc-from-hsl($value);
53
+ }
54
+
55
+ // Return value
56
+ @else {
57
+ //@warn "Could not cast `#{inspect($value)}` to color.";
58
+ @return null;
59
+ }
64
60
  }