Skip to content

Commit

Permalink
feat(api): take lc as parameter instead of Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Dec 2, 2015
1 parent 521a31a commit d856576
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ Linear conversion class for *[linear-converter](https://github.com/javiercejudo/
## Basic usage

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

// using linear-presets@1.x
var temp = require('linear-presets').PRESETS.temperature;

var celsiusToFahrenheit = new LinearConversion(temp.celsiusToFahrenheit);
Expand All @@ -25,6 +27,18 @@ celsiusToFahrenheit.convert(25); // => new Decimal('77')

See [CodePen example](http://codepen.io/javiercejudo/pen/bdoBvW?editors=101) for a quick interactive intro.

A simpler (although less flexible) setup is possible using
[linear-converter-to-go](https://github.com/javiercejudo/linear-converter-to-go):

```js
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
// linear-presets@3.x or higher
var temp = lc.PRESETS.temperature;
```

## Conversion inversion

```js
Expand All @@ -44,7 +58,7 @@ kelvinToFahrenheit.convert(293.15); // => 68 (as decimal)

## Custom conversions

Custom conversions are easily achieved by passing an array with 2 scales, each
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:
Expand Down Expand Up @@ -72,8 +86,6 @@ More examples:

## Coefficients

Creating presets from a given function is trivial; to find the function from a given preset, two methods are provided: `getCoefficientA` and `getCoefficientB`.

```js
// f(x) = 2x + 1
var doublePlus1 = new LinearConversion([[0, 1], [1, 3]]);
Expand Down Expand Up @@ -103,8 +115,8 @@ notEq.equates(new LinearConversion([[0, 1], [0, 3]])); // => false (new one is f

## Arbitrary precision

Arbitrary precision support is provided via [arbitrary-precision](https://github.com/javiercejudo/arbitrary-precision).
See [all available adapters](https://www.npmjs.com/browse/keyword/arbitrary-precision-adapter).
Arbitrary precision support is provided via [linear-arbitrary-precision](https://github.com/javiercejudo/linear-arbitrary-precision).
See [all available adapters](https://www.npmjs.com/browse/keyword/linear-arbitrary-precision-adapter).

```js
var doublePlusPoint1 = new LinearConversion([[0, 0.1], [0.1, 0.3]]);
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
"gulp-coveralls": "^0.1.4",
"gulp-istanbul": "^0.10.2",
"gulp-mocha": "^2.1.3",
"arbitrary-precision": "^1.1.2",
"linear-arbitrary-precision": "^3.4.0",
"linear-converter": "^7.1.0",
"linear-preset-to-decimal": "^1.2.0",
"rimraf": "^2.4.3",
"should": "^7.1.1"
},
"dependencies": {
"linear-converter": "^7.0.2"
}
}
6 changes: 1 addition & 5 deletions src/linear-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

'use strict';

var lcFactory = require('linear-converter');

module.exports = function factory(Decimal) {
var lc = lcFactory(Decimal);

module.exports = function factory(lc) {
function LinearConversion(conversion) {
this.getConversion = function getConversion() {
return conversion;
Expand Down
34 changes: 19 additions & 15 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,51 @@

require('should');

var Decimal = require('arbitrary-precision')(require('floating-adapter'));
var LinearConversion = require('../src/linear-conversion')(Decimal);
var Decimal = require('linear-arbitrary-precision')(require('floating-adapter'));
var linearPresetToDecimal = require('linear-preset-to-decimal')(Decimal);
var lc = require('linear-converter')(Decimal);
var LinearConversion = require('../src/linear-conversion')(lc);

describe('LinearConversion', function() {
it('Just Works™', function() {
var double = new LinearConversion([[0, 1], [0, 2]]);

double.convert(50).val().val().should.be.exactly(100);
double.invert().convert(100).val().val().should.be.exactly(50);
double.convert(50).val().val().should.be.exactly(100);
double.convert(50).toString().should.be.exactly('100');
double.invert().convert(100).toString().should.be.exactly('50');
double.convert(50).toString().should.be.exactly('100');


var fourthMinus2 = new LinearConversion([[0, 4], [-2, -1]]);

fourthMinus2.convert(100).val().val().should.be.exactly(23);
fourthMinus2.invert().convert(23).val().val().should.be.exactly(100);
fourthMinus2.convert(100).toString().should.be.exactly('23');
fourthMinus2.invert().convert(23).toString().should.be.exactly('100');

fourthMinus2.getCoefficientA().val().val().should.be.exactly(1/4);
fourthMinus2.getCoefficientB().val().val().should.be.exactly(-2);
fourthMinus2.getCoefficientA().toString().should.be.exactly('0.25');
fourthMinus2.getCoefficientB().toString().should.be.exactly('-2');


var halfMinus4 = fourthMinus2.compose(double);

halfMinus4.convert(100).val().val().should.be.exactly(46);
halfMinus4.convert(100).toString().should.be.exactly('46');

halfMinus4.toString().should.be.exactly([[0,4], [-4,-2]].toString());

JSON.stringify(halfMinus4).should.be.exactly('[["0","4"],["-4","-2"]]');
// going through the trouble of manually calling toJSON
// to make this test work with linear-converter-to-go
JSON.stringify(linearPresetToDecimal(halfMinus4.toJSON())).should.be.exactly('[["0","4"],["-4","-2"]]');
JSON.parse('[[0,4],[-4,-2]]', LinearConversion.reviver).should.eql(halfMinus4);


var plus1 = new LinearConversion([[0, -1], [1, 0]]);

plus1.compose(plus1).compose(plus1).convert(50).val().val().should.be.exactly(53);
plus1.compose(plus1).compose(plus1).convert(50).toString().should.be.exactly('53');


var timesMinus3 = new LinearConversion([[0, 1], [0, -3]]);

timesMinus3.compose(timesMinus3).convert(1).val().val().should.be.exactly(9);
timesMinus3.compose(timesMinus3).compose(timesMinus3).convert(1).val().val()
.should.be.exactly(-27);
timesMinus3.compose(timesMinus3).convert(1).toString().should.be.exactly('9');
timesMinus3.compose(timesMinus3).compose(timesMinus3).convert(1).toString()
.should.be.exactly('-27');


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

0 comments on commit d856576

Please sign in to comment.