From 58e3247c2766023b3afdac2abaedfe36c24e2f62 Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 6 Aug 2016 12:31:49 -0600 Subject: [PATCH] Added splitUnit function --- lib/type/unit/Unit.js | 39 ++++++++++++++++++++++++++++++++++++++- lib/type/unit/index.js | 3 +++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/type/unit/Unit.js b/lib/type/unit/Unit.js index 981e5441d..9518a5769 100644 --- a/lib/type/unit/Unit.js +++ b/lib/type/unit/Unit.js @@ -11,6 +11,7 @@ function factory (type, config, load, typed, math) { var divide = load(require('../../function/arithmetic/divideScalar')); var pow = load(require('../../function/arithmetic/pow')); var abs = load(require('../../function/arithmetic/abs')); + var fix = load(require('../../function/arithmetic/fix')); var equal = load(require('../../function/relational/equal')); var isNumeric = load(require('../../function/utils/isNumeric')); var format = load(require('../../function/string/format')); @@ -856,7 +857,12 @@ function factory (type, config, load, typed, math) { * @return {number | BigNumber | Fraction} Returns the unit value */ Unit.prototype.toNumeric = function (valuelessUnit) { - var other = this.to(valuelessUnit); + var other = this; + if(valuelessUnit) { + // Allow getting the numeric value without converting to a different unit + other = this.to(valuelessUnit); + } + if(other._isDerived()) { return other._denormalize(other.value); } @@ -1165,6 +1171,37 @@ function factory (type, config, load, typed, math) { return bestPrefix; }; + /** + * Returns an array of units whose sum is equal to this unit + * @memberof Unit + * @param {Array} [parts] An array of strings or valueless units. + * + * Example: + * + * var u = new Unit(1, 'm'); + * u.splitUnit(['feet', 'inch']); + * [ 3 feet, 3.3700787401575 inch ] + * + * @return {Array} An array of units. + */ + Unit.prototype.splitUnit = function(parts) { + + var x = this.clone(); + var ret = []; + for(var i=0; i