mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
Added base property to createUnit to help user-defined units to be simplified easier
This commit is contained in:
parent
efaa989cad
commit
ec8ab2d86d
@ -2988,6 +2988,7 @@ function factory (type, config, load, typed, math) {
|
||||
var offset = 0;
|
||||
var definition;
|
||||
var prefixes;
|
||||
var userBaseUnit;
|
||||
if(obj && obj.type === 'Unit') {
|
||||
defUnit = obj.clone();
|
||||
}
|
||||
@ -3001,6 +3002,7 @@ function factory (type, config, load, typed, math) {
|
||||
prefixes = obj.prefixes;
|
||||
offset = obj.offset;
|
||||
aliases = obj.aliases;
|
||||
userBaseUnit = obj.base;
|
||||
}
|
||||
else {
|
||||
throw new TypeError('Cannot create unit "' + name + '" from "' + obj.toString() + '": expecting "string" or "Unit" or "Object"');
|
||||
@ -3059,16 +3061,33 @@ function factory (type, config, load, typed, math) {
|
||||
value: 1,
|
||||
dimensions: BASE_UNITS[baseName].dimensions,
|
||||
prefixes: prefixes,
|
||||
offset: offset
|
||||
offset: offset,
|
||||
base: baseName
|
||||
};
|
||||
}
|
||||
else {
|
||||
newUnit = {
|
||||
name: name,
|
||||
value: defUnit.value,
|
||||
dimensions: defUnit.dimensions.splice(0),
|
||||
dimensions: defUnit.dimensions.slice(0),
|
||||
prefixes: prefixes,
|
||||
offset: offset
|
||||
offset: offset,
|
||||
base: userBaseUnit
|
||||
};
|
||||
|
||||
// Register the userBaseUnit
|
||||
if(userBaseUnit) {
|
||||
BASE_UNITS[userBaseUnit] = {
|
||||
dimensions: newUnit.dimensions.slice(0),
|
||||
key: userBaseUnit
|
||||
};
|
||||
|
||||
if(!currentUnitSystem.hasOwnProperty(userBaseUnit)) {
|
||||
currentUnitSystem[userBaseUnit] = {
|
||||
unit: newUnit,
|
||||
prefix: PREFIXES.NONE['']
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user