├── test ├── to-ms.js ├── mocha.opts └── test.js ├── index.js ├── .gitignore ├── .travis.yml ├── .jshintrc ├── bower.json ├── lib └── to-ms.js ├── package.json ├── dist └── to-ms.js └── readme.md /test/to-ms.js: -------------------------------------------------------------------------------- 1 | ../dest/to-ms.js -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/to-ms"); 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | ._* 3 | node_modules 4 | .nyc_output 5 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --reporter spec 2 | --colors 3 | --require should 4 | --ui bdd 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "10" 4 | after_success: 5 | - ./node_modules/.bin/mocha test -R mocha-lcov-reporter 6 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "browser": false, 3 | "curly": true, 4 | "expr": true, 5 | "indent": false, 6 | "jquery": false, 7 | "laxcomma": true, 8 | "laxbreak": true, 9 | "maxcomplexity": 10, 10 | "maxdepth": 3, 11 | "maxparams": 4, 12 | "node": true, 13 | "trailing": true, 14 | "quotmark": "single", 15 | "strict": true, 16 | "undef": true 17 | } 18 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "to-ms", 3 | "main": "dist/to-ms.js", 4 | "version": "1.1.0", 5 | "homepage": "https://github.com/shuvalov-anton/to-ms", 6 | "authors": ["Shuvalov Anton "], 7 | "description": "Tiny chained milliseconds creation util", 8 | "keywords": [ 9 | "ms", 10 | "milliseconds", 11 | "chaining", 12 | "setTimeout", 13 | "setInterval", 14 | "time", 15 | "years", 16 | "weeks", 17 | "days", 18 | "hours", 19 | "minutes", 20 | "seconds" 21 | ], 22 | "license": "MIT", 23 | "ignore": [ 24 | "**/.*", 25 | "node_modules", 26 | "bower_components", 27 | "test", 28 | "tests", 29 | "coverage.html" 30 | ] 31 | } 32 | -------------------------------------------------------------------------------- /lib/to-ms.js: -------------------------------------------------------------------------------- 1 | /* jshint -W053 */ 2 | 3 | "use strict"; 4 | 5 | module.exports = extended(new Number(0)); 6 | 7 | function extended(number) { 8 | number.second = addMs.bind(number, 1e3, 1); 9 | number.seconds = addMs.bind(number, 1e3); 10 | number.minute = addMs.bind(number, 6e4, 1); 11 | number.minutes = addMs.bind(number, 6e4); 12 | number.hour = addMs.bind(number, 36e5, 1); 13 | number.hours = addMs.bind(number, 36e5); 14 | number.day = addMs.bind(number, 864e5, 1); 15 | number.days = addMs.bind(number, 864e5); 16 | number.week = addMs.bind(number, 6048e5, 1); 17 | number.weeks = addMs.bind(number, 6048e5); 18 | number.year = addMs.bind(number, 31536e6, 1); 19 | number.years = addMs.bind(number, 31536e6); 20 | return number; 21 | } 22 | 23 | function addMs(mlp, val) { 24 | val = mlp * (+val || 0); 25 | return extended(new Number(this + val)); 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "to-ms", 3 | "version": "1.1.0", 4 | "description": "Tiny chained ms creation util", 5 | "main": "index.js", 6 | "directories": { 7 | "test": "tests" 8 | }, 9 | "scripts": { 10 | "test": "nyc mocha", 11 | "make": "umd toMs -c < ./lib/to-ms.js > dist/to-ms.js" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git@github.com:shuvalov-anton/to-ms.git" 16 | }, 17 | "keywords": [ 18 | "ms", 19 | "milliseconds", 20 | "chaining", 21 | "setTimeout", 22 | "setInterval", 23 | "time", 24 | "days", 25 | "hours", 26 | "minutes", 27 | "seconds" 28 | ], 29 | "author": "Shuvalov Anton", 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/shuvalov-anton/to-ms/issues" 33 | }, 34 | "homepage": "https://github.com/shuvalov-anton/to-ms", 35 | "devDependencies": { 36 | "mocha": "^6.1.4", 37 | "mocha-lcov-reporter": "1.3.0", 38 | "nyc": "^14.1.1", 39 | "should": "^13.2.3", 40 | "umd": "^3.0.3" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /dist/to-ms.js: -------------------------------------------------------------------------------- 1 | !(function(e) { 2 | if ("object" == typeof exports && "undefined" != typeof module) 3 | module.exports = e(); 4 | else if ("function" == typeof define && define.amd) define([], e); 5 | else { 6 | var f; 7 | "undefined" != typeof window 8 | ? (f = window) 9 | : "undefined" != typeof global 10 | ? (f = global) 11 | : "undefined" != typeof self && (f = self), 12 | (f.toMs = e()); 13 | } 14 | })(function() { 15 | var define, module, exports; 16 | module = { exports: (exports = {}) }; 17 | /* jshint -W053 */ 18 | 19 | ("use strict"); 20 | 21 | module.exports = extended(new Number(0)); 22 | 23 | function extended(number) { 24 | number.second = addMs.bind(number, 1e3, 1); 25 | number.seconds = addMs.bind(number, 1e3); 26 | number.minute = addMs.bind(number, 6e4, 1); 27 | number.minutes = addMs.bind(number, 6e4); 28 | number.hour = addMs.bind(number, 36e5, 1); 29 | number.hours = addMs.bind(number, 36e5); 30 | number.day = addMs.bind(number, 864e5, 1); 31 | number.days = addMs.bind(number, 864e5); 32 | number.week = addMs.bind(number, 6048e5, 1); 33 | number.weeks = addMs.bind(number, 6048e5); 34 | number.year = addMs.bind(number, 31536e6, 1); 35 | number.years = addMs.bind(number, 31536e6); 36 | return number; 37 | } 38 | 39 | function addMs(mlp, val) { 40 | val = mlp * (+val || 0); 41 | return extended(new Number(this + val)); 42 | } 43 | 44 | return module.exports; 45 | }); 46 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # toMs 2 | 3 | [![Build Status](https://travis-ci.org/shuvalov-anton/to-ms.svg)](https://travis-ci.org/shuvalov-anton/to-ms) 4 | [![Code Climate](https://codeclimate.com/github/shuvalov-anton/to-ms/badges/gpa.svg)](https://codeclimate.com/github/shuvalov-anton/to-ms) 5 | [![Coverage Status](https://coveralls.io/repos/shuvalov-anton/to-ms/badge.png?branch=master)](https://coveralls.io/r/shuvalov-anton/to-ms?branch=master) 6 | 7 | [![NPM](https://nodei.co/npm/to-ms.png?downloads=true)](https://nodei.co/npm/to-ms/) 8 | 9 | Simple and human friendly millisecond implementation with awesome chaining. Thanks [@subzey][1] for a sketch and advise :D 10 | 11 | [1]: https://github.com/subzey 12 | 13 | ## Install 14 | 15 | ``` 16 | npm install to-ms 17 | ``` 18 | 19 | ## Examples 20 | 21 | ```js 22 | var toMs = require('to-ms'); 23 | 24 | setTimeout( 25 | function () { 26 | /* do something */ 27 | }, toMs 28 | .hour() 29 | .minutes(15) 30 | .seconds(30) 31 | ); 32 | ``` 33 | 34 | ## Chaining 35 | 36 | All methods are chainable. I mean, you can pretty simple write something like this: 37 | 38 | ```js 39 | var ms = toMs 40 | .days(30) 41 | .hours(10) 42 | .minutes(30) 43 | .seconds(45); 44 | ``` 45 | 46 | ## Browser Support 47 | 48 | Just use `window.toMs`. To support legacy browsers you must use [ES5-Shim][2]. 49 | 50 | [2]: https://github.com/es-shims/es5-shim 51 | 52 | ## Methods 53 | 54 | - `toMs.second()` 55 | - `toMs.seconds(Number)` 56 | - `toMs.minute()` 57 | - `toMs.minutes(Number)` 58 | - `toMs.hour()` 59 | - `toMs.hours(Number)` 60 | - `toMs.day()` 61 | - `toMs.days(Number)` 62 | - `toMs.week()` 63 | - `toMs.weeks(Number)` 64 | - `toMs.year()` 65 | - `toMs.years(Number)` 66 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const should = require("should"); 2 | 3 | /* globals toMs, should, it, describe, window */ 4 | 5 | 'use strict'; 6 | 7 | /** 8 | * Dependencies 9 | */ 10 | var isNode = typeof window === 'undefined'; 11 | if (isNode) { global.toMs = require('../'); } 12 | 13 | // End dependencies 14 | 15 | describe('to-ms', function () { 16 | it('should be a Number', function () { 17 | toMs.should.be.a.Number; 18 | }); 19 | describe('seconds()', function () { 20 | var ms; 21 | it('should returns a number', function () { 22 | ms = toMs.seconds('60'); 23 | ms.should.be.a.Number; 24 | }); 25 | it('should returns correct ms value', function () { 26 | ms.should.be.equal(60 * 1000); 27 | }); 28 | }); 29 | describe('second()', function () { 30 | var ms; 31 | it('should returns a number', function () { 32 | ms = toMs.second(); 33 | ms.should.be.a.Number; 34 | }); 35 | it('should returns correct ms value', function () { 36 | ms.should.be.equal(1000); 37 | }); 38 | }); 39 | describe('minute()', function () { 40 | var ms; 41 | it('should returns a number', function () { 42 | ms = toMs.minute(); 43 | ms.should.be.a.Number; 44 | }); 45 | it('should returns correct ms value', function () { 46 | ms.should.be.equal(60 * 1000); 47 | }); 48 | }); 49 | describe('minutes()', function () { 50 | var ms; 51 | it('should returns a number', function () { 52 | ms = toMs.minutes('60'); 53 | ms.should.be.a.Number; 54 | }); 55 | it('should returns correct ms value', function () { 56 | ms.should.be.equal(60 * 60 * 1000); 57 | }); 58 | }); 59 | describe('hour()', function () { 60 | var ms; 61 | it('should returns a number', function () { 62 | ms = toMs.hour(); 63 | ms.should.be.a.Number; 64 | }); 65 | it('should returns correct ms value', function () { 66 | ms.should.be.equal(60 * 60 * 1000); 67 | }); 68 | }); 69 | describe('hours()', function () { 70 | var ms; 71 | it('should returns a number', function () { 72 | ms = toMs.hours('24'); 73 | ms.should.be.a.Number; 74 | }); 75 | it('should returns correct ms value', function () { 76 | ms.should.be.equal(24 * 60 * 60 * 1000); 77 | }); 78 | }); 79 | describe('day()', function () { 80 | var ms; 81 | it('should returns a number', function () { 82 | ms = toMs.day(); 83 | ms.should.be.a.Number; 84 | }); 85 | it('should returns correct ms value', function () { 86 | ms.should.be.equal(24 * 60 * 60 * 1000); 87 | }); 88 | }); 89 | describe('days()', function () { 90 | var ms; 91 | it('should returns a number', function () { 92 | ms = toMs.days('7'); 93 | ms.should.be.a.Number; 94 | }); 95 | it('should returns correct ms value', function () { 96 | ms.should.be.equal(7 * 24 * 60 * 60 * 1000); 97 | }); 98 | }); 99 | describe('week()', function () { 100 | var ms; 101 | it('should returns a number', function () { 102 | ms = toMs.week(); 103 | ms.should.be.a.Number; 104 | }); 105 | it('should returns correct ms value', function () { 106 | ms.should.be.equal(7 * 24 * 60 * 60 * 1000); 107 | }); 108 | }); 109 | describe('weeks()', function () { 110 | var ms; 111 | it('should returns a number', function () { 112 | ms = toMs.weeks('7'); 113 | ms.should.be.a.Number; 114 | }); 115 | it('should returns correct ms value', function () { 116 | ms.should.be.equal(7 * 7 * 24 * 60 * 60 * 1000); 117 | }); 118 | }); 119 | describe('year()', function () { 120 | var ms; 121 | it('should returns a number', function () { 122 | ms = toMs.year(); 123 | ms.should.be.a.Number; 124 | }); 125 | it('should returns correct ms value', function () { 126 | ms.should.be.equal(365 * 24 * 60 * 60 * 1000); 127 | }); 128 | }); 129 | describe('years()', function () { 130 | var ms; 131 | it('should returns a number', function () { 132 | ms = toMs.years(3); 133 | ms.should.be.a.Number; 134 | }); 135 | it('should returns correct ms value', function () { 136 | ms.should.be.equal(3 * 365 * 24 * 60 * 60 * 1000); 137 | }); 138 | }); 139 | describe('chaining', function () { 140 | var ms; 141 | it('methods should chains', function () { 142 | ms = toMs 143 | .days(2) 144 | .hours(12) 145 | .minutes(20) 146 | .seconds(15); 147 | ms.should.be.a.Number; 148 | }); 149 | it('singles shoulds chains too', function () { 150 | ms = toMs 151 | .day() 152 | .hour() 153 | .minute() 154 | .second(); 155 | ms.should.be.a.Number; 156 | }); 157 | }); 158 | }); 159 | --------------------------------------------------------------------------------