Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercejudo committed Jul 9, 2015
0 parents commit 15cb2df
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
.coveralls.yml

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Users Environment Variables
.lock-wscript
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- 'iojs'
- '0.10'
- '0.12'
before_install:
- npm install -g gulp
after_success:
- gulp coveralls
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Javier Cejudo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# linear-converter-adapter

[![Build Status](https://travis-ci.org/javiercejudo/linear-converter-adapter.svg)](https://travis-ci.org/javiercejudo/linear-converter-adapter)
[![Coverage Status](https://coveralls.io/repos/javiercejudo/linear-converter-adapter/badge.svg?branch=master)](https://coveralls.io/r/javiercejudo/linear-converter-adapter?branch=master)
[![Code Climate](https://codeclimate.com/github/javiercejudo/linear-converter-adapter/badges/gpa.svg)](https://codeclimate.com/github/javiercejudo/linear-converter-adapter)

[big.js](https://github.com/MikeMcl/big.js) adapter for
[linear-arbitrary-precision](https://github.com/javiercejudo/linear-arbitrary-precision)

## Install

npm i linear-converter-adapter

## Usage

See [linear-arbitrary-precision docs](https://github.com/javiercejudo/linear-arbitrary-precision/blob/master/README.md#usage).

See [spec](test/spec.js).
28 changes: 28 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var gulp = require('gulp');
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var rimraf = require('rimraf');
var coveralls = require('gulp-coveralls');

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

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

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

gulp.task('coveralls', function () {
gulp.src('coverage/lcov.info')
.pipe(coveralls());
});

gulp.task('default', ['test']);
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "linear-converter-adapter",
"version": "0.0.0",
"description": "linear-converter adapter for linear-arbitrary-precision",
"main": "src/linear-converter-adapter.js",
"scripts": {
"test": "gulp test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/javiercejudo/linear-converter-adapter.git"
},
"keywords": [
"linear-arbitrary-precision-adapter"
],
"author": "Javier Cejudo <javier@javiercejudo.com> (http://www.javiercejudo.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/javiercejudo/linear-converter-adapter/issues"
},
"homepage": "https://github.com/javiercejudo/linear-converter-adapter#readme",
"devDependencies": {
"gulp": "^3.8.11",
"gulp-coverage": "^0.3.34",
"gulp-coveralls": "^0.1.3",
"gulp-istanbul": "^0.6.0",
"gulp-mocha": "^2.0.0",
"linear-arbitrary-precision": "^3.0.0",
"rimraf": "^2.3.2",
"should": "^5.0.0"
},
"dependencies": {
"linear-converter": "^2.1.0"
}
}
67 changes: 67 additions & 0 deletions src/linear-converter-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*jshint node:true */

'use strict';

var lc = require('linear-converter');

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

// not necessarily true nor enforceable
var precision = 17;

function LC(x) {
this.val = function() {
return x;
};
}

function getPrecision() {
return precision;
}

function setPrecision(LC, n) {
precision = n;
}

function plus(xLC, yLC) {
var y = yLC.val();

return new LC(lc.convert(xLC.val(), [[0, 1], [y, 1 + y]]));
}

function minus(xLC, yLC) {
var y = yLC.val();

return new LC(lc.convert(xLC.val(), [[y, 1 + y], [0, 1]]));
}

function times(xLC, yLC) {
return new LC(lc.convert(xLC.val(), [[0, 1], [0, yLC.val()]]));
}

function div(xLC, yLC) {
return new LC(lc.convert(xLC.val(), [[0, yLC.val()], [0, 1]]));
}

function toString(x) {
return x.val().toString();
}

function valueOf(x) {
return x.val().valueOf();
}

function getInstance() {
return LC;
}
78 changes: 78 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*jshint node:true, mocha:true */

'use strict';

require('should');

var decimalFactory = require('linear-arbitrary-precision');
var adapter = require('../src/linear-converter-adapter');

describe('linear arbitrary precision with big.js', function() {
var Decimal = decimalFactory(adapter);

describe('precision', function() {
var initialPrecision = Decimal.getPrecision();

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

describe('operations', function() {
it('should have a plus method', function() {
new Decimal('0.1').plus(new Decimal('0.2')).valueOf().should.be.exactly(0.3);
});

it('should have a minus method', function() {
new Decimal('0.3').minus(new Decimal('0.1')).valueOf().should.be.exactly(0.2);
});

it('should have a times method', function() {
new Decimal('0.6').times(new Decimal('3')).valueOf().should.be.exactly(1.8);
});

it('should have a div method', function() {
new Decimal('0.3').div(new Decimal('0.2')).valueOf().should.be.exactly(1.5);
});
});

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

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

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

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

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

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

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

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

0 comments on commit 15cb2df

Please sign in to comment.