linear-converter
Version:
Flexible linear converter
80 lines (66 loc) • 2.67 kB
HTML
<html>
<head>
<meta charset="utf-8">
<title>Standalone Test</title>
</head>
<body>
<!-- markup generation: p[id=ex$@0]*6 -->
<p id="ex0"></p>
<p id="ex1"></p>
<p id="ex2"></p>
<p id="ex3"></p>
<p id="ex4"></p>
<p id="ex5"></p>
<!--[if lte IE 8]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.1/es5-shim.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.1/es5-sham.js"></script>
<![endif]-->
<script src="../../tmp/arbitrary-precision"></script>
<script src="../../tmp/linear-preset-factory"></script>
<script src="../../tmp/linear-presets"></script>
<script src="../../tmp/floating-adapter"></script>
<script src="../../tmp/linear-converter.js"></script>
<script>
var arbitraryPrecision = require('arbitrary-precision');
var floatingAdapter = require('floating-adapter');
var presetFactory = require('linear-preset-factory');
var lcFactory = require('linear-converter');
var temp = presetFactory(require('linear-presets').PRESETS.temperature);
var Decimal = arbitraryPrecision(floatingAdapter);
var converter = lcFactory(Decimal);
var convert = converter.convert;
var invert = converter.invertConversion;
var compose = converter.composeConversions;
var getCoefficientA = converter.getCoefficientA;
var getCoefficientB = converter.getCoefficientB;
var equivalent = converter.equivalentConversions;
var ex = [];
ex[0] = { desc: '25 Celsius to Fahrenheit' };
ex[0].value = convert(temp.celsius_fahrenheit, 25);
ex[1] = { desc: '77 Fahrenheit to Celsius' }
ex[1].value = convert(invert(temp.celsius_fahrenheit), 77);
var kelvinToFahrenheit = compose(
invert(temp.celsius_kelvin),
temp.celsius_fahrenheit
);
ex[2] = { desc: '293.15 Kelvin to Fahrenheit' };
ex[2].value = convert(kelvinToFahrenheit, 293.15);
// calculate the coefficients for the underlying
// linear function from a preset
ex[3] = { desc: 'coefficient "a" of f(x) = 2x + 1 as described by [[0, 1], [1, 3]]' };
ex[3].value = getCoefficientA([[0, 1], [1, 3]]);
ex[4] = { desc: 'coefficient "b" of f(x) = 2x + 1 as described by [[0, 1], [1, 3]]' };
ex[4].value = getCoefficientB([[0, 1], [1, 3]]);
ex[5] = { desc: 'equivalent conversion [[1, 5], [3, -9]], [[-1, 100], [9, -294]]' };
ex[5].value = equivalent(
[[1, 5], [3, -9]],
[[-1, 100], [9, -294]]
);
ex.forEach(function(example, index) {
document.getElementById('ex' + index).innerHTML =
'<b>' + example.value + '</b>: ' + example.desc;
});
</script>
</body>
</html>