Skip to content

Commit

Permalink
feat(adapters): embrace the adapter pattern
Browse files Browse the repository at this point in the history
Breaking changes

Full refactoring, everything shall break :)
  • Loading branch information
javiercejudo committed Jul 7, 2015
1 parent c2c4073 commit 046ee56
Show file tree
Hide file tree
Showing 16 changed files with 835 additions and 178 deletions.
30 changes: 3 additions & 27 deletions gulpfile.js
Expand Up @@ -3,45 +3,21 @@ var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var rimraf = require('rimraf');
var coveralls = require('gulp-coveralls');
var combine = require('istanbul-combine');

var decimalDep = process.env.DECIMAL ? process.env.DECIMAL : 'big.js';

gulp.task('clean', function (cb) {
rimraf('./coverage', cb);
});

gulp.task('cleanReport', function (cb) {
rimraf('./coverage/' + decimalDep, cb);
});

gulp.task('instrument', function () {
return gulp.src(['src/*.js'])
.pipe(istanbul())
.pipe(istanbul.hookRequire());
});

gulp.task('test', ['cleanReport', 'instrument'], function () {
return gulp.src(['test/*.js'])
gulp.task('test', ['clean', 'instrument'], function () {
return gulp.src(['test/!(linear)*.js'])
.pipe(mocha())
.pipe(istanbul.writeReports({
reportOpts: {
dir: './coverage/' + decimalDep
}
}));
});

gulp.task('combineReports', function (cb) {
var opts = {
pattern: 'coverage/**/coverage-final.json',
print: 'both',
reporters: {
lcov: {}
}
};

combine.sync(opts);
cb();
.pipe(istanbul.writeReports());
});

gulp.task('coveralls', function () {
Expand Down
18 changes: 8 additions & 10 deletions package.json
Expand Up @@ -4,7 +4,7 @@
"description": "Linear arbitrary precision",
"main": "src/arbitrary-precision.js",
"scripts": {
"test": "gulp clean && node_modules/rescale-arbitrary-precision/scripts/tester.sh && gulp combineReports"
"test": "gulp test"
},
"repository": {
"type": "git",
Expand All @@ -29,18 +29,16 @@
"gulp-coveralls": "^0.1.3",
"gulp-istanbul": "^0.6.0",
"gulp-mocha": "^2.0.0",
"istanbul-combine": "^0.3.0",
"rescale-arbitrary-precision": "^1.2.1",
"rimraf": "^2.3.2",
"should": "^5.0.0",
"sinon": "^1.13.0"
"should": "^5.0.0"
},
"dependencies": {
"optionale": "^1.1.0"
},
"optionalDependencies": {
"big.js": "^3.0.2",
"bignumber.js": "^2.0.7",
"decimal.js": "^4.0.2"
}
"decimal.js": "^4.0.2",
"floating": "^1.0.0",
"linear-conversion": "^1.0.1",
"very-simple-statistics": "^1.0.3"
},
"optionalDependencies": {}
}
66 changes: 18 additions & 48 deletions src/arbitrary-precision.js
Expand Up @@ -2,16 +2,13 @@

'use strict';

var optionale = require('optionale');
var DecimalImpl = optionale.some(['big.js', 'bignumber.js', 'decimal.js']);

module.exports = factory;

function factory() {
var DecimalImplPristine = getDecimanlImplPristine();
function factory(adapter) {
var DecimalImpl = adapter.getInstance();

function Decimal(x) {
var value = new DecimalImplPristine(x);
var value = new DecimalImpl(x);

this.val = function val() {
return value;
Expand All @@ -25,51 +22,39 @@ function factory() {
var p = Decimal.prototype;

p.plus = function plus(x) {
return new Decimal(this.val().plus(x));
return new Decimal(adapter.toJSON(adapter.plus(this.val(), x.val())));
};

p.minus = function minus(x) {
return new Decimal(this.val().minus(x));
return new Decimal(adapter.toJSON(adapter.minus(this.val(), x.val())));
};

p.times = function times(x) {
return new Decimal(this.val().times(x));
return new Decimal(adapter.toJSON(adapter.times(this.val(), x.val())));
};

p.div = function div(x) {
return new Decimal(this.val().div(x));
return new Decimal(adapter.toJSON(adapter.div(this.val(), x.val())));
};

p.toString = p.valueOf = p.toJSON = function toString() {
return this.val().toString();
p.toString = function toString() {
return adapter.toString(this.val());
};

function getPrecision() {
// big.js
if (DecimalImplPristine.hasOwnProperty('DP')) {
return DecimalImplPristine.DP;
}
p.valueOf = function valueOf() {
return adapter.valueOf(this.val());
};

// DecimalImpl.js
if (DecimalImplPristine.hasOwnProperty('precision')) {
return DecimalImplPristine.precision;
}
p.toJSON = function toJSON() {
return adapter.toJSON(this.val());
};

// bignumber.js
return DecimalImplPristine.config().DECIMAL_PLACES;
function getPrecision() {
return adapter.getPrecision(DecimalImpl);
}

function setPrecision(n) {
if (DecimalImplPristine.hasOwnProperty('DP')) {
// big.js
DecimalImplPristine.DP = n;
} else if (DecimalImplPristine.hasOwnProperty('precision')) {
// decimal.js
DecimalImplPristine.precision = n;
} else {
// bignumber.js
DecimalImplPristine.config(n);
}
adapter.setPrecision(DecimalImpl, n);
}

function JSONReviver(key, value) {
Expand All @@ -82,18 +67,3 @@ function factory() {

return Decimal;
}

function getDecimanlImplPristine() {
// decimal.js
if (DecimalImpl.hasOwnProperty('constructor')) {
return DecimalImpl.constructor();
}

// bignumber.js
if (DecimalImpl.hasOwnProperty('another')) {
return DecimalImpl.another();
}

// big.js
return DecimalImpl.call();
}
50 changes: 50 additions & 0 deletions src/bigjs-adapter.js
@@ -0,0 +1,50 @@
/*jshint node:true */

'use strict';

var bigjs = require('big.js');

module.exports = {
getInstance: bigjs,
getPrecision: getPrecision,
setPrecision: setPrecision,
plus: plus,
minus: minus,
times: times,
div: div,
toString: toString,
valueOf: valueOf,
toJSON: valueOf
};

function getPrecision(Big) {
return Big.DP;
}

function setPrecision(Big, n) {
Big.DP = n;
}

function plus(big, x) {
return big.plus(x);
}

function minus(big, x) {
return big.minus(x);
}

function times(big, x) {
return big.times(x);
}

function div(big, x) {
return big.div(x);
}

function toString(big) {
return big.toString();
}

function valueOf(big) {
return big.valueOf();
}
54 changes: 54 additions & 0 deletions src/bignumberjs-adapter.js
@@ -0,0 +1,54 @@
/*jshint node:true */

'use strict';

var bignumber = require('bignumber.js');

module.exports = {
getInstance: getInstance,
getPrecision: getPrecision,
setPrecision: setPrecision,
plus: plus,
minus: minus,
times: times,
div: div,
toString: toString,
valueOf: valueOf,
toJSON: valueOf
};

function getPrecision(Bignumber) {
return Bignumber.config().DECIMAL_PLACES;
}

function setPrecision(Bignumber, n) {
Bignumber.config(n);
}

function plus(bignumber, x) {
return bignumber.plus(x);
}

function minus(bignumber, x) {
return bignumber.minus(x);
}

function times(bignumber, x) {
return bignumber.times(x);
}

function div(bignumber, x) {
return bignumber.div(x);
}

function toString(bignumber) {
return bignumber.toString();
}

function valueOf(bignumber) {
return bignumber.valueOf();
}

function getInstance() {
return bignumber.another();
}
54 changes: 54 additions & 0 deletions src/decimaljs-adapter.js
@@ -0,0 +1,54 @@
/*jshint node:true */

'use strict';

var decimal = require('decimal.js');

module.exports = {
getInstance: getInstance,
getPrecision: getPrecision,
setPrecision: setPrecision,
plus: plus,
minus: minus,
times: times,
div: div,
toString: toString,
valueOf: valueOf,
toJSON: valueOf
};

function getPrecision(Decimal) {
return Decimal.precision;
}

function setPrecision(Decimal, n) {
Decimal.precision = n;
}

function plus(decimal, x) {
return decimal.plus(x);
}

function minus(decimal, x) {
return decimal.minus(x);
}

function times(decimal, x) {
return decimal.times(x);
}

function div(decimal, x) {
return decimal.div(x);
}

function toString(decimal) {
return decimal.toString();
}

function valueOf(decimal) {
return decimal.valueOf();
}

function getInstance() {
return decimal.constructor();
}

0 comments on commit 046ee56

Please sign in to comment.