Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rename parseUnitString to extractUnit and beef it up

  • Loading branch information...
commit 6d551047e25b0442fb54031fa2ee96f5af8221e9 1 parent eddc612
@iamdustan iamdustan authored
Showing with 53 additions and 9 deletions.
  1. +8 −4 src/tools.js
  2. +45 −5 test/tools-spec.js
View
12 src/tools.js
@@ -345,14 +345,18 @@ define([], function() {
/**
* Parse a given unit string into the amount and unit
*
- * @param {String} any The unit string. E.g. '90deg', '7em'
+ * @param {any} any Anything that can be converted to a string. E.g. '7deg', ['50%']
* @returns {String} unit
*/
- parseUnitString: function (any) {
+ extractUnit: function (any) {
+ if (typeof any === 'undefined' || typeof any === 'number') {
+ return '';
+ }
+
// 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];
+ return unit.match(/[a-z%]*$/i)[0];
},
/**
@@ -364,7 +368,7 @@ define([], function() {
*/
parseAngle: function(angle) {
var radians,
- unit = tools.parseUnitString(angle),
+ unit = tools.extractUnit(angle),
amount = parseFloat(angle);
switch (unit) {
View
50 test/tools-spec.js
@@ -286,30 +286,57 @@ define([
/*--------------------------------------------------------------------------*/
- describe('tools.parseUnitString', function() {
- var parseUnitString = tools.parseUnitString;
+ describe('tools.extractUnit', function() {
+ var extractUnit = tools.extractUnit;
it('return the unit from a unit string', function() {
- var returned = parseUnitString('9deg');
+ var returned = extractUnit('9deg');
var expected = 'deg';
expect(returned).toEqual(expected);
});
it('should work with negative numbers', function() {
- var returned = parseUnitString('-741232grad');
+ var returned = extractUnit('-741232grad');
var expected = 'grad';
expect(returned).toEqual(expected);
});
it('should work with trailing whitespace', function() {
- var returned = parseUnitString('-741232grad ');
+ var returned = extractUnit('-741232grad ');
var expected = 'grad';
expect(returned).toEqual(expected);
});
+ it('should work with leading whitespace', function() {
+ var returned = extractUnit('-741232 grad ');
+ var expected = 'grad';
+
+ expect(returned).toEqual(expected);
+ });
+
+ it('should return an empty string in case of a number', function () {
+ var returned = extractUnit(7531);
+ var expected = '';
+
+ expect(returned).toEqual(expected);
+ });
+
+ it('should return the string when given an array', function () {
+ var returned = extractUnit(['50s']);
+ var expected = 's';
+
+ expect(returned).toEqual(expected);
+ });
+
+ it('should work with percentages', function () {
+ var returned = extractUnit('50%');
+ var expected = '%';
+
+ expect(returned).toEqual(expected);
+ });
});
describe('tools.parseAngle', function() {
@@ -336,6 +363,19 @@ define([
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 (number)', function () {
+ var calculated = parseAngle(-0.5026548245743669);
+ var expected = -0.5026548245743669;
+
+ expect(calculated).toBeCloseTo(expected, 10);
+ });
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.