Skip to content

Commit

Permalink
feat: add inequality methods and abs
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed May 10, 2016
1 parent e6ee4c1 commit ae00b32
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/decimaljs-adapter.js
Expand Up @@ -16,6 +16,12 @@ module.exports = {
mod: mod,
pow: pow,
sqrt: sqrt,
gt: gt,
gte: gte,
lt: lt,
lte: lte,
cmp: cmp,
abs: abs,
equals: equals,
toString: toString,
valueOf: toString,
Expand Down Expand Up @@ -58,6 +64,30 @@ function sqrt(x) {
return x.sqrt();
}

function lt(x, y) {
return x.lt(y);
}

function lte(x, y) {
return x.lte(y);
}

function gt(x, y) {
return x.gt(y);
}

function gte(x, y) {
return x.gte(y);
}

function cmp(x, y) {
return x.cmp(y);
}

function abs(x) {
return x.abs();
}

function equals(x, y) {
return x.eq(y);
}
Expand Down
36 changes: 36 additions & 0 deletions test/spec.js
Expand Up @@ -7,6 +7,7 @@ var decimalFactory = require('arbitrary-precision');
var adapter = require('../src/decimaljs-adapter');

var Decimal = decimalFactory(adapter);
var DecimalJs = adapter.getInstance();

describe('arbitrary precision with decimal.js', function() {
describe('precision', function() {
Expand Down Expand Up @@ -61,6 +62,41 @@ describe('arbitrary precision with decimal.js', function() {
new Decimal('2').equals(new Decimal('2')).should.be.exactly(true);
new Decimal('2').equals(new Decimal('3')).should.be.exactly(false);
});

it('should have a gt method', function() {
adapter.gt(new Decimal('2'), new Decimal('2')).should.be.exactly(false);
adapter.gt(new Decimal('2'), new Decimal('3')).should.be.exactly(false);
adapter.gt(new Decimal('2'), new Decimal('1')).should.be.exactly(true);
});

it('should have a gte method', function() {
adapter.gte(new Decimal('2'), new Decimal('2')).should.be.exactly(true);
adapter.gte(new Decimal('2'), new Decimal('3')).should.be.exactly(false);
adapter.gte(new Decimal('2'), new Decimal('1')).should.be.exactly(true);
});

it('should have a lt method', function() {
adapter.lt(new Decimal('2'), new Decimal('2')).should.be.exactly(false);
adapter.lt(new Decimal('2'), new Decimal('3')).should.be.exactly(true);
adapter.lt(new Decimal('2'), new Decimal('1')).should.be.exactly(false);
});

it('should have a lte method', function() {
adapter.lte(new Decimal('2'), new Decimal('2')).should.be.exactly(true);
adapter.lte(new Decimal('2'), new Decimal('3')).should.be.exactly(true);
adapter.lte(new Decimal('2'), new Decimal('1')).should.be.exactly(false);
});

it('should have a cmp method', function() {
adapter.cmp(new Decimal('2'), new Decimal('2')).valueOf().should.be.exactly(0);
adapter.cmp(new Decimal('2'), new Decimal('3')).valueOf().should.be.exactly(-1);
adapter.cmp(new Decimal('2'), new Decimal('1')).valueOf().should.be.exactly(1);
});

it('should have a abs method', function() {
adapter.abs(new DecimalJs('16')).toNumber().should.be.exactly(16);
adapter.abs(new DecimalJs('-5')).toNumber().should.be.exactly(5);
});
});

describe('toString, valueOf and JSON', function() {
Expand Down

0 comments on commit ae00b32

Please sign in to comment.