/**
* @author Larry Battle <http://bateru.com/news/contact-me>
* @license MIT
* @version 1.0
* @date May 08, 2012
* @purpose To provide a function that converts a decimal to a simplified fraction.
* @info <http://bateru.com/news/2012/05/code-of-the-day-javascript-convert-decimal-to-a-simplified-fraction/>
*/
// ###
// Main Code
// ###
var mixin = {};
mixin.getKeys = function (obj) {
var props = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
props.push(prop);
}
}
return props;
};
mixin.getFractionObject = (function () {
var obj = {
0 : '0',
1 : '1'
},
num = 10,
den = 11,
value;
while (--num) {
while (--den > 1) {
value = (num / den).toFixed(3);
if (value < 1) {
obj[value] = num + "/" + den;
}
}
den = 11;
}
obj.keys = mixin.getKeys(obj);
return function () {
return obj;
};
}
());
mixin.getClosestNum = function (arr, val) {
if (typeof arr !== "object" || !(arr.hasOwnProperty('length')) || isNaN(val)) {
return false;
}
var i = arr.length,
j = i - 1,
minDiff = Math.abs(+val - arr[j]),
diff;
while (i--) {
diff = Math.abs(+val - arr[i]);
if (diff < minDiff) {
minDiff = diff;
j = i;
}
}
return arr[j];
};
mixin.getFractionFromDecimal = function (dec) {
if (isNaN(dec) || !isFinite(dec)) {
return false;
}
if (!/\./.test(dec)) {
return dec;
}
var fracs = mixin.getFractionObject(),
matches = dec.toString().match(/(\d+)(\.\d+)/),
fraction = fracs[mixin.getClosestNum(fracs.keys, Math.abs(+matches[2]))],
sign = ( 0 < dec || (fraction == "0" && Math.abs(dec) < 1) ) ? '' : '-';
if (1 < Math.abs(dec)) {
if (isNaN(fraction)) {
fraction = +matches[1] + " " + fraction;
} else {
fraction = +matches[1] + (+fraction);
}
}
return sign + fraction;
};
// ###
// Qunit Testcases
// ###
var runAllTests = function () {
test("Test getKeys()", function () {
var obj = {};
deepEqual([], mixin.getKeys(obj), "Empty objects should have no props.");
obj.a = 1;
deepEqual(['a'], mixin.getKeys(obj), "a should be returned as a property.");
obj = {
1 : 1,
2 : 1,
3 : 1
};
deepEqual(['1', '2', '3'], mixin.getKeys(obj), "1,2,3 should be properties.");
});
test("Test mixin.getClosestNum()", function () {
var arr = [0, 1, 2, 3, 10, 99, 1000];
equal(false, mixin.getClosestNum({}, 1), "First argument must be an array.");
equal(false, mixin.getClosestNum(arr), "Second argument must be an defined.");
equal(false, mixin.getClosestNum(arr, {}), "Second argument must be an number.");
var runThisTest = function (nums, expect) {
var i = expect.length;
while (i--) {
equal(mixin.getClosestNum(nums, expect[i][0]), expect[i][1], expect[i][0] + " is the closest to " + expect[i][1]);
}
};
runThisTest(arr, [
[-10, 0],
[0.3, 0],
[0.5, 1],
[2.1, 2],
[10.1, 10],
[100, 99],
[600, 1000]
]);
});
test("Test mixin.getFractionObject()", function () {
var fracs = mixin.getFractionObject();
equal(fracs[0], "0", "0.500 should be 0");
equal(fracs['0.333'], "1/3", "0.333 should be 1/3");
equal(fracs['0.500'], "1/2", "0.500 should be 1/2");
equal(fracs['0.900'], "9/10", "0.900 should be 9/10");
equal(fracs[1], "1", "0.500 should be 1");
});
test("Test mixin.getFractionFromDecimal() with invalid inputs", function () {
equal(mixin.getFractionFromDecimal("ten"), false);
equal(mixin.getFractionFromDecimal(NaN), false);
equal(mixin.getFractionFromDecimal(undefined), false);
equal(mixin.getFractionFromDecimal(Infinity), false);
});
test("Test mixin.getFractionFromDecimal() with only decimal values <= 1", function () {
equal(mixin.getFractionFromDecimal(0.001), "0");
equal(mixin.getFractionFromDecimal(0.23), "2/9");
equal(mixin.getFractionFromDecimal(0.335), "1/3");
equal(mixin.getFractionFromDecimal(0.756), "3/4");
equal(mixin.getFractionFromDecimal(0.995), "1");
});
test("Test mixin.getFractionFromDecimal() with integers", function () {
equal(mixin.getFractionFromDecimal(12), "12");
equal(mixin.getFractionFromDecimal(3), "3");
});
test("Test mixin.getFractionFromDecimal() with negative values", function () {
equal(mixin.getFractionFromDecimal(-0.001), "0");
equal(mixin.getFractionFromDecimal(-0.23), "-2/9");
equal(mixin.getFractionFromDecimal(-0.335), "-1/3");
equal(mixin.getFractionFromDecimal(-0.756), "-3/4");
equal(mixin.getFractionFromDecimal(-0.995), "-1");
});
test("Test mixin.getFractionFromDecimal() with numbers bigger than 1.", function () {
equal(mixin.getFractionFromDecimal(10.001), "10");
equal(mixin.getFractionFromDecimal(-10.001), "-10");
equal(mixin.getFractionFromDecimal(12.23), "12 2/9");
equal(mixin.getFractionFromDecimal(-12.23), "-12 2/9");
equal(mixin.getFractionFromDecimal(3.335), "3 1/3");
equal(mixin.getFractionFromDecimal(99.756), "99 3/4");
equal(mixin.getFractionFromDecimal(-99.756), "-99 3/4");
equal(mixin.getFractionFromDecimal(34.995), "35");
});
};
runAllTests();
// ###
// Demo Code
// ###
$(function () {
$('#input').keyup(function () {
var result = mixin.getFractionFromDecimal($(this).val());
if (result) {
$('#output').val(result);
} else {
$('#output').val("Please enter a number.");
}
});
$('#input').val(Math.random() * 100);
$('#input').triggerHandler('keyup');
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
</head>
<body>
<h1>Convert a Decimal to a Simplified Fraction</h1>
<p>Check out <a href="http://bateru.com/news/2012/05/code-of-the-day-javascript-convert-decimal-to-a-simplified-fraction/">http://bateru.com/new</a> for more information.<br/>
Created by Larry Battle</p>
<div name="page">
Input: <input type="text" id="input"><br/>
Output: <input type="text" id="output">
</div>
<hr/>
<h1 id="qunit-header">QUnit example</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
</body>
</html>