From 5279250b6479e8dd5785a8b4d7bb0d8817b2815d Mon Sep 17 00:00:00 2001 From: Javier Cejudo Date: Tue, 28 Jul 2015 00:18:02 +1000 Subject: [PATCH] first commit --- .gitignore | 29 +++++++++++++++++++++++ .travis.yml | 9 +++++++ LICENSE | 22 +++++++++++++++++ README.md | 33 ++++++++++++++++++++++++++ data/presets.json | 14 +++++++++++ gulpfile.js | 28 ++++++++++++++++++++++ package.json | 35 +++++++++++++++++++++++++++ src/linear-presets-time.js | 5 ++++ test/presets.js | 48 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 223 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 data/presets.json create mode 100644 gulpfile.js create mode 100644 package.json create mode 100644 src/linear-presets-time.js create mode 100644 test/presets.js 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..c5fe12a --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# linear-presets-time + +[![Build Status](https://travis-ci.org/javiercejudo/linear-presets-time.svg)](https://travis-ci.org/javiercejudo/linear-presets-time) +[![Coverage Status](https://coveralls.io/repos/javiercejudo/linear-presets-time/badge.svg?branch=master)](https://coveralls.io/r/javiercejudo/linear-presets-time?branch=master) +[![Code Climate](https://codeclimate.com/github/javiercejudo/linear-presets-time/badges/gpa.svg)](https://codeclimate.com/github/javiercejudo/linear-presets-time) + +Linear presets for time units + +## Install + + npm i linear-presets-time + +## Presets + +Base: [Celsius](https://en.wikipedia.org/wiki/Celsius) + +- [Nanosecond](https://en.wikipedia.org/wiki/Nanosecond) +- [Microsecond](https://en.wikipedia.org/wiki/Microsecond) +- [Millisecond](https://en.wikipedia.org/wiki/Millisecond) +- [Minute](https://en.wikipedia.org/wiki/Minute) +- [Hour](https://en.wikipedia.org/wiki/Hour) +- [Day](https://en.wikipedia.org/wiki/Day) +- [Week](https://en.wikipedia.org/wiki/Week) +- [Month](https://en.wikipedia.org/wiki/Month) +- [Year](https://en.wikipedia.org/wiki/Year) +- [Decade](https://en.wikipedia.org/wiki/Decade) +- [Century](https://en.wikipedia.org/wiki/Century) +- [Millennium](https://en.wikipedia.org/wiki/Millennium) + +## 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/data/presets.json b/data/presets.json new file mode 100644 index 0000000..a07a461 --- /dev/null +++ b/data/presets.json @@ -0,0 +1,14 @@ +{ + "secondToNanosecond": [[0, 1], [0, 1e9]], + "secondToMicrosecond": [[0, 1], [0, 1e6]], + "secondToMillisecond": [[0, 1], [0, 1e3]], + "secondToMinute": [[0, 60], [0, 1]], + "secondToHour": [[0, 3600], [0, 1]], + "secondToDay": [[0, 86400], [0, 1]], + "secondToWeek": [[0, 604800], [0, 1]], + "secondToMonth": [[0, 2629746], [0, 1]], + "secondToYear": [[0, 31556952], [0, 1]], + "secondToDecade": [[0, 315569520], [0, 1]], + "secondToCentury": [[0, 3155695200], [0, 1]], + "secondToMillennium": [[0, 31556952000], [0, 1]] +} 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..66a87d8 --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "linear-presets-time", + "version": "0.0.0", + "description": "Linear presets for time units", + "main": "src/linear-presets-time.js", + "scripts": { + "test": "gulp test" + }, + "repository": { + "type": "git", + "url": "https://github.com/javiercejudo/linear-presets-time" + }, + "keywords": [ + "linear-presets", + "conversion", + "time", + "scales" + ], + "author": "Javier Cejudo", + "license": "MIT", + "bugs": { + "url": "https://github.com/javiercejudo/linear-presets-time/issues" + }, + "homepage": "https://github.com/javiercejudo/linear-presets-time", + "devDependencies": { + "floating-adapter": "^1.0.0", + "gulp": "^3.8.11", + "gulp-coveralls": "^0.1.3", + "gulp-istanbul": "^0.6.0", + "gulp-mocha": "^2.0.0", + "rescale": "^6.1.0", + "rimraf": "^2.3.2", + "should": "^5.0.0" + } +} diff --git a/src/linear-presets-time.js b/src/linear-presets-time.js new file mode 100644 index 0000000..8129077 --- /dev/null +++ b/src/linear-presets-time.js @@ -0,0 +1,5 @@ +/*jshint node:true */ + +'use strict'; + +module.exports = require('../data/presets.json'); diff --git a/test/presets.js b/test/presets.js new file mode 100644 index 0000000..6023324 --- /dev/null +++ b/test/presets.js @@ -0,0 +1,48 @@ +/*jshint node:true, mocha:true */ + +'use strict'; + +require('should'); + +var rescale = require('rescale')(require('floating-adapter')).rescale; +var time = require('../src/linear-presets-time'); + +function convert(x, preset) { + return rescale(x, preset[0], preset[1]); +}; + +function invert(preset) { + return preset.slice(0).reverse(); +}; + +describe('time presets', function() { + it('should convert correctly', function() { + (259200).should.be.exactly(convert(259200000000000, invert(time.secondToNanosecond)), 'secondToNanosecond') + .and.exactly(convert(259200000000, invert(time.secondToMicrosecond)), 'secondToMicrosecond') + .and.exactly(convert(259200000000, invert(time.secondToMicrosecond)), 'secondToMicrosecond') + .and.exactly(convert(259200000, invert(time.secondToMillisecond)), 'secondToMillisecond') + .and.exactly(convert(4320, invert(time.secondToMinute)), 'secondToMinute') + .and.exactly(convert(72, invert(time.secondToHour)), 'secondToHour') + .and.exactly(convert(3, invert(time.secondToDay)), 'secondToDay') + .and.exactly(convert(0.42857142857142856, invert(time.secondToWeek)), 'secondToWeek') + .and.approximately(convert(0.09856465225158627, invert(time.secondToMonth)), 10e-11, 'secondToMonth') + .and.exactly(convert(0.008213721020965523, invert(time.secondToYear)), 'secondToYear') + .and.exactly(convert(0.0008213721020965523, invert(time.secondToDecade)), 'secondToDecade') + .and.exactly(convert(0.00008213721020965523, invert(time.secondToCentury)), 'secondToCentury') + .and.approximately(convert(0.000008213721020965522, invert(time.secondToMillennium)), 10e-11, 'secondToMillennium'); + + (0).should.be.exactly(convert(0, time.secondToNanosecond), 'secondToNanosecond') + .and.exactly(convert(0, time.secondToMicrosecond), 'secondToMicrosecond') + .and.exactly(convert(0, time.secondToMicrosecond), 'secondToMicrosecond') + .and.exactly(convert(0, time.secondToMillisecond), 'secondToMillisecond') + .and.exactly(convert(0, time.secondToMinute), 'secondToMinute') + .and.exactly(convert(0, time.secondToHour), 'secondToHour') + .and.exactly(convert(0, time.secondToDay), 'secondToDay') + .and.exactly(convert(0, time.secondToWeek), 'secondToWeek') + .and.exactly(convert(0, time.secondToMonth), 'secondToMonth') + .and.exactly(convert(0, time.secondToYear), 'secondToYear') + .and.exactly(convert(0, time.secondToDecade), 'secondToDecade') + .and.exactly(convert(0, time.secondToCentury), 'secondToCentury') + .and.exactly(convert(0, time.secondToMillennium), 'secondToMillennium'); + }); +});