Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate iat param and document the function #895

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/psSupported.js
@@ -1,3 +1,9 @@
var semver = require('semver');

/**
* Checks if the current Node.js version is supported by this library.
*
* @returns {boolean}
*/

module.exports = semver.satisfies(process.version, '^6.12.0 || >=8.0.0');
30 changes: 22 additions & 8 deletions lib/timespan.js
@@ -1,18 +1,32 @@
var ms = require('ms');

/**
*
* - Returns the timespan of *time* from *iat*
* - if *iat* is undefined it return the timespan of time from current time
* - if params are invalid, it returns undefined
*
* @param {string|number} time -> 1m,1h...
* @param {number} iat -> in ms
* @returns {number} in s
*
*/
module.exports = function (time, iat) {
var timestamp = iat || Math.floor(Date.now() / 1000);

if (typeof iat != 'number')
return

var timestamp = iat || Math.floor(Date.now() / 1000)

if (typeof time === 'string') {
var milliseconds = ms(time);
if (typeof milliseconds === 'undefined') {
return;
}
return Math.floor(timestamp + milliseconds / 1000);
var milliseconds = ms(time)
if (typeof milliseconds === 'undefined')
return
return Math.floor(timestamp + milliseconds / 1000)
} else if (typeof time === 'number') {
return timestamp + time;
return timestamp + time
} else {
return;
return
}

};