npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

linear-conversion

v5.0.0

Published

Linear conversion class for linear-converter

Downloads

18

Readme

linear-conversion

Build Status Coverage Status Code Climate

Linear conversion class for linear-converter

Install

npm i linear-conversion

Basic usage

var Decimal = require('linear-arbitrary-precision')(require('floating-adapter'));
var lc = require('linear-converter')(Decimal);
var LinearConversion = require('linear-conversion')(lc);

// using [email protected]
var temp = require('linear-presets').PRESETS.temperature;

var celsiusToFahrenheit = new LinearConversion(temp.celsiusToFahrenheit);

celsiusToFahrenheit.convert(25); // => new Decimal('77')

See CodePen example for a quick interactive intro.

A simpler (although less flexible) setup is possible using linear-converter-to-go:

var lc = require('linear-converter-to-go');
var LinearConversion = require('linear-conversion')(lc);

// notice that in this case, the presets for common units are bundled
// [email protected] or higher
var temp = lc.PRESETS.temperature;

Conversion inversion

var fahrenheitToCelsius = celsiusToFahrenheit.invert();

fahrenheitToCelsius.convert(77); // => 25 (as decimal)

Conversion composition

var celsiusToKelvin = new LinearConversion(temp.celsiusToKelvin);
var kelvinToFahrenheit = celsiusToKelvin.invert().compose(celsiusToFahrenheit);

kelvinToFahrenheit.convert(293.15); // => 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 + b
(new LinearConversion([[0, 1], [b, a+b]])).convert(x); // => ax + b
(new LinearConversion([[1/a, -b/a], [b+1, 0]])).convert(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
(new LinearConversion([[0, 180], [0, Math.PI]])).convert(240); // => 4 * Math.PI / 3

// f(x) = 3x
(new LinearConversion([[0, 1/3], [0, 1]])).convert(5); // => 15

// f(x) = -2x - 46
(new LinearConversion([[0, 1], [-46, -48]])).convert(-23); // => 0

Coefficients

// f(x) = 2x + 1
var doublePlus1 = new LinearConversion([[0, 1], [1, 3]]);

doublePlus1.getCoefficientA(); // => 2
doublePlus1.getCoefficientB(); // => 1

// f(x) = ax + b
var timesAPlusB = new LinearConversion([[x1, x2], [f(x1), f(x2)]]);

timesAPlusB.getCoefficientA(); // => a
timesAPlusB.getCoefficientB(); // => b

Preset equivalence

var eq = new LinearConversion([[1, 5], [3, -9]]);

eq.equates(new LinearConversion([[-1, 100], [9, -294]])); // => true (both f(x) = -3x + 6)


var notEq = new LinearConversion([[0, 1], [0, 2]]); // f(x) = 2x

notEq.equates(new LinearConversion([[0, 1], [0, 3]])); // => false (new one is f(x) = 3x)

Arbitrary precision

Arbitrary precision support is provided via linear-arbitrary-precision. See all available adapters.

var doublePlusPoint1 = new LinearConversion([[0, 0.1], [0.1, 0.3]]);

// without arbitrary precision adapters
doublePlusPoint1.getCoefficientA(); // => 1.9999999999999998

// with arbitrary precision adapters
doublePlusPoint1.getCoefficientA(); // => 2

See CodePen example.

See more

Related projects