Skip to content

Commit

Permalink
Custom granularity precision should honor 0 if it is passed in closes p…
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Kendall authored and dluxemburg committed Jul 17, 2018
1 parent 168372f commit 5cbbf63
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/cpmBucketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions test/spec/cpmBucketManager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': [{
Expand Down

0 comments on commit 5cbbf63

Please sign in to comment.