From dfbc575bc125c3ec00c9272e69e1b631941d05ef Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 6 Aug 2015 12:50:48 +0000 Subject: [PATCH 1/3] Added electrical units and unit tests --- lib/type/unit/Unit.js | 217 +++++++++++++++++++++++++++++++++--- test/type/unit/Unit.test.js | 14 ++- 2 files changed, 215 insertions(+), 16 deletions(-) diff --git a/lib/type/unit/Unit.js b/lib/type/unit/Unit.js index 5850138dd..1fdf21312 100644 --- a/lib/type/unit/Unit.js +++ b/lib/type/unit/Unit.js @@ -1136,6 +1136,33 @@ function factory (type, config, load, typed) { PRESSURE: { dimensions: [1, -1, -2, 0, 0, 0, 0, 0, 0] }, + + ELECTRIC_CHARGE: { + dimensions: [0, 0, 1, 1, 0, 0, 0, 0, 0] + }, + ELECTRIC_CAPACITANCE: { + dimensions: [-1, -2, 4, 2, 0, 0, 0, 0, 0] + }, + ELECTRIC_POTENTIAL: { + dimensions: [1, 2, -3, -1, 0, 0, 0, 0, 0] + }, + ELECTRIC_RESISTANCE: { + dimensions: [1, 2, -3, -2, 0, 0, 0, 0, 0] + }, + ELECTRIC_INDUCTANCE: { + dimensions: [1, 2, -2, -2, 0, 0, 0, 0, 0] + }, + ELECTRIC_CONDUCTANCE: { + dimensions: [-1, -2, 3, 2, 0, 0, 0, 0, 0] + }, + MAGNETIC_FLUX: { + dimensions: [1, 2, -2, -1, 0, 0, 0, 0, 0] + }, + MAGNETIC_FLUX_DENSITY: { + dimensions: [1, 0, -2, -1, 0, 0, 0, 0, 0] + }, + + ANGLE: { dimensions: [0, 0, 0, 0, 0, 0, 0, 1, 0] }, @@ -1964,6 +1991,21 @@ function factory (type, config, load, typed) { value: 1055.05585262, offset: 0 }, + eV: { + name: 'eV', + base: BASE_UNITS.ENERGY, + prefixes: PREFIXES.SHORT, + value: 1.602176565e-19, + offset: 0 + }, + electronvolt: { + name: 'electronvolt', + base: BASE_UNITS.ENERGY, + prefixes: PREFIXES.LONG, + value: 1.602176565e-19, + offset: 0 + }, + // Power W: { @@ -2004,6 +2046,129 @@ function factory (type, config, load, typed) { offset: 0 }, + // Electric charge + coulomb: { + name: 'coulomb', + base: BASE_UNITS.ELECTRIC_CHARGE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0, + }, + C: { + name: 'C', + base: BASE_UNITS.ELECTRIC_CHARGE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Electric capacitance + farad: { + name: 'farad', + base: BASE_UNITS.ELECTRIC_CAPACITANCE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + F: { + name: 'F', + base: BASE_UNITS.ELECTRIC_CAPACITANCE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Electric potential + volt: { + name: 'volt', + base: BASE_UNITS.ELECTRIC_POTENTIAL, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + V: { + name: 'V', + base: BASE_UNITS.ELECTRIC_POTENTIAL, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Electric resistance + ohm: { + name: 'ohm', + base: BASE_UNITS.ELECTRIC_RESISTANCE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + /* + Ω: { + name: 'Ω', + base: BASE_UNITS.ELECTRIC_RESISTANCE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + */ + // Electric inductance + henry: { + name: 'henry', + base: BASE_UNITS.ELECTRIC_INDUCTANCE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + H: { + name: 'H', + base: BASE_UNITS.ELECTRIC_INDUCTANCE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Electric conductance + siemens: { + name: 'siemens', + base: BASE_UNITS.ELECTRIC_CONDUCTANCE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + S: { + name: 'S', + base: BASE_UNITS.ELECTRIC_CONDUCTANCE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Magnetic flux + weber: { + name: 'weber', + base: BASE_UNITS.MAGNETIC_FLUX, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + Wb: { + name: 'Wb', + base: BASE_UNITS.MAGNETIC_FLUX, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Magnetic flux density + tesla: { + name: 'tesla', + base: BASE_UNITS.MAGNETIC_FLUX_DENSITY, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + T: { + name: 'T', + base: BASE_UNITS.MAGNETIC_FLUX_DENSITY, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Binary b: { name: 'b', @@ -2083,8 +2248,20 @@ function factory (type, config, load, typed) { gradients: 'grad', cycles: 'cycle', + joules: 'joule', + watts: 'watt', + BTUs: 'BTU', + amperes: 'ampere', - moles: 'mole' + coulombs: 'coulomb', + volts: 'volts', + ohms: 'ohm', + farads: 'farad', + webers: 'weber', + teslas: 'tesla', + electronvolts: 'electronvolt', + moles: 'mole' + }; /** @@ -2095,22 +2272,30 @@ function factory (type, config, load, typed) { var UNIT_SYSTEMS = { si: { // Base units - NONE: {unit: UNIT_NONE, prefix: PREFIXES.NONE['']}, - LENGTH: {unit: UNITS.m, prefix: PREFIXES.SHORT['']}, - MASS: {unit: UNITS.g, prefix: PREFIXES.SHORT['k']}, - TIME: {unit: UNITS.s, prefix: PREFIXES.SHORT['']}, - CURRENT: {unit: UNITS.A, prefix: PREFIXES.SHORT['']}, - TEMPERATURE: {unit: UNITS.K, prefix: PREFIXES.SHORT['']}, - LUMINOUS_INTENSITY: {unit: UNITS.cd, prefix: PREFIXES.SHORT['']}, - AMOUNT_OF_SUBSTANCE: {unit: UNITS.mol, prefix: PREFIXES.SHORT['']}, - ANGLE: {unit: UNITS.rad, prefix: PREFIXES.SHORT['']}, - BIT: {unit: UNITS.bit, prefix: PREFIXES.SHORT['']}, + NONE: {unit: UNIT_NONE, prefix: PREFIXES.NONE['']}, + LENGTH: {unit: UNITS.m, prefix: PREFIXES.SHORT['']}, + MASS: {unit: UNITS.g, prefix: PREFIXES.SHORT['k']}, + TIME: {unit: UNITS.s, prefix: PREFIXES.SHORT['']}, + CURRENT: {unit: UNITS.A, prefix: PREFIXES.SHORT['']}, + TEMPERATURE: {unit: UNITS.K, prefix: PREFIXES.SHORT['']}, + LUMINOUS_INTENSITY: {unit: UNITS.cd, prefix: PREFIXES.SHORT['']}, + AMOUNT_OF_SUBSTANCE: {unit: UNITS.mol, prefix: PREFIXES.SHORT['']}, + ANGLE: {unit: UNITS.rad, prefix: PREFIXES.SHORT['']}, + BIT: {unit: UNITS.bit, prefix: PREFIXES.SHORT['']}, // Derived units - FORCE: {unit: UNITS.N, prefix: PREFIXES.SHORT['']}, - ENERGY: {unit: UNITS.J, prefix: PREFIXES.SHORT['']}, - POWER: {unit: UNITS.W, prefix: PREFIXES.SHORT['']}, - PRESSURE: {unit: UNITS.Pa, prefix: PREFIXES.SHORT['']}, + FORCE: {unit: UNITS.N, prefix: PREFIXES.SHORT['']}, + ENERGY: {unit: UNITS.J, prefix: PREFIXES.SHORT['']}, + POWER: {unit: UNITS.W, prefix: PREFIXES.SHORT['']}, + PRESSURE: {unit: UNITS.Pa, prefix: PREFIXES.SHORT['']}, + ELECTRIC_CHARGE: {unit: UNITS.C, prefix: PREFIXES.SHORT['']}, + ELECTRIC_CAPACITANCE: {unit: UNITS.F, prefix: PREFIXES.SHORT['']}, + ELECTRIC_POTENTIAL: {unit: UNITS.V, prefix: PREFIXES.SHORT['']}, + ELECTRIC_RESISTANCE: {unit: UNITS.ohm, prefix: PREFIXES.SHORT['']}, + ELECTRIC_INDUCTANCE: {unit: UNITS.H, prefix: PREFIXES.SHORT['']}, + ELECTRIC_CONDUCTANCE: {unit: UNITS.S, prefix: PREFIXES.SHORT['']}, + MAGNETIC_FLUX: {unit: UNITS.Wb, prefix: PREFIXES.SHORT['']}, + MAGNETIC_FLUX_DENSITY: {unit: UNITS.T, prefix: PREFIXES.SHORT['']} } }; @@ -2120,6 +2305,8 @@ function factory (type, config, load, typed) { UNIT_SYSTEMS.cgs.MASS = {unit: UNITS.g, prefix: PREFIXES.SHORT['']}; UNIT_SYSTEMS.cgs.FORCE = {unit: UNITS.dyn, prefix: PREFIXES.SHORT['']}; UNIT_SYSTEMS.cgs.ENERGY = {unit: UNITS.erg, prefix: PREFIXES.NONE['']}; + // there are wholly 4 unique cgs systems for electricity and magnetism, + // so let's not worry about it unless somebody complains UNIT_SYSTEMS.us = JSON.parse(JSON.stringify(UNIT_SYSTEMS.si)); UNIT_SYSTEMS.us.LENGTH = {unit: UNITS.ft, prefix: PREFIXES.NONE['']}; diff --git a/test/type/unit/Unit.test.js b/test/type/unit/Unit.test.js index 102553e2e..d9dab90de 100644 --- a/test/type/unit/Unit.test.js +++ b/test/type/unit/Unit.test.js @@ -704,5 +704,17 @@ describe('unit', function() { }); }); - // TODO: test the value of each of the available units... + describe('UNITS', function() { + it('should be of the correct value and dimension', function() { + assert.equal(new Unit(1, 's A'), new Unit(1, 'C')); + assert.equal(new Unit(1, 'W/A'), new Unit(1, 'V')); + assert.equal(new Unit(1, 'V/A'), new Unit(1, 'ohm')); + assert.equal(new Unit(1, 'C/V'), new Unit(1, 'F')); + assert.equal(new Unit(1, 'J/A'), new Unit(1, 'Wb')); + assert.equal(new Unit(1, 'Wb/m^2'), new Unit(1, 'T')); + assert.equal(new Unit(1, 'Wb/A'), new Unit(1, 'H')); + assert.equal(new Unit(1, 'ohm^-1'), new Unit(1, 'S')); + assert.equal(new Unit(1, 'eV'), new Unit(1.602176565e-19, 'J')); + }); + }); }); From 4f6cc1254bcf2ff5e1ede98100cd4ecc56cdec23 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 6 Aug 2015 13:34:12 +0000 Subject: [PATCH 2/3] Updated docs and examples --- docs/reference/units.md | 5 +++-- examples/units.js | 23 +++++++++++++++++++++++ lib/type/unit/Unit.js | 24 ++++++++++++++++++------ test/type/unit/Unit.test.js | 19 ++++++++++--------- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/docs/reference/units.md b/docs/reference/units.md index 2859b3b80..2c871701d 100644 --- a/docs/reference/units.md +++ b/docs/reference/units.md @@ -20,9 +20,10 @@ Temperature | kelvin (K), celsius (degC), fahrenheit (degF), rankine (de Amount of substance | mole (mol) Luminous intensity | candela (cd) Force | newton (N), dyne (dyn), poundforce (lbf) -Energy | J, erg, Wh, BTU -Power | W, hp +Energy | joule (J), erg, Wh, BTU, electronvolt (eV) +Power | watt (W), hp Pressure | Pa, psi, atm +Electricity and Magnetism | ampere (A), coulomb (C), watt (W), volt (V), ohm, farad (F), weber (Wb), tesla (T), henry (H), siemens (S), electronvolt (eV) Binary | bit (b), byte (B) Note that all relevant units can also be written in plural form, for example `5 meters` instead of `5 meter` or `10 seconds` instead of `10 second`. diff --git a/examples/units.js b/examples/units.js index 03cc9cae9..aa88e1913 100644 --- a/examples/units.js +++ b/examples/units.js @@ -83,3 +83,26 @@ console.log('h = '); print(h); console.log('v = (2 g h) ^ 0.5 ='); print(v); +console.log(); + +console.log('electrical power consumption:'); +print(math.eval('460 V * 20 A * 30 days to kWh')); // 6624 kWh +console.log(); + +console.log('circuit design:'); +print(math.eval('24 V / (6 mA)')); // 4 kΩ +console.log(); + +console.log('operations on arrays:'); +var B = math.eval('[1, 0, 0] T'); +var v = math.eval('[0, 1, 0] m/s'); +var q = math.eval('1 C'); +var F = math.multiply(q, math.cross(v, B)); +console.log('B (magnetic field strength) = '); +print(B); +console.log('v (particle velocity) = '); +print(v); +console.log('q (particle charge) = '); +print(q); +console.log('F (force) = q (v cross B) = '); +print(F); diff --git a/lib/type/unit/Unit.js b/lib/type/unit/Unit.js index 1fdf21312..10c53edbe 100644 --- a/lib/type/unit/Unit.js +++ b/lib/type/unit/Unit.js @@ -1969,6 +1969,13 @@ function factory (type, config, load, typed) { prefixes: PREFIXES.SHORT, value: 1, offset: 0 + }, + joule: { + name: 'joule', + base: BASE_UNITS.ENERGY, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 }, erg: { name: 'erg', @@ -2014,6 +2021,13 @@ function factory (type, config, load, typed) { prefixes: PREFIXES.SHORT, value: 1, offset: 0 + }, + watt: { + name: 'W', + base: BASE_UNITS.POWER, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 }, hp: { name: 'hp', @@ -2099,7 +2113,6 @@ function factory (type, config, load, typed) { value: 1, offset: 0 }, - /* Ω: { name: 'Ω', base: BASE_UNITS.ELECTRIC_RESISTANCE, @@ -2107,7 +2120,6 @@ function factory (type, config, load, typed) { value: 1, offset: 0 }, - */ // Electric inductance henry: { name: 'henry', @@ -2248,13 +2260,13 @@ function factory (type, config, load, typed) { gradients: 'grad', cycles: 'cycle', - joules: 'joule', - watts: 'watt', BTUs: 'BTU', + watts: 'watt', + joules: 'joule', amperes: 'ampere', coulombs: 'coulomb', - volts: 'volts', + volts: 'volt', ohms: 'ohm', farads: 'farad', webers: 'weber', @@ -2291,7 +2303,7 @@ function factory (type, config, load, typed) { ELECTRIC_CHARGE: {unit: UNITS.C, prefix: PREFIXES.SHORT['']}, ELECTRIC_CAPACITANCE: {unit: UNITS.F, prefix: PREFIXES.SHORT['']}, ELECTRIC_POTENTIAL: {unit: UNITS.V, prefix: PREFIXES.SHORT['']}, - ELECTRIC_RESISTANCE: {unit: UNITS.ohm, prefix: PREFIXES.SHORT['']}, + ELECTRIC_RESISTANCE: {unit: UNITS.Ω, prefix: PREFIXES.SHORT['']}, ELECTRIC_INDUCTANCE: {unit: UNITS.H, prefix: PREFIXES.SHORT['']}, ELECTRIC_CONDUCTANCE: {unit: UNITS.S, prefix: PREFIXES.SHORT['']}, MAGNETIC_FLUX: {unit: UNITS.Wb, prefix: PREFIXES.SHORT['']}, diff --git a/test/type/unit/Unit.test.js b/test/type/unit/Unit.test.js index d9dab90de..020dcf062 100644 --- a/test/type/unit/Unit.test.js +++ b/test/type/unit/Unit.test.js @@ -706,15 +706,16 @@ describe('unit', function() { describe('UNITS', function() { it('should be of the correct value and dimension', function() { - assert.equal(new Unit(1, 's A'), new Unit(1, 'C')); - assert.equal(new Unit(1, 'W/A'), new Unit(1, 'V')); - assert.equal(new Unit(1, 'V/A'), new Unit(1, 'ohm')); - assert.equal(new Unit(1, 'C/V'), new Unit(1, 'F')); - assert.equal(new Unit(1, 'J/A'), new Unit(1, 'Wb')); - assert.equal(new Unit(1, 'Wb/m^2'), new Unit(1, 'T')); - assert.equal(new Unit(1, 'Wb/A'), new Unit(1, 'H')); - assert.equal(new Unit(1, 'ohm^-1'), new Unit(1, 'S')); - assert.equal(new Unit(1, 'eV'), new Unit(1.602176565e-19, 'J')); + + assert.equal(new Unit(1, 's A') .equals(new Unit(1, 'C')) , true); + assert.equal(new Unit(1, 'W/A') .equals(new Unit(1, 'V')) , true); + assert.equal(new Unit(1, 'V/A') .equals(new Unit(1, 'ohm')), true); + assert.equal(new Unit(1, 'C/V') .equals(new Unit(1, 'F')) , true); + assert.equal(new Unit(1, 'J/A') .equals(new Unit(1, 'Wb')) , true); + assert.equal(new Unit(1, 'Wb/m^2').equals(new Unit(1, 'T')) , true); + assert.equal(new Unit(1, 'Wb/A') .equals(new Unit(1, 'H')) , true); + assert.equal(new Unit(1, 'ohm^-1').equals(new Unit(1, 'S')) , true); + assert.equal(new Unit(1, 'eV') .equals(new Unit(1.602176565e-19, 'J')), true); }); }); }); From 23044e29ca02338def0feafbc6542e389c0a8b2a Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 6 Aug 2015 13:47:01 +0000 Subject: [PATCH 3/3] Updated more docs Fixed tabs --- docs/datatypes/units.md | 15 +- examples/units.js | 10 +- lib/type/unit/Unit.js | 1036 +++++++++++++++++------------------ test/type/unit/Unit.test.js | 220 ++++---- 4 files changed, 646 insertions(+), 635 deletions(-) diff --git a/docs/datatypes/units.md b/docs/datatypes/units.md index 0094f14b0..32337679d 100644 --- a/docs/datatypes/units.md +++ b/docs/datatypes/units.md @@ -87,12 +87,23 @@ var c = math.unit(45, 'deg'); // Unit 45 deg math.cos(c); // Number 0.7071067811865476 // Kinetic energy of average sedan on highway -var d = math.unit('80 mi/h') // Unit 80 mi/h -var e = math.unit('2 tonne') // Unit 2 tonne +var d = math.unit('80 mi/h') // Unit 80 mi/h +var e = math.unit('2 tonne') // Unit 2 tonne var f = math.multiply(0.5, math.multipy(math.pow(d, 2), e)); // 1.2790064742399996 MJ ``` +Operations with arrays are supported too: + +```js +// Force on a charged particle moving through a magnetic field +var B = math.eval('[1, 0, 0] T'); // [1 T, 0 T, 0 T] +var v = math.eval('[0, 1, 0] m/s'); // [0 m / s, 1 m / s, 0 m / s] +var q = math.eval('1 C'); // 1 C + +var F = math.multiply(q, math.cross(v, B)); // [0 N, 0 N, -1 N] +``` + The expression parser supports units too. This is described in the section about units on the page [Syntax](../expressions/syntax.md#units). diff --git a/examples/units.js b/examples/units.js index aa88e1913..be2ae048a 100644 --- a/examples/units.js +++ b/examples/units.js @@ -26,9 +26,9 @@ console.log('perform operations'); print(math.add(a, b)); // 0.55 m print(math.multiply(b, 2)); // 200 mm print(math.divide(math.unit('1 m'), math.unit('1 s'))); - // 1 m / s + // 1 m / s print(math.pow(math.unit('12 in'), 3)); - // 1728 in^3 + // 1728 in^3 console.log(); // units can be converted to a specific type, or to a number @@ -53,7 +53,7 @@ console.log(); // simplify units console.log('simplify units'); -print(math.eval('100000 N / m^2')); // 100 kPa +print(math.eval('100000 N / m^2')); // 100 kPa print(math.eval('9.81 m/s^2 * 100 kg * 40 m')); // 39.24 kJ console.log(); @@ -86,11 +86,11 @@ print(v); console.log(); console.log('electrical power consumption:'); -print(math.eval('460 V * 20 A * 30 days to kWh')); // 6624 kWh +print(math.eval('460 V * 20 A * 30 days to kWh')); // 6624 kWh console.log(); console.log('circuit design:'); -print(math.eval('24 V / (6 mA)')); // 4 kΩ +print(math.eval('24 V / (6 mA)')); // 4 kΩ console.log(); console.log('operations on arrays:'); diff --git a/lib/type/unit/Unit.js b/lib/type/unit/Unit.js index 10c53edbe..d9372e8d3 100644 --- a/lib/type/unit/Unit.js +++ b/lib/type/unit/Unit.js @@ -36,9 +36,9 @@ function factory (type, config, load, typed) { } if (name != undefined) { - var u = Unit.parse(name); - this.units = u.units; - this.dimensions = u.dimensions; + var u = Unit.parse(name); + this.units = u.units; + this.dimensions = u.dimensions; } else { this.units = [ @@ -57,9 +57,9 @@ function factory (type, config, load, typed) { // best prefix but leave it as initially provided. // fixPrefix is set true by the method Unit.to - // The justification behind this is that if the constructor is explicitly called, - // the caller wishes the units to be returned exactly as he supplied. - this.isUnitListSimplified = true; + // The justification behind this is that if the constructor is explicitly called, + // the caller wishes the units to be returned exactly as he supplied. + this.isUnitListSimplified = true; } @@ -186,24 +186,24 @@ function factory (type, config, load, typed) { var code = text.charCodeAt(index); } - // Must begin with [a-zA-Z] + // Must begin with [a-zA-Z] var code = unitName.charCodeAt(0); if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) { - return unitName || null; - } - else { - return null; - } + return unitName || null; + } + else { + return null; + } } function parseCharacter(toFind) { if (c === toFind) { - next(); - return toFind; + next(); + return toFind; } else { - return null; + return null; } } @@ -245,7 +245,7 @@ function factory (type, config, load, typed) { var valueStr = parseNumber(); var value = null; if(valueStr) { - value = parseFloat(valueStr); + value = parseFloat(valueStr); } skipWhitespace(); // Whitespace is not required here @@ -253,80 +253,80 @@ function factory (type, config, load, typed) { var powerMultiplier = 1.0; var expectingDenominator = false; while (true) { - skipWhitespace(); - if(c) { - var uStr = parseUnit(); - if(uStr == null) { - // No more units. - throw new SyntaxError('Could not parse'); - } - } - else { - // End of input. - break; - } - var res = _findUnit(uStr); - if(res == null) { - // Unit not found. - //return null; - throw new SyntaxError('Unit "' + uStr + '" not found.'); - } - var power = 1.0 * powerMultiplier; - // Is there a "^ number"? - skipWhitespace(); - if (parseCharacter('^')) { - skipWhitespace(); - var p = parseNumber(); - if(p == null) { - // No valid number found for the power! - throw new SyntaxError('In "' + str + '", "^" must be followed by a floating-point number'); - } - power = p * powerMultiplier; - } - // Add the unit - unit.units.push( { - unit: res.unit, - prefix: res.prefix, - power: power - }); - for(var i=0; i 1 || Math.abs(this.units[0].power - 1.0) > 1e-15; - } + Unit.prototype._isDerived = function() { + if(this.units.length === 0) { + return false; + } + return this.units.length > 1 || Math.abs(this.units[0].power - 1.0) > 1e-15; + } /** * Normalize a value, based on its currently set unit(s) @@ -378,21 +378,21 @@ function factory (type, config, load, typed) { */ Unit.prototype._normalize = function (value) { if (this.units.length === 0) { - return value; + return value; } else if (this._isDerived()) { - // This is a derived unit, so do not apply offsets. - // For example, with J kg^-1 degC^-1 you would NOT want to apply the offset. - var res = value; - for(var i=0; i < this.units.length; i++) { - res = res * Math.pow(this.units[i].unit.value * this.units[i].prefix.value, this.units[i].power); - } - return res; - } - else { - // This is a single unit of power 1, like kg or degC - return (value + this.units[0].unit.offset) * this.units[0].unit.value * this.units[0].prefix.value; - } + // This is a derived unit, so do not apply offsets. + // For example, with J kg^-1 degC^-1 you would NOT want to apply the offset. + var res = value; + for(var i=0; i < this.units.length; i++) { + res = res * Math.pow(this.units[i].unit.value * this.units[i].prefix.value, this.units[i].power); + } + return res; + } + else { + // This is a single unit of power 1, like kg or degC + return (value + this.units[0].unit.offset) * this.units[0].unit.value * this.units[0].prefix.value; + } }; /** @@ -403,29 +403,29 @@ function factory (type, config, load, typed) { * @private */ Unit.prototype._denormalize = function (value, prefixValue) { - if (this.units.length === 0) { - return value; - } - else if (this._isDerived()) { - // This is a derived unit, so do not apply offsets. - // For example, with J kg^-1 degC^-1 you would NOT want to apply the offset. - // Also, prefixValue is ignored--but we will still use the prefix value stored in each unit, since kg is usually preferrable to g unless the user decides otherwise. - var res = value; - for(var i=0; i 1e-12) { - proposedUnitList.push({ - unit: currentUnitSystem[baseDim].unit, - prefix: currentUnitSystem[baseDim].prefix, - power: this.dimensions[i] - }); - } - } + else { + // Multiple units or units with powers are formatted like this: + // 5 (kg m^2) / (s^3 mol) + // Build an representation from the base units of the current unit system + for(var i=0; i 1e-12) { + proposedUnitList.push({ + unit: currentUnitSystem[baseDim].unit, + prefix: currentUnitSystem[baseDim].prefix, + power: this.dimensions[i] + }); + } + } - // Is the proposed unit list "simpler" than the existing one? - if(proposedUnitList.length < this.units.length) { - // Replace this unit list with the proposed list - this.units = proposedUnitList; - } - this.isUnitListSimplified = true; - return; - } + // Is the proposed unit list "simpler" than the existing one? + if(proposedUnitList.length < this.units.length) { + // Replace this unit list with the proposed list + this.units = proposedUnitList; + } + this.isUnitListSimplified = true; + return; + } } /** * Get a string representation of the units of this Unit, without the value. * @return {string} */ - Unit.prototype.formatUnits = function () { + Unit.prototype.formatUnits = function () { - // Lazy evaluation of the unit list - this.simplifyUnitListLazy(); + // Lazy evaluation of the unit list + this.simplifyUnitListLazy(); - var strNum = ""; - var strDen = ""; - var nNum = 0; - var nDen = 0; + var strNum = ""; + var strDen = ""; + var nNum = 0; + var nDen = 0; - for(var i=0; i 0) { - nNum++; - strNum += " " + this.units[i].prefix.name + this.units[i].unit.name; - if(Math.abs(this.units[i].power - 1.0) > 1e-15) { - strNum += "^" + this.units[i].power; - } - } - else if(this.units[i].power < 0) { - nDen++; - } - } + for(var i=0; i 0) { + nNum++; + strNum += " " + this.units[i].prefix.name + this.units[i].unit.name; + if(Math.abs(this.units[i].power - 1.0) > 1e-15) { + strNum += "^" + this.units[i].power; + } + } + else if(this.units[i].power < 0) { + nDen++; + } + } - if(nDen > 0) { - for(var i=0; i 0) { - strDen += " " + this.units[i].prefix.name + this.units[i].unit.name; - if(Math.abs(this.units[i].power + 1.0) > 1e-15) { - strDen += "^" + (-this.units[i].power); - } - } - else { - strDen += " " + this.units[i].prefix.name + this.units[i].unit.name; - strDen += "^" + (this.units[i].power); - } - } - } - } - // Remove leading " " - strNum = strNum.substr(1); - strDen = strDen.substr(1); + if(nDen > 0) { + for(var i=0; i 0) { + strDen += " " + this.units[i].prefix.name + this.units[i].unit.name; + if(Math.abs(this.units[i].power + 1.0) > 1e-15) { + strDen += "^" + (-this.units[i].power); + } + } + else { + strDen += " " + this.units[i].prefix.name + this.units[i].unit.name; + strDen += "^" + (this.units[i].power); + } + } + } + } + // Remove leading " " + strNum = strNum.substr(1); + strDen = strDen.substr(1); - // Add parans for better copy/paste back into the eval, for example, or for better pretty print formatting - if(nNum > 1 && nDen > 0) { - strNum = "(" + strNum + ")"; - } - if(nDen > 1 && nNum > 0) { - strDen = "(" + strDen + ")"; - } + // Add parans for better copy/paste back into the eval, for example, or for better pretty print formatting + if(nNum > 1 && nDen > 0) { + strNum = "(" + strNum + ")"; + } + if(nDen > 1 && nNum > 0) { + strDen = "(" + strDen + ")"; + } - var str = strNum; - if(nNum > 0 && nDen > 0) { - str += " / "; - } - str += strDen; + var str = strNum; + if(nNum > 0 && nDen > 0) { + str += " / "; + } + str += strDen; - return str; - }; + return str; + }; /** * Get a string representation of the Unit, with optional formatting options. @@ -847,52 +847,52 @@ function factory (type, config, load, typed) { * options. * @return {string} */ - Unit.prototype.format = function (options) { + Unit.prototype.format = function (options) { // Simplfy the unit list, if necessary - this.simplifyUnitListLazy(); + this.simplifyUnitListLazy(); - var value, - str; - if (this._isDerived()) { - value = this._denormalize(this.value); - str = (this.value !== null) ? (format(value, options)) : ''; - var unitStr = this.formatUnits(); - if(unitStr.length > 0 && str.length > 0) { - str += " "; - } - str += unitStr; - } - else if (this.units.length === 1) { - if (this.value !== null && !this.fixPrefix) { - var bestPrefix = this._bestPrefix(); - value = this._denormalize(this.value, bestPrefix.value); - str = format(value, options) + ' '; - str += bestPrefix.name + this.units[0].unit.name; - } - else { - value = this._denormalize(this.value); - str = (this.value !== null) ? (format(value, options) + ' ') : ''; - str += this.units[0].prefix.name + this.units[0].unit.name; - } - } - else if (this.units.length === 0) { - str = format(this.value, options); - } + var value, + str; + if (this._isDerived()) { + value = this._denormalize(this.value); + str = (this.value !== null) ? (format(value, options)) : ''; + var unitStr = this.formatUnits(); + if(unitStr.length > 0 && str.length > 0) { + str += " "; + } + str += unitStr; + } + else if (this.units.length === 1) { + if (this.value !== null && !this.fixPrefix) { + var bestPrefix = this._bestPrefix(); + value = this._denormalize(this.value, bestPrefix.value); + str = format(value, options) + ' '; + str += bestPrefix.name + this.units[0].unit.name; + } + else { + value = this._denormalize(this.value); + str = (this.value !== null) ? (format(value, options) + ' ') : ''; + str += this.units[0].prefix.name + this.units[0].unit.name; + } + } + else if (this.units.length === 0) { + str = format(this.value, options); + } - return str; - }; + return str; + }; /** * Calculate the best prefix using current value. * @returns {Object} prefix * @private */ - Unit.prototype._bestPrefix = function () { - if(this._isDerived()) { - throw "Can only compute the best prefix for non-derived units, like kg, s, N, and so forth!"; - } + Unit.prototype._bestPrefix = function () { + if(this._isDerived()) { + throw "Can only compute the best prefix for non-derived units, like kg, s, N, and so forth!"; + } // find the best prefix value (resulting in the value of which // the absolute value of the log10 is closest to zero, @@ -1137,30 +1137,30 @@ function factory (type, config, load, typed) { dimensions: [1, -1, -2, 0, 0, 0, 0, 0, 0] }, - ELECTRIC_CHARGE: { - dimensions: [0, 0, 1, 1, 0, 0, 0, 0, 0] - }, - ELECTRIC_CAPACITANCE: { - dimensions: [-1, -2, 4, 2, 0, 0, 0, 0, 0] - }, - ELECTRIC_POTENTIAL: { - dimensions: [1, 2, -3, -1, 0, 0, 0, 0, 0] - }, - ELECTRIC_RESISTANCE: { - dimensions: [1, 2, -3, -2, 0, 0, 0, 0, 0] - }, - ELECTRIC_INDUCTANCE: { - dimensions: [1, 2, -2, -2, 0, 0, 0, 0, 0] - }, - ELECTRIC_CONDUCTANCE: { - dimensions: [-1, -2, 3, 2, 0, 0, 0, 0, 0] - }, - MAGNETIC_FLUX: { - dimensions: [1, 2, -2, -1, 0, 0, 0, 0, 0] - }, - MAGNETIC_FLUX_DENSITY: { - dimensions: [1, 0, -2, -1, 0, 0, 0, 0, 0] - }, + ELECTRIC_CHARGE: { + dimensions: [0, 0, 1, 1, 0, 0, 0, 0, 0] + }, + ELECTRIC_CAPACITANCE: { + dimensions: [-1, -2, 4, 2, 0, 0, 0, 0, 0] + }, + ELECTRIC_POTENTIAL: { + dimensions: [1, 2, -3, -1, 0, 0, 0, 0, 0] + }, + ELECTRIC_RESISTANCE: { + dimensions: [1, 2, -3, -2, 0, 0, 0, 0, 0] + }, + ELECTRIC_INDUCTANCE: { + dimensions: [1, 2, -2, -2, 0, 0, 0, 0, 0] + }, + ELECTRIC_CONDUCTANCE: { + dimensions: [-1, -2, 3, 2, 0, 0, 0, 0, 0] + }, + MAGNETIC_FLUX: { + dimensions: [1, 2, -2, -1, 0, 0, 0, 0, 0] + }, + MAGNETIC_FLUX_DENSITY: { + dimensions: [1, 0, -2, -1, 0, 0, 0, 0, 0] + }, ANGLE: { @@ -1172,7 +1172,7 @@ function factory (type, config, load, typed) { }; for(var key in BASE_UNITS) { - BASE_UNITS[key].key = key; + BASE_UNITS[key].key = key; } var BASE_UNIT_NONE = {}; @@ -1970,7 +1970,7 @@ function factory (type, config, load, typed) { value: 1, offset: 0 }, - joule: { + joule: { name: 'joule', base: BASE_UNITS.ENERGY, prefixes: PREFIXES.SHORT, @@ -1998,15 +1998,15 @@ function factory (type, config, load, typed) { value: 1055.05585262, offset: 0 }, - eV: { - name: 'eV', + eV: { + name: 'eV', base: BASE_UNITS.ENERGY, prefixes: PREFIXES.SHORT, value: 1.602176565e-19, offset: 0 }, - electronvolt: { - name: 'electronvolt', + electronvolt: { + name: 'electronvolt', base: BASE_UNITS.ENERGY, prefixes: PREFIXES.LONG, value: 1.602176565e-19, @@ -2022,7 +2022,7 @@ function factory (type, config, load, typed) { value: 1, offset: 0 }, - watt: { + watt: { name: 'W', base: BASE_UNITS.POWER, prefixes: PREFIXES.LONG, @@ -2052,134 +2052,134 @@ function factory (type, config, load, typed) { value: 6894.75729276459, offset: 0 }, - atm: { - name: 'atm', - base: BASE_UNITS.PRESSURE, - prefixes: PREFIXES.NONE, - value: 101325, - offset: 0 - }, + atm: { + name: 'atm', + base: BASE_UNITS.PRESSURE, + prefixes: PREFIXES.NONE, + value: 101325, + offset: 0 + }, - // Electric charge - coulomb: { - name: 'coulomb', - base: BASE_UNITS.ELECTRIC_CHARGE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0, - }, - C: { - name: 'C', - base: BASE_UNITS.ELECTRIC_CHARGE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Electric capacitance - farad: { - name: 'farad', - base: BASE_UNITS.ELECTRIC_CAPACITANCE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - F: { - name: 'F', - base: BASE_UNITS.ELECTRIC_CAPACITANCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Electric potential - volt: { - name: 'volt', - base: BASE_UNITS.ELECTRIC_POTENTIAL, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - V: { - name: 'V', - base: BASE_UNITS.ELECTRIC_POTENTIAL, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Electric resistance - ohm: { - name: 'ohm', - base: BASE_UNITS.ELECTRIC_RESISTANCE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - Ω: { - name: 'Ω', - base: BASE_UNITS.ELECTRIC_RESISTANCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Electric inductance - henry: { - name: 'henry', - base: BASE_UNITS.ELECTRIC_INDUCTANCE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - H: { - name: 'H', - base: BASE_UNITS.ELECTRIC_INDUCTANCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Electric conductance - siemens: { - name: 'siemens', - base: BASE_UNITS.ELECTRIC_CONDUCTANCE, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - S: { - name: 'S', - base: BASE_UNITS.ELECTRIC_CONDUCTANCE, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Magnetic flux - weber: { - name: 'weber', - base: BASE_UNITS.MAGNETIC_FLUX, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - Wb: { - name: 'Wb', - base: BASE_UNITS.MAGNETIC_FLUX, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, - // Magnetic flux density - tesla: { - name: 'tesla', - base: BASE_UNITS.MAGNETIC_FLUX_DENSITY, - prefixes: PREFIXES.LONG, - value: 1, - offset: 0 - }, - T: { - name: 'T', - base: BASE_UNITS.MAGNETIC_FLUX_DENSITY, - prefixes: PREFIXES.SHORT, - value: 1, - offset: 0 - }, + // Electric charge + coulomb: { + name: 'coulomb', + base: BASE_UNITS.ELECTRIC_CHARGE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0, + }, + C: { + name: 'C', + base: BASE_UNITS.ELECTRIC_CHARGE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Electric capacitance + farad: { + name: 'farad', + base: BASE_UNITS.ELECTRIC_CAPACITANCE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + F: { + name: 'F', + base: BASE_UNITS.ELECTRIC_CAPACITANCE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Electric potential + volt: { + name: 'volt', + base: BASE_UNITS.ELECTRIC_POTENTIAL, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + V: { + name: 'V', + base: BASE_UNITS.ELECTRIC_POTENTIAL, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Electric resistance + ohm: { + name: 'ohm', + base: BASE_UNITS.ELECTRIC_RESISTANCE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + Ω: { + name: 'Ω', + base: BASE_UNITS.ELECTRIC_RESISTANCE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Electric inductance + henry: { + name: 'henry', + base: BASE_UNITS.ELECTRIC_INDUCTANCE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + H: { + name: 'H', + base: BASE_UNITS.ELECTRIC_INDUCTANCE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Electric conductance + siemens: { + name: 'siemens', + base: BASE_UNITS.ELECTRIC_CONDUCTANCE, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + S: { + name: 'S', + base: BASE_UNITS.ELECTRIC_CONDUCTANCE, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Magnetic flux + weber: { + name: 'weber', + base: BASE_UNITS.MAGNETIC_FLUX, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + Wb: { + name: 'Wb', + base: BASE_UNITS.MAGNETIC_FLUX, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, + // Magnetic flux density + tesla: { + name: 'tesla', + base: BASE_UNITS.MAGNETIC_FLUX_DENSITY, + prefixes: PREFIXES.LONG, + value: 1, + offset: 0 + }, + T: { + name: 'T', + base: BASE_UNITS.MAGNETIC_FLUX_DENSITY, + prefixes: PREFIXES.SHORT, + value: 1, + offset: 0 + }, // Binary b: { @@ -2260,19 +2260,19 @@ function factory (type, config, load, typed) { gradients: 'grad', cycles: 'cycle', - BTUs: 'BTU', - watts: 'watt', - joules: 'joule', + BTUs: 'BTU', + watts: 'watt', + joules: 'joule', amperes: 'ampere', coulombs: 'coulomb', - volts: 'volt', - ohms: 'ohm', - farads: 'farad', - webers: 'weber', - teslas: 'tesla', - electronvolts: 'electronvolt', - moles: 'mole' + volts: 'volt', + ohms: 'ohm', + farads: 'farad', + webers: 'weber', + teslas: 'tesla', + electronvolts: 'electronvolt', + moles: 'mole' }; @@ -2283,31 +2283,31 @@ function factory (type, config, load, typed) { */ var UNIT_SYSTEMS = { si: { - // Base units - NONE: {unit: UNIT_NONE, prefix: PREFIXES.NONE['']}, - LENGTH: {unit: UNITS.m, prefix: PREFIXES.SHORT['']}, - MASS: {unit: UNITS.g, prefix: PREFIXES.SHORT['k']}, - TIME: {unit: UNITS.s, prefix: PREFIXES.SHORT['']}, - CURRENT: {unit: UNITS.A, prefix: PREFIXES.SHORT['']}, - TEMPERATURE: {unit: UNITS.K, prefix: PREFIXES.SHORT['']}, - LUMINOUS_INTENSITY: {unit: UNITS.cd, prefix: PREFIXES.SHORT['']}, - AMOUNT_OF_SUBSTANCE: {unit: UNITS.mol, prefix: PREFIXES.SHORT['']}, - ANGLE: {unit: UNITS.rad, prefix: PREFIXES.SHORT['']}, - BIT: {unit: UNITS.bit, prefix: PREFIXES.SHORT['']}, + // Base units + NONE: {unit: UNIT_NONE, prefix: PREFIXES.NONE['']}, + LENGTH: {unit: UNITS.m, prefix: PREFIXES.SHORT['']}, + MASS: {unit: UNITS.g, prefix: PREFIXES.SHORT['k']}, + TIME: {unit: UNITS.s, prefix: PREFIXES.SHORT['']}, + CURRENT: {unit: UNITS.A, prefix: PREFIXES.SHORT['']}, + TEMPERATURE: {unit: UNITS.K, prefix: PREFIXES.SHORT['']}, + LUMINOUS_INTENSITY: {unit: UNITS.cd, prefix: PREFIXES.SHORT['']}, + AMOUNT_OF_SUBSTANCE: {unit: UNITS.mol, prefix: PREFIXES.SHORT['']}, + ANGLE: {unit: UNITS.rad, prefix: PREFIXES.SHORT['']}, + BIT: {unit: UNITS.bit, prefix: PREFIXES.SHORT['']}, - // Derived units - FORCE: {unit: UNITS.N, prefix: PREFIXES.SHORT['']}, - ENERGY: {unit: UNITS.J, prefix: PREFIXES.SHORT['']}, - POWER: {unit: UNITS.W, prefix: PREFIXES.SHORT['']}, - PRESSURE: {unit: UNITS.Pa, prefix: PREFIXES.SHORT['']}, - ELECTRIC_CHARGE: {unit: UNITS.C, prefix: PREFIXES.SHORT['']}, - ELECTRIC_CAPACITANCE: {unit: UNITS.F, prefix: PREFIXES.SHORT['']}, - ELECTRIC_POTENTIAL: {unit: UNITS.V, prefix: PREFIXES.SHORT['']}, - ELECTRIC_RESISTANCE: {unit: UNITS.Ω, prefix: PREFIXES.SHORT['']}, - ELECTRIC_INDUCTANCE: {unit: UNITS.H, prefix: PREFIXES.SHORT['']}, - ELECTRIC_CONDUCTANCE: {unit: UNITS.S, prefix: PREFIXES.SHORT['']}, - MAGNETIC_FLUX: {unit: UNITS.Wb, prefix: PREFIXES.SHORT['']}, - MAGNETIC_FLUX_DENSITY: {unit: UNITS.T, prefix: PREFIXES.SHORT['']} + // Derived units + FORCE: {unit: UNITS.N, prefix: PREFIXES.SHORT['']}, + ENERGY: {unit: UNITS.J, prefix: PREFIXES.SHORT['']}, + POWER: {unit: UNITS.W, prefix: PREFIXES.SHORT['']}, + PRESSURE: {unit: UNITS.Pa, prefix: PREFIXES.SHORT['']}, + ELECTRIC_CHARGE: {unit: UNITS.C, prefix: PREFIXES.SHORT['']}, + ELECTRIC_CAPACITANCE: {unit: UNITS.F, prefix: PREFIXES.SHORT['']}, + ELECTRIC_POTENTIAL: {unit: UNITS.V, prefix: PREFIXES.SHORT['']}, + ELECTRIC_RESISTANCE: {unit: UNITS.Ω, prefix: PREFIXES.SHORT['']}, + ELECTRIC_INDUCTANCE: {unit: UNITS.H, prefix: PREFIXES.SHORT['']}, + ELECTRIC_CONDUCTANCE: {unit: UNITS.S, prefix: PREFIXES.SHORT['']}, + MAGNETIC_FLUX: {unit: UNITS.Wb, prefix: PREFIXES.SHORT['']}, + MAGNETIC_FLUX_DENSITY: {unit: UNITS.T, prefix: PREFIXES.SHORT['']} } }; @@ -2317,8 +2317,8 @@ function factory (type, config, load, typed) { UNIT_SYSTEMS.cgs.MASS = {unit: UNITS.g, prefix: PREFIXES.SHORT['']}; UNIT_SYSTEMS.cgs.FORCE = {unit: UNITS.dyn, prefix: PREFIXES.SHORT['']}; UNIT_SYSTEMS.cgs.ENERGY = {unit: UNITS.erg, prefix: PREFIXES.NONE['']}; - // there are wholly 4 unique cgs systems for electricity and magnetism, - // so let's not worry about it unless somebody complains + // there are wholly 4 unique cgs systems for electricity and magnetism, + // so let's not worry about it unless somebody complains UNIT_SYSTEMS.us = JSON.parse(JSON.stringify(UNIT_SYSTEMS.si)); UNIT_SYSTEMS.us.LENGTH = {unit: UNITS.ft, prefix: PREFIXES.NONE['']}; @@ -2343,14 +2343,14 @@ function factory (type, config, load, typed) { * Set a unit system for formatting derived units. * @param {string} [name] The name of the unit system. */ - Unit.setUnitSystem = function(name) { - if(UNIT_SYSTEMS.hasOwnProperty(name)) { - currentUnitSystem = UNIT_SYSTEMS[name]; - } - else { - var mess = "Unit system " + name + " does not exist. Choices are: " + listAvailableUnitSystems(); - } - } + Unit.setUnitSystem = function(name) { + if(UNIT_SYSTEMS.hasOwnProperty(name)) { + currentUnitSystem = UNIT_SYSTEMS[name]; + } + else { + var mess = "Unit system " + name + " does not exist. Choices are: " + listAvailableUnitSystems(); + } + } /** * Return a list of the available unit systems. diff --git a/test/type/unit/Unit.test.js b/test/type/unit/Unit.test.js index 020dcf062..242aad4de 100644 --- a/test/type/unit/Unit.test.js +++ b/test/type/unit/Unit.test.js @@ -60,15 +60,15 @@ describe('unit', function() { assert.throws(function () { new Unit(0, 3); }); }); - it('should flag unit as already simplified', function() { + it('should flag unit as already simplified', function() { unit1 = new Unit(9.81, "kg m/s^2"); - assert.equal(unit1.isUnitListSimplified, true); - assert.equal(unit1.toString(), "9.81 (kg m) / s^2"); + assert.equal(unit1.isUnitListSimplified, true); + assert.equal(unit1.toString(), "9.81 (kg m) / s^2"); unit1 = new Unit(null, "kg m/s^2"); - assert.equal(unit1.isUnitListSimplified, true); - assert.equal(unit1.toString(), "(kg m) / s^2"); - }); + assert.equal(unit1.isUnitListSimplified, true); + assert.equal(unit1.toString(), "(kg m) / s^2"); + }); }); @@ -370,56 +370,56 @@ describe('unit', function() { }); describe('simplifyUnitListLazy', function() { - it('should not simplify units created with new Unit()', function() { - var unit1 = new Unit(10, "kg m/s^2"); - assert.equal(unit1.units[0].unit.name, "g"); - assert.equal(unit1.units[1].unit.name, "m"); - assert.equal(unit1.units[2].unit.name, "s"); - assert.equal(unit1.toString(), "10 (kg m) / s^2"); - }); + it('should not simplify units created with new Unit()', function() { + var unit1 = new Unit(10, "kg m/s^2"); + assert.equal(unit1.units[0].unit.name, "g"); + assert.equal(unit1.units[1].unit.name, "m"); + assert.equal(unit1.units[2].unit.name, "s"); + assert.equal(unit1.toString(), "10 (kg m) / s^2"); + }); - it('should only simplify units with values', function() { - var unit1 = new Unit(null, "kg m mol / s^2 mol"); - unit1.isUnitListSimplified = false; - unit1.simplifyUnitListLazy(); - assert.equal(unit1.toString(), "(kg m mol) / (s^2 mol)"); - unit1 = math.multiply(unit1, 1); - assert.equal(unit1.toString(), "1 N"); - }); + it('should only simplify units with values', function() { + var unit1 = new Unit(null, "kg m mol / s^2 mol"); + unit1.isUnitListSimplified = false; + unit1.simplifyUnitListLazy(); + assert.equal(unit1.toString(), "(kg m mol) / (s^2 mol)"); + unit1 = math.multiply(unit1, 1); + assert.equal(unit1.toString(), "1 N"); + }); - it('should simplify units resulting from multiply/divide/power functions only when formatting for output', function() { - var unit1 = new Unit(2, "kg"); - var unit2 = new Unit(5, "m/s^2"); - var unit3 = math.multiply(unit1, unit2); - assert.equal(unit3.units[0].unit.name, "g"); - assert.equal(unit3.units[1].unit.name, "m"); - assert.equal(unit3.units[2].unit.name, "s"); - assert.equal(unit3.toString(), "10 N"); // Triggers simplification - assert.equal(unit3.units[0].unit.name, "N"); + it('should simplify units resulting from multiply/divide/power functions only when formatting for output', function() { + var unit1 = new Unit(2, "kg"); + var unit2 = new Unit(5, "m/s^2"); + var unit3 = math.multiply(unit1, unit2); + assert.equal(unit3.units[0].unit.name, "g"); + assert.equal(unit3.units[1].unit.name, "m"); + assert.equal(unit3.units[2].unit.name, "s"); + assert.equal(unit3.toString(), "10 N"); // Triggers simplification + assert.equal(unit3.units[0].unit.name, "N"); - }); + }); - it('should simplify units according to chosen unit system', function() { - var unit1 = new Unit(10, "N"); - Unit.setUnitSystem('us'); - unit1.isUnitListSimplified = false; - assert.equal(unit1.toString(), "2.248089430997105 lbf"); - assert.equal(unit1.units[0].unit.name, "lbf"); + it('should simplify units according to chosen unit system', function() { + var unit1 = new Unit(10, "N"); + Unit.setUnitSystem('us'); + unit1.isUnitListSimplified = false; + assert.equal(unit1.toString(), "2.248089430997105 lbf"); + assert.equal(unit1.units[0].unit.name, "lbf"); - Unit.setUnitSystem('cgs'); - unit1.isUnitListSimplified = false; - assert.equal(unit1.format(2), "1 Mdyn"); - assert.equal(unit1.units[0].unit.name, "dyn"); - }); + Unit.setUnitSystem('cgs'); + unit1.isUnitListSimplified = false; + assert.equal(unit1.format(2), "1 Mdyn"); + assert.equal(unit1.units[0].unit.name, "dyn"); + }); - it('should correctly simplify units when unit system is "auto"', function() { - Unit.setUnitSystem('auto'); - var unit1 = new Unit(5, "lbf min / s"); - unit1.isUnitListSimplified = false; - assert.equal(unit1.toString(), "300 lbf"); - }); + it('should correctly simplify units when unit system is "auto"', function() { + Unit.setUnitSystem('auto'); + var unit1 = new Unit(5, "lbf min / s"); + unit1.isUnitListSimplified = false; + assert.equal(unit1.toString(), "300 lbf"); + }); - + }); describe('valueOf', function() { @@ -582,13 +582,13 @@ describe('unit', function() { assert.equal(unit1.units[4].power, -1); assert.equal(unit1.units[0].prefix.name, 'k'); - unit1 = Unit.parse('5exabytes'); - approx.equal(unit1.value, 4e19); - assert.equal(unit1.units[0].unit.name, 'bytes'); + unit1 = Unit.parse('5exabytes'); + approx.equal(unit1.value, 4e19); + assert.equal(unit1.units[0].unit.name, 'bytes'); }); it('should return null (update: throw exception --ericman314) when parsing an invalid unit', function() { - // I'm worried something else will break if Unit.parse throws an exception instead of returning null???? --ericman314 + // I'm worried something else will break if Unit.parse throws an exception instead of returning null???? --ericman314 assert.throws(function () {Unit.parse('.meter')}, /Could not parse/); assert.throws(function () {Unit.parse('5e')}, /Unit "e" not found/); assert.throws(function () {Unit.parse('5e.')}, /Unit "e" not found/); @@ -616,52 +616,52 @@ describe('unit', function() { }); }); - describe('_isDerived', function() { - it('should return the correct value', function () { - assert.equal(Unit.parse('34 kg')._isDerived(), false); - assert.equal(Unit.parse('34 kg/s')._isDerived(), true); - assert.equal(Unit.parse('34 kg^2')._isDerived(), true); - assert.equal(Unit.parse('34 N')._isDerived(), false); - assert.equal(Unit.parse('34 kg m / s^2')._isDerived(), true); - var unit1 = Unit.parse('34 kg m / s^2'); - assert.equal(unit1._isDerived(), true); - unit1.isUnitListSimplified = false; - unit1.simplifyUnitListLazy(); - assert.equal(unit1._isDerived(), false); - }); - }); - - describe('multiply, divide, and pow', function() { - it('should flag the unit as requiring simplification', function() { - var unit1 = new Unit(10, 'kg'); - var unit2 = new Unit(9.81, 'm/s^2'); - assert.equal(unit1.multiply(unit2).isUnitListSimplified, false); - assert.equal(unit1.divide(unit2).isUnitListSimplified, false); - assert.equal(unit1.pow(2).isUnitListSimplified, false); - }); + describe('_isDerived', function() { + it('should return the correct value', function () { + assert.equal(Unit.parse('34 kg')._isDerived(), false); + assert.equal(Unit.parse('34 kg/s')._isDerived(), true); + assert.equal(Unit.parse('34 kg^2')._isDerived(), true); + assert.equal(Unit.parse('34 N')._isDerived(), false); + assert.equal(Unit.parse('34 kg m / s^2')._isDerived(), true); + var unit1 = Unit.parse('34 kg m / s^2'); + assert.equal(unit1._isDerived(), true); + unit1.isUnitListSimplified = false; + unit1.simplifyUnitListLazy(); + assert.equal(unit1._isDerived(), false); + }); + }); + + describe('multiply, divide, and pow', function() { + it('should flag the unit as requiring simplification', function() { + var unit1 = new Unit(10, 'kg'); + var unit2 = new Unit(9.81, 'm/s^2'); + assert.equal(unit1.multiply(unit2).isUnitListSimplified, false); + assert.equal(unit1.divide(unit2).isUnitListSimplified, false); + assert.equal(unit1.pow(2).isUnitListSimplified, false); + }); - it('should retain the units of their operands without simplifying', function() { - var unit1 = new Unit(10, "N/s"); - var unit2 = new Unit(10, "h"); - var unitM = unit1.multiply(unit2); - assert.equal(unitM.units[0].unit.name, 'N'); - assert.equal(unitM.units[1].unit.name, 's'); - assert.equal(unitM.units[2].unit.name, 'h'); + it('should retain the units of their operands without simplifying', function() { + var unit1 = new Unit(10, "N/s"); + var unit2 = new Unit(10, "h"); + var unitM = unit1.multiply(unit2); + assert.equal(unitM.units[0].unit.name, 'N'); + assert.equal(unitM.units[1].unit.name, 's'); + assert.equal(unitM.units[2].unit.name, 'h'); - var unit3 = new Unit(14.7, "lbf"); - var unit4 = new Unit(1, "in in"); - var unitD = unit3.divide(unit4); - assert.equal(unitD.units[0].unit.name, 'lbf'); - assert.equal(unitD.units[1].unit.name, 'in'); - assert.equal(unitD.units[2].unit.name, 'in'); + var unit3 = new Unit(14.7, "lbf"); + var unit4 = new Unit(1, "in in"); + var unitD = unit3.divide(unit4); + assert.equal(unitD.units[0].unit.name, 'lbf'); + assert.equal(unitD.units[1].unit.name, 'in'); + assert.equal(unitD.units[2].unit.name, 'in'); - var unit5 = new Unit(1, "N h/s"); - var unitP = unit5.pow(-3.5); - assert.equal(unitP.units[0].unit.name, 'N'); - assert.equal(unitP.units[1].unit.name, 'h'); - assert.equal(unitP.units[2].unit.name, 's'); - }); - }); + var unit5 = new Unit(1, "N h/s"); + var unitP = unit5.pow(-3.5); + assert.equal(unitP.units[0].unit.name, 'N'); + assert.equal(unitP.units[1].unit.name, 'h'); + assert.equal(unitP.units[2].unit.name, 's'); + }); + }); describe('plurals', function() { it('should support plurals', function () { @@ -704,18 +704,18 @@ describe('unit', function() { }); }); - describe('UNITS', function() { - it('should be of the correct value and dimension', function() { - - assert.equal(new Unit(1, 's A') .equals(new Unit(1, 'C')) , true); - assert.equal(new Unit(1, 'W/A') .equals(new Unit(1, 'V')) , true); - assert.equal(new Unit(1, 'V/A') .equals(new Unit(1, 'ohm')), true); - assert.equal(new Unit(1, 'C/V') .equals(new Unit(1, 'F')) , true); - assert.equal(new Unit(1, 'J/A') .equals(new Unit(1, 'Wb')) , true); - assert.equal(new Unit(1, 'Wb/m^2').equals(new Unit(1, 'T')) , true); - assert.equal(new Unit(1, 'Wb/A') .equals(new Unit(1, 'H')) , true); - assert.equal(new Unit(1, 'ohm^-1').equals(new Unit(1, 'S')) , true); - assert.equal(new Unit(1, 'eV') .equals(new Unit(1.602176565e-19, 'J')), true); - }); - }); + describe('UNITS', function() { + it('should be of the correct value and dimension', function() { + + assert.equal(new Unit(1, 's A') .equals(new Unit(1, 'C')) , true); + assert.equal(new Unit(1, 'W/A') .equals(new Unit(1, 'V')) , true); + assert.equal(new Unit(1, 'V/A') .equals(new Unit(1, 'ohm')), true); + assert.equal(new Unit(1, 'C/V') .equals(new Unit(1, 'F')) , true); + assert.equal(new Unit(1, 'J/A') .equals(new Unit(1, 'Wb')) , true); + assert.equal(new Unit(1, 'Wb/m^2').equals(new Unit(1, 'T')) , true); + assert.equal(new Unit(1, 'Wb/A') .equals(new Unit(1, 'H')) , true); + assert.equal(new Unit(1, 'ohm^-1').equals(new Unit(1, 'S')) , true); + assert.equal(new Unit(1, 'eV') .equals(new Unit(1.602176565e-19, 'J')), true); + }); + }); });