From 96aca3a7bb4059b40f87362eedf6ebf477edffd5 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 7 Aug 2015 13:17:23 +0000 Subject: [PATCH] _bestPrefix now chooses shortest prefix when prefix values are the same --- lib/type/unit/Unit.js | 9 ++++++--- test/type/unit/Unit.test.js | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/type/unit/Unit.js b/lib/type/unit/Unit.js index 112cb66d4..3cb63105a 100644 --- a/lib/type/unit/Unit.js +++ b/lib/type/unit/Unit.js @@ -911,9 +911,12 @@ function factory (type, config, load, typed) { var diff = Math.abs( Math.log(absValue / prefix.value) / Math.LN10 - 1.2); - if (diff < bestDiff) { - bestPrefix = prefix; - bestDiff = diff; + if (diff < bestDiff + || (diff === bestDiff && prefix.name.length < bestPrefix.name.length)) { + // choose the prefix with the smallest diff, or if equal, choose the one + // with the shortest name (can happen with SHORTLONG for example) + bestPrefix = prefix; + bestDiff = diff; } } } diff --git a/test/type/unit/Unit.test.js b/test/type/unit/Unit.test.js index 242aad4de..913d66691 100644 --- a/test/type/unit/Unit.test.js +++ b/test/type/unit/Unit.test.js @@ -364,6 +364,7 @@ describe('unit', function() { assert.equal(new Unit(500 ,'m').toString(), '500 m'); assert.equal(new Unit(600 ,'m').toString(), '0.6 km'); assert.equal(new Unit(1000 ,'m').toString(), '1 km'); + assert.equal(new Unit(1000 ,'ohm').toString(), '1 kohm'); });