famulus
Advanced tools
Comparing version 1.3.3 to 2.0.0
@@ -33,7 +33,8 @@ # Contributing | ||
- Navigate to the cloned directory | ||
- Install all dependencies `npm install` | ||
- Crate a new branch for the feature `git checkout -b new-feature` | ||
- Make changes | ||
- Run tests `npm run test` | ||
- Commit changes `git commit -am 'What is feature about? :)'` | ||
- Push to the branch `git push origin new-feature` | ||
- Submit a PR | ||
@@ -1,18 +0,13 @@ | ||
var MILLISECONDS_IN_DAY = 86400000; | ||
var MILLISECONDS_IN_HOUR = 3600000; | ||
var MILLISECONDS_IN_MINUTE = 60000; | ||
var dates = require('./.internal/dates'); | ||
var HOURS_IN_DAY = 24; | ||
var MINUTES_IN_DAY = 1440; | ||
var MINUTES_IN_HOUR = 60; | ||
/** | ||
* Difference between now and date which is passed, in formats 'milliseconds', 'days', 'hours', 'minutes' | ||
* Difference between dates which are passed, in formats 'milliseconds', 'days', 'hours', 'minutes' | ||
* | ||
* @customNeeds - | ||
* | ||
* @since 1.2.0 | ||
* @since 2.0.0 | ||
* @category Date | ||
* | ||
* @param {Date} date - The Date to inspect | ||
* @param {Date} date1 - The Date for compare | ||
* @param {Date} date2 - The Date for compare | ||
* @param {String} differenceType - [ 'days', 'hours', 'minutes', 'milliseconds', 'all' ] | ||
@@ -23,24 +18,28 @@ * | ||
* @example | ||
* example result for now is new Date('12-26-2017') | ||
* | ||
* famulus.dateDifference(new Date('12-20-2017'), 'milliseconds') | ||
* // => 555261242 | ||
* | ||
* famulus.dateDifference(new Date('12-20-2017'), 'days') | ||
* famulus.dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'days') | ||
* // => 6 | ||
* | ||
* famulus.dateDifference(new Date('12-20-2017'), 'hours') | ||
* // => 156 | ||
* famulus.dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'hours') | ||
* // => 144 | ||
* | ||
* famulus.dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'minutes') | ||
* // => 8640 | ||
* | ||
* famulus.dateDifference(new Date('06-26-2018'), new Date('06-20-2018'), 'milliseconds') | ||
* // => 518400000 | ||
* | ||
* famulus.dateDifference(new Date('06-26-2018 10:10'), new Date('06-20-2018 08:00'), 'all') | ||
* // => {days: 6, hours: 2, minutes: 10, milliseconds: 526200000} | ||
*/ | ||
function dateDifference(date, differenceType) { | ||
var now = new Date(), | ||
diffMilliseconds = Math.abs(date - now); | ||
function dateDifference(date1, date2, differenceType) { | ||
var diffMilliseconds = Math.abs(date1 - date2); | ||
switch(differenceType) { | ||
case 'days': | ||
return _getDaysDiff(diffMilliseconds); | ||
return dates._getDaysDiff(diffMilliseconds); | ||
case 'hours': | ||
return _differenceInHours(diffMilliseconds); | ||
return dates._differenceInHours(diffMilliseconds); | ||
case 'minutes': | ||
return _differenceInMinutes(diffMilliseconds); | ||
return dates._differenceInMinutes(diffMilliseconds); | ||
case 'milliseconds': | ||
@@ -51,5 +50,5 @@ return diffMilliseconds; | ||
return { | ||
days: _getDaysDiff(diffMilliseconds), | ||
hours: _getHoursDiff(diffMilliseconds), | ||
minutes: _getMinutesDiff(diffMilliseconds), | ||
days: dates._getDaysDiff(diffMilliseconds), | ||
hours: dates._getHoursDiff(diffMilliseconds), | ||
minutes: dates._getMinutesDiff(diffMilliseconds), | ||
milliseconds: diffMilliseconds | ||
@@ -60,64 +59,2 @@ } | ||
/** | ||
* @param {Number} milliseconds | ||
* @return {Number} | ||
* @private | ||
*/ | ||
function _getDaysDiff(milliseconds) { | ||
return Math.abs(Math.floor(milliseconds / MILLISECONDS_IN_DAY)); | ||
} | ||
/** | ||
* @param {Number} milliseconds | ||
* @return {Number} | ||
* @private | ||
*/ | ||
function _getHoursDiff(milliseconds) { | ||
return Math.abs(Math.floor((milliseconds % MILLISECONDS_IN_DAY) / MILLISECONDS_IN_HOUR)); | ||
} | ||
/** | ||
* @param {Number} milliseconds | ||
* @return {Number} | ||
* @private | ||
*/ | ||
function _getMinutesDiff(milliseconds) { | ||
return Math.abs(Math.round(((milliseconds % MILLISECONDS_IN_DAY) % MILLISECONDS_IN_HOUR) / MILLISECONDS_IN_MINUTE)); | ||
} | ||
/** | ||
* @param {Number} milliseconds | ||
* @return {Number} | ||
* @private | ||
*/ | ||
function _differenceInHours(milliseconds) { | ||
var days = _getDaysDiff(milliseconds); | ||
if (days !== 0) { | ||
return _getHoursDiff(milliseconds) + (days * HOURS_IN_DAY); | ||
} | ||
return _getHoursDiff(milliseconds); | ||
} | ||
/** | ||
* @param {Number} milliseconds | ||
* @return {Number} | ||
* @private | ||
*/ | ||
function _differenceInMinutes(milliseconds) { | ||
var days = _getDaysDiff(milliseconds), | ||
hours = _getHoursDiff(milliseconds); | ||
if (days !== 0) { | ||
days = days * MINUTES_IN_DAY; | ||
} | ||
if (hours !== 0) { | ||
hours = hours * MINUTES_IN_HOUR; | ||
} | ||
return _getMinutesDiff(milliseconds) + days + hours; | ||
} | ||
module.exports = dateDifference; |
@@ -1,5 +0,5 @@ | ||
## dateDifference(date, differenceType) | ||
## dateDifference(date1, date2, differenceType) | ||
[Source](../dateDifference.js) | ||
Difference between now and date which is passed, in formats 'milliseconds', 'days', 'hours', 'minutes' | ||
Difference between dates which are passed, in formats 'milliseconds', 'days', 'hours', 'minutes' | ||
@@ -9,9 +9,10 @@ #### Custom Needs | ||
#### Since | ||
1.2.0 | ||
2.0.0 | ||
#### Category | ||
#### Category | ||
Date | ||
#### Arguments | ||
{Date} date - The Date to inspect<br> | ||
{Date} date1 - The Date for compare<br> | ||
{Date} date2 - The Date for compare<br> | ||
{String} differenceType - [ 'days', 'hours', 'minutes', 'milliseconds', 'all' ] | ||
@@ -23,6 +24,5 @@ | ||
#### Example | ||
Example result for now is Date('12-26-2017') | ||
```javascript | ||
dateDifference(new Date('12-20-2017'), 'days') | ||
dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'days') | ||
// => 6 | ||
@@ -32,19 +32,19 @@ ``` | ||
```javascript | ||
dateDifference(new Date('12-20-2017'), 'hours') | ||
// => 156 | ||
dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'hours') | ||
// => 144 | ||
``` | ||
```javascript | ||
dateDifference(new Date('12-20-2017'), 'minutes') | ||
// => 9381 | ||
dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'minutes') | ||
// => 8640 | ||
``` | ||
```javascript | ||
dateDifference(new Date('12-20-2017'), 'milliseconds') | ||
// => 555261242 | ||
dateDifference(new Date('06-26-2018'), new Date('06-20-2018'), 'milliseconds') | ||
// => 518400000 | ||
``` | ||
```javascript | ||
dateDifference(new Date('12-20-2017'), 'all') | ||
// => {days: 6, hours: 12, minutes: 30, milliseconds: 563406381} | ||
``` | ||
dateDifference(new Date('06-26-2018 10:10'), new Date('06-20-2018 08:00'), 'all') | ||
// => {days: 6, hours: 2, minutes: 10, milliseconds: 526200000} | ||
``` |
var substr = require('./substr'); | ||
var sortAndAddFirstElement = require('./sortAndAddFirstElement'); | ||
var dateDifference = require('./dateDifference'); | ||
var dateDifferenceFromNow = require('./dateDifferenceFromNow'); | ||
var isValuesUnique = require('./isValuesUnique'); | ||
@@ -9,4 +9,4 @@ | ||
sortAndAddFirstElement: sortAndAddFirstElement, | ||
dateDifference: dateDifference, | ||
dateDifferenceFromNow: dateDifferenceFromNow, | ||
isValuesUnique: isValuesUnique | ||
}; |
@@ -14,3 +14,3 @@ var _uniqBy = require('lodash/uniqBy'); | ||
* @param {String} keyName - Name of the object property from an array in which unique will be checking | ||
* | ||
* | ||
* @returns {Boolean} Returns true if values are unique and false if not | ||
@@ -17,0 +17,0 @@ * |
{ | ||
"name": "famulus", | ||
"version": "1.3.3", | ||
"version": "2.0.0", | ||
"description": "JavaScript library that provides a useful functional programming helpers. Add your own.", | ||
@@ -5,0 +5,0 @@ "main": "famulus.js", |
@@ -41,2 +41,3 @@ # famulus [![Twitter URL](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?hashtags=javascript%20%23helpers%20%23utilities&original_referer=https%3A%2F%2Fpublish.twitter.com%2F&ref_src=twsrc%5Etfw&text=JavaScript%20library%20that%20provides%20a%20useful%20functional%20programming%20helpers.%20Do%20not%20wait%2C%20add%20your%20own%20%F0%9F%94%A7%F0%9F%92%AA&tw_p=tweetbutton&url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Ffamulus&via=shystrukk) # | ||
### Date | ||
- [dateDifferenceFromNow](docs/dateDifferenceFromNow.md) | ||
- [dateDifference](docs/dateDifference.md) | ||
@@ -43,0 +44,0 @@ |
import test from 'ava'; | ||
import dateDifference from './../dateDifference'; | ||
import dateDifference from '../dateDifference'; | ||
@@ -8,20 +8,22 @@ test('module should be a function', t => { | ||
test('should return Number: days', t => { | ||
t.is(typeof dateDifference(new Date('12-20-2017'), 'days'), 'number'); | ||
test('should return difference in days', t => { | ||
t.is(dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'days'), 6); | ||
}); | ||
test('should return Number: hours', t => { | ||
t.is(typeof dateDifference(new Date('12-20-2017'), 'hours'), 'number'); | ||
test('should return difference in hours', t => { | ||
t.is(dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'hours'), 144); | ||
t.is(dateDifference(new Date('06-26-2018 10:00'), new Date('06-26-2018 08:00'), 'hours'), 2) | ||
}); | ||
test('should return Number: minutes', t => { | ||
t.is(typeof dateDifference(new Date('12-20-2017'), 'minutes'), 'number'); | ||
test('should return difference in minutes', t => { | ||
t.is(dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'minutes'), 8640); | ||
}); | ||
test('should return Number: milliseconds', t => { | ||
t.is(typeof dateDifference(new Date('12-20-2017'), 'milliseconds'), 'number'); | ||
test('should return difference in milliseconds', t => { | ||
t.is(dateDifference(new Date('06-26-2018'), new Date('06-20-2018'), 'milliseconds'), 518400000); | ||
}); | ||
test('should return Object: all', t => { | ||
t.deepEqual(typeof dateDifference(new Date('12-20-2017'), 'all'), 'object'); | ||
test('should return all difference types in object', t => { | ||
const diffResult = { days: 6, hours: 2, minutes: 10, milliseconds: 526200000 }; | ||
t.deepEqual(dateDifference(new Date('06-26-2018 10:10'), new Date('06-20-2018 08:00'), 'all'), diffResult); | ||
}); |
Sorry, the diff of this file is not supported yet
24
340
51
21706