Skip to content

Commit

Permalink
feat(equivalence): add presets equivalence function
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Aug 16, 2015
1 parent c812fba commit ddd2b22
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 34 deletions.
46 changes: 26 additions & 20 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,32 @@ var banner = ['/**',
].join('\n');

function tmpBrowserify(pkgName) {
var b = browserify()
.require('./node_modules/' + pkgName + '/src/' + pkgName + '.js', {expose: pkgName});
var pkgPath = './node_modules/' + pkgName + '/src/' + pkgName + '.js';
var b = browserify().require(pkgPath, {expose: pkgName});

return b.bundle()
.pipe(source(pkgName))
.pipe(buffer())
.pipe(gulp.dest('./tmp/'));
}

function browserifyLc(dest) {
var b = browserify().require('./' + pkg.main, {expose: pkg.name});

return b.bundle()
.pipe(source(pkg.name + '.js'))
.pipe(buffer())
.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest(dest))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify()).on('error', gutil.log)
.pipe(rename(pkg.name + '.min.js'))
.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest(dest))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(dest));
}

gulp.task('clean:coverage', function(cb) {
del(['coverage'], cb);
});
Expand All @@ -59,7 +76,7 @@ gulp.task('browserify-floating-adapter', [], function() {
return tmpBrowserify('floating-adapter');
});

gulp.task('test', ['clean:coverage', 'instrument', 'browserify-linear-presets', 'browserify-bigjs-adapter', 'browserify-floating-adapter'], function() {
gulp.task('test', ['clean:coverage', 'instrument'], function() {
return gulp.src(['test/iojs/*.js'])
.pipe(mocha())
.pipe(istanbul.writeReports());
Expand All @@ -71,25 +88,14 @@ gulp.task('coveralls', function() {
});

gulp.task('browserify', ['clean:dist'], function() {
var b = browserify()
.require('./' + pkg.main, {expose: pkg.name});
return browserifyLc('./dist/');
});

Object.keys(pkg.optionalDependencies || {}).forEach(function(optionalDep) {
b.ignore(optionalDep);
});
var devDeps = ['browserify-linear-presets',
'browserify-bigjs-adapter', 'browserify-floating-adapter'];

return b.bundle()
.pipe(source(pkg.name + '.js'))
.pipe(buffer())
.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest('./dist/'))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify()).on('error', gutil.log)
.pipe(rename(pkg.name + '.min.js'))
.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest('./dist/'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/'));
gulp.task('dev', devDeps, function() {
return browserifyLc('./tmp/');
});

gulp.task('build', ['browserify']);
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function(config) {
'tmp/linear-presets',
'tmp/bigjs-adapter',
'tmp/floating-adapter',
'dist/linear-converter.js',
'tmp/linear-converter.js',
'test/browser/*.js'
],

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
},
"dependencies": {
"linear-arbitrary-precision": "^3.0.2",
"olsen": "^2.0.0",
"rescale": "^6.1.0"
}
}
20 changes: 11 additions & 9 deletions scripts/browser_tester.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

set -e

if [ "$TRAVIS_NODE_VERSION" = "iojs" ]; then
if [ "$SAUCE_ACCESS_KEY" ]; then
BROWSERS=$SL_BROWSERS
else
BROWSERS=PhantomJS
fi

echo -e '\nO_o Browser tests'
./node_modules/karma/bin/karma start --single-run --browsers $BROWSERS
BROWSERS=PhantomJS

if [[ "$TRAVIS_NODE_VERSION" = "iojs" && "$SAUCE_ACCESS_KEY" ]]; then
BROWSERS=$SL_BROWSERS
fi

echo -e '\nO_o Browser tests'
echo -e '\nO_o Preparing...'
gulp dev

echo -e '\nO_o Running browser tests...'
karma start --single-run --browsers $BROWSERS
45 changes: 45 additions & 0 deletions src/linear-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var arbitraryPrecision = require('linear-arbitrary-precision');
var rescaleFactory = require('rescale');
var twoOfAKind = require('olsen');

/**
* Returns the linear converter api based on the given adapter
Expand All @@ -16,6 +17,7 @@ module.exports = function factory(adapter) {
var Decimal = arbitraryPrecision(adapter);
var rescale = rescaleFactory(adapter);
var api = {};
var equivalenceRequisites;

/**
* Linearly converts x as described by a preset
Expand Down Expand Up @@ -78,6 +80,17 @@ module.exports = function factory(adapter) {
return rescale.rescale(0, preset[0], preset[1]);
};

/**
* Check equivalence of two or more presets
*
* @param {Array} presets The array of the presets to check for equivalence
*
* @return {Boolean} whether the presets are equivalent or not
*/
api.equivalentPresets = function equivalentPresets(presets) {
return everyAgainstFirst(presets, equivalent2presets);
};

/**
* Composes two presets to create a single preset
*
Expand All @@ -95,5 +108,37 @@ module.exports = function factory(adapter) {
];
}

/**
* Returns an array of api functions that determine equivalence
*
* @return {Array}
*/
function presetEquivalenceRequisites() {
return [
api.getCoefficientA,
api.getCoefficientB
];
}

/**
* Check equivalence of two presets
*
* @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
*/
function equivalent2presets(presetA, presetB) {
return presetEquivalenceRequisites().every(twoOfAKind(presetA, presetB));
}

function everyAgainstFirst(array, callback, thisArg) {
var first = array.shift();

return array.every(function(current, index) {
return callback.call(thisArg, first, current, index + 1, array);
});
}

return api;
};
19 changes: 19 additions & 0 deletions test/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
var convert = converter.convert;
var invert = converter.invertPreset;
var compose = converter.composePresets;
var equivalent = converter.equivalentPresets;

it('should support convertion', function() {
convert(25, temp.celsiusToFahrenheit).should.be.exactly(77);
Expand All @@ -38,6 +39,24 @@
$hould(converter.getCoefficientA([[0, 0.1], [0.1, 0.3]])).be.approximately(2, 1e-15);
converter.getCoefficientB([[0.1, 0.3], [0, 0.1]]).should.be.approximately(-0.05, 1e-15);
});

it('should support checking for preset equivalence', function() {
equivalent([
[[1, 5], [3, -9]],
[[0, 2], [6, 0]],
[[-1, 100], [9, -294]],
]).should.be.exactly(true);

equivalent([
[[0, 1], [0, 2]],
[[0, 1], [0, 3]]
]).should.be.exactly(false);

equivalent([
[[0, 1], [1, 3]],
[[0, 1], [2, 4]]
]).should.be.exactly(false);
});
});

describe('arbitrary precision support', function() {
Expand Down
8 changes: 4 additions & 4 deletions test/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<div id="mocha"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.2.5/mocha.js"></script>
<script src="https://cdn.rawgit.com/shouldjs/should.js/6.0.3/should.js"></script>
<script src="https://wzrd.in/bundle/linear-presets"></script>
<script src="https://wzrd.in/bundle/floating-adapter"></script>
<script src="https://wzrd.in/bundle/bigjs-adapter"></script>
<script src="../../dist/linear-converter.js"></script>
<script src="../../tmp/linear-presets"></script>
<script src="../../tmp/floating-adapter"></script>
<script src="../../tmp/bigjs-adapter"></script>
<script src="../../tmp/linear-converter.js"></script>
<script>
mocha.setup('bdd');
</script>
Expand Down
52 changes: 52 additions & 0 deletions test/iojs/equivalence/equivalenceTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*jshint node:true, mocha:true */

'use strict';

require('should');

var equivalent = require('../../../src/linear-converter')(require('floating-adapter')).equivalentPresets;

exports.returnTrue = function() {
it('should return true', function() {
equivalent([
[[0, 10], [10, 20]],
[[430245.1, -44.5], [430255.1, -34.5]]
]).should.be.exactly(true);

equivalent([
[[1, 5], [3, -9]],
[[0, 2], [6, 0]],
[[-1, 100], [9, -294]],
]).should.be.exactly(true);

equivalent([
[[0, 1], [0, 1]],
[[0, 1], [0, 1]]
]).should.be.exactly(true);

equivalent([
[[0, 1], [0, 3]],
[[0, 3], [0, 9]]
]).should.be.exactly(true);

equivalent([
[[0, 1], [0, 1]],
]).should.be.exactly(true);

equivalent([]).should.be.exactly(true);
});
};

exports.returnFalse = function() {
it('should return false', function() {
equivalent([
[[0, 1], [0, 2]],
[[0, 1], [0, 3]]
]).should.be.exactly(false);

equivalent([
[[0, 1], [1, 3]],
[[0, 1], [2, 4]]
]).should.be.exactly(false);
});
};
6 changes: 6 additions & 0 deletions test/iojs/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var invertPresetTests = require('./invertPreset/invertPresetTests');
var composePresetsTests = require('./composePresets/composePresetsTests');
var coefficientATests = require('./coefficients/coefficientATests');
var coefficientBTests = require('./coefficients/coefficientBTests');
var equivalenceTests = require('./equivalence/equivalenceTests');

var o_o = describe;

Expand All @@ -32,3 +33,8 @@ o_o('computing coefficient b', function() { var o_O = coefficientBTests;
o_o('when arbitrary precision is available', better('work with arbitrary precision', o_O));
o_o('when arbitrary precision is not available', better('work with floating-point numbers', o_O));
});

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));
});

0 comments on commit ddd2b22

Please sign in to comment.