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

Conversation

m-housni
Copy link

  • validate iat param to prevent unexpected output, for example: time=1, iat='1' ==> output = '11'
  • document the function
/**
 *
 * - 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) {

  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)
  } else if (typeof time === 'number') {
    return timestamp + time
  } else {
    return
  }

};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant