linear-converter
Flexible linear converter
Table of contents
- Installation
- Basic usage
- Conversion inversion
- Conversions composition
- Custom conversions
- Coefficients
- Conversion 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 the arbitrary-precision package to support arbitrary precision. See all available adapters.
var Decimal = ;var lc = Decimal; // 0°C and 100°C are 32°F and 212°Fvar celsiusToFahrenheit = 0 100 32 212; lc; // => new Decimal('77') // also accepts Decimalslc;
Ready-to-use conversions can be found in the linear-presets package.
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.
Conversion inversion
var fahrenheitToCelsius = lc; lc; // => 25 (as decimal)
Conversions composition
var kelvinToCelsius = 27315 37315 0 100;var kelvinToFahrenheit = lc; lc; // => 68 (as decimal)
Custom conversions
Custom conversions are 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 + blc; // => ax + b (as Decimal)lc; // => ax + b (as Decimal)
For an arbitrary f(x) = ax + b, any [[x1, x2], [f(x1), f(x2)]] is a valid conversion.
More examples:
// degrees to radianslc; // => 4 * Math.PI / 3 (as Decimal) // f(x) = 3xlc; // => 15 (as Decimal) // f(x) = -2x - 46lc; // => 0 (as Decimal)
Coefficients
// f(x) = 2x + 1lc; // => 2 (as Decimal)lc; // => 1 (as Decimal) // f(x) = ax + blc; // => a (as Decimal)lc; // => b (as Decimal)
Conversion equivalence
// f(x) = -3x + 6lc; // => true lc; // => false
Arbitrary precision
Arbitrary precision support is provided via the arbitrary-precision package. See all available adapters.
// without arbitrary precision (very lightweight)var Decimal = ;var lc = Decimal; lc; // => 1.9999999999999998 (as Decimal) // with arbitrary precisionvar Decimal = ;var lc = Decimal; lc; // => 2 (as Decimal)
See CodePen example.
Currying
var convert = lcconvert; ; // => 77 (as Decimal) var cToF = ; ; // => 77 (as Decimal)
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.