Last active
December 5, 2019 20:15
-
-
Save gkucmierz/66ed209add3343db76482040d93d8a03 to your computer and use it in GitHub Desktop.
select data based on ranges description
This file contains hidden or 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
// select data based on ranges description | |
// min and max are optional | |
// ranges are testet from bottom to top and vice versa | |
const conditionalRange = (n, ranges, def) => { | |
for (let i = 0; i < ranges.length; ++i) { | |
const r = ranges[i]; | |
if ('max' in r) { | |
if ('min' in r) { | |
if (r.min <= n && n <= r.max) return r.data; | |
} else { | |
if (n <= r.max) return r.data; | |
} | |
} | |
} | |
for (let i = ranges.length-1; 0 <= i; --i) { | |
const r = ranges[i]; | |
if ('min' in r) { | |
if (!('max' in r)) { | |
if (r.min <= n) return r.data; | |
} | |
} | |
} | |
return def; | |
}; | |
const ranges = [ | |
{ max: 10, data: 1 }, | |
{ min: 0, max: 20, data: 2 }, | |
{ min: 21, data: 3 } | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment