Skip to content

Instantly share code, notes, and snippets.

@guz-anton
Last active January 26, 2020 13:19
Show Gist options
  • Save guz-anton/c6fb1a26c25a3c0d4f6edad1291ec8ae to your computer and use it in GitHub Desktop.
Save guz-anton/c6fb1a26c25a3c0d4f6edad1291ec8ae to your computer and use it in GitHub Desktop.
const fnBy = fn => x => y => !fn || !Math[fn] || !x || !y ? 0 : y < 0 ? 0 : y > x ? x : Math[fn] (10 * y / x) * x / 10;
// const ceilBy = max => y => !max || !y ? 0 : y < 0 ? 0 : y > max ? max : Math.ceil (10 * y / max) * max / 10;
const ceilBy = fnBy('ceil');
const roundBy = fnBy('round');
const floorBy = fnBy('floor');
const fiveStarRating = ceilBy(5);
const oneHundredPoints = roundBy(100);
// Example
[1,3,4.5,'4.2','4.8',5,17]
.map(fiveStarRating)
.map(console.log.bind(console));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment