@lhncbc/ucum-lhc 6.0.2 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -28
- package/browser-dist/ucum-lhc.min.js +2 -0
- package/browser-dist/ucum-lhc.min.js.map +1 -0
- package/package.json +2 -5
- package/source/ucumLhcUtils.js +77 -55
- package/source/unit.js +215 -162
- package/source/unitString.js +2 -0
- package/source-cjs/ucumLhcUtils.js +66 -42
- package/source-cjs/ucumLhcUtils.js.map +1 -1
- package/source-cjs/unit.js +204 -160
- package/source-cjs/unit.js.map +1 -1
- package/source-cjs/unitString.js +2 -0
- package/source-cjs/unitString.js.map +1 -1
- package/browser-dist/ucum-lhc.js +0 -5234
package/source-cjs/unit.js
CHANGED
|
@@ -351,7 +351,7 @@ class Unit {
|
|
|
351
351
|
// reject request if both units have dimensions that are not equal
|
|
352
352
|
if (fromUnit.dim_ && this.dim_ && !fromUnit.dim_.equals(this.dim_)) {
|
|
353
353
|
// check first to see if a mole<->mass conversion is appropriate
|
|
354
|
-
if (this.
|
|
354
|
+
if (this.isMolMassCommensurable(fromUnit)) {
|
|
355
355
|
throw new Error(Ucum.needMoleWeightMsg_);
|
|
356
356
|
} else {
|
|
357
357
|
throw new Error(`Sorry. ${fromUnit.csCode_} cannot be converted ` + `to ${this.csCode_}.`);
|
|
@@ -458,116 +458,118 @@ class Unit {
|
|
|
458
458
|
} // end mutateCoherent
|
|
459
459
|
|
|
460
460
|
/**
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
|
|
466
|
-
* mass must be convertible. No validation of this requirement is performed.
|
|
467
|
-
*
|
|
468
|
-
* @param amt the quantity of this unit to be converted
|
|
469
|
-
* @param molUnit the target/to unit for which the converted # is wanted
|
|
470
|
-
* @param molecularWeight the molecular weight of the substance for which the
|
|
471
|
-
* conversion is being made
|
|
472
|
-
* @return the equivalent amount in molUnit
|
|
473
|
-
*/
|
|
474
|
-
convertMassToMol(amt, molUnit, molecularWeight) {
|
|
475
|
-
// The prefix values that have been applied to this unit, which is the mass
|
|
476
|
-
// (grams) unit, are reflected in the magnitude. So the number of moles
|
|
477
|
-
// represented by this unit equals the number of grams -- amount * magnitude
|
|
478
|
-
// divided by the molecular Weight
|
|
479
|
-
let molAmt = this.magnitude_ * amt / molecularWeight;
|
|
480
|
-
// The molUnit's basic magnitude, before prefixes are applied,
|
|
481
|
-
// is avogadro's number, get that and divide it out of the current magnitude.
|
|
482
|
-
let tabs = this._getUnitTables();
|
|
483
|
-
let avoNum = tabs.getUnitByCode('mol').magnitude_;
|
|
484
|
-
let molesFactor = molUnit.magnitude_ / avoNum;
|
|
485
|
-
// return the molAmt divided by the molesFactor as the number of moles
|
|
486
|
-
// for the molUnit
|
|
487
|
-
return molAmt / molesFactor;
|
|
488
|
-
} // end convertMassToMol
|
|
489
|
-
|
|
490
|
-
/**
|
|
491
|
-
* Calculates the number of units that would result from converting a unit
|
|
492
|
-
* expressed in moles to a unit expressed in mass (grams). The "this" unit
|
|
493
|
-
* is the unit expressed in some form of moles, e.g., mol, umol, mmol, etc.,
|
|
494
|
-
* and the target or "to" unit is a unit expressed in some form of mass, e.g.,
|
|
495
|
-
* g, mg, mmg, kg, etc. Any unit expressions surrounding the moles and mass
|
|
496
|
-
* must be convertible. No validation of this requirement is performed.
|
|
497
|
-
*
|
|
461
|
+
* This function converts between mol and mass (in either direction)
|
|
462
|
+
* using the molecular weight of the substance. It assumes that the
|
|
463
|
+
* isMolMassCommensurable" function has been called to check that the units are
|
|
464
|
+
* commensurable.
|
|
465
|
+
*
|
|
498
466
|
* @param amt the quantity of this unit to be converted
|
|
499
|
-
* @param
|
|
467
|
+
* @param toUnit the target/to unit for which the converted # is wanted
|
|
500
468
|
* @param molecularWeight the molecular weight of the substance for which the
|
|
501
469
|
* conversion is being made
|
|
502
|
-
* @return the equivalent amount in
|
|
470
|
+
* @return the equivalent amount in toUnit
|
|
503
471
|
*/
|
|
504
|
-
|
|
472
|
+
convertMolMass(amt, toUnit, molecularWeight) {
|
|
473
|
+
// In the calculations below we are treating "molecularWeight" (measured in
|
|
474
|
+
// a.m.u) as the molar weight (measured in g/mol). The values are the same,
|
|
475
|
+
// though the units differ.
|
|
476
|
+
|
|
477
|
+
// Determine the number of powers of mol we have to convert to mass.
|
|
478
|
+
// Typically this will be just 1, or -1, but not necessarily. If it is a
|
|
479
|
+
// negative number, then we are really converting mass to moles.
|
|
480
|
+
const molPowersToConvert = this.moleExp_ - toUnit.moleExp_;
|
|
505
481
|
// A simple mole unit has a magnitude of avogadro's number. Get that
|
|
506
482
|
// number now (since not everyone agrees on what it is, and what is
|
|
507
483
|
// being used in this system might change).
|
|
508
484
|
let tabs = this._getUnitTables();
|
|
509
485
|
let avoNum = tabs.getUnitByCode('mol').magnitude_;
|
|
510
|
-
//
|
|
511
|
-
//
|
|
512
|
-
//
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
//
|
|
516
|
-
//
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
// Finally, we return the mass amount/grams for this particular unit
|
|
520
|
-
// divided by any effects of prefixes applied to the "to" unit, which
|
|
521
|
-
// is assumed to be some form of a gram unit
|
|
522
|
-
return massAmt / massUnit.magnitude_;
|
|
523
|
-
} // end convertMolToMass
|
|
524
|
-
|
|
525
|
-
/**
|
|
526
|
-
* Converts equivalents to mass.
|
|
527
|
-
*
|
|
528
|
-
* @param {number} equivalents - The amount in equivalents to be converted.
|
|
529
|
-
* @param {object} targetUnit - The target/to unit for which the converted number is wanted.
|
|
530
|
-
* @param {number} molecularWeight - The molecular weight of the substance for which the conversion is being made.
|
|
531
|
-
* @param {number} charge - The absolute value of the charge of the substance for which the conversion is being made.
|
|
532
|
-
* @returns {number} - The equivalent mass in the specified mass unit.
|
|
533
|
-
*/
|
|
534
|
-
convertEqToMass(equivalents, targetUnit, molecularWeight, charge) {
|
|
535
|
-
let standardMoleUnit = this._getUnitTables().getUnitByCode('mol');
|
|
536
|
-
const molAmount = this.convertEqToMol(equivalents, standardMoleUnit, charge);
|
|
537
|
-
return this.convertMolToMass(molAmount, targetUnit, molecularWeight);
|
|
538
|
-
} // end convertEqToMass
|
|
486
|
+
// For each molPowersToConvert, we need to multiply the mol unit by the
|
|
487
|
+
// molar weight (g/mol) and divide by avoNum (1/mol) to get a weight per
|
|
488
|
+
// molecule. (Note that the magnitude_ of each unit will contain factors of
|
|
489
|
+
// avoNum, of which we are thus getting rid of some).
|
|
490
|
+
let moleUnitFactor = Math.pow(molecularWeight / avoNum, molPowersToConvert);
|
|
491
|
+
// The new value is proportional to this.magnitude_, amt, and
|
|
492
|
+
// moleUnitFactor, and inversely proportional to toUnit_.magnitude.
|
|
493
|
+
return this.magnitude_ / toUnit.magnitude_ * moleUnitFactor * amt;
|
|
494
|
+
} // end convertMolMass
|
|
539
495
|
|
|
540
496
|
/**
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
*
|
|
544
|
-
*
|
|
497
|
+
* This function converts between equivalants and mass (in either direction)
|
|
498
|
+
* using the charge of the substance. It assumes that the
|
|
499
|
+
* isEqMassCommensurable" function has been called to check that the units are
|
|
500
|
+
* commensurable.
|
|
501
|
+
*
|
|
502
|
+
* @param {number} amt - The amount of this unit to be converted.
|
|
503
|
+
* @param {object} toUnit - The target/to unit for which the converted number is wanted.
|
|
545
504
|
* @param {number} molecularWeight - The molecular weight of the substance for which the conversion is being made.
|
|
546
505
|
* @param {number} charge - The absolute value of the charge of the substance for which the conversion is being made.
|
|
547
|
-
* @returns {number} - The
|
|
506
|
+
* @returns {number} - The amount in the specified toUnit.
|
|
548
507
|
*/
|
|
549
|
-
|
|
508
|
+
convertEqMass(amt, toUnit, molecularWeight, charge) {
|
|
509
|
+
// Determine the number of powers of mass we have to convert to equivalents.
|
|
510
|
+
// Typically this will be just 1, or -1, but not necessarily. If it is a
|
|
511
|
+
// negative number, then we are converting in the opposite direciton.
|
|
512
|
+
// Because the units are presumed commensurable, we can use the
|
|
513
|
+
// equivalentExp_ instead of the mass dimension.
|
|
514
|
+
const massPowersToConvert = toUnit.equivalentExp_ - this.equivalentExp_;
|
|
550
515
|
// Calculate equivalent mass by dividing molecular weight by charge
|
|
551
516
|
let equivalentMass = molecularWeight / charge;
|
|
552
|
-
// Calculate equivalents by dividing mass by equivalent mass
|
|
553
|
-
let equivalents = mass / equivalentMass;
|
|
554
517
|
// Get Avogadro's number from the unit tables
|
|
555
518
|
let avogadroNumber = this._getUnitTables().getUnitByCode('mol').magnitude_;
|
|
556
|
-
// Calculate
|
|
557
|
-
//
|
|
558
|
-
let
|
|
519
|
+
// Calculate equivalents by dividing mass by equivalent mass, for each
|
|
520
|
+
// power to be converted.
|
|
521
|
+
let equivalents = this.magnitude_ * amt / Math.pow(equivalentMass, massPowersToConvert);
|
|
522
|
+
// Calculate mole factor by dividing the magnitude of the equivalent unit by
|
|
523
|
+
// Avogadro's number. toUnit may have a prefix (e.g. meq) and we need to adjust for that, for
|
|
524
|
+
// each massPowersToConvert.
|
|
525
|
+
let moleFactor = toUnit.magnitude_ / Math.pow(avogadroNumber, massPowersToConvert);
|
|
559
526
|
// Adjust equivalents by dividing by the mole factor
|
|
560
527
|
let adjustedEquivalents = equivalents / moleFactor;
|
|
561
528
|
// Return the adjusted equivalents
|
|
562
529
|
return adjustedEquivalents;
|
|
563
530
|
} // end convertMassToEq
|
|
564
531
|
|
|
532
|
+
/**
|
|
533
|
+
* Converts a unit with eq/mol/mass to another unit with eq/mol/mass. It
|
|
534
|
+
* assumes the units an commensurable, which can be checked via
|
|
535
|
+
* isEqMolMassCommensurable. It also assumes that the powers of eq/mol/mass
|
|
536
|
+
* are different between the two units; otherwise it would be more efficient
|
|
537
|
+
* to call one of the other convert... functions.
|
|
538
|
+
*
|
|
539
|
+
* @param {number} amt - The amount of this unit to be converted.
|
|
540
|
+
* @param {object} toUnit - The target/to unit for which the converted number is wanted.
|
|
541
|
+
* @param {number} molecularWeight - The molecular weight of the substance for which the conversion is being made.
|
|
542
|
+
* @param {number} charge - The absolute value of the charge of the substance for which the conversion is being made.
|
|
543
|
+
* @returns {number} - The equivalent amount in the specified equivalent unit.
|
|
544
|
+
*/
|
|
545
|
+
convertEqMolMass(amt, toUnit, molecularWeight, charge) {
|
|
546
|
+
// Handle the equivalent differences. It important for the following
|
|
547
|
+
// calculations (for consistency) that we consider the difference in
|
|
548
|
+
// equivalent powers and not mol powers, so we are not calling
|
|
549
|
+
// convertEqToMol, in case its implementation changes.
|
|
550
|
+
// See convertEqToMol for details. One difference is that we do not scale
|
|
551
|
+
// by magnitude_ until the end.
|
|
552
|
+
const eqPowersToConvert = this.equivalentExp_ - toUnit.equivalentExp_;
|
|
553
|
+
const molAmt = amt / Math.pow(charge, eqPowersToConvert);
|
|
554
|
+
// Now for the mol/mass converstion part, we consider only the mass power
|
|
555
|
+
// differences, and not the mol power differences (which were partially
|
|
556
|
+
// handled in the eq/mol step above).
|
|
557
|
+
// Again, see convertMolToMass for details on the calculations.
|
|
558
|
+
const tabs = this._getUnitTables();
|
|
559
|
+
const d = tabs.getMassDimensionIndex();
|
|
560
|
+
const massPowersToConvert = this.dim_.getElementAt(d) - toUnit.dim_.getElementAt(d);
|
|
561
|
+
const molPowersToConvert = -massPowersToConvert; // so the formulas follow convertMolToMass
|
|
562
|
+
const avoNum = tabs.getUnitByCode('mol').magnitude_;
|
|
563
|
+
let moleUnitFactor = Math.pow(molecularWeight / avoNum, molPowersToConvert);
|
|
564
|
+
return this.magnitude_ / toUnit.magnitude_ * moleUnitFactor * molAmt;
|
|
565
|
+
}
|
|
566
|
+
|
|
565
567
|
/**
|
|
566
568
|
* Checks if the given unit is an equivalent unit.
|
|
567
|
-
*
|
|
568
|
-
* Note: equivalent units are also be molar units, so a unit can return true for
|
|
569
|
+
*
|
|
570
|
+
* Note: equivalent units are also be molar units, so a unit can return true for
|
|
569
571
|
* both isEquivalentUnit and isMolarUnit.
|
|
570
|
-
*
|
|
572
|
+
*
|
|
571
573
|
* @returns {boolean} - Returns true if the unit is an equivalent unit, false otherwise.
|
|
572
574
|
*/
|
|
573
575
|
isEquivalentUnit() {
|
|
@@ -576,7 +578,7 @@ class Unit {
|
|
|
576
578
|
|
|
577
579
|
/**
|
|
578
580
|
* Checks if the given unit is a molar unit.
|
|
579
|
-
*
|
|
581
|
+
*
|
|
580
582
|
* @returns {boolean} - Returns true if the unit is a molar unit, false otherwise.
|
|
581
583
|
*/
|
|
582
584
|
isMolarUnit() {
|
|
@@ -584,40 +586,36 @@ class Unit {
|
|
|
584
586
|
} // end isMolarUnit
|
|
585
587
|
|
|
586
588
|
/**
|
|
587
|
-
* This function converts
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
589
|
+
* This function converts between equivalants and moles (in either direction)
|
|
590
|
+
* using the charge of the substance. It assumes that the
|
|
591
|
+
* isEqMolCommensurable" function has been called to check that the units are
|
|
592
|
+
* commensurable.
|
|
593
|
+
* As with the other "convert" functions, it assumes the appropriate
|
|
594
|
+
* "is...Commensurable" function has been called.
|
|
595
|
+
*
|
|
596
|
+
* @param {number} amt - The amount of this unit for which the conversion is being made.
|
|
597
|
+
* @param {object} toUnit - The target unit for which the converted number is wanted.
|
|
591
598
|
* @param {number} charge - The absolute value of the charge of the substance for which the conversion is being made.
|
|
592
|
-
* @return {number} - The amount in
|
|
593
|
-
*/
|
|
594
|
-
convertEqToMol(eqFromVal, molToUnit, charge) {
|
|
595
|
-
// Check if molToUnit is a molar unit and eqFromVal is a eq unit
|
|
596
|
-
if (!molToUnit.isMolarUnit() || !this.isEquivalentUnit()) {
|
|
597
|
-
throw new Error("Invalid units for conversion of Eq to Mol. Please provide an equivalent and a molar unit.");
|
|
598
|
-
}
|
|
599
|
-
// The conversion from equivalents to moles is based on the principle that one equivalent is equal to 1/valencyFactor moles.
|
|
600
|
-
// The relative magnitude is accounted for via the current unit's magnitude (this.magnitude_) and the target unit's magnitude (molToUnit.magnitude_)
|
|
601
|
-
return eqFromVal * (this.magnitude_ / molToUnit.magnitude_) / charge;
|
|
602
|
-
} // end convertEqToMol
|
|
603
|
-
|
|
604
|
-
/**
|
|
605
|
-
* This function converts moles to equivalent amount using the charge of the substance.
|
|
606
|
-
*
|
|
607
|
-
* @param {number} molFromVal - The mole amount for which the conversion is being made
|
|
608
|
-
* @param {object} eqToUnit - The target unit for which the converted number is wanted
|
|
609
|
-
* @param {number} charge - The absolute value of the charge of the substance for which the conversion is being made
|
|
610
|
-
* @return {number} - The amount in equivalent
|
|
599
|
+
* @return {number} - The amount in molToUnit.
|
|
611
600
|
*/
|
|
612
|
-
|
|
613
|
-
//
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
//
|
|
619
|
-
|
|
620
|
-
|
|
601
|
+
convertEqMol(amt, toUnit, charge) {
|
|
602
|
+
// Determine the number of powers of eq we have to convert to mol.
|
|
603
|
+
// Typically this will be just 1, or -1, but not necessarily. If it is a
|
|
604
|
+
// negative number, then we are really converting mol to eq.
|
|
605
|
+
const eqPowersToConvert = this.equivalentExp_ - toUnit.equivalentExp_;
|
|
606
|
+
|
|
607
|
+
// A simple mole unit has a magnitude of avogadro's number.
|
|
608
|
+
// So does 'eq' (equivalent) because in ucum it is defined as 1 mol, though
|
|
609
|
+
// that does not account for the charge. Therefore, we don't need to
|
|
610
|
+
// account for that factor in this conversion.
|
|
611
|
+
|
|
612
|
+
// The conversion from equivalents to moles is based on the principle that
|
|
613
|
+
// one equivalent is equal to 1/charge moles (per eqPowersToConvert).
|
|
614
|
+
// The relative magnitude is accounted for via the current unit's magnitude
|
|
615
|
+
// (this.magnitude_) and the target unit's magnitude (molToUnit.magnitude_)
|
|
616
|
+
// For each eqPowersToConvert, we need to divide by the charge.
|
|
617
|
+
return amt * (this.magnitude_ / toUnit.magnitude_) / Math.pow(charge, eqPowersToConvert);
|
|
618
|
+
} // end convertEqMol
|
|
621
619
|
|
|
622
620
|
/**
|
|
623
621
|
* Mutates this unit into a unit on a ratio scale and converts a specified
|
|
@@ -691,6 +689,10 @@ class Unit {
|
|
|
691
689
|
retUnit.dim_.add(unit2.dim_);
|
|
692
690
|
}
|
|
693
691
|
|
|
692
|
+
// Add the values of equivalentExp_ and moleExp for the two units
|
|
693
|
+
retUnit.equivalentExp_ += unit2.equivalentExp_;
|
|
694
|
+
retUnit.moleExp_ += unit2.moleExp_;
|
|
695
|
+
|
|
694
696
|
// Concatenate the unit info (name, code, etc) for all cases
|
|
695
697
|
// where the multiplication was performed (an error wasn't thrown)
|
|
696
698
|
retUnit.name_ = this._concatStrs(retUnit.name_, '*', unit2.name_, '[', ']');
|
|
@@ -699,10 +701,6 @@ class Unit {
|
|
|
699
701
|
retUnit.resetFieldsForDerivedUnit();
|
|
700
702
|
if (retUnit.printSymbol_ && unit2.printSymbol_) retUnit.printSymbol_ = this._concatStrs(retUnit.printSymbol_, '.', unit2.printSymbol_, '(', ')');else if (unit2.printSymbol_) retUnit.printSymbol_ = unit2.printSymbol_;
|
|
701
703
|
|
|
702
|
-
// Update the mole exponent count by adding the count for unit2 to the
|
|
703
|
-
// count for this unit.
|
|
704
|
-
retUnit.moleExp_ = retUnit.moleExp_ + unit2.moleExp_;
|
|
705
|
-
|
|
706
704
|
// A unit that has the arbitrary attribute taints any unit created from it
|
|
707
705
|
// via an arithmetic operation. Taint accordingly
|
|
708
706
|
// if (!retUnit.isMole_)
|
|
@@ -761,8 +759,10 @@ class Unit {
|
|
|
761
759
|
} // end if unit2 has a dimension object
|
|
762
760
|
|
|
763
761
|
// Update the mole exponent count by subtracting the count for unit2 from
|
|
764
|
-
// the
|
|
765
|
-
retUnit.moleExp_
|
|
762
|
+
// the count for this unit.
|
|
763
|
+
retUnit.moleExp_ -= unit2.moleExp_;
|
|
764
|
+
// Also update the equivalent exponent.
|
|
765
|
+
retUnit.equivalentExp_ -= unit2.equivalentExp_;
|
|
766
766
|
|
|
767
767
|
// A unit that has the arbitrary attribute taints any unit created from
|
|
768
768
|
// it via an arithmetic operation. Taint accordingly
|
|
@@ -773,6 +773,9 @@ class Unit {
|
|
|
773
773
|
} // end divide
|
|
774
774
|
|
|
775
775
|
/**
|
|
776
|
+
* This function is not actually used by the other code, except for some test
|
|
777
|
+
* code, and might not be adequately tested.
|
|
778
|
+
*
|
|
776
779
|
* Invert this unit with respect to multiplication. If this unit is not
|
|
777
780
|
* on a ratio scale an exception is thrown. Mutating to a ratio scale unit
|
|
778
781
|
* is not possible for a unit, only for a measurement (the magnitude and
|
|
@@ -783,11 +786,16 @@ class Unit {
|
|
|
783
786
|
* @throws and error if this unit is not on a ratio scale
|
|
784
787
|
*/
|
|
785
788
|
invert() {
|
|
789
|
+
var retUnit = this.clone();
|
|
786
790
|
if (this.cnv_ != null) throw new Error(`Attempt to invert a non-ratio unit - ${this.name_}`);
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
+
retUnit.name_ = this.invertString(this.name_);
|
|
792
|
+
retUnit.magnitude_ = 1 / this.magnitude_;
|
|
793
|
+
retUnit.dim_.minus();
|
|
794
|
+
|
|
795
|
+
// Also update equivalentExp_ and moleExp
|
|
796
|
+
retUnit.equivalentExp_ = -this.equivalentExp_;
|
|
797
|
+
retUnit.moleExp_ = -this.moleExp_;
|
|
798
|
+
return retUnit;
|
|
791
799
|
} // end invert
|
|
792
800
|
|
|
793
801
|
/**
|
|
@@ -865,6 +873,9 @@ class Unit {
|
|
|
865
873
|
}
|
|
866
874
|
|
|
867
875
|
/**
|
|
876
|
+
* This function is not actually used by the other code, except for some test
|
|
877
|
+
* code, and might not be adequately tested.
|
|
878
|
+
*
|
|
868
879
|
* Raises the unit to a power. For example
|
|
869
880
|
* kg.m/s2 raised to the -2 power would be kg-2.m-2/s-4
|
|
870
881
|
*
|
|
@@ -883,7 +894,7 @@ class Unit {
|
|
|
883
894
|
*/
|
|
884
895
|
power(p) {
|
|
885
896
|
if (this.cnv_ != null) throw new Error(`Attempt to raise a non-ratio unit, ${this.name_}, ` + 'to a power.');
|
|
886
|
-
|
|
897
|
+
var retUnit = this.clone();
|
|
887
898
|
//this.name_ = UnitString.pow(this.name_, p);
|
|
888
899
|
// the above line is replaced with the code below, as the pow method
|
|
889
900
|
// never actually existing in the UnitString class. (Tried to use
|
|
@@ -920,12 +931,16 @@ class Unit {
|
|
|
920
931
|
} // end do for each element of the units array
|
|
921
932
|
|
|
922
933
|
// reassemble the updated units array to a string
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
if (
|
|
926
|
-
|
|
934
|
+
retUnit.csCode_ = uArray.join('');
|
|
935
|
+
retUnit.magnitude_ = Math.pow(this.magnitude_, p);
|
|
936
|
+
if (retUnit.dim_) {
|
|
937
|
+
retUnit.dim_.mul(p);
|
|
927
938
|
}
|
|
928
|
-
|
|
939
|
+
|
|
940
|
+
// Also update equivalentExp_ and moleExp
|
|
941
|
+
retUnit.equivalentExp_ *= p;
|
|
942
|
+
retUnit.moleExp_ *= p;
|
|
943
|
+
return retUnit;
|
|
929
944
|
} // end power
|
|
930
945
|
|
|
931
946
|
/*
|
|
@@ -944,22 +959,16 @@ class Unit {
|
|
|
944
959
|
* @param unit2 the unit to be compared to this one
|
|
945
960
|
* @returns boolean indicating commensurability
|
|
946
961
|
*/
|
|
947
|
-
|
|
962
|
+
isMolMassCommensurable(unit2) {
|
|
948
963
|
let tabs = this._getUnitTables();
|
|
949
964
|
let d = tabs.getMassDimensionIndex();
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
let testDim = unit2.dim_.clone();
|
|
958
|
-
let curVal = testDim.getElementAt(d);
|
|
959
|
-
testDim.setElementAt(d, curVal + unit2.moleExp_);
|
|
960
|
-
commensurable = testDim.equals(this.dim_);
|
|
961
|
-
}
|
|
962
|
-
return commensurable;
|
|
965
|
+
// Add the moleExp_ values to the mass values in the dimension vectors
|
|
966
|
+
// of each unit, and then compare them.
|
|
967
|
+
const unit1Dim = this.dim_.clone();
|
|
968
|
+
unit1Dim.setElementAt(d, unit1Dim.getElementAt(d) + this.moleExp_);
|
|
969
|
+
const unit2Dim = unit2.dim_.clone();
|
|
970
|
+
unit2Dim.setElementAt(d, unit2Dim.getElementAt(d) + unit2.moleExp_);
|
|
971
|
+
return unit1Dim.equals(unit2Dim);
|
|
963
972
|
}
|
|
964
973
|
|
|
965
974
|
/**
|
|
@@ -981,19 +990,54 @@ class Unit {
|
|
|
981
990
|
isEqMassCommensurable(unit2) {
|
|
982
991
|
let tabs = this._getUnitTables();
|
|
983
992
|
let d = tabs.getMassDimensionIndex();
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
993
|
+
// Add the equivalentExp_ values to the mass values in the dimension vectors
|
|
994
|
+
// of each unit, and then compare them.
|
|
995
|
+
const unit1Dim = this.dim_.clone();
|
|
996
|
+
unit1Dim.setElementAt(d, unit1Dim.getElementAt(d) + this.equivalentExp_);
|
|
997
|
+
const unit2Dim = unit2.dim_.clone();
|
|
998
|
+
unit2Dim.setElementAt(d, unit2Dim.getElementAt(d) + unit2.equivalentExp_);
|
|
999
|
+
return unit1Dim.equals(unit2Dim);
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* This function tests this unit against the unit passed in to see if the
|
|
1004
|
+
* two are eq to mass commensurable-- that the equivalents could be converted
|
|
1005
|
+
* to the mass or vice-versa, in a way that makes the units commensurable.
|
|
1006
|
+
*
|
|
1007
|
+
* The check is made by adding the mole dimension to the equivalent dimension
|
|
1008
|
+
* and comparing that result for the two units, along with the units'
|
|
1009
|
+
* dimension vectors. If they match, the units are
|
|
1010
|
+
* commensurable. Otherwise they are not.
|
|
1011
|
+
*
|
|
1012
|
+
* @param {Unit} unit2 the unit to be compared to this one
|
|
1013
|
+
* @returns {boolean} boolean indicating commensurability
|
|
1014
|
+
*/
|
|
1015
|
+
isEqMolCommensurable(unit2) {
|
|
1016
|
+
const unit1Sum = this.equivalentExp_ + this.moleExp_;
|
|
1017
|
+
const unit2Sum = unit2.equivalentExp_ + unit2.moleExp_;
|
|
1018
|
+
return unit1Sum == unit2Sum && this.dim_.equals(unit2.dim_);
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* This function tests this unit against the unit passed in to see if the
|
|
1023
|
+
* two are commensurable if eq, mol, and mass units are converted in some
|
|
1024
|
+
* direction.
|
|
1025
|
+
*
|
|
1026
|
+
* The check is made by adding the eq, mol, and mass dimensions
|
|
1027
|
+
* and comparing that result for the two units, along with the units'
|
|
1028
|
+
* dimension vectors. If they match, the units are
|
|
1029
|
+
* commensurable. Otherwise they are not.
|
|
1030
|
+
*
|
|
1031
|
+
* @param {Unit} unit2 the unit to be compared to this one
|
|
1032
|
+
* @returns {boolean} boolean indicating commensurability
|
|
1033
|
+
*/
|
|
1034
|
+
isEqMolMassCommensurable(unit2) {
|
|
1035
|
+
const d = this._getUnitTables().getMassDimensionIndex();
|
|
1036
|
+
const unit1Dim = this.dim_.clone();
|
|
1037
|
+
unit1Dim.setElementAt(d, unit1Dim.getElementAt(d) + this.equivalentExp_ + this.moleExp_);
|
|
1038
|
+
const unit2Dim = unit2.dim_.clone();
|
|
1039
|
+
unit2Dim.setElementAt(d, unit2Dim.getElementAt(d) + unit2.equivalentExp_ + unit2.moleExp_);
|
|
1040
|
+
return unit1Dim.equals(unit2Dim);
|
|
997
1041
|
}
|
|
998
1042
|
|
|
999
1043
|
/**
|