Skip to content

Instantly share code, notes, and snippets.

@letsgetrandy
Last active August 29, 2015 14:07
Show Gist options
  • Save letsgetrandy/1a6f5307f2f2ec1d6615 to your computer and use it in GitHub Desktop.
Save letsgetrandy/1a6f5307f2f2ec1d6615 to your computer and use it in GitHub Desktop.
Provide values for ranges
var Rangemap = function(points, values) {
this.points = points || [];
this.values = values || [];
};
Rangemap.prototype = {
"equalInRange": true,
"compare": function(a, b) {
if (a === b) {
return this.equalInRange;
}
if (a.constructor === b.constructor === String) {
// for text ranges
var arr = [a, b];
if (this.comparator instanceof Function) {
// allow a custom comparator
arr.sort(this.comparator);
} else {
arr.sort();
}
return arr[0] === a;
} else {
// for numbered ranges
if (this.comparator instanceof Function) {
return this.comparator(a, b);
} else {
return a < b;
}
}
},
"valueForIndex": function(index) {
var indexOffset = 0,
valueOffset = 0;
// if points.length > values.length, assume a minimum value was specified
if (this.points.length > this.values.length) {
if (index < this.points[0]) {
throw 'the specified index was below the minimum range';
// return undefined;
}
indexOffset++;
}
while (indexOffset < this.points.length && valueOffset < this.values.length) {
if (this.compare(index, this.points[indexOffset])) {
return this.values[valueOffset];
}
indexOffset++;
valueOffset++;
}
// if values.length > points.length, assume a global upper value
if (this.values.length > valueOffset) {
return this.values[valueOffset];
}
return undefined;
}
};
describe('de.Rangemap', function() {
it('should return the correct value for an index in a range', function() {
var range = new Rangemap([4, 8, 10], ['a', 'b', 'c']);
expect(range.valueForIndex(3)).toBe('a');
expect(range.valueForIndex(4)).toBe('a');
expect(range.valueForIndex(6)).toBe('b');
expect(range.valueForIndex(8)).toBe('b');
expect(range.valueForIndex(9)).toBe('c');
});
it('should assume a minimum value when more values provided', function() {
var range = new Rangemap([0, 4, 8, 10], ['a', 'b', 'c']);
expect(function(){range.valueForIndex(-1);}).toThrow();
expect(range.valueForIndex(4)).toBe('a');
expect(range.valueForIndex(6)).toBe('b');
expect(range.valueForIndex(8)).toBe('b');
expect(range.valueForIndex(9)).toBe('c');
});
it('should assume a global upper value when fewer values provided', function() {
var range = new Rangemap([4, 8, 10], ['a', 'b', 'c', 'd']);
expect(range.valueForIndex(3)).toBe('a');
expect(range.valueForIndex(6)).toBe('b');
expect(range.valueForIndex(9)).toBe('c');
expect(range.valueForIndex(11)).toBe('d');
});
it('should work with strings', function() {
var range = new Rangemap(['clown', 'pony', 'snake'], [1, 2, 3, 4]);
expect(range.valueForIndex('animal')).toBe(1);
expect(range.valueForIndex('cat')).toBe(1);
expect(range.valueForIndex('dog')).toBe(2);
expect(range.valueForIndex('lion')).toBe(2);
expect(range.valueForIndex('rabbit')).toBe(3);
expect(range.valueForIndex('zebra')).toBe(4);
});
});
@letsgetrandy
Copy link
Author

Example usage:

var testScores = new Rangemap([60, 70, 80, 90], ['F', 'D', 'C', 'B', 'A']);
// johnny scored 85 on his test. what grade does he get?
testScores.valueForIndex(85); // returns "B"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment