cleaner-node
Advanced tools
Comparing version 0.3.2 to 0.3.3
@@ -104,3 +104,3 @@ module.exports = { | ||
"jsx-quotes": "error", | ||
"key-spacing": "error", | ||
"key-spacing": "off", | ||
"keyword-spacing": [ | ||
@@ -130,3 +130,3 @@ "error", | ||
"max-statements-per-line": "off", | ||
"multiline-comment-style": "error", | ||
"multiline-comment-style": "off", | ||
"new-cap": "error", | ||
@@ -179,3 +179,3 @@ "new-parens": "error", | ||
"no-multi-assign": "error", | ||
"no-multi-spaces": "error", | ||
"no-multi-spaces": "off", | ||
"no-multi-str": "error", | ||
@@ -182,0 +182,0 @@ "no-multiple-empty-lines": "error", |
{ | ||
"name": "cleaner-node", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "Helpful utilities and scripts to make Node projects more legible and easier for the next developer to take over.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -65,2 +65,7 @@ const moment = require('moment'); | ||
const isUnix = value => { | ||
if (isNaN(value)) { return false; } | ||
return isValid(fromUnixDateStamp(value)); | ||
} | ||
module.exports = { | ||
@@ -73,2 +78,4 @@ isValid, | ||
toBlockDate, | ||
isUnix, | ||
isEpoch : isUnix, | ||
toUnixDateStamp, | ||
@@ -75,0 +82,0 @@ toUnix: toUnixDateStamp, |
@@ -15,2 +15,3 @@ const arrays = require('./arrays'); | ||
const strings = require('./strings'); | ||
const urls = require('./urls'); | ||
const uuids = require('./uuids'); | ||
@@ -33,3 +34,4 @@ | ||
strings, | ||
urls, | ||
uuids | ||
}; |
const jwt = require('jsonwebtoken'); | ||
const dates = require('./dates'); | ||
const strings = require('./strings'); | ||
@@ -12,7 +14,71 @@ const decode = (token, secret, ignoreExpiration = true) => { | ||
const sign = jwt.sign; | ||
const sign = (payload, certOrSecret, options) => { | ||
return jwt.sign(payload, certOrSecret, options); | ||
}; | ||
const toPayload = token => { | ||
return jwt.decode(token); | ||
} | ||
const toId = value => { | ||
if (typeof value === 'undefined') { return undefined; } | ||
if (!isNaN(value)) { return Number(value); } | ||
if (strings.isValid(value)) { return value; } | ||
return undefined; | ||
} | ||
const toDate = value => { | ||
if (isNaN(value)) { return undefined; } | ||
return dates.fromUnix(value); | ||
} | ||
const toDetails = payloadOrToken => { | ||
const payload = (typeof payloadOrToken === 'string') | ||
? toPayload(payloadOrToken) | ||
: payloadOrToken; | ||
if (typeof payload !== 'object') { return undefined; } | ||
return { | ||
userId : toId(payload.sub), | ||
accountId : toId(payload.aid), | ||
sessionId : toId(payload.iss), | ||
created : toDate(payload.iat), | ||
expiry : toDate(payload.exp) | ||
}; | ||
} | ||
const isValidPayload = value => { | ||
return ( | ||
typeof value === 'object' && | ||
!(value instanceof Array) && | ||
(strings.isValid(value.sub) || !isNaN(value.sub)) && | ||
(typeof value.aid === 'undefined' || strings.isValid(value.sub) || !isNaN(value.sub)) && | ||
(strings.isValid(value.iss) || !isNaN(value.iss)) && | ||
(typeof value.iat !== 'undefined' && !isNaN(value.iat)) && | ||
(typeof value.exp !== 'undefined' && !isNaN(value.exp)) | ||
); | ||
} | ||
const toToken = (userId, accountId, sessionId, createdDate, expiryDate, secret) => { | ||
if (typeof secret === 'undefined' && typeof accountId !== 'undefined') { | ||
secret = expiryDate; | ||
expiryDate = createdDate; | ||
createdDate = sessionId; | ||
accountId = undefined; | ||
} | ||
const payload = { | ||
sub : userId, | ||
aid : accountId, | ||
iss : sessionId, | ||
iat : dates.toUnix(createdDate), | ||
exp : dates.toUnix(expiryDate) | ||
}; | ||
return jwt.sign(payload, secret); | ||
} | ||
module.exports = { | ||
decode, | ||
sign | ||
sign, | ||
toDetails, | ||
toPayload, | ||
toToken, | ||
isValidPayload | ||
} |
@@ -0,1 +1,3 @@ | ||
const strings = require('./strings'); | ||
const getId = item => { | ||
@@ -71,2 +73,23 @@ if (typeof item === 'object' && item instanceof Array) { return undefined; } | ||
const toDto = value => { | ||
if (typeof value === 'undefined') { return value; } | ||
if (value instanceof Array) { return value; } | ||
const copy = JSON.parse(JSON.stringify(value)); | ||
const all = Object.keys(copy).filter(strings.isValid); | ||
// eslint-disable-next-line no-eq-null | ||
const toRemove = all.filter(k => (copy[k] == null)); | ||
if (toRemove.includes('id') && !toRemove.includes('uid') && Object.keys(copy).includes('uid')) { | ||
toRemove('id'); | ||
} | ||
toRemove.forEach(key => { | ||
Reflect.deleteProperty(copy, key); | ||
}); | ||
return copy | ||
} | ||
const toDtos = values => { | ||
return [].concat(values) | ||
.filter(v => (typeof v !== 'undefined')) | ||
.map(v => (toDto(v))); | ||
} | ||
module.exports = { | ||
@@ -79,3 +102,5 @@ findOne, | ||
isDefined, | ||
notDefined | ||
notDefined, | ||
toDto, | ||
toDtos | ||
}; |
@@ -7,3 +7,3 @@ const cc = require('camelcase'); | ||
const { returnValue } = require('./common'); | ||
const { ALPHA, DIGITS, ALPHANUMERIC } = require('./constants'); | ||
const { ALPHA, DIGITS, ALPHANUMERIC } = require('./constants').strings; | ||
@@ -10,0 +10,0 @@ const isPossible = value => { |
@@ -135,3 +135,5 @@ const uuidV1 = require('uuid/v1'); | ||
toGuid : toGuidFormat, | ||
toGuidFormat, | ||
toUid : toUidFormat, | ||
toUidFormat, | ||
@@ -138,0 +140,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51577
23
1262