diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86823c8 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4e6f54b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: node_js +node_js: + - 'iojs' + - '0.10' + - '0.12' +before_install: + - npm i -g gulp +after_success: + - gulp coveralls diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e6c4402 --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..d7c7edc --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# linear-preset-by-coefficients + +[![Build Status](https://travis-ci.org/javiercejudo/linear-preset-by-coefficients.svg)](https://travis-ci.org/javiercejudo/linear-preset-by-coefficients) +[![Coverage Status](https://coveralls.io/repos/javiercejudo/linear-preset-by-coefficients/badge.svg?branch=master)](https://coveralls.io/r/javiercejudo/linear-preset-by-coefficients?branch=master) +[![Code Climate](https://codeclimate.com/github/javiercejudo/linear-preset-by-coefficients/badges/gpa.svg)](https://codeclimate.com/github/javiercejudo/linear-preset-by-coefficients) + +Generate linear presets from coefficients + +## Install + + npm i linear-preset-by-coefficients + +## Usage + +```js +var Decimal = require('linear-arbitrary-precision')(require('floating-adapter')); +var preset = require('linear-preset-by-coefficients')(Decimal); + +// all numbers are Decimal +preset(temp, 9/5, 32); // => [[0, 1], [32, 33.8]] +``` + +## Related projects + +- [linear-presets](https://github.com/javiercejudo/linear-presets): linear presets for common units. +- [linear-converter](https://github.com/javiercejudo/linear-converter): flexible linear converter. diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..4796c05 --- /dev/null +++ b/gulpfile.js @@ -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']); diff --git a/package.json b/package.json new file mode 100644 index 0000000..421c974 --- /dev/null +++ b/package.json @@ -0,0 +1,40 @@ +{ + "name": "linear-preset-by-coefficients", + "version": "0.0.0", + "description": "Generate linear presets from coefficients", + "main": "src/index.js", + "scripts": { + "test": "gulp test" + }, + "repository": { + "type": "git", + "url": "https://github.com/javiercejudo/linear-preset-by-coefficients" + }, + "keywords": [ + "conversion", + "units" + ], + "author": "Javier Cejudo", + "license": "MIT", + "bugs": { + "url": "https://github.com/javiercejudo/linear-preset-by-coefficients/issues" + }, + "homepage": "https://github.com/javiercejudo/linear-preset-by-coefficients", + "devDependencies": { + "floating-adapter": "^1.2.1", + "gulp": "^3.9.0", + "gulp-coveralls": "^0.1.4", + "gulp-istanbul": "^0.10.2", + "gulp-mocha": "^2.1.3", + "linear-arbitrary-precision": "^3.1.0", + "linear-presets-temperature": "^2.0.3", + "lodash.isfunction": "^3.0.6", + "rimraf": "^2.4.3", + "should": "^7.1.1" + }, + "dependencies": { + "linear-converter": "^7.0.2", + "unit-preset": "^1.0.1", + "unit-scale": "^1.0.1" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..3d006e5 --- /dev/null +++ b/src/index.js @@ -0,0 +1,27 @@ +/*jshint node:true */ + +'use strict'; + +var lcFactory = require('linear-converter'); +var unitScale = require('unit-scale'); +var unitPreset = require('unit-preset'); + +module.exports = function factory(Decimal) { + var lc = lcFactory(Decimal); + + return function coefficients(a, b) { + var decimalA = new Decimal(a.toString()); + var decimalB = new Decimal(b.toString()); + + return lc.composeConversions( + [ + unitScale, + [ + decimalB, + decimalA.plus(decimalB) + ] + ], + unitPreset + ); + }; +}; diff --git a/test/spec.js b/test/spec.js new file mode 100644 index 0000000..09f1cec --- /dev/null +++ b/test/spec.js @@ -0,0 +1,28 @@ +/*jshint node:true, mocha:true */ + +'use strict'; + +require('should'); + +var adapter = require('floating-adapter'); +var Decimal = require('linear-arbitrary-precision')(adapter); +var coefficients = require('../src/')(Decimal); +var isFunction = require('lodash.isfunction'); + +function scaleVals(scale) { + return scale.map(Number); +} + +describe('coefficients', function() { + it('creates presets from coefficients', function() { + coefficients(9/5, 32).map(scaleVals).should.eql([[0, 1], [32, 33.8]]); + coefficients(-5, -2).map(scaleVals).should.eql([[0, 1], [-2, -7]]); + coefficients(757, -567).map(scaleVals).should.eql([[0, 1], [-567, 190]]); + }); + + it('creates presets with Decimals', function() { + coefficients(9/5, 32).every(function(scale) { + return [scale[0].plus, scale[1].plus].every(isFunction); + }).should.be.exactly(true); + }); +});