From 5cbbf63d005e9a8de926a52103a436334f651175 Mon Sep 17 00:00:00 2001 From: Matt Kendall Date: Fri, 15 Sep 2017 10:33:14 -0400 Subject: [PATCH] Custom granularity precision should honor 0 if it is passed in closes #1479 (#1591) --- src/cpmBucketManager.js | 8 ++++++-- test/spec/cpmBucketManager_spec.js | 33 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/cpmBucketManager.js b/src/cpmBucketManager.js index f95159943428..5eb66bc13763 100644 --- a/src/cpmBucketManager.js +++ b/src/cpmBucketManager.js @@ -88,7 +88,11 @@ function getCpmStringValue(cpm, config, granularityMultiplier) { }); let bucket = config.buckets.find(bucket => { if (cpm > cap.max * granularityMultiplier) { - const precision = bucket.precision || _defaultPrecision; + // cpm exceeds cap, just return the cap. + let precision = bucket.precision; + if (typeof precision === 'undefined') { + precision = _defaultPrecision; + } cpmStr = (bucket.max * granularityMultiplier).toFixed(precision); } else if (cpm <= bucket.max * granularityMultiplier && cpm >= bucket.min * granularityMultiplier) { return bucket; @@ -114,7 +118,7 @@ function isValidPriceConfig(config) { } function getCpmTarget(cpm, increment, precision, granularityMultiplier) { - if (!precision) { + if (typeof precision === 'undefined') { precision = _defaultPrecision; } let bucketSize = 1 / (increment * granularityMultiplier); diff --git a/test/spec/cpmBucketManager_spec.js b/test/spec/cpmBucketManager_spec.js index f1f7d6973976..e5e2d03c66c8 100644 --- a/test/spec/cpmBucketManager_spec.js +++ b/test/spec/cpmBucketManager_spec.js @@ -58,6 +58,39 @@ describe('cpmBucketManager', () => { expect(JSON.stringify(output)).to.deep.equal(expected); }); + it('gets custom bucket strings and it should honor 0', () => { + let cpm = 16.50908; + let customConfig = { + 'buckets': [ + { + 'precision': 0, + 'min': 3, + 'max': 18, + 'increment': 0.05, + } + ] + }; + let expected = '{"low":"5.00","med":"16.50","high":"16.50","auto":"16.50","dense":"16.50","custom":"17"}'; + let output = getPriceBucketString(cpm, customConfig); + expect(JSON.stringify(output)).to.deep.equal(expected); + }); + + it('gets the custom bucket strings without passing precision and it should honor the default precision', () => { + let cpm = 16.50908; + let customConfig = { + 'buckets': [ + { + 'min': 3, + 'max': 18, + 'increment': 0.05, + } + ] + }; + let expected = '{"low":"5.00","med":"16.50","high":"16.50","auto":"16.50","dense":"16.50","custom":"16.50"}'; + let output = getPriceBucketString(cpm, customConfig); + expect(JSON.stringify(output)).to.deep.equal(expected); + }); + it('checks whether custom config is valid', () => { let badConfig = { 'buckets': [{