SassyBitwise 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5ecf3c37e8178ad93df44d11ddd33bfac361cfa4
4
+ data.tar.gz: b0bece9d963e5763a55072f898719797259eaf5f
5
+ SHA512:
6
+ metadata.gz: 1cb99b81efd15b9564f7b093710d889c36334c219fff17ca350234ba6cde9453ca4e8de179c0ab29766b2691941e5a4c339eb72138e905df27268d8a8f69ab06
7
+ data.tar.gz: cc086c572c15a7f7caf0f953eb1c2e044066be36a49469d290bff5b2bbcf2572cd9f789e8d8338021d810a9808c5019656bb43ee06c776b9b75cd6509c7bd449
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ * `1.0.0`: stable API, gem release
4
+ * `0.0.1`: initial commit
@@ -0,0 +1,14 @@
1
+ require 'compass'
2
+ extension_path = File.expand_path(File.join(File.dirname(__FILE__), ".."))
3
+ Compass::Frameworks.register('SassyBitwise', :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 SassyBitwise
8
+ VERSION = "1.0.0"
9
+ DATE = "2014-06-04"
10
+ end
11
+
12
+ module Sass::Script::Functions
13
+
14
+ end
@@ -0,0 +1,46 @@
1
+ // Bitwise helper function
2
+ // ---
3
+ // @param [list] $args: list of parameters
4
+ // ---
5
+ // @return [number]
6
+ // ---
7
+ @function bitwise($args) {
8
+ @if not arguments-validator($args) {
9
+ @return false;
10
+ }
11
+
12
+ // Handling not first
13
+ @if nth($args, 1) == "~" {
14
+ @return bw-not(nth($args, 2));
15
+ }
16
+
17
+ // Then all others
18
+ $operator: to-lower-case(nth($args, 2));
19
+ $x: nth($args, 1);
20
+ $y: nth($args, 3);
21
+
22
+ @if $operator == "^" {
23
+ @return bw-xor($x, $y);
24
+ }
25
+
26
+ @else if $operator == "|" {
27
+ @return bw-or($x, $y);
28
+ }
29
+
30
+ @else if $operator == "&" {
31
+ @return bw-and($x, $y);
32
+ }
33
+
34
+ @else if $operator == "<<" {
35
+ @return bw-shift-left($x, $y);
36
+ }
37
+
38
+ @else if $operator == ">>" {
39
+ @return bw-shift-right($x, $y);
40
+ }
41
+
42
+ @else {
43
+ @warn "Wrong operator `#{$operator}` for `bitwise` function.";
44
+ @return false
45
+ }
46
+ }
@@ -0,0 +1,9 @@
1
+ // Returns binary length
2
+ // ---
3
+ // @param [number] $value: value
4
+ // ---
5
+ // @return [number]
6
+ // ---
7
+ @function binary-length($value) {
8
+ @return str-length(decimal-to-binary($value) + unquote(""))
9
+ }
@@ -0,0 +1,27 @@
1
+ // Convert from base 2 to base 10
2
+ // ---
3
+ // @param [number] $value: binary to convert
4
+ // ---
5
+ // @return [number]
6
+ // ---
7
+ @function binary-to-decimal($value) {
8
+ $value: $value + unquote('');
9
+ $result: if(str-slice($value, 1, 1) == "0", 0, 1);
10
+
11
+ @for $i from 2 through str-length($value) {
12
+ $c: str-slice($value, $i, $i);
13
+
14
+ @if not index("0" "1", $c) {
15
+ @warn "Not binary.";
16
+ @return false
17
+ }
18
+
19
+ $result: $result * 2;
20
+
21
+ @if $c == "1" {
22
+ $result: $result + 1
23
+ }
24
+ }
25
+
26
+ @return $result
27
+ }
@@ -0,0 +1,24 @@
1
+ // Convert from base 10 to base 2
2
+ // ---
3
+ // @param [number] $value: decimal to convert
4
+ // ---
5
+ // @return [number]
6
+ // ---
7
+ @function decimal-to-binary($value) {
8
+ $largest: largest-power-of-2($value);
9
+ $result: 0;
10
+
11
+ @while $largest >= 1 {
12
+ @if $value - $largest >= 0 {
13
+ $value: $value - $largest;
14
+ $result: $result * 10 + 1
15
+ }
16
+ @else {
17
+ $result: $result * 10
18
+ }
19
+
20
+ $largest: $largest / 2
21
+ }
22
+
23
+ @return $result
24
+ }
@@ -0,0 +1,16 @@
1
+ // Returns largest power of 2 lesser than $value
2
+ // ---
3
+ // @param [number] $value: value
4
+ // ---
5
+ // @return [number]
6
+ // ---
7
+ @function largest-power-of-2($value) {
8
+ $result: 1;
9
+ @while $result <= $value {
10
+ @if $result * 2 > $value {
11
+ @return $result
12
+ }
13
+ $result: $result * 2
14
+ }
15
+ @return $result
16
+ }
@@ -0,0 +1,25 @@
1
+ // Power function
2
+ // Using Compass if defined
3
+ // ---
4
+ // @param [number] $x: first value
5
+ // @param [number] $y: second value
6
+ // ---
7
+ // @return [number]
8
+ // ---
9
+ @function power($x, $y) {
10
+ @if function-exists('pow') == true {
11
+ @return pow($x, $y)
12
+ }
13
+
14
+ $ret: 1;
15
+ @if $y >= 0 {
16
+ @for $i from 1 through $y {
17
+ $ret: $ret * $x
18
+ }
19
+ } @else {
20
+ @for $i from $y to 0 {
21
+ $ret: $ret / $x
22
+ }
23
+ }
24
+ @return $ret
25
+ }
@@ -0,0 +1,9 @@
1
+ // Returns sign of $x
2
+ // ---
3
+ // @param [number] $x: value
4
+ // ---
5
+ // @return [number]
6
+ // ---
7
+ @function sign($x) {
8
+ @return if($x > 0, 1, if($x < 0, -1, 0))
9
+ }
@@ -0,0 +1,19 @@
1
+ // `and` bitwise operator
2
+ // $x & $y
3
+ // ---
4
+ // @param [number] $x: first value
5
+ // @param [number] $y: second value
6
+ // ---
7
+ // @return [number]
8
+ // ---
9
+ @function bw-and($x, $y) {
10
+ $b: binary-length($x);
11
+ $res: 0;
12
+
13
+ @for $i from 0 through $b {
14
+ $h: power(2, $i);
15
+ $res: $res + $h * (floor($x / $h) % 2) * (floor($y / $h) % 2)
16
+ }
17
+
18
+ @return $res
19
+ }
@@ -0,0 +1,10 @@
1
+ // `not` bitwise operator
2
+ // ~ $x
3
+ // ---
4
+ // @param [number] $x: value
5
+ // ---
6
+ // @return [number]
7
+ // ---
8
+ @function bw-not($x) {
9
+ @return ($x + sign($x)) * -1
10
+ }
@@ -0,0 +1,21 @@
1
+ // `or` bitwise operator
2
+ // $x | $y
3
+ // ---
4
+ // @param [number] $x: first value
5
+ // @param [number] $y: second value
6
+ // ---
7
+ // @return [number]
8
+ // ---
9
+ @function bw-or($x, $y) {
10
+ $b: binary-length($x);
11
+ $res: 0;
12
+
13
+ @for $i from 0 through $b {
14
+ $h: power(2, $i);
15
+ $tx: floor($x / $h) % 2;
16
+ $ty: floor($y / $h) % 2;
17
+ $res: $res + $h * (($tx + $ty + $tx * $ty % 2) % 2)
18
+ }
19
+
20
+ @return $res
21
+ }
@@ -0,0 +1,11 @@
1
+ // `<<` bitwise operator
2
+ // $x << $y
3
+ // ---
4
+ // @param [number] $x: first value
5
+ // @param [number] $y: second value
6
+ // ---
7
+ // @return [number]
8
+ // ---
9
+ @function bw-shift-left($x, $y) {
10
+ @return $x * power(2, $y)
11
+ }
@@ -0,0 +1,11 @@
1
+ // `>>` bitwise operator
2
+ // $x >> $y
3
+ // ---
4
+ // @param [number] $x: first value
5
+ // @param [number] $y: second value
6
+ // ---
7
+ // @return [number]
8
+ // ---
9
+ @function bw-shift-right($x, $y) {
10
+ @return floor($x / power(2, $y))
11
+ }
@@ -0,0 +1,19 @@
1
+ // `xor` bitwise operator
2
+ // $x ^ $y
3
+ // ---
4
+ // @param [number] $x: first value
5
+ // @param [number] $y: second value
6
+ // ---
7
+ // @return [number]
8
+ // ---
9
+ @function bw-xor($x, $y) {
10
+ $b: binary-length($x);
11
+ $res: 0;
12
+
13
+ @for $i from 0 through $b {
14
+ $h: power(2, $i);
15
+ $res: $res + $h * (((floor($x / $h) % 2) + (floor($y / $h) % 2)) % 2)
16
+ }
17
+
18
+ @return $res
19
+ }
@@ -0,0 +1,45 @@
1
+ // Checks whether argument list is valid
2
+ // ---
3
+ // @param [list] $args: argument list
4
+ // ---
5
+ // @return [bool]
6
+ // ---
7
+ @function arguments-validator($args) {
8
+ $valid-operators: "^", "|", "&", "<<", ">>";
9
+
10
+ @if length($args) == 2 {
11
+ @if nth($args, 1) != "~" {
12
+ @warn "Only 2 arguments specified but first is not `~`.";
13
+ @return false;
14
+ }
15
+
16
+ @if not is-number(nth($args, 2)) {
17
+ @warn "Only 2 arguments specified but second is not a valid unitless number.";
18
+ @return false;
19
+ }
20
+ }
21
+
22
+ @else if length($args) == 3 {
23
+ @if not index($valid-operators, nth($args, 2)) {
24
+ @warn "Invalid operator: `#{nth($args, 2)}` given, one of #{inspect($valid-operators)} expected.";
25
+ @return false;
26
+ }
27
+
28
+ @if not is-number(nth($args, 1)) {
29
+ @warn "Invalid number as first argument: `#{nth($args, 1)} given, valid unitless number expected.";
30
+ @return false;
31
+ }
32
+
33
+ @if not is-number(nth($args, 3)) {
34
+ @warn "Invalid number as third argument: `#{nth($args, 3)} given, valid unitless number expected.";
35
+ @return false;
36
+ }
37
+ }
38
+
39
+ @else {
40
+ @warn "Invalid number of arguments: #{length($args)} given, 2 or 3 expected.";
41
+ @return false;
42
+ }
43
+
44
+ @return true;
45
+ }
@@ -0,0 +1,9 @@
1
+ // Checks whether $value is valid number
2
+ // ---
3
+ // @param [number] $value
4
+ // ---
5
+ // @return [bool]
6
+ // ---
7
+ @function is-number($value) {
8
+ @return type-of($value) == "number" and unitless($value)
9
+ }
@@ -0,0 +1,15 @@
1
+ @import "SassyBitwise/helpers/power";
2
+ @import "SassyBitwise/helpers/sign";
3
+ @import "SassyBitwise/helpers/largest-power-of-2";
4
+ @import "SassyBitwise/helpers/binary-to-decimal";
5
+ @import "SassyBitwise/helpers/decimal-to-binary";
6
+ @import "SassyBitwise/helpers/binary-length";
7
+ @import "SassyBitwise/validators/is-number";
8
+ @import "SassyBitwise/validators/arguments-validator";
9
+ @import "SassyBitwise/operators/and";
10
+ @import "SassyBitwise/operators/not";
11
+ @import "SassyBitwise/operators/or";
12
+ @import "SassyBitwise/operators/xor";
13
+ @import "SassyBitwise/operators/shift-left";
14
+ @import "SassyBitwise/operators/shift-right";
15
+ @import "SassyBitwise/api/bitwise";
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: SassyBitwise
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Hugo Giraudel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sass
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: compass
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.12'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.12'
41
+ description: Bitwise operators in Sass.
42
+ email:
43
+ - hugo.giraudel@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - lib/SassyBitwise.rb
50
+ - stylesheets/SassyBitwise/api/_bitwise.scss
51
+ - stylesheets/SassyBitwise/helpers/_binary-length.scss
52
+ - stylesheets/SassyBitwise/helpers/_binary-to-decimal.scss
53
+ - stylesheets/SassyBitwise/helpers/_decimal-to-binary.scss
54
+ - stylesheets/SassyBitwise/helpers/_largest-power-of-2.scss
55
+ - stylesheets/SassyBitwise/helpers/_power.scss
56
+ - stylesheets/SassyBitwise/helpers/_sign.scss
57
+ - stylesheets/SassyBitwise/operators/_and.scss
58
+ - stylesheets/SassyBitwise/operators/_not.scss
59
+ - stylesheets/SassyBitwise/operators/_or.scss
60
+ - stylesheets/SassyBitwise/operators/_shift-left.scss
61
+ - stylesheets/SassyBitwise/operators/_shift-right.scss
62
+ - stylesheets/SassyBitwise/operators/_xor.scss
63
+ - stylesheets/SassyBitwise/validators/_arguments-validator.scss
64
+ - stylesheets/SassyBitwise/validators/_is-number.scss
65
+ - stylesheets/_SassyBitwise.scss
66
+ homepage: http://github.com/HugoGiraudel/SassyBitwise
67
+ licenses: []
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.3.6
83
+ requirements: []
84
+ rubyforge_project: SassyBitwise
85
+ rubygems_version: 2.2.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: An implementation of bitwise operators in Sass without the use of Ruby.
89
+ test_files: []