Package Exports
- linear-converter
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (linear-converter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
linear-converter
Flexible linear converter with built in conversions for common units
Table of contents
- Installation
- Basic usage
- Preset inversion
- Presets composition
- Custom conversions
- Coefficients
- Arbitrary precision
- See more
- Related projects
Installation
npm
npm i linear-converter
Bower
bower install linear-converter
To use it in the browser, include the following on your site:
<script src="bower_components/linear-converter/dist/linear-converter.min.js"></script>
Basic usage
var lc = require('linear-converter');
lc.convert(25, lc.PRESETS.temperature.celsiusToFahrenheit); // => 77
See CodePen example for a quick interactive intro.
For CLI use, see linear-converter-cli.
Preset inversion
The provided presets go in one direction only; to invert the direction, use invertPreset
:
var fahrenheitToCelsius = lc.invertPreset(lc.PRESETS.temperature.celsiusToFahrenheit);
lc.convert(77, fahrenheitToCelsius); // => 25
Presets composition
The provided presets have a base unit from which to convert. Any to any conversions are straightforward using inversion and composition with composePresets
:
var kelvinToFahrenheit = lc.composePresets([
lc.invertPreset(lc.PRESETS.temperature.celsiusToKelvin),
lc.PRESETS.temperature.celsiusToFahrenheit
]);
lc.convert(293.15, kelvinToFahrenheit); // => 68
Custom conversions
Custom conversions are easily achieved by passing an array with 2 scales, each of those an array with 2 values. For example, [[0, 1], [0, 2]] means that 0 and 1 in the first scale map to 0 and 2 in the second scale respectively; in short, it multiplies by 2. Any linear function can be described that way:
// f(x) = ax + b
lc.convert(x, [[0, 1], [b, a+b]]); // => ax + b
lc.convert(x, [[1/a, -b/a], [b+1, 0]]); // => ax + b
For an arbitrary f(x) = ax + b, any [[x1, x2], [f(x1), f(x2)]] is a valid preset.
More examples:
// degrees to radians
lc.convert(240, [[0, 180], [0, Math.PI]]); // => 4 * Math.PI / 3
// f(x) = 3x
lc.convert(5, [[0, 1/3], [0, 1]]); // => 15
// f(x) = -2x - 46
lc.convert(-23, [[0, 1], [-46, -48]]); // => 0
Coefficients
Creating presets from a given function is trivial; to find the function from a given preset, two methods are provided: getCoefficientA
and getCoefficientB
.
// f(x) = 2x + 1
lc.getCoefficientA([[0, 1], [1, 3]]); // => 2
lc.getCoefficientB([[0, 1], [1, 3]]); // => 1
// f(x) = ax + b
lc.getCoefficientA([[x1, x2], [f(x1), f(x2)]]); // => a
lc.getCoefficientB([[x1, x2], [f(x1), f(x2)]]); // => b
Arbitrary precision
By default, linear-converter works with native floating-point numbers. However, it will work with arbitrary precision if big.js is available;
// without big.js
lc.getCoefficientA([[0, 0.1], [0.1, 0.3]]); // => 1.9999999999999998
// with big.js
lc.getCoefficientA([[0, 0.1], [0.1, 0.3]]); // => 2
In the browser, you will need to generate a bundled big.js package by running the following command:
browserify -r node_modules/big.js/big.js:big.js > browserified-big.js
Alternatively, grab it from https://wzrd.in/bundle/big.js.
Then simply include that file before linear-converter.
See more
Related projects
- linear-converter-cli: CLI for linear-converter.
- rescale: rescales a point given two scales.
- scale: scales normalised data.
- normalise: normalise data to [0, 1].
- rescale-util: rescale utilities.
- rescale-arbitrary-precision: arbitrary precision for rescale.