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
Table of contents
- Installation
- Basic usage
- Preset inversion
- Presets composition
- Custom conversions
- Coefficients
- Preset equivalence
- Arbitrary precision
- Currying
- 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
linear-converter uses arbitrary-precision to support arbitrary precision. See all available adapters.
var Decimal = require('arbitrary-precision')(require('floating-adapter'));
var lc = require('linear-converter')(Decimal);
var temp = require('linear-presets').PRESETS.temperature;
lc.convert(temp.celsiusToFahrenheit, 25); // => new Decimal('77')
// also accepts Decimals
lc.convert(temp.celsiusToFahrenheit, new Decimal('25'));
For a quick interactive intro, see CodePen example.
Variants:
- linear-converter-to-go: opinionated, zero-configuration version with floating point precision and built-in conversion presets.
- linear-conversion: if you prefer the object-oriented paradigm.
- linear-converter-cli: for CLI use.
Preset inversion
var fahrenheitToCelsius = lc.invertPreset(temp.celsiusToFahrenheit);
lc.convert(fahrenheitToCelsius, 77); // => 25 (as decimal)
Presets composition
var kelvinToFahrenheit = lc.composePresets(
lc.invertPreset(temp.celsiusToKelvin),
temp.celsiusToFahrenheit
);
lc.convert(kelvinToFahrenheit, 293.15); // => 68 (as decimal)
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 conversion can be described that way:
// f(x) = ax + b
lc.convert([[0, 1], [b, a+b]], x); // => ax + b
lc.convert([[1/a, -b/a], [b+1, 0]], x); // => 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([[0, 180], [0, Math.PI]], 240); // => 4 * Math.PI / 3
// f(x) = 3x
lc.convert([[0, 1/3], [0, 1]], 5); // => 15
// f(x) = -2x - 46
lc.convert([[0, 1], [-46, -48]], -23); // => 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
Preset equivalence
// f(x) = -3x + 6
lc.equivalentPresets(
[[1, 5], [3, -9]],
[[-1, 100], [9, -294]]
); // => true
lc.equivalentPresets(
[[0, 1], [0, 2]], // f(x) = 2x
[[0, 1], [0, 3]] // f(x) = 3x
); // => false
Arbitrary precision
Arbitrary precision support is provided via arbitrary-precision. See all available adapters.
// without arbitrary precision (very lightweight)
var Decimal = require('arbitrary-precision')(require('floating-adapter'));
var lc = require('linear-converter')(Decimal);
lc.getCoefficientA([[0, 0.1], [0.1, 0.3]]); // => 1.9999999999999998
// with arbitrary precision
var Decimal = require('arbitrary-precision')(require('bigjs-adapter'));
var lc = require('linear-converter')(Decimal);
lc.getCoefficientA([[0, 0.1], [0.1, 0.3]]); // => 2
See CodePen example.
Currying
The convert
function is designed to play nicely with currying:
var curry = require('lodash.curry');
var curriedConvert = curry(lc.convert);
var celsiusToFahrenheit = curriedConvert(temp.celsiusToFahrenheit);
var fahrenheitToCelsius = curriedConvert(lc.invertPreset(temp.celsiusToFahrenheit));
celsiusToFahrenheit(25); // => new Decimal('77')
fahrenheitToCelsius(77); // => new Decimal('25')
See CodePen example.
See more
Related projects
- linear-conversion: linear conversion class for linear-converter.
- 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.