Skip to content

Commit

Permalink
refactor: use external times and div
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Nov 8, 2015
1 parent 6459204 commit a2bd662
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 39 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
"should": "^7.1.1"
},
"dependencies": {
"assert-error": "^1.0.3",
"core-arbitrary-precision": "^1.0.1",
"div-arbitrary-precision": "^1.0.0",
"equals-arbitrary-precision": "^1.0.1",
"lodash.flow": "^3.2.1",
"lodash.isstring": "^3.0.1",
"minus-arbitrary-precision": "^1.0.0",
"plus-arbitrary-precision": "^1.0.0"
"plus-arbitrary-precision": "^1.0.0",
"times-arbitrary-precision": "^1.0.0"
}
}
21 changes: 3 additions & 18 deletions src/linear-arbitrary-precision.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,17 @@

'use strict';

var isString = require('lodash.isstring');
var assert = require('assert-error');
var coreArbitraryPrecision = require('core-arbitrary-precision');
var flow = require('lodash.flow');

var extend = flow(
require('plus-arbitrary-precision'),
require('minus-arbitrary-precision'),
require('times-arbitrary-precision'),
require('div-arbitrary-precision'),
require('equals-arbitrary-precision')
);

module.exports = function factory(adapter) {
var Decimal = coreArbitraryPrecision(adapter);
var p = Decimal.prototype;

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

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

function newDecimalFromImpl(x) {
return new Decimal(adapter.toString(x));
}

return extend(Decimal);
return extend(coreArbitraryPrecision(adapter));
};
28 changes: 10 additions & 18 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@ describe('linear operations with Floating', function() {

it('should be able to get the current precision', function() {
Decimal.getPrecision().should.have.type('number');

new Decimal('1').div(new Decimal('3')).toString().should.be.exactly('0.3333333333333333');
});

it('should be able to set the current precision', function() {
Decimal.setPrecision(42);
Decimal.getPrecision().should.be.exactly(42);
new Decimal('1').div(new Decimal('3')).toString().should.be.exactly('0.3333333333333333');

Decimal.setPrecision(initialPrecision);
});
});
Expand Down Expand Up @@ -74,31 +70,27 @@ describe('linear operations with Floating', function() {

describe('toString, valueOf and JSON', function() {
it('should be able to return a string representation', function() {
var decimalThird = new Decimal('1').div(new Decimal('3'));
var decimalOne = new Decimal('1');

decimalThird.toString().should.be.exactly('0.3333333333333333')
.and.exactly(decimalThird.toJSON());
decimalOne.toString().should.be.exactly('1')
.and.exactly(decimalOne.toJSON());

decimalThird.valueOf().should.be.exactly(1/3);
decimalOne.valueOf().should.be.exactly(1);
});

it('should play nicely with Number()', function() {
var decimalThird = new Decimal('1').div(new Decimal('3'));
var decimalOne = new Decimal('1');

Number(decimalThird).should.be.exactly(1/3);
Number(decimalOne).should.be.exactly(1);
});

it('should play nicely with JSON.stringify()', function() {
var Decimal40 = decimalFactory(adapter);

Decimal40.setPrecision(40);

var decimalThird = new Decimal40('1').div(new Decimal40('3'));
var stringified = JSON.stringify([decimalThird]);
var decimalOne = new Decimal('1');
var stringified = JSON.stringify([decimalOne]);

stringified.should.be.exactly('["0.3333333333333333"]');
stringified.should.be.exactly('["1"]');

JSON.parse(stringified, Decimal40.JSONReviver)[0].should.eql(decimalThird);
JSON.parse(stringified, Decimal.JSONReviver)[0].should.eql(decimalOne);
});
});
});

0 comments on commit a2bd662

Please sign in to comment.