Skip to content
Browse files

Merge pull request #167 from iamdustan/parse-angle

tools.extractUnit and tools.parseAngle
  • Loading branch information...
1 parent 155597e commit 728d02add353b3c9bf76558a0146233c8b7dc09d @basecode basecode committed
Showing with 144 additions and 7 deletions.
  1. +8 −6 CHANGELOG
  2. +42 −1 src/tools.js
  3. +94 −0 test/tools-spec.js
View
14 CHANGELOG
@@ -1,21 +1,23 @@
v0.4.2
-------------------
-
* 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.
* Added support for getAbsoluteBoundingBox and getAbsoluteMatrix on
DisplayObject
-* Added an destroy event for DisplayObjects, which is fired on a displayobject when
- destroy is called on it.
+* Added an destroy event for DisplayObjects, which is fired on a
+ DisplayObject when destroy is called on it.
* Updated KeyframeAnimation.js documentation
-* Fixed an issue with KeyframeAnimation where it would trip over certain undefined values
-* Fixed a bug in KeyframeAnimation#_fillInProperties where it would refer to the wrong
- instance property easingFn (-> easing)
+* Fixed an issue with KeyframeAnimation where it would trip over
+ certain undefined values
+* Fixed a bug in KeyframeAnimation#_fillInProperties where it would
+ refer to the wrong instance property easingFn (-> easing)
* Sending of user messages to runner contexts is deferred until context startup
* Allow for an ID string to be passed to as the first argument to `run`
* Fix a memory leak with frame rate logging
* Fix matrix attribute setter to update scale values correctly
+* Add tools.parseAngle to accept angle units as strings.
+ http://www.w3.org/TR/css3-values/#angles
v0.4.1 / 2012-10-26
-------------------
View
43 src/tools.js
@@ -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
94 test/tools-spec.js
@@ -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 728d02a

Please sign in to comment.
Something went wrong with that request. Please try again.