Created
September 25, 2015 13:25
-
-
Save tadjik1/20af2ab9acd876c9fd0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import calculateQuantity from '../calculateQuantity'; | |
const quantity = { | |
1920: 10, | |
1366: 8, | |
1024: 4, | |
1280: 6 | |
}; | |
describe('calculateQuantity function', () => { | |
it('should return maximum value', () => { | |
expect(calculateQuantity(quantity, 1980)).to.equal(10); | |
}); | |
it('should return minimum value', () => { | |
expect(calculateQuantity(quantity, 960)).to.equal(4); | |
}); | |
it('should return value from range', () => { | |
expect(calculateQuantity(quantity, 1300)).to.equal(6); | |
}); | |
it('should return value with eqal width', () => { | |
expect(calculateQuantity(quantity, 1024)).to.equal(4); | |
expect(calculateQuantity(quantity, 1280)).to.equal(6); | |
expect(calculateQuantity(quantity, 1366)).to.equal(8); | |
expect(calculateQuantity(quantity, 1920)).to.equal(10); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { last } from 'lodash'; | |
export default function calculateQuantity(quantityObject, width) { | |
const sortedValues = Object.keys(quantityObject) | |
.map(key => Number(key)) | |
.sort((a, b) => (b - a)); | |
const currentWidth = sortedValues.find(value => { | |
return width >= value; | |
}); | |
return quantityObject[currentWidth || last(sortedValues)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment