Permalink
Browse files

Merge pull request #167 from iamdustan/parse-angle

tools.extractUnit and tools.parseAngle
  • Loading branch information...
2 parents 155597e + 2d5afc7 commit 46b612dd6d523173eda2ae19f33264e1fb79af9d @basecode basecode committed Dec 6, 2012
Showing with 137 additions and 2 deletions.
  1. +1 −1 CHANGELOG
  2. +42 −1 src/tools.js
  3. +94 −0 test/tools-spec.js
View
@@ -1,6 +1,6 @@
v0.4.2
-------------------
-
+* Add tools.parseAngle to accept angle units as strings. http://www.w3.org/TR/css3-values/#angles
* Add support for an `interactive` attribute on all DisplayObjects which allows
pointer-events to be received (true) or to pass through (false)
* Setting an attribute to the actual value won't trigger a render message.
View
@@ -6,7 +6,8 @@ define([], function() {
noop = function() {},
push = [].push,
slice = [].slice,
- toString = {}.toString;
+ toString = {}.toString,
+ PI = Math.PI;
/**
*
@@ -339,6 +340,46 @@ define([], function() {
array.length -= numRemoved;
return array;
+ },
+
+ /**
+ * Parse a given unit string into the amount and unit
+ *
+ * @param {any} any Anything that can be converted to a string. E.g. '7deg', ['50%']
+ * @returns {String} unit
+ */
+ extractUnit: function (any) {
+ // make sure it's a string and remove trailing whitespace
+ var unit = String(any).replace(/\s+$/, '');
+ // returns extracted unit or empty string
+ return unit.match(/[a-z%]*$/i)[0];
+ },
+
+ /**
+ * Parses the angle string to radians
+ * https://developer.mozilla.org/en-US/docs/CSS/angle
+ *
+ * @param {Number|String} angle The angle/unit string
+ * @returns {Number} The angle in radians
+ */
+ parseAngle: function(angle) {
+ var radians,
+ unit = tools.extractUnit(angle),
+ amount = parseFloat(angle);
+
+ switch (unit) {
+ case '': // default
+ case 'rad':
+ radians = amount; break;
+ case 'deg':
+ radians = amount * PI / 180; break;
+ case 'grad':
+ radians = amount * PI / 200; break;
+ case 'turn':
+ radians = amount * 2 * PI; break;
+ }
+
+ return radians;
}
};
View
@@ -284,4 +284,98 @@ define([
});
});
+ /*--------------------------------------------------------------------------*/
+
+ describe('tools.extractUnit', function() {
+ var extractUnit = tools.extractUnit;
+
+ it('return the unit from a unit string', function() {
+ var returned = extractUnit('9deg');
+ var expected = 'deg';
+
+ expect(returned).toBe(expected);
+ });
+
+ it('should work with negative numbers', function() {
+ var returned = extractUnit('-741232grad');
+ var expected = 'grad';
+
+ expect(returned).toBe(expected);
+ });
+
+ it('should work with trailing whitespace', function() {
+ var returned = extractUnit('-741232grad ');
+ var expected = 'grad';
+
+ expect(returned).toBe(expected);
+ });
+
+ it('should work with leading whitespace', function() {
+ var returned = extractUnit('-741232 grad ');
+ var expected = 'grad';
+
+ expect(returned).toBe(expected);
+ });
+
+ it('should return an empty string in case of a number', function () {
+ var returned = extractUnit(7531);
+ var expected = '';
+
+ expect(returned).toBe(expected);
+ });
+
+ it('should return the string when given an array', function () {
+ var returned = extractUnit(['50s']);
+ var expected = 's';
+
+ expect(returned).toBe(expected);
+ });
+
+ it('should work with percentages', function () {
+ var returned = extractUnit('50%');
+ var expected = '%';
+
+ expect(returned).toBe(expected);
+ });
+ });
+
+ describe('tools.parseAngle', function() {
+ var parseAngle = tools.parseAngle;
+
+ it('calculates radians for degrees', function() {
+ var calculated = parseAngle('45deg');
+ var expected = 0.7853981633974483;
+
+ expect(calculated).toBeCloseTo(expected, 10);
+ });
+
+ it('calculates radians for turns', function() {
+ var calculated = parseAngle('1turn');
+ var expected = 6.283185307179586;
+
+ expect(calculated).toBeCloseTo(expected, 10);
+ });
+
+ it('calculates radians for gradians', function () {
+ var calculated = parseAngle('-32grad');
+ var expected = -0.5026548245743669;
+
+ expect(calculated).toBeCloseTo(expected, 10);
+ });
+
+ it('calculates radians for radians (string)', function () {
+ var calculated = parseAngle('-0.5026548245743669rad');
+ var expected = -0.5026548245743669;
+
+ expect(calculated).toBeCloseTo(expected, 10);
+ });
+
+ it('calculates radians for radians', function () {
+ var calculated = parseAngle('-0.5026548245743669');
+ var expected = -0.5026548245743669;
+
+ expect(calculated).toBeCloseTo(expected, 10);
+ });
+ });
+
});

0 comments on commit 46b612d

Please sign in to comment.