Skip to content

Instantly share code, notes, and snippets.

@tadjik1
Created September 25, 2015 13:25
Show Gist options
  • Save tadjik1/20af2ab9acd876c9fd0a to your computer and use it in GitHub Desktop.
Save tadjik1/20af2ab9acd876c9fd0a to your computer and use it in GitHub Desktop.
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);
});
});
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