SassyCast 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/README.md +119 -0
- data/lib/SassyCast.rb +14 -0
- data/stylesheets/SassyCast.scss +7 -0
- data/stylesheets/types/bool/_bool.scss +12 -0
- data/stylesheets/types/color/_color.scss +62 -0
- data/stylesheets/types/color/helpers/_from-hex.scss +34 -0
- data/stylesheets/types/color/helpers/_from-hsl.scss +41 -0
- data/stylesheets/types/color/helpers/_from-rgb.scss +41 -0
- data/stylesheets/types/color/helpers/_get-color-value.scss +12 -0
- data/stylesheets/types/color/helpers/_hex-to-dec.scss +14 -0
- data/stylesheets/types/list/_list.scss +19 -0
- data/stylesheets/types/map/_map.scss +17 -0
- data/stylesheets/types/null/_null.scss +9 -0
- data/stylesheets/types/number/_number.scss +65 -0
- data/stylesheets/types/number/helpers/_find-digits.scss +36 -0
- data/stylesheets/types/number/helpers/_find-integer.scss +35 -0
- data/stylesheets/types/number/helpers/_length.scss +12 -0
- data/stylesheets/types/number/helpers/_pow.scss +22 -0
- data/stylesheets/types/string/_string.scss +12 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4770cd04049b0257d92291e040aaebe64e28ecad
|
4
|
+
data.tar.gz: 166fce1bcd86158f1b2304e4057ac43705fbd027
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8b118cf974c1a739297ad915521d36fc182d3f139eb1a24ff32d75cd01b244f879125a7d37d270838560a44dc628556f913ded32d08b90b23e68938ba2e65eba
|
7
|
+
data.tar.gz: 7de8734df558b6079ef63f840bd9fd0c67ada77a24ace850ea47937ed1b262f670f7733c05355d308998a632ca1daf749b413154e218fa9444d3bdf345a998ad
|
data/CHANGELOG.md
ADDED
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
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
|
data/lib/SassyCast.rb
ADDED
@@ -0,0 +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 = "0.0.1"
|
9
|
+
DATE = "2014-01-27"
|
10
|
+
end
|
11
|
+
|
12
|
+
module Sass::Script::Functions
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
// Convert to bool
|
2
|
+
// --------------------------------------------------------------------------------
|
3
|
+
// @param $value: value to cast
|
4
|
+
// --------------------------------------------------------------------------------
|
5
|
+
// @return [bool]
|
6
|
+
|
7
|
+
@function to-bool($value) {
|
8
|
+
@if not $value or $value == "" or $value == 0 {
|
9
|
+
@return false;
|
10
|
+
}
|
11
|
+
@return true;
|
12
|
+
}
|
@@ -0,0 +1,62 @@
|
|
1
|
+
@import "helpers/from-rgb";
|
2
|
+
@import "helpers/from-hsl";
|
3
|
+
@import "helpers/from-hex";
|
4
|
+
@import "helpers/get-color-value";
|
5
|
+
@import "helpers/hex-to-dec";
|
6
|
+
|
7
|
+
// Convert to color
|
8
|
+
// --------------------------------------------------------------------------------
|
9
|
+
// @param $value: value to cast
|
10
|
+
// --------------------------------------------------------------------------------
|
11
|
+
// @return [color] | null
|
12
|
+
|
13
|
+
@function to-color($value) {
|
14
|
+
@if type-of($value) == color {
|
15
|
+
@return $value;
|
16
|
+
}
|
17
|
+
|
18
|
+
@if type-of($value) != string {
|
19
|
+
//@warn "Could not cast `#{inspect($value)}` to color.";
|
20
|
+
@return null;
|
21
|
+
}
|
22
|
+
|
23
|
+
$value-lower: to-lower-case($value);
|
24
|
+
$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;
|
25
|
+
$keywords: ();
|
26
|
+
|
27
|
+
// Filling $keywords with stringified color keywords
|
28
|
+
@each $color in $colors {
|
29
|
+
$keywords: append($keywords, $color + "");
|
30
|
+
}
|
31
|
+
|
32
|
+
// Deal with inherit keyword
|
33
|
+
@if $value-lower == "inherit" {
|
34
|
+
@return unquote($value);
|
35
|
+
}
|
36
|
+
|
37
|
+
// Deal with color keywords
|
38
|
+
@if index($keywords, $value-lower) {
|
39
|
+
@return nth($colors, index($keywords, $value-lower));
|
40
|
+
}
|
41
|
+
|
42
|
+
// Deal with hexadecimal triplets
|
43
|
+
@else if str-slice($value-lower, 1, 1) == '#' {
|
44
|
+
@return _from-hex($value);
|
45
|
+
}
|
46
|
+
|
47
|
+
// Deal with rgb(a) colors
|
48
|
+
@else if str-slice($value-lower, 1, 3) == 'rgb' {
|
49
|
+
@return _from-rgb($value);
|
50
|
+
}
|
51
|
+
|
52
|
+
// Deal with hsl(a) colors
|
53
|
+
@else if str-slice($value-lower, 1, 3) == 'hsl' {
|
54
|
+
@return _from-hsl($value);
|
55
|
+
}
|
56
|
+
|
57
|
+
// Return value
|
58
|
+
@else {
|
59
|
+
//@warn "Could not cast `#{inspect($value)}` to color.";
|
60
|
+
@return null;
|
61
|
+
}
|
62
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
@function _from-hex($string) {
|
2
|
+
$string-lower: to-lower-case($string);
|
3
|
+
$r: ""; $g: ""; $b: "";
|
4
|
+
$hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
|
5
|
+
$length: str-length($string);
|
6
|
+
$max: if($length == 4, 1, 2);
|
7
|
+
|
8
|
+
// Check for length accuracy
|
9
|
+
@if $length != 4 and $length != 7 {
|
10
|
+
@return $string;
|
11
|
+
}
|
12
|
+
|
13
|
+
// Loop from the second character (omitting #)
|
14
|
+
@for $i from 2 through $length {
|
15
|
+
$c: str-slice($string-lower, $i, $i);
|
16
|
+
|
17
|
+
// If wrong character, return
|
18
|
+
@if not index($hex, $c) {
|
19
|
+
@return $string;
|
20
|
+
}
|
21
|
+
|
22
|
+
@if str-length($r) < $max { $r: $r + $c }
|
23
|
+
@else if str-length($g) < $max { $g: $g + $c }
|
24
|
+
@else if str-length($b) < $max { $b: $b + $c }
|
25
|
+
}
|
26
|
+
|
27
|
+
@if $length == 4 {
|
28
|
+
$r: $r + $r;
|
29
|
+
$g: $g + $g;
|
30
|
+
$b: $b + $b;
|
31
|
+
}
|
32
|
+
|
33
|
+
@return rgb(_hex-to-dec($r), _hex-to-dec($g), _hex-to-dec($b));
|
34
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
@function _from-hsl($string) {
|
2
|
+
$frags: ();
|
3
|
+
$string-lower: to-lower-case($string);
|
4
|
+
$is-alpha: str-slice($string-lower, 4, 4) == 'a';
|
5
|
+
$length: str-length($string);
|
6
|
+
$start: str-index($string, "(");
|
7
|
+
|
8
|
+
@for $i from $start through $length {
|
9
|
+
$token: str-slice($string-lower, $i, $i);
|
10
|
+
@if $token == ' ' {
|
11
|
+
// @continue;
|
12
|
+
}
|
13
|
+
@else if $token == '(' or $token == ',' {
|
14
|
+
$frags: append($frags, "");
|
15
|
+
}
|
16
|
+
@else if $token == ')' {
|
17
|
+
@if length($frags) != if($is-alpha, 4, 3) { @return $string; } // Parsing error
|
18
|
+
$hue: _get-color-value(nth($frags, 1));
|
19
|
+
$saturation: _get-color-value(nth($frags, 2));
|
20
|
+
$lightness: _get-color-value(nth($frags, 3));
|
21
|
+
|
22
|
+
@if not $hue or not $saturation or not $lightness {
|
23
|
+
@return $string;
|
24
|
+
}
|
25
|
+
|
26
|
+
@if $is-alpha {
|
27
|
+
@if length($frags) != 4 { @return $string; } // No alpha channel found
|
28
|
+
$alpha: _get-color-value(nth($frags, 4));
|
29
|
+
@if not $alpha { @return $string; } // Error parsing alpha channel
|
30
|
+
@return hsla($hue, $saturation, $lightness, $alpha);
|
31
|
+
}
|
32
|
+
|
33
|
+
@return hsl($hue, $saturation, $lightness);
|
34
|
+
}
|
35
|
+
@else {
|
36
|
+
$frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
@return $string;
|
41
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
@function _from-rgb($string) {
|
2
|
+
$string-lower: to-lower-case($string);
|
3
|
+
$frags: ();
|
4
|
+
$is-alpha: str-slice($string-lower, 4, 4) == 'a';
|
5
|
+
$start: str-index($string, "(");
|
6
|
+
$length: str-length($string);
|
7
|
+
|
8
|
+
@for $i from $start through $length {
|
9
|
+
$token: str-slice($string-lower, $i, $i);
|
10
|
+
@if $token == ' ' {
|
11
|
+
// @continue;
|
12
|
+
}
|
13
|
+
@else if $token == '(' or $token == ',' {
|
14
|
+
$frags: append($frags, "");
|
15
|
+
}
|
16
|
+
@else if $token == ')' {
|
17
|
+
@if length($frags) != if($is-alpha, 4, 3) { @return $string; } // Parsing error
|
18
|
+
$red: _get-color-value(nth($frags, 1));
|
19
|
+
$green: _get-color-value(nth($frags, 2));
|
20
|
+
$blue: _get-color-value(nth($frags, 3));
|
21
|
+
|
22
|
+
@if not $red or not $green or not $blue {
|
23
|
+
@return $string;
|
24
|
+
}
|
25
|
+
|
26
|
+
@if $is-alpha {
|
27
|
+
@if length($frags) != 4 { @return $string; } // No alpha channel found
|
28
|
+
$alpha: _get-color-value(nth($frags, 4));
|
29
|
+
@if not $alpha { @return $string; } // Error parsing alpha channel
|
30
|
+
@return rgba($red, $green, $blue, $alpha);
|
31
|
+
}
|
32
|
+
|
33
|
+
@return rgb($red, $green, $blue);
|
34
|
+
}
|
35
|
+
@else {
|
36
|
+
$frags: set-nth($frags, length($frags), nth($frags, length($frags)) + $token);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
@return $string;
|
41
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
@function _get-color-value($string) {
|
2
|
+
$first: str-slice($string, 1, 1);
|
3
|
+
|
4
|
+
// Pad <1 values with a leading 0
|
5
|
+
@if $first == '.' {
|
6
|
+
$string: '0' + $string;
|
7
|
+
}
|
8
|
+
|
9
|
+
$last: str-slice($string, -1, -1);
|
10
|
+
|
11
|
+
@return if($last == '%', to-number(str-slice($string, 1, -2), 2) * 1%, to-number($string));
|
12
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
@function _hex-to-dec($string){
|
2
|
+
$hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
|
3
|
+
$string: to-lower-case($string);
|
4
|
+
$length: str-length($string);
|
5
|
+
|
6
|
+
$dec: 0;
|
7
|
+
@for $i from 1 through $length {
|
8
|
+
$factor: 1 + ( 15 * ( $length - $i ));
|
9
|
+
$index: index($hex, str-slice($string, $i, $i));
|
10
|
+
$dec: $dec + $factor * ($index - 1);
|
11
|
+
}
|
12
|
+
|
13
|
+
@return $dec;
|
14
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
// Convert to list
|
2
|
+
// --------------------------------------------------------------------------------
|
3
|
+
// @param $value: value to cast
|
4
|
+
// --------------------------------------------------------------------------------
|
5
|
+
// @return [list]
|
6
|
+
|
7
|
+
@function to-list($value) {
|
8
|
+
@if type-of($value) == map {
|
9
|
+
$keys: ();
|
10
|
+
$values: ();
|
11
|
+
@each $key, $val in $value {
|
12
|
+
$keys: append($keys, $key);
|
13
|
+
$values: append($values, $val);
|
14
|
+
}
|
15
|
+
@return zip($keys, $values);
|
16
|
+
}
|
17
|
+
|
18
|
+
@return if(type-of($value) != list, ($value,), $value);
|
19
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
// Convert to map
|
2
|
+
// --------------------------------------------------------------------------------
|
3
|
+
// @param $value: value to cast
|
4
|
+
// --------------------------------------------------------------------------------
|
5
|
+
// @return [map]
|
6
|
+
|
7
|
+
@function to-map($value) {
|
8
|
+
@if type-of($value) == list {
|
9
|
+
$map: ();
|
10
|
+
@for $i from 1 through length($value) {
|
11
|
+
$map: map-merge($map, ($i: nth($value, $i)));
|
12
|
+
}
|
13
|
+
@return $map;
|
14
|
+
}
|
15
|
+
|
16
|
+
@return if(type-of($value) != map, (1: $value), $value);
|
17
|
+
}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
// Convert to null
|
2
|
+
// --------------------------------------------------------------------------------
|
3
|
+
// @param $value: value to cast
|
4
|
+
// --------------------------------------------------------------------------------
|
5
|
+
// @return null
|
6
|
+
|
7
|
+
@function to-null($value) {
|
8
|
+
@return null;
|
9
|
+
}
|
@@ -0,0 +1,65 @@
|
|
1
|
+
@import "helpers/find-integer";
|
2
|
+
@import "helpers/find-digits";
|
3
|
+
@import "helpers/pow";
|
4
|
+
@import "helpers/length";
|
5
|
+
|
6
|
+
// Parses a JSON encoded number
|
7
|
+
// --------------------------------------------------------------------------------
|
8
|
+
// @param [string] $source: JSON complete source
|
9
|
+
// @param [number] $pointer: current pointer
|
10
|
+
// --------------------------------------------------------------------------------
|
11
|
+
// @throw "Unexpected token $token."
|
12
|
+
// @throw "Unexpected end of stream."
|
13
|
+
// --------------------------------------------------------------------------------
|
14
|
+
// @return [list|false] (new pointer, parsed number)
|
15
|
+
|
16
|
+
@function to-number($source) {
|
17
|
+
@if type-of($source) == number {
|
18
|
+
@return $source;
|
19
|
+
}
|
20
|
+
|
21
|
+
@if $source == true {
|
22
|
+
@return 1;
|
23
|
+
}
|
24
|
+
|
25
|
+
@if type-of($source) != string {
|
26
|
+
@return 0;
|
27
|
+
}
|
28
|
+
|
29
|
+
$pointer: 1;
|
30
|
+
$result: 0;
|
31
|
+
$allowed: '-' '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'; // Allowed characted to start with
|
32
|
+
$first: str-slice($source, $pointer, $pointer); // First character of the number
|
33
|
+
$minus: $first == '-'; // Is it negative?
|
34
|
+
|
35
|
+
// Early check for errors
|
36
|
+
@if not index($allowed, $first) {
|
37
|
+
//@warn "Could not cast `#{inspect($source)} into number.";
|
38
|
+
@return 0;
|
39
|
+
}
|
40
|
+
|
41
|
+
// Find the integer part
|
42
|
+
$find-integer: _find-integer($source, $pointer);
|
43
|
+
$pointer: nth($find-integer, 1);
|
44
|
+
$result: nth($find-integer, 2);
|
45
|
+
|
46
|
+
// Find digits
|
47
|
+
@if str-slice($source, $pointer, $pointer) == '.' {
|
48
|
+
$find-digits: _find-digits($source, $pointer);
|
49
|
+
$pointer: nth($find-digits, 1);
|
50
|
+
$digits: nth($find-digits, 2);
|
51
|
+
$result: $result + $digits;
|
52
|
+
}
|
53
|
+
|
54
|
+
@if $minus {
|
55
|
+
$result: $result * -1;
|
56
|
+
}
|
57
|
+
|
58
|
+
|
59
|
+
// @return $result $pointer str-length($source);
|
60
|
+
@if $pointer < str-length($source) {
|
61
|
+
$result: _length($result, str-slice($source, $pointer));
|
62
|
+
}
|
63
|
+
|
64
|
+
@return $result;
|
65
|
+
}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
// Parses a JSON encoded number to find the digits
|
2
|
+
// --------------------------------------------------------------------------------
|
3
|
+
// @param [string] $source: JSON complete source
|
4
|
+
// @param [number] $pointer: current pointer
|
5
|
+
// --------------------------------------------------------------------------------
|
6
|
+
// @throw "Unexpected token $token."
|
7
|
+
// --------------------------------------------------------------------------------
|
8
|
+
// @return [list|false] (new pointer, parsed number)
|
9
|
+
|
10
|
+
@function _find-digits($source, $pointer) {
|
11
|
+
$source: to-lower-case($source);
|
12
|
+
$length: str-length($source);
|
13
|
+
$numbers: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
|
14
|
+
$result: 0;
|
15
|
+
$runs: 1;
|
16
|
+
|
17
|
+
@while $pointer <= $length {
|
18
|
+
$token: str-slice($source, $pointer, $pointer);
|
19
|
+
$index: index($numbers, $token);
|
20
|
+
|
21
|
+
@if $token == '.' {
|
22
|
+
// @continue;
|
23
|
+
}
|
24
|
+
@else if $index and $index > 0 {
|
25
|
+
$runs: $runs * 10;
|
26
|
+
$result: $result * 10 + ($index - 1);
|
27
|
+
}
|
28
|
+
@else {
|
29
|
+
@return $pointer, $result / $runs;
|
30
|
+
}
|
31
|
+
|
32
|
+
$pointer: $pointer + 1;
|
33
|
+
}
|
34
|
+
|
35
|
+
@return $pointer, $result / $runs;
|
36
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
// Parses a JSON encoded number to find the integer part
|
2
|
+
// --------------------------------------------------------------------------------
|
3
|
+
// @param [string] $source: JSON complete source
|
4
|
+
// @param [number] $pointer: current pointer
|
5
|
+
// --------------------------------------------------------------------------------
|
6
|
+
// @throw "Unexpected token $token."
|
7
|
+
// --------------------------------------------------------------------------------
|
8
|
+
// @return [list|false] (new pointer, parsed number)
|
9
|
+
|
10
|
+
@function _find-integer($source, $pointer) {
|
11
|
+
$source: to-lower-case($source);
|
12
|
+
$length: str-length($source);
|
13
|
+
$numbers: '0' '1' '2' '3' '4' '5' '6' '7' '8' '9';
|
14
|
+
$result: 0;
|
15
|
+
|
16
|
+
@while $pointer <= $length {
|
17
|
+
$token: str-slice($source, $pointer, $pointer);
|
18
|
+
$index: index($numbers, $token);
|
19
|
+
|
20
|
+
@if $token == '-' {
|
21
|
+
// do nothing
|
22
|
+
}
|
23
|
+
@else if $index {
|
24
|
+
$result: $result * 10 + ($index - 1);
|
25
|
+
}
|
26
|
+
@else {
|
27
|
+
@return $pointer, $result;
|
28
|
+
}
|
29
|
+
|
30
|
+
$pointer: $pointer + 1;
|
31
|
+
}
|
32
|
+
|
33
|
+
@return $pointer, $result;
|
34
|
+
}
|
35
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
@function _length($number, $unit) {
|
2
|
+
$strings: 'px' 'cm' 'mm' '%' 'ch' 'pica' 'in' 'em' 'rem' 'pt' 'pc' 'ex' 'vw' 'vh' 'vmin' 'vmax';
|
3
|
+
$units: 1px 1cm 1mm 1% 1ch 1pica 1in 1em 1rem 1pt 1pc 1ex 1vw 1vh 1vmin 1vmax;
|
4
|
+
$index: index($strings, $unit);
|
5
|
+
|
6
|
+
@if not $index {
|
7
|
+
@warn "Unknown unit `#{inspect($unit)}`.";
|
8
|
+
@return 0;
|
9
|
+
}
|
10
|
+
|
11
|
+
@return $number * nth($units, $index);
|
12
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
// Power function
|
2
|
+
// --------------------------------------------------------------------------------
|
3
|
+
// @param [number] $x: number
|
4
|
+
// @param [number] $n: power
|
5
|
+
// --------------------------------------------------------------------------------
|
6
|
+
// @return [number] $x ^ $n
|
7
|
+
|
8
|
+
@function _pow($x, $n) {
|
9
|
+
$ret: 1;
|
10
|
+
|
11
|
+
@if $n >= 0 {
|
12
|
+
@for $i from 1 through $n {
|
13
|
+
$ret: $ret * $x;
|
14
|
+
}
|
15
|
+
} @else {
|
16
|
+
@for $i from $n to 0 {
|
17
|
+
$ret: $ret / $x;
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
@return $ret;
|
22
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
// Convert to string
|
2
|
+
// --------------------------------------------------------------------------------
|
3
|
+
// @param $value: value to cast
|
4
|
+
// --------------------------------------------------------------------------------
|
5
|
+
// @return [string]
|
6
|
+
|
7
|
+
@function to-string($value) {
|
8
|
+
@if type-of($value) == color {
|
9
|
+
@warn "Beware! Sass does some color conversion. The resulting string may be different from the color input.";
|
10
|
+
}
|
11
|
+
@return if(type-of($value) != string, inspect($value), $value);
|
12
|
+
}
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: SassyCast
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hugo Giraudel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Sass API for type conversion.
|
14
|
+
email:
|
15
|
+
- hugo.giraudel@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- CHANGELOG.md
|
22
|
+
- lib/SassyCast.rb
|
23
|
+
- stylesheets/SassyCast.scss
|
24
|
+
- stylesheets/types/bool/_bool.scss
|
25
|
+
- stylesheets/types/color/helpers/_from-hex.scss
|
26
|
+
- stylesheets/types/color/helpers/_from-hsl.scss
|
27
|
+
- stylesheets/types/color/helpers/_from-rgb.scss
|
28
|
+
- stylesheets/types/color/helpers/_get-color-value.scss
|
29
|
+
- stylesheets/types/color/helpers/_hex-to-dec.scss
|
30
|
+
- stylesheets/types/color/_color.scss
|
31
|
+
- stylesheets/types/list/_list.scss
|
32
|
+
- stylesheets/types/map/_map.scss
|
33
|
+
- stylesheets/types/null/_null.scss
|
34
|
+
- stylesheets/types/number/helpers/_find-digits.scss
|
35
|
+
- stylesheets/types/number/helpers/_find-integer.scss
|
36
|
+
- stylesheets/types/number/helpers/_length.scss
|
37
|
+
- stylesheets/types/number/helpers/_pow.scss
|
38
|
+
- stylesheets/types/number/_number.scss
|
39
|
+
- stylesheets/types/string/_string.scss
|
40
|
+
homepage: https://github.com/HugoGiraudel/SassyCast/
|
41
|
+
licenses: []
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.3.6
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project: SassyCast
|
59
|
+
rubygems_version: 2.0.3
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: SassyCast is a Sass-powered API for type conversion.
|
63
|
+
test_files: []
|