Skip to content

Commit cb4fbd7

Browse files
committed
Release v0.8.2
1 parent 6f94136 commit cb4fbd7

4 files changed

Lines changed: 32 additions & 30 deletions

File tree

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# History
22

3+
## 2019-06-01, v0.8.2
4+
- Fixed unit complexity calculation for deciding whether to simplify units
5+
- Added undocumented second parameter to `conv` function, which could be removed at any time
6+
37
## 2019-06-01, v0.8.1
48
- Format function can now be used with number or custom types
59
- Now supports passing parameters to custom format function

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unitmath",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"description": "JavaScript library for unit conversion and arithmetic",
55
"main": "index.js",
66
"module": "src/Unit.js",

src/Unit.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -587,16 +587,14 @@ let _config = function _config (options) {
587587

588588
// Determine if the simplified unit is simpler
589589

590-
591-
592590
let calcComplexity = (unitList) => {
593591
// Number of total units, each adds one symbol
594592
let comp = unitList.length
595593

596594
// Number of units in denominator and numerator
597595
let nDen = unitList.filter(a => a.power < 1e-14).length
598596
let nNum = unitList.length - nDen
599-
597+
600598
// If there are no units in the numerator, then any units in the denominator will need a ^-1
601599

602600
// Number of units in the numerator containing powers !== 1, i.e. kg^2, adds two symbols
@@ -728,8 +726,8 @@ let _config = function _config (options) {
728726
let value1, value2
729727
if (unit1.value === null && unit2.value === null) {
730728
// If both units are valueless, get the normalized value of 1 to compare only the unit lists
731-
value1 = normalize(unit1.units, options.type.conv(1), options.type)
732-
value2 = normalize(unit2.units, options.type.conv(1), options.type)
729+
value1 = normalize(unit1.units, options.type.conv(1, unit1.value), options.type)
730+
value2 = normalize(unit2.units, options.type.conv(1, unit2.value), options.type)
733731
} else if (unit1.value !== null && unit2.value !== null) {
734732
// Both units have values
735733
value1 = normalize(unit1.units, unit1.value, options.type)
@@ -870,7 +868,7 @@ let _config = function _config (options) {
870868
}
871869

872870
if (result.value !== null) {
873-
result.value = options.type.pow(result.value, options.type.conv(p))
871+
result.value = options.type.pow(result.value, options.type.conv(p, unit.value))
874872
} else {
875873
result.value = null
876874
}
@@ -879,7 +877,7 @@ let _config = function _config (options) {
879877
}
880878

881879
function _sqrt (unit) {
882-
return _pow(unit, options.type.conv(0.5))
880+
return _pow(unit, options.type.conv(0.5, unit.value))
883881
}
884882

885883
/**
@@ -919,12 +917,12 @@ let _config = function _config (options) {
919917
// But instead of comparing x, the remainder, with zero--we will compare the sum of
920918
// all the parts so far with the original value. If they are nearly equal,
921919
// we set the remainder to 0.
922-
let testSum = options.type.conv(0)
920+
let testSum = options.type.conv(0, unit.value)
923921
for (let i = 0; i < result.length; i++) {
924922
testSum = options.type.add(testSum, normalize(result[i].units, result[i].value, options.type))
925923
}
926924
if (options.type.eq(testSum, normalize(unit.units, unit.value, options.type))) {
927-
x.value = options.type.conv(0)
925+
x.value = options.type.conv(0, unit.value)
928926
}
929927

930928
result.push(x)
@@ -983,7 +981,7 @@ let _config = function _config (options) {
983981
// Unit has power of 0, so prefix will have no effect
984982
return unit
985983
}
986-
if (opts.type.lt(opts.type.abs(unit.value), opts.type.conv(1e-50))) {
984+
if (opts.type.lt(opts.type.abs(unit.value), opts.type.conv(1e-50, unit.value))) {
987985
// Unit is too small for the prefix to matter
988986
return unit
989987
}
@@ -1003,10 +1001,10 @@ let _config = function _config (options) {
10031001
unit.value,
10041002
opts.type.pow(
10051003
options.type.div(
1006-
options.type.conv(piece.unit.prefixes[prefix]),
1007-
options.type.conv(piece.unit.prefixes[piece.prefix])
1004+
options.type.conv(piece.unit.prefixes[prefix], unit.value),
1005+
options.type.conv(piece.unit.prefixes[piece.prefix], unit.value)
10081006
),
1009-
options.type.conv(piece.power)
1007+
options.type.conv(piece.power, unit.value)
10101008
)
10111009
)
10121010
}
@@ -1015,18 +1013,18 @@ let _config = function _config (options) {
10151013
let thisValue = calcValue(prefix)
10161014
if (opts.type.lt(thisValue, opts.prefixMin)) {
10171015
// prefix makes the value too small
1018-
return opts.type.abs(opts.type.div(options.type.conv(opts.prefixMin), thisValue))
1016+
return opts.type.abs(opts.type.div(options.type.conv(opts.prefixMin, unit.value), thisValue))
10191017
}
10201018
if (opts.type.gt(thisValue, opts.prefixMax)) {
10211019
// prefix makes the value too large
1022-
return opts.type.abs(opts.type.div(thisValue, options.type.conv(opts.prefixMax)))
1020+
return opts.type.abs(opts.type.div(thisValue, options.type.conv(opts.prefixMax, unit.value)))
10231021
}
10241022

10251023
// The prefix is in range, but return a score that says how close it is to the original value.
10261024
if (opts.type.le(thisValue, unit.value)) {
1027-
return opts.type.mul(opts.type.abs(opts.type.div(thisValue, unit.value)), opts.type.conv(-1))
1025+
return opts.type.mul(opts.type.abs(opts.type.div(thisValue, unit.value)), opts.type.conv(-1, unit.value))
10281026
} else {
1029-
return opts.type.mul(opts.type.abs(opts.type.div(unit.value, thisValue)), opts.type.conv(-1))
1027+
return opts.type.mul(opts.type.abs(opts.type.div(unit.value, thisValue)), opts.type.conv(-1, unit.value))
10301028
}
10311029
}
10321030

src/utils.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ export function normalize (unitPieces, value, type) {
1818
let result = value
1919

2020
for (let i = 0; i < unitPieces.length; i++) {
21-
unitValue = type.conv(unitPieces[i].unit.value)
22-
unitPrefixValue = type.conv(unitPieces[i].unit.prefixes[unitPieces[i].prefix])
23-
unitPower = type.conv(unitPieces[i].power)
21+
unitValue = type.conv(unitPieces[i].unit.value, value)
22+
unitPrefixValue = type.conv(unitPieces[i].unit.prefixes[unitPieces[i].prefix], value)
23+
unitPower = type.conv(unitPieces[i].power, value)
2424
result = type.mul(result, type.pow(type.mul(unitValue, unitPrefixValue), unitPower))
2525
}
2626

2727
return result
2828
} else {
2929
// units is a single unit of power 1, like kg or degC
30-
unitValue = type.conv(unitPieces[0].unit.value)
31-
unitOffset = type.conv(unitPieces[0].unit.offset)
32-
unitPrefixValue = type.conv(unitPieces[0].unit.prefixes[unitPieces[0].prefix])
30+
unitValue = type.conv(unitPieces[0].unit.value, value)
31+
unitOffset = type.conv(unitPieces[0].unit.offset, value)
32+
unitPrefixValue = type.conv(unitPieces[0].unit.prefixes[unitPieces[0].prefix], value)
3333
return type.mul(type.add(value, unitOffset), type.mul(unitValue, unitPrefixValue))
3434
}
3535
}
@@ -53,19 +53,19 @@ export function denormalize (unitPieces, value, type) {
5353
let result = value
5454

5555
for (let i = 0; i < unitPieces.length; i++) {
56-
unitValue = type.conv(unitPieces[i].unit.value)
57-
unitPrefixValue = type.conv(unitPieces[i].unit.prefixes[unitPieces[i].prefix])
58-
unitPower = type.conv(unitPieces[i].power)
56+
unitValue = type.conv(unitPieces[i].unit.value, value)
57+
unitPrefixValue = type.conv(unitPieces[i].unit.prefixes[unitPieces[i].prefix], value)
58+
unitPower = type.conv(unitPieces[i].power, value)
5959
result = type.div(result, type.pow(type.mul(unitValue, unitPrefixValue), unitPower))
6060
}
6161

6262
return result
6363
} else {
6464
// unit is a single unit of power 1, like kg or degC
6565

66-
unitValue = type.conv(unitPieces[0].unit.value)
67-
unitPrefixValue = type.conv(unitPieces[0].unit.prefixes[unitPieces[0].prefix])
68-
unitOffset = type.conv(unitPieces[0].unit.offset)
66+
unitValue = type.conv(unitPieces[0].unit.value, value)
67+
unitPrefixValue = type.conv(unitPieces[0].unit.prefixes[unitPieces[0].prefix], value)
68+
unitOffset = type.conv(unitPieces[0].unit.offset, value)
6969

7070
return type.sub(type.div(type.div(value, unitValue), unitPrefixValue), unitOffset)
7171
}

0 commit comments

Comments
 (0)