Updated fromScale util to return top or bottom of scale, updated specs to reflect new fromScale behavior.
This commit is contained in:
parent
9e1ffad6f6
commit
0869909521
10
lib/utils.js
10
lib/utils.js
|
@ -255,7 +255,15 @@ var addCoreExtensions = function addCoreExtensions() {
|
|||
//
|
||||
// Returns an integer representing the scaled value
|
||||
Number.prototype.fromScale = function(start, end) {
|
||||
return (this - Math.min(start, end)) / (Math.max(start, end) - Math.min(start, end));
|
||||
var val = (this - Math.min(start, end)) / (Math.max(start, end) - Math.min(start, end));
|
||||
|
||||
if (val > 1) {
|
||||
val = 1;
|
||||
} else if (val < 0){
|
||||
val = 0;
|
||||
}
|
||||
|
||||
return val;
|
||||
};
|
||||
|
||||
// Public: Convert value from (0..1) scale to new (start, end) scale
|
||||
|
|
|
@ -25,6 +25,14 @@ describe("Utils", function() {
|
|||
it("converts floats", function() {
|
||||
expect(2.5.fromScale(0, 10)).to.be.eql(0.25);
|
||||
});
|
||||
|
||||
it("should return 1 if the number goes above the top of the scale", function() {
|
||||
expect((15).fromScale(0, 10)).to.be.eql(1);
|
||||
});
|
||||
|
||||
it("should return 0 if the number goes below the bottom of the scale", function() {
|
||||
expect((5).fromScale(10, 20)).to.be.eql(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#toScale", function() {
|
||||
|
@ -32,6 +40,14 @@ describe("Utils", function() {
|
|||
expect((0.5).toScale(0, 10)).to.be.eql(5);
|
||||
});
|
||||
|
||||
it("bottom of scale should be returned when value goes below it", function() {
|
||||
expect((-5).toScale(0, 10)).to.be.eql(0);
|
||||
});
|
||||
|
||||
it("top of scale should be returned when value goes above it", function() {
|
||||
expect((15).toScale(0, 10)).to.be.eql(10);
|
||||
});
|
||||
|
||||
it("converts to floats", function() {
|
||||
expect(0.25.toScale(0, 10)).to.be.eql(2.5);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue