mirror of
https://github.com/JonnyBro/JaBa.git
synced 2024-11-23 05:34:58 +05:00
39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
$.RangeMapClass = RangeMap = createClass({
|
|
init: function (map) {
|
|
var key, range, rangelist = [];
|
|
for (key in map) {
|
|
if (map.hasOwnProperty(key) && typeof key === 'string' && key.indexOf(':') > -1) {
|
|
range = key.split(':');
|
|
range[0] = range[0].length === 0 ? -Infinity : parseFloat(range[0]);
|
|
range[1] = range[1].length === 0 ? Infinity : parseFloat(range[1]);
|
|
range[2] = map[key];
|
|
rangelist.push(range);
|
|
}
|
|
}
|
|
this.map = map;
|
|
this.rangelist = rangelist || false;
|
|
},
|
|
|
|
get: function (value) {
|
|
var rangelist = this.rangelist,
|
|
i, range, result;
|
|
if ((result = this.map[value]) !== undefined) {
|
|
return result;
|
|
}
|
|
if (rangelist) {
|
|
for (i = rangelist.length; i--;) {
|
|
range = rangelist[i];
|
|
if (range[0] <= value && range[1] >= value) {
|
|
return range[2];
|
|
}
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
});
|
|
|
|
// Convenience function
|
|
$.range_map = function(map) {
|
|
return new RangeMap(map);
|
|
};
|
|
|