Skip to content

Commit

Permalink
feat(api): rename api methods to replace preset with conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Oct 4, 2015
1 parent 6009910 commit 766764f
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 88 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ To use it in the browser, include the following on your site:

## Basic usage

*linear-converter* uses [arbitrary-precision](https://github.com/javiercejudo/arbitrary-precision)
to support arbitrary precision. See [all available adapters](https://www.npmjs.com/browse/keyword/arbitrary-precision-adapter).
*linear-converter* uses the [arbitrary-precision](https://github.com/javiercejudo/arbitrary-precision)
package to support arbitrary precision.
See [all available adapters](https://www.npmjs.com/browse/keyword/arbitrary-precision-adapter).

```js
var Decimal = require('arbitrary-precision')(require('floating-adapter'));
Expand Down Expand Up @@ -141,7 +142,7 @@ lc.equivalentPresets(

## Arbitrary precision

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

```js
Expand Down
60 changes: 30 additions & 30 deletions src/linear-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,72 @@ module.exports = function factory(Decimal) {
var api = {};

/**
* Linearly converts x as described by a preset
* Linearly converts x as described by a conversion
*
* @param {Array} preset The preset that describes the conversion
* @param {Array} conversion The conversion
* @param {Number} x The number to be converted
* @return {Number} The converted x
*/
api.convert = function convert(preset, x) {
return rescale.rescale(preset[0], preset[1], x);
api.convert = function convert(conversion, x) {
return rescale.rescale(conversion[0], conversion[1], x);
};

/**
* Inverts a preset to change the direction of the conversion
* Inverts a conversion
*
* @param {Array} preset The preset to invert
* @return {Array} The inverted preset
* @param {Array} conversion The conversion to invert
* @return {Array} The inverted conversion
*/
api.invertPreset = function invertPreset(preset) {
return api.composePresets(preset.slice().reverse(), unitPreset);
api.invertConversion = function invertConversion(conversion) {
return api.composeConversions(conversion.slice().reverse(), unitPreset);
};

/**
* Composes two presets to create a single preset
* Composes two conversions to create a single conversion
*
* @param {Array} presetA The first preset to compose
* @param {Array} presetB The second preset to compose
* @return {Array} The composed preset
* @param {Array} conversionA The first conversion to compose
* @param {Array} conversionB The second conversion to compose
* @return {Array} The composed conversion
*/
api.composePresets = function composePresets(presetA, presetB) {
api.composeConversions = function composeConversions(conversionA, conversionB) {
return [
[api.convert(unitPreset, presetA[0][0]), api.convert(unitPreset, presetA[0][1])],
[api.convert(presetB, presetA[1][0]), api.convert(presetB, presetA[1][1])]
[api.convert(unitPreset, conversionA[0][0]), api.convert(unitPreset, conversionA[0][1])],
[api.convert(conversionB, conversionA[1][0]), api.convert(conversionB, conversionA[1][1])]
];
};

/**
* Calculates the a coefficient in the f(x) = ax + b function that describes
* the given preset.
* the given conversion.
*
* @param {Array} preset The preset for which to calculate its a coefficient
* @param {Array} conversion The conversion for which to calculate its a coefficient
* @return {Number} The coefficient a
*/
api.getCoefficientA = function getCoefficientA(preset) {
return api.convert(preset, 1).minus(api.getCoefficientB(preset));
api.getCoefficientA = function getCoefficientA(conversion) {
return api.convert(conversion, 1).minus(api.getCoefficientB(conversion));
};

/**
* Calculates the b coefficient in the f(x) = ax + b function that describes
* the given preset.
* the given conversion.
*
* @param {Array} preset The preset for which to calculate its b coefficient
* @param {Array} conversion The conversion for which to calculate its b coefficient
* @return {Number} The coefficient b
*/
api.getCoefficientB = function getCoefficientB(preset) {
return api.convert(preset, 0);
api.getCoefficientB = function getCoefficientB(conversion) {
return api.convert(conversion, 0);
};

/**
* Check equivalence of two presets
* Check equivalence of two conversions
*
* @param {Array} presetA The first preset to check for equivalence
* @param {Array} presetB The second preset to check for equivalence
* @return {Boolean} whether the presets are equivalent or not
* @param {Array} conversionA The first conversion to check for equivalence
* @param {Array} conversionB The second conversion to check for equivalence
* @return {Boolean} whether the conversions are equivalent or not
*/
api.equivalentPresets = function equivalentPresets(presetA, presetB) {
api.equivalentConversions = function equivalentConversions(conversionA, conversionB) {
return [api.getCoefficientB, api.getCoefficientA].every(function(coefficient) {
return coefficient(presetA).equals(coefficient(presetB));
return coefficient(conversionA).equals(coefficient(conversionB));
});
};

Expand Down
26 changes: 15 additions & 11 deletions test/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
var converter = lcFactory(Decimal);

var convert = converter.convert;
var invert = converter.invertPreset;
var compose = converter.composePresets;
var equivalent = converter.equivalentPresets;
var invert = converter.invertConversion;
var compose = converter.composeConversions;
var getCoefficientA = converter.getCoefficientA;
var getCoefficientB = converter.getCoefficientB;
var equivalent = converter.equivalentConversions;

it('should support convertion', function() {
it('should support conversion', function() {
convert(temp.celsius_fahrenheit, 25).equals(new Decimal('77')).should.be.exactly(true);
});

it('should support preset invertion', function() {
it('should support conversion invertion', function() {
convert(invert(temp.celsius_fahrenheit), 77).equals(new Decimal('25')).should.be.exactly(true);
});

it('should support preset composition', function() {
it('should support conversion composition', function() {
var kelvinToFahrenheit = compose(
invert(temp.celsius_kelvin),
temp.celsius_fahrenheit
Expand All @@ -39,14 +41,14 @@

it('should support calculating coefficients', function() {
// using Should() to make IE9 happy: https://github.com/shouldjs/should.js/wiki/Known-Bugs#ie9
$hould(converter.getCoefficientA([[0, 0.1], [0.1, 0.3]]).val().val())
$hould(getCoefficientA([[0, 0.1], [0.1, 0.3]]).val().val())
.be.approximately(2, 1e-15);

converter.getCoefficientB([[0.1, 0.3], [0, 0.1]]).val().val()
getCoefficientB([[0.1, 0.3], [0, 0.1]]).val().val()
.should.be.approximately(-0.05, 1e-15);
});

it('should support checking for preset equivalence', function() {
it('should support checking for conversion equivalence', function() {
equivalent(
[[1, 5], [3, -9]],
[[-1, 100], [9, -294]]
Expand All @@ -67,12 +69,14 @@
describe('arbitrary precision support', function() {
var Decimal = arbitraryPrecision(bigjsAdapter);
var converter = lcFactory(Decimal);
var getCoefficientA = converter.getCoefficientA;
var getCoefficientB = converter.getCoefficientB;

it('should support calculating coefficients with arbitrary precision', function() {
converter.getCoefficientA([[0, 0.1], [0.1, 0.3]]).equals(new Decimal('2'))
getCoefficientA([[0, 0.1], [0.1, 0.3]]).equals(new Decimal('2'))
.should.be.exactly(true);

converter.getCoefficientB([[0.1, 0.3], [0, 0.1]]).equals(new Decimal('-0.05'))
getCoefficientB([[0.1, 0.3], [0, 0.1]]).equals(new Decimal('-0.05'))
.should.be.exactly(true);
});
});
Expand Down
41 changes: 18 additions & 23 deletions test/browser/standalone.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,47 @@

var Decimal = arbitraryPrecision(floatingAdapter);
var converter = lcFactory(Decimal);

var convert = converter.convert;
var invert = converter.invertConversion;
var compose = converter.composeConversions;
var getCoefficientA = converter.getCoefficientA;
var getCoefficientB = converter.getCoefficientB;
var equivalent = converter.equivalentConversions;

var ex = [];
ex[0] = {};
ex[0].desc = '25 Celsius to Fahrenheit';
ex[0].value = convert(temp.celsius_fahrenheit, 25);

// easy inversion of presets
var invert = converter.invertPreset;
ex[0] = { desc: '25 Celsius to Fahrenheit' };
ex[0].value = convert(temp.celsius_fahrenheit, 25);

ex[1] = {};
ex[1].desc = '77 Fahrenheit to Celsius';
ex[1] = { desc: '77 Fahrenheit to Celsius' }
ex[1].value = convert(invert(temp.celsius_fahrenheit), 77);

// convert presets any to any using inversion and composition
var compose = converter.composePresets;

var kelvinToFahrenheit = compose(
invert(temp.celsius_kelvin),
temp.celsius_fahrenheit
);

ex[2] = {};
ex[2].desc = '293.15 Kelvin to Fahrenheit';
ex[2] = { desc: '293.15 Kelvin to Fahrenheit' };
ex[2].value = convert(kelvinToFahrenheit, 293.15);

// calculate the coefficients for the underlying
// linear function from a preset
ex[3] = {};
ex[3].desc = 'coefficient "a" of f(x) = 2x + 1 as described by [[0, 1], [1, 3]]';
ex[3].value = converter.getCoefficientA([[0, 1], [1, 3]]);
ex[3] = { desc: 'coefficient "a" of f(x) = 2x + 1 as described by [[0, 1], [1, 3]]' };
ex[3].value = getCoefficientA([[0, 1], [1, 3]]);

ex[4] = {};
ex[4].desc = 'coefficient "b" of f(x) = 2x + 1 as described by [[0, 1], [1, 3]]';
ex[4].value = converter.getCoefficientB([[0, 1], [1, 3]]);
ex[4] = { desc: 'coefficient "b" of f(x) = 2x + 1 as described by [[0, 1], [1, 3]]' };
ex[4].value = getCoefficientB([[0, 1], [1, 3]]);

ex[5] = {};
ex[5].desc = 'equivalente presets [[1, 5], [3, -9]], [[-1, 100], [9, -294]]';
ex[5].value = converter.equivalentPresets(
ex[5] = { desc: 'equivalent conversion [[1, 5], [3, -9]], [[-1, 100], [9, -294]]' };
ex[5].value = equivalent(
[[1, 5], [3, -9]],
[[-1, 100], [9, -294]]
);

ex.forEach(function(example, index) {
document.getElementById('ex' + index).innerHTML =
'<b>' + example.value + '</b>: ' + example.desc;
document.getElementById('ex' + index).innerHTML =
'<b>' + example.value + '</b>: ' + example.desc;
});
</script>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ function scaleVals(scale) {
return scale.map(Number);
}

exports.composeThePresets = function() {
exports.composeTheConversions = function() {
var Decimal = arbitraryPrecision(floatingAdapter);
var compose = lcFactory(Decimal).composePresets;
var compose = lcFactory(Decimal).composeConversions;

it('should compose the presets', function() {
it('should compose the conversions', function() {
compose(
[[0, 10], [10.5, 20.5]],
[[10, 20], [50, 60]]
Expand Down
4 changes: 2 additions & 2 deletions test/node/convert/convertTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ var arbitraryPrecision = require('arbitrary-precision');
var floatingAdapter = require('floating-adapter');
var lcFactory = require('../../../src/linear-converter');

exports.convertBasedOnTheProvidedPreset = function() {
exports.convertBasedOnTheProvidedConversion = function() {
var Decimal = arbitraryPrecision(floatingAdapter);
var convert = lcFactory(Decimal).convert;

it('convert based on the provided preset', function() {
it('convert based on the provided conversion', function() {
convert([[0, 10], [10, 20]], -273.15).equals(new Decimal('-263.15')).should.be.exactly(true);
convert([[0, 10], [10, 20]], 24).equals(new Decimal('34')).should.be.exactly(true);
});
Expand Down
4 changes: 2 additions & 2 deletions test/node/equivalence/equivalenceTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var lcFactory = require('../../../src/linear-converter');

exports.returnTrue = function() {
var Decimal = arbitraryPrecision(floatingAdapter);
var equivalent = lcFactory(Decimal).equivalentPresets;
var equivalent = lcFactory(Decimal).equivalentConversions;

it('should return true', function() {
equivalent(
Expand Down Expand Up @@ -39,7 +39,7 @@ exports.returnTrue = function() {

exports.returnFalse = function() {
var Decimal = arbitraryPrecision(floatingAdapter);
var equivalent = lcFactory(Decimal).equivalentPresets;
var equivalent = lcFactory(Decimal).equivalentConversions;

it('should return false', function() {
equivalent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ function scaleVals(scale) {
return scale.map(Number);
}

exports.invertThePreset = function() {
exports.invertTheConversion = function() {
var Decimal = arbitraryPrecision(floatingAdapter);
var invert = lcFactory(Decimal).invertPreset;
var invert = lcFactory(Decimal).invertConversion;

it('should invert the preset', function() {
invert([[0, 10], [10, 20]]).map(scaleVals)
it('should invert the conversion', function() {
invert([[0, 10], [10, 20]])
.map(scaleVals)
.should.eql([[10, 20], [0, 10]]);

invert([[-5, 4], [0.05, -5.4]]).map(scaleVals)
invert([[-5, 4], [0.05, -5.4]])
.map(scaleVals)
.should.eql([[0.05, -5.4], [-5, 4]]);
});
};
18 changes: 9 additions & 9 deletions test/node/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

var better = require('betterer').better;
var convertTests = require('./convert/convertTests');
var invertPresetTests = require('./invertPreset/invertPresetTests');
var composePresetsTests = require('./composePresets/composePresetsTests');
var invertConversionTests = require('./invertConversion/invertConversionTests');
var composeConversionsTests = require('./composeConversions/composeConversionsTests');
var coefficientATests = require('./coefficients/coefficientATests');
var coefficientBTests = require('./coefficients/coefficientBTests');
var equivalenceTests = require('./equivalence/equivalenceTests');

var o_o = describe;

o_o('converting', function() { var o_O = convertTests;
o_o('with a valid preset', better('convert based on the provided preset', o_O));
o_o('with a valid conversion', better('convert based on the provided conversion', o_O));
});

o_o('inverting', function() { var o_O = invertPresetTests;
o_o('a valid preset', better('invert the preset', o_O));
o_o('inverting', function() { var o_O = invertConversionTests;
o_o('a valid conversion', better('invert the conversion', o_O));
});

o_o('composing', function() { var o_O = composePresetsTests;
o_o('valid presets', better('compose the presets', o_O));
o_o('composing', function() { var o_O = composeConversionsTests;
o_o('valid conversions', better('compose the conversions', o_O));
});

o_o('computing coefficient a', function() { var o_O = coefficientATests;
Expand All @@ -35,6 +35,6 @@ o_o('computing coefficient b', function() { var o_O = coefficientBTests;
});

o_o('checking for equivalence', function() { var o_O = equivalenceTests;
o_o('when presets are equivalent', better('return true', o_O));
o_o('when presets are not equivalent', better('return false', o_O));
o_o('when conversions are equivalent', better('return true', o_O));
o_o('when conversions are not equivalent', better('return false', o_O));
});

0 comments on commit 766764f

Please sign in to comment.