SassyCast 0.0.1 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4770cd04049b0257d92291e040aaebe64e28ecad
4
- data.tar.gz: 166fce1bcd86158f1b2304e4057ac43705fbd027
3
+ metadata.gz: 4f42a0fe044be49a40d427fe6af9cdc0fa60639a
4
+ data.tar.gz: 8a4c35110c665195d42761ae5a9aa389ea86bb5f
5
5
  SHA512:
6
- metadata.gz: 8b118cf974c1a739297ad915521d36fc182d3f139eb1a24ff32d75cd01b244f879125a7d37d270838560a44dc628556f913ded32d08b90b23e68938ba2e65eba
7
- data.tar.gz: 7de8734df558b6079ef63f840bd9fd0c67ada77a24ace850ea47937ed1b262f670f7733c05355d308998a632ca1daf749b413154e218fa9444d3bdf345a998ad
6
+ metadata.gz: bf461c41c1d0a48c9496a755996edff6ced8011b14541e7708968a46027cbcaf2dab57d15fe1d9971948bcd3d4818bc1f65184f46267b09c2b5bc88cec3cb6ea
7
+ data.tar.gz: e16b8b9828b79152b02b54f697e51a39b9ccaed02fd0526023c8de3646a84aff6528e78cf876e38337f44524c4f9aa0cf4fbe4eefc798aca5af19df0cc7e57b6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,4 @@
1
1
  # Changelog
2
2
 
3
+ * `1.0.0`: `to-list` improvements, stable API
3
4
  * `0.0.1`: initial commit
data/lib/SassyCast.rb CHANGED
@@ -5,8 +5,8 @@ Compass::Frameworks.register('SassyCast', :path => extension_path)
5
5
  # Version is a number. If a version contains alphas, it will be created as a prerelease version
6
6
  # Date is in the form of YYYY-MM-DD
7
7
  module SassyCast
8
- VERSION = "0.0.1"
9
- DATE = "2014-01-27"
8
+ VERSION = "1.0.0"
9
+ DATE = "2014-01-31"
10
10
  end
11
11
 
12
12
  module Sass::Script::Functions
@@ -1,3 +1,5 @@
1
+ // Helpers
2
+
1
3
  @import "helpers/from-rgb";
2
4
  @import "helpers/from-hsl";
3
5
  @import "helpers/from-hex";
@@ -1,3 +1,9 @@
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
+
1
7
  @function _from-hex($string) {
2
8
  $string-lower: to-lower-case($string);
3
9
  $r: ""; $g: ""; $b: "";
@@ -1,3 +1,9 @@
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
+
1
7
  @function _from-hsl($string) {
2
8
  $frags: ();
3
9
  $string-lower: to-lower-case($string);
@@ -1,3 +1,9 @@
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
+
1
7
  @function _from-rgb($string) {
2
8
  $string-lower: to-lower-case($string);
3
9
  $frags: ();
@@ -1,3 +1,9 @@
1
+ // Cast a stringified number / stringified percentage into number type
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $string: string
4
+ // --------------------------------------------------------------------------------
5
+ // @return [number] unitless number or percentage
6
+
1
7
  @function _get-color-value($string) {
2
8
  $first: str-slice($string, 1, 1);
3
9
 
@@ -1,3 +1,9 @@
1
+ // Convert an hexadecimal number to a decimal number
2
+ // --------------------------------------------------------------------------------
3
+ // @param [string] $string: hexadecimal value
4
+ // --------------------------------------------------------------------------------
5
+ // @return [number] decimal number
6
+
1
7
  @function _hex-to-dec($string){
2
8
  $hex: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "a" "b" "c" "d" "e" "f";
3
9
  $string: to-lower-case($string);
@@ -4,7 +4,9 @@
4
4
  // --------------------------------------------------------------------------------
5
5
  // @return [list]
6
6
 
7
- @function to-list($value) {
7
+ @function to-list($value, $keep: 'both') {
8
+ $keep: if(index('keys' 'values' 'both', $keep), $keep, 'both');
9
+
8
10
  @if type-of($value) == map {
9
11
  $keys: ();
10
12
  $values: ();
@@ -12,7 +14,16 @@
12
14
  $keys: append($keys, $key);
13
15
  $values: append($values, $val);
14
16
  }
15
- @return zip($keys, $values);
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
+ }
16
27
  }
17
28
 
18
29
  @return if(type-of($value) != list, ($value,), $value);
@@ -1,64 +1,66 @@
1
+ // Helpers
2
+
1
3
  @import "helpers/find-integer";
2
4
  @import "helpers/find-digits";
3
5
  @import "helpers/pow";
4
6
  @import "helpers/length";
5
7
 
6
- // Parses a JSON encoded number
7
- // --------------------------------------------------------------------------------
8
- // @param [string] $source: JSON complete source
9
- // @param [number] $pointer: current pointer
8
+ // Cast a value to a number if possible or return 0
10
9
  // --------------------------------------------------------------------------------
11
- // @throw "Unexpected token $token."
12
- // @throw "Unexpected end of stream."
10
+ // @param [string] $value: complete source
13
11
  // --------------------------------------------------------------------------------
14
- // @return [list|false] (new pointer, parsed number)
12
+ // @return [number] number (0 if cast failed)
15
13
 
16
- @function to-number($source) {
17
- @if type-of($source) == number {
18
- @return $source;
14
+ @function to-number($value) {
15
+ // In case everything's good
16
+ @if type-of($value) == number {
17
+ @return $value;
19
18
  }
20
19
 
21
- @if $source == true {
20
+ // Boolean to number
21
+ @if $value == true {
22
22
  @return 1;
23
23
  }
24
24
 
25
- @if type-of($source) != string {
25
+ // Fail
26
+ @if type-of($value) != string {
26
27
  @return 0;
27
28
  }
28
29
 
30
+ // Trying to cast
29
31
  $pointer: 1;
30
32
  $result: 0;
31
33
  $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
34
+ $first: str-slice($value, $pointer, $pointer); // First character of the number
33
35
  $minus: $first == '-'; // Is it negative?
34
36
 
35
37
  // Early check for errors
36
38
  @if not index($allowed, $first) {
37
- //@warn "Could not cast `#{inspect($source)} into number.";
39
+ @warn "Could not cast `#{inspect($value)} into number.";
38
40
  @return 0;
39
41
  }
40
42
 
41
43
  // Find the integer part
42
- $find-integer: _find-integer($source, $pointer);
44
+ $find-integer: _find-integer($value, $pointer);
43
45
  $pointer: nth($find-integer, 1);
44
46
  $result: nth($find-integer, 2);
45
47
 
46
48
  // Find digits
47
- @if str-slice($source, $pointer, $pointer) == '.' {
48
- $find-digits: _find-digits($source, $pointer);
49
+ @if str-slice($value, $pointer, $pointer) == '.' {
50
+ $find-digits: _find-digits($value, $pointer);
49
51
  $pointer: nth($find-digits, 1);
50
52
  $digits: nth($find-digits, 2);
51
53
  $result: $result + $digits;
52
54
  }
53
55
 
56
+ // In case of negative
54
57
  @if $minus {
55
58
  $result: $result * -1;
56
59
  }
57
60
 
58
-
59
- // @return $result $pointer str-length($source);
60
- @if $pointer < str-length($source) {
61
- $result: _length($result, str-slice($source, $pointer));
61
+ // In case of possible CSS unit
62
+ @if $pointer < str-length($value) {
63
+ $result: _length($result, str-slice($value, $pointer));
62
64
  }
63
65
 
64
66
  @return $result;
@@ -1,11 +1,9 @@
1
- // Parses a JSON encoded number to find the digits
1
+ // Finding the digits part of a stringified number
2
2
  // --------------------------------------------------------------------------------
3
- // @param [string] $source: JSON complete source
3
+ // @param [string] $source: string source
4
4
  // @param [number] $pointer: current pointer
5
5
  // --------------------------------------------------------------------------------
6
- // @throw "Unexpected token $token."
7
- // --------------------------------------------------------------------------------
8
- // @return [list|false] (new pointer, parsed number)
6
+ // @return [list] (new pointer, parsed number)
9
7
 
10
8
  @function _find-digits($source, $pointer) {
11
9
  $source: to-lower-case($source);
@@ -1,11 +1,9 @@
1
- // Parses a JSON encoded number to find the integer part
1
+ // Finding the integer part of a stringified number
2
2
  // --------------------------------------------------------------------------------
3
- // @param [string] $source: JSON complete source
3
+ // @param [string] $source: string source
4
4
  // @param [number] $pointer: current pointer
5
5
  // --------------------------------------------------------------------------------
6
- // @throw "Unexpected token $token."
7
- // --------------------------------------------------------------------------------
8
- // @return [list|false] (new pointer, parsed number)
6
+ // @return [list] (new pointer, parsed number)
9
7
 
10
8
  @function _find-integer($source, $pointer) {
11
9
  $source: to-lower-case($source);
@@ -18,7 +16,7 @@
18
16
  $index: index($numbers, $token);
19
17
 
20
18
  @if $token == '-' {
21
- // do nothing
19
+ // @continue;
22
20
  }
23
21
  @else if $index {
24
22
  $result: $result * 10 + ($index - 1);
@@ -1,3 +1,10 @@
1
+ // Tries to find a unit that would match a CSS length
2
+ // --------------------------------------------------------------------------------
3
+ // @param [number] $number: number
4
+ // @param [unit] $unit: potential unit
5
+ // --------------------------------------------------------------------------------
6
+ // @return [number] length (0 if cast failed)
7
+
1
8
  @function _length($number, $unit) {
2
9
  $strings: 'px' 'cm' 'mm' '%' 'ch' 'pica' 'in' 'em' 'rem' 'pt' 'pc' 'ex' 'vw' 'vh' 'vmin' 'vmax';
3
10
  $units: 1px 1cm 1mm 1% 1ch 1pica 1in 1em 1rem 1pt 1pc 1ex 1vw 1vh 1vmin 1vmax;
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: SassyCast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hugo Giraudel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-27 00:00:00.000000000 Z
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Sass API for type conversion.
14
14
  email: