Last active
July 27, 2020 12:37
-
-
Save Phil-Venter/b4f12d1f54f25d3d2c0d5f5ec23f1b44 to your computer and use it in GitHub Desktop.
Simple validators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* a simple validation class with no external requirements | |
* @class V | |
*/ | |
class V { | |
/** | |
* some of the possible data types | |
* @static | |
* @enum | |
* @memberof V | |
*/ | |
static types = Object.freeze({ | |
'array': 'array', | |
'boolean': 'boolean', | |
'date': 'date', | |
'error': 'error', | |
'function': 'function', | |
'json': 'json', | |
'math': 'math', | |
'null': 'null', | |
'number': 'number', | |
'object': 'object', | |
'regexp': 'regexp', | |
'string': 'string', | |
}); | |
/** | |
* predefined regular expressions, all stolen from https://regexr.com community | |
* @static | |
* @enum | |
* @memberof V | |
* @see https://regexr.com | |
*/ | |
static regex = Object.freeze({ | |
'alpha': /\w+/, | |
'alphaNumeric': /[\d\w]+/, | |
'bitch': /\bbitch+(?:(?:\s+)?[esdingr]+)?|B+(?:(?:\s+)?[\sESDITCHNGR]+)?|b+(?:(?:\s+)?[btchesdrgin]+)+[^bitsed]/g, | |
'email': /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g, | |
'fuck': /\b(?:(?:ass+(?:\s+)?|i+(?:\s+)?|butt+(?:\s+)?|mo(?:(?:m|t|d)h?(?:e|a)?r?)(?:\s+)?)?f(?:(?:\s+)?u+)?(?:(?:\s+)?c+)?(?:(?:\s+)?k+)?(?:(?:e|a)(?:r+)?|i(?:n(?:g)?)?)?(?:s+)?(?:\s+)?(?:hole|head|(?:yo?)?u?)?)+\b/igm, | |
'lowerAlpha': /[a-z]+/, | |
'numeric': /\d+/, | |
'shit': /\bshit+(?:[a,d,i,n,g,z,e,r,s]+)?|s+(?:(?:\s+)?h+)+(?:(?:\s+)?[i,a,t,n,g,]+)+/g, | |
'upperAlpha': /[A-Z]+/, | |
'url': /^(https?:\/\/)?([\da-z\.-]+\.[a-z\.]{2,6}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?$/igm, | |
}); | |
/** | |
* a function that takes all of its parameters and 'ands' them | |
* @static | |
* @param {Boolean} values | |
* @returns {Boolean} | |
* @memberof V | |
*/ | |
static all(...values) { | |
return values.reduce((prev, curr) => { | |
return prev && Boolean(curr); | |
}, true); | |
} | |
/** | |
* tests if two values are of the same type and equal | |
* @static | |
* @param {*} value | |
* @param {*} comparator | |
* @returns {Boolean} | |
* @memberof V | |
*/ | |
static equal(value, comparator) { | |
return value === comparator; | |
} | |
/** | |
* checks to see if the <value> matches the <comparator> passed using the new typeof | |
* @static | |
* @param {*} value | |
* @param {String|RegExp} comparator | |
* @returns {Boolean} | |
* @memberof V | |
*/ | |
static is(value, comparator) { | |
switch (V.type(comparator)) { | |
case V.types.string: | |
return V.type(value) === comparator; | |
case V.types.regexp: | |
return comparator.test(value); | |
} | |
return false; | |
} | |
/** | |
* checks to see if the <value> is the keys of the <comparator> | |
* @static | |
* @param {*} value | |
* @param {Object} comparator | |
* @returns {Boolean} | |
* @memberof V | |
*/ | |
static inKeys(value, comparator) { | |
return V.type(comparator) === V.types.object ? | |
Object.keys(comparator).indexOf(value) !== -1 : | |
false; | |
} | |
/** | |
* checks to see if <value> is in the values of <comparator> | |
* @static | |
* @param {*} value | |
* @param {Array|Object|String} comparator | |
* @returns {Boolean} | |
* @memberof V | |
*/ | |
static inValues(value, comparator) { | |
switch (V.type(comparator)) { | |
case V.types.object: | |
comparator = Object.values(comparator); | |
case V.types.string: | |
case V.types.array: | |
return comparator.indexOf(value) !== -1; | |
} | |
return false; | |
} | |
/** | |
* tests if two values are of the same type and larger/greater than | |
* @static | |
* @param {*} value | |
* @param {*} comparator | |
* @returns {Boolean} | |
* @memberof V | |
*/ | |
static larger(value, comparator) { | |
return V.type(value) === V.type(comparator) ? | |
value > comparator : | |
false; | |
} | |
/** | |
* tests if two values are of the same type and lesser/smaller than | |
* @static | |
* @param {*} value | |
* @param {*} comparator | |
* @returns {Boolean} | |
* @memberof V | |
*/ | |
static lesser(value, comparator) { | |
return V.type(value) === V.type(comparator) ? | |
value < comparator : | |
false; | |
} | |
/** | |
* a function that negates its parameter | |
* @static | |
* @param {Boolean} value | |
* @returns {Boolean} | |
* @memberof V | |
*/ | |
static not(value) { | |
return !Boolean(value); | |
} | |
/** | |
* a function that takes all of its parameters and 'ors' them | |
* @static | |
* @param {Boolean} values | |
* @returns {Boolean} | |
* @memberof V | |
*/ | |
static some(...values) { | |
return values.reduce((prev, curr) => { | |
return prev || Boolean(curr); | |
}, false); | |
} | |
/** | |
* better typeof... | |
* @static | |
* @param {*} value | |
* @returns {String} | |
* @memberof V | |
* @author Angus Croll | |
* @see https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ | |
*/ | |
static type(value) { | |
return ({}).toString.call(value).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment