You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

cleaner-node

Package Overview
Dependencies
Maintainers
1
Versions
175
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.0 to 0.1.0

.eslintrc.js

21

package.json
{
"name": "cleaner-node",
"version": "0.0.0",
"version": "0.1.0",
"description": "Helpful utilities and scripts to make Node projects more legible and easier for the next developer to take over.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"lint": "eslint src/**/*.js",
"lint:fix": "eslint --fix src/**/*.js"
},

@@ -34,7 +35,21 @@ "repository": {

"dependencies": {
"camelcase": "^5.0.0",
"email-addresses": "^3.0.3",
"gravatar": "^1.6.0",
"is-html": "^1.1.0",
"jsonwebtoken": "^8.4.0",
"know-your-http-well": "^0.5.0",
"moment": "^2.22.2",
"uuid": "^3.3.2"
"semver": "^5.6.0",
"uuid": "^3.3.2",
"uuid-validate": "0.0.3"
},
"devDependencies": {
"eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0"
}
}

@@ -0,3 +1,30 @@

const count = (value) => {
return (typeof value === 'object' && value instanceof Array)
? value.length
: -1;
}
const isValid = (value, isEmptyOkay = false) => {
return (
(typeof value === 'object') &&
(value instanceof Array) &&
(isEmptyOkay || value.length > 0)
);
}
const first = value => (count(value) > 0 ? value[0] : undefined);
const last = value => (count(value) > 0 ? value[value.length - 1] : undefined);
const single = value => (count(value) === 1 ? value[0] : undefined);
const toArray = valueOrValues => ([].concat(valueOrValues).filter(x => (typeof x !== 'undefined')));
module.exports = {
count,
isValid,
first,
last,
single,
};
toArray,
init: toArray,
parse: toArray
}

@@ -1,3 +0,31 @@

module.exports = {
const TRUE_STRINGS = ['true', 'yes', 'on', '1'];
const FALSE_STRINGS = ['false', 'no', 'off', '0'];
const parse = (value, strict = true) => {
if (typeof value === 'boolean') { return value; }
if (typeof value === 'string') {
if (TRUE_STRINGS.indexOf(value.toLowerCase())) { return true; }
if (FALSE_STRINGS.indexOf(value.toLowerCase())) { return false; }
return undefined;
}
if (typeof value === 'number') {
if (value === 1) { return true; }
if (value === 0) { return false; }
return undefined;
}
return strict ? undefined : Boolean(value);
};
const isValid = (value, strict = true) => {
return ([true, false].indexOf(parse(value, strict)) >= 0);
};
const ifValid = (value, defaultValue, strict = true) => {
return (isValid(value, strict)) ? value : defaultValue;
};
module.exports = {
parse,
isValid,
ifValid
};

9

src/constants.js

@@ -9,5 +9,10 @@ const wells = require('know-your-http-well');

},
statusCodes: wells.statusPhrasesToCodes
http: {
status: {
codes: wells.statusPhrasesToCodes,
phrases: wells.statusPhrasesToCodes
}
}
};
module.exports = me;
module.exports = me;

@@ -0,3 +1,41 @@

const crypto = require('crypto');
const constants = require('./constants');
const strings = require('./strings');
const SALT_OPTION = 'base64';
const HMAC_OPTION = 'sha1';
const DIGEST_OPTION = 'hex';
const createCode = (totalLength, chars = constants.strings.ALPHANUMERIC) => {
const rnd = crypto.randomBytes(totalLength);
const value = new Array(totalLength);
const len = chars.length;
for (let i = 0; i < totalLength; i += 1) {
value[i] = chars[rnd[i] % len];
}
return value.join('');
}
const createSalt = (byteCount, slatOption = SALT_OPTION) => {
return crypto.randomBytes(byteCount).toString(slatOption);
}
const hash = (value, salt, hmacOption = HMAC_OPTION, digestOption = DIGEST_OPTION) => {
const hmac = crypto.createHmac(hmacOption, salt);
return hmac.update(value).digest(digestOption);
}
const hashString = value => {
if (!strings.isValid(value, true)) { return undefined; }
value = strings.trimToUndefined(value);
return crypto.createHash('md5').update(value).digest('hex');
}
module.exports = {
createCode,
createSalt,
hash,
hashString
};

@@ -1,3 +0,53 @@

module.exports = {
const moment = require('moment');
const parse = value => {
const date = moment(value);
if (!date.isValid()) { return undefined; }
return date.toDate();
}
const add = (value, quantity, duration) => {
return moment(value).add(quantity, duration).toDate();
};
const subtract = (value, quantity, duration) => {
return moment(value).subtract(quantity, duration).toDate();
};
const toUnixDateStamp = value => moment(value).unix();
const fromUnixDateStamp = value => {
if (isNaN(value)) { return undefined; }
return new Date(value * 1000);
};
const min = values => {
values = [].concat(values).filter(x => (x && x instanceof Date));
if (values.length === 0) { return undefined; }
let result = values[0];
values.forEach(v => {
if (result > v) { result = v; }
});
return result;
}
const max = values => {
values = [].concat(values).filter(x => (x && x instanceof Date));
if (values.length === 0) { return undefined; }
let result = values[0];
values.forEach(v => {
if (result < v) { result = v; }
});
return result;
}
module.exports = {
parse,
add,
subtract,
toUnixDateStamp,
toUnix: toUnixDateStamp,
fromUnixDateStamp,
fromUnix: fromUnixDateStamp,
min,
max
};

@@ -0,3 +1,64 @@

const email = require('email-addresses');
const gravatar = require('gravatar');
const strings = require('./strings');
const isValid = value => {
if (!strings.isValid(value)) { return false; }
try {
const address = email.parseOneAddress(value);
return (address &&
strings.isValid(address.local) &&
strings.isValid(address.domain));
} catch (err) { return false; }
};
const getText = value => {
if (!isValid(value)) { return undefined; }
const address = email.parseOneAddress(value);
return (address.local + '@' + address.domain);
};
const isValidText = value => {
if (!isValid(value)) { return false; }
const address = getText(value);
return (address === value);
};
const getLocalPart = value => {
if (!isValid(value)) { return undefined; }
return email.parseOneAddress(value).local;
};
const isValidLocalPart = value => {
if (!strings.isValid(value)) { return false; }
if (!isValid(`${value}@nowhere.com`)) { return false; }
return getLocalPart(`${value}@nowhere.com`);
};
const getDomainName = value => {
if (!isValid(value)) { return undefined; }
return email.parseOneAddress(value).domain;
};
const isValidDomainName = value => {
if (!strings.isValid(value)) { return false; }
if (!isValid(`test@${value}`)) { return false; }
return getDomainName(`test@${value}`);
};
const toGravatarUrl = (value, options, protocol) => {
if (!isValidText(value)) { return null; }
return gravatar.url(value, options, protocol);
};
module.exports = {
isValid,
isValidText,
isValidLocalPart,
isValidDomainName,
getLocalPart,
getDomainName,
getText,
toGravatarUrl
};

@@ -0,3 +1,27 @@

const constants = require('./constants');
const util = require('util');
function ApiError(message, status, context) {
this.name = 'ApiError';
this.message = message || '';
this.status = status || constants.http.status.codes.INTERNAL_SERVER_ERROR;
Error.captureStackTrace(this, (context || ApiError));
}
util.inherits(ApiError, Error);
function init(errorOrMessage, status) {
errorOrMessage = errorOrMessage || '';
var message = (typeof errorOrMessage === 'string')
? errorOrMessage
: errorOrMessage.message;
return (new ApiError(message, status, init));
}
module.exports = {
ApiError,
init
};
const arrays = require('./arrays');
const authorization = require('./authorization');
const booleans = require('./booleans');
const common = require('./common');
const constants = require('./constants');

@@ -8,4 +10,6 @@ const crypto = require('./crypto');

const errors = require('./errors');
const jwt = require('./jwt');
const numbers = require('./numbers');
const objects = require('./objects');
const phoneNumber = require('./phone-number');
const strings = require('./strings');

@@ -16,3 +20,5 @@ const uuids = require('./uuids');

arrays,
authorization,
booleans,
common,
constants,

@@ -23,6 +29,8 @@ crypto,

errors,
jwt,
numbers,
objects,
phoneNumber,
strings,
uuids
};

@@ -0,3 +1,62 @@

const constants = require('./constants');
const DIGITS = constants.strings.DIGITS;
const sortAscending = (a, b) => {
return (a - b);
}
const sortDescending = (a, b) => {
return (b - a);
}
const toDigits = value => {
if (typeof value === 'number') {
return value.toString().split('').filter(x => DIGITS.indexOf(x) >= 0);
} else if (typeof value === 'string') {
return value.split('').filter(x => DIGITS.indexOf(x) >= 0);
} else {
return null;
}
}
const unique = values => {
const results = [];
[].concat(values).filter(x => (!isNaN(x))).forEach(x => {
if (results.indexOf(Number(x)) < 0) {
results.push(Number(x));
}
})
return results;
}
const ascending = values => {
values = [].concat(values).filter(x => (!isNaN(x))).map(x => (Number(x)));
values.sort(sortAscending);
return values;
}
const descending = values => {
values = [].concat(values).filter(x => (!isNaN(x))).map(x => (Number(x)));
values.sort(sortDescending);
return values;
}
const max = values => {
values = descending(values);
return (values.length > 0) ? values[0] : undefined;
}
const min = values => {
values = ascending(values);
return (values.length > 0) ? values[0] : undefined;
}
module.exports = {
};
toDigits,
unique,
ascending,
descending,
sortAscending,
sortDescending,
max,
min
}

@@ -0,3 +1,79 @@

const getId = item => {
if (typeof item === 'object' && item instanceof Array) { return undefined; }
if (item && item.id) { return item.id; }
const typeName = (typeof item);
if (['number', 'string'].indexOf(typeName) >= 0) { return item; }
return undefined;
}
const getIds = items => {
items = [].concat(items);
const result = [];
items.forEach(function (item) {
const id = getId(item);
if (typeof id !== 'undefined') {
result.push(id);
}
});
return result;
}
const getUid = item => {
if (item && item.uid) { return item.uid; }
if (typeof item === 'string') { return item; }
return undefined;
}
const getUids = items => {
items = [].concat(items);
const result = [];
items.forEach(function (item) {
const id = getUid(item);
if (typeof id !== 'undefined') {
result.push(id);
}
});
return result;
}
const findOne = (obj, key, cache) => {
if (typeof obj !== 'object') { return undefined; }
if (typeof key !== 'string' || key.trim().length === 0) { return undefined; }
cache = cache || { items: [] };
if (cache.items.indexOf(obj) >= 0) { return undefined; }
if (obj instanceof Array) {
for (let i = 0; i < obj.length; i += 1) {
const result = findOne(obj[i], key, cache);
if (result) { return result; }
}
return undefined;
}
if (obj[key]) { return obj[key]; }
cache.items.push(obj);
const keys = Object.keys(obj);
for (let k = 0; k < keys.length; k += 1) {
const nextKey = keys[k];
const nextObj = obj[nextKey];
const match = findOne(nextObj, key, cache);
if (match) { return match; }
}
return undefined;
}
const isDefined = value => (typeof value !== 'undefined');
const notDefined = value => (typeof value === 'undefined');
module.exports = {
findOne,
getId,
getIds,
getUid,
getUids,
isDefined,
notDefined
};

@@ -0,3 +1,181 @@

const cc = require('camelcase');
const semver = require('semver');
const isHTML = require('is-html');
const { first } = require('./arrays');
const { returnValue } = require('./common');
const { ALPHA, DIGITS, ALPHANUMERIC } = require('./constants');
const has = (value, target, isCaseSensitive = false) => {
if (typeof value !== 'string') { return false; }
if (typeof target !== 'string') { return false; }
return (isCaseSensitive && value.indexOf(target) >= 0) ||
(value.toUpperCase().indexOf(target.toUpperCase()) >= 0);
};
const isValid = (value, isEmptyOkay = false) => {
return (typeof value === 'string' &&
(isEmptyOkay || value.trim().length > 0));
}
const isValidLength = (value, min = 0, max = 0, trim = false) => {
if (!isValid(value)) { return false; }
const length = (trim ? value.trim() : value).length;
if (min > 0 && length < min) { return false; }
if (max > 0 && length > max) { return false; }
return true;
}
const clean = (value, valid = ALPHANUMERIC, invalid = '', isCaseSensitive = false) => {
if (!isValid(value, true)) { return undefined; }
return value.split('').filter(ch => ((!valid || has(valid, ch, isCaseSensitive)) &&
(!invalid || !has(invalid, ch, isCaseSensitive))
)).join('');
}
const isValidChars = (value, valid = ALPHANUMERIC, invalid = '', isCaseSensitive = false) => {
return (value === clean(value, valid, invalid, isCaseSensitive));
}
const cleanDigits = value => (clean(value, DIGITS));
const cleanAlphanumeric = value => (clean(value, ALPHA + ALPHANUMERIC));
const cleanVersion = value => {
if (!isValid(value)) { return undefined; }
try {
const version = semver(value);
return isValid(version) ? version : undefined;
} catch (ex) {
return undefined;
}
}
const isUpperCase = value => (isValid(value) && (value === value.toUpperCase()));
const isLowerCase = value => (isValid(value) && (value === value.toLowerCase()));
const toCamelCase = valueOrValues => {
const results = [].concat(valueOrValues).filter(isValid).map(value => (cc(value)));
return returnValue(valueOrValues, results);
}
const toPascalCase = valueOrValues => {
const results = [].concat(valueOrValues).filter(isValid).map(value => (cc(value, { pascalCase: true })));
return returnValue(valueOrValues, results);
}
const toLowerCase = valueOrValues => {
const results = [].concat(valueOrValues).filter(isValid).map(value => (value.toLowerCase()));
return returnValue(valueOrValues, results);
}
const toUpperCase = valueOrValues => {
const results = [].concat(valueOrValues).filter(isValid).map(value => (value.toUpperCase()));
return returnValue(valueOrValues, results);
}
const ifValid = (value, defaultValue) => {
return isValid(value) ? value : defaultValue;
}
const isAlpha = value => (isValidChars(value, ALPHA, undefined, false));
const isDigits = value => (isValidChars(value, DIGITS, undefined, false));
const isAlphanumeric = value => (isValidChars(value, ALPHANUMERIC, undefined, false));
const undouble = value => {
if (!isValid(value, true)) { return undefined; }
const chars = value.split('');
let result = value;
chars.forEach(ch => {
while (result.indexOf(`${ch}${ch}`) >= 0) {
result = result.replace(`${ch}${ch}`, ch);
}
});
return result;
}
const unique = (values, isCaseSensitive = false, trim = true) => {
const result = [];
const lower = [];
[].concat(values).filter(x => (isValid(x, true))).forEach(v => {
const value = (trim ? v.trim() : v);
if ((isCaseSensitive && result.indexOf(value) < 0) || (!isCaseSensitive && lower.indexOf(value.toLowerCase()) < 0)) {
result.push(value);
lower.push(value.toLowerCase());
}
});
return result;
}
const longest = values => {
let length = 0;
values = [].concat(values).filter(isValid);
values.forEach(x => {
if (x.trim().length > length) { length = x.trim(); }
});
return values.filter(x => (x.length === length));
}
const trim = (valueOrValues, toUndefined = false) => {
const values = [].concat(valueOrValues);
for (let i = 0; i < values.length; i += 1) {
if (typeof values[i] !== 'string') { continue; }
values[i] = values[i].trim();
if (values[i].length === 0 && toUndefined) {
values[i] = undefined;
}
}
return returnValue(valueOrValues, values);
}
const trimToUndefined = valueOrValues => {
return trim(valueOrValues, true);
}
const isPrefix = (prefix, values) => {
if (!isValid(prefix, true)) { return false; }
values = [].concat(values).filter(x => (isValid(x, true)));
if (values.length === 0) { return false; }
return (values.length === values.filter(x => (x.indexOf(prefix) === 0)).length);
}
const findPrefix = values => {
values = [].concat(values).filter(isValid);
const item = first(longest(values));
if (!item) { return undefined; }
let test = '';
let prefix = '';
item.split('').forEach(ch => {
test += ch;
if (isPrefix(test, values)) {
prefix = test;
}
});
return isValid(prefix, false) ? prefix : undefined;
}
module.exports = {
isHtml: isHTML,
has,
clean,
cleanDigits,
cleanAlphanumeric,
cleanVersion,
findPrefix,
isLowerCase,
isUpperCase,
toCamelCase,
toPascalCase,
toLowerCase,
toUpperCase,
unique,
isValid,
isValidChars,
isValidLength,
ifValid,
isAlpha,
isDigits,
isAlphanumeric,
isPrefix,
trim,
trimToUndefined,
undouble,
longest
};

@@ -0,7 +1,140 @@

const uuidV1 = require('uuid/v1');
const uuidV3 = require('uuid/v3');
const uuidV4 = require('uuid/v4');
const uuidV5 = require('uuid/v5');
const validate = require('uuid-validate');
const strings = require('./strings');
const constants = require('./constants');
const { ALPHANUMERIC } = constants.strings;
const EMPTY_GUID = '00000000-0000-0000-0000-000000000000';
const EMPTY_UID = '00000000000000000000000000000000';
const isGuidFormat = value => {
if (!strings.isValidChars(value, ALPHANUMERIC + '-', false)) {
return false;
}
const emptyParts = EMPTY_GUID.split('-');
const parts = value.split('-');
if (emptyParts.length !== parts.length) { return false; }
for (let i = 0; i < parts.length; i += 1) {
if (parts[i].length !== emptyParts[i].length) { return false; }
if (!strings.isAlphanumeric(parts[i], false)) { return false; }
}
return true;
}
const isUidFormat = value => {
return strings.isValidChars(value, ALPHANUMERIC, false) && value.length == EMPTY_UID.length;
}
const isValidFormat = value => {
return isGuidFormat(value) || isUidFormat(value);
}
const toGuidFormat = value => {
if (isGuidFormat(value)) { return value; }
if (!isUidFormat(value)) { return undefined; }
return [
value.substr(0, 8),
value.substr(8, 4),
value.substr(12, 4),
value.substr(16, 4),
value.substr(20)
].join('-').toLowerCase();
}
const toUidFormat = value => {
if (isUidFormat(value)) { return value; }
if (!isGuidFormat(value)) { return undefined; }
return value.split('-').join('').toUpperCase();
}
const version = value => {
value = toGuidFormat(value);
if (!value) { return undefined; }
try {
return validate.version(value);
} catch (ex) {
return undefined;
}
}
const isValidGuid = (value, isEmptyOkay = false) => {
if (isEmptyOkay && value === EMPTY_GUID) { return true; }
if (!isEmptyOkay && value === EMPTY_GUID) { return false; }
if (!isGuidFormat(value)) { return false; }
return validate(value);
}
const isValidUid = (value, isEmptyOkay = false) => {
if (isEmptyOkay && value === EMPTY_UID) { return true; }
if (!isEmptyOkay && value === EMPTY_UID) { return false; }
if (!isUidFormat(value)) { return false; }
return isValidGuid(toGuidFormat(value), isEmptyOkay);
}
const isValid = (value, isEmptyOkay = false) => {
return isValidGuid(value, isEmptyOkay) || isValidUid(value, isEmptyOkay);
}
const initGuid = version => {
if (typeof version === 'undefined') { version = 4; }
switch (version) {
case '1':
case 1:
return uuidV1();
case '3':
case 3:
return uuidV3();
case '4':
case 4:
return uuidV4();
case '5':
case 5:
return uuidV5();
default:
return undefined;
}
}
const initUid = version => {
const guid = initGuid(version);
if (typeof guid !== 'string') { return undefined; }
return guid.split('-').join('').toUpperCase();
}
const unique = values => {
const result = [];
const cache = [];
[].concat(values).filter(isValid).forEach(function (x) {
if (x && cache.indexOf(toUidFormat(x)) < 0) {
cache.push(toUidFormat(x));
result.push(x);
}
});
return result;
}
module.exports = {
EMPTY_GUID,
EMPTY_UID
EMPTY_UID,
initGuid,
initUid,
isGuidFormat,
isUidFormat,
isValidFormat,
isValid,
isValidGuid,
isValidUid,
toGuidFormat,
toUidFormat,
unique,
version,
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc