Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom granularity precision should honor 0 if it is passed in #1591

Merged
merged 1 commit into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Custom granularity precision should honor 0 if it is passed in closes #…
  • Loading branch information
mkendall07 committed Sep 15, 2017
commit d2ddc7a6a7588212129180eabffca28b2a8bb92e
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