@darkwolf/validator
Advanced tools
Comparing version 2.0.2 to 2.0.3
283
index.js
class Validator { | ||
constructor(value) { | ||
this.value = value | ||
this.equal = true | ||
this.isValid = undefined | ||
@@ -17,12 +16,2 @@ } | ||
get and() { | ||
this.equal = true | ||
return this | ||
} | ||
get or() { | ||
this.equal = false | ||
return this | ||
} | ||
call(callback) { | ||
@@ -32,4 +21,4 @@ return callback(this) | ||
throw(e) { | ||
if (!this.isValid) throw e | ||
throw(callback) { | ||
if (!this.isUndefined(this.isValid) && !this.isValid) throw callback() | ||
return this | ||
@@ -42,6 +31,10 @@ } | ||
_isNotType(...args) { | ||
return !this._isType(...args) | ||
_isObject(value) { | ||
return this._isType(value, 'object') | ||
} | ||
_isUndefined(value) { | ||
return this._isType(value, 'undefined') | ||
} | ||
_isBoolean(value) { | ||
@@ -51,6 +44,2 @@ return this._isType(value, 'boolean') | ||
_isNotBoolean(...args) { | ||
return !this._isBoolean(...args) | ||
} | ||
_isNumber(value) { | ||
@@ -60,6 +49,2 @@ return this._isType(value, 'number') | ||
_isNotNumber(...args) { | ||
return !this._isNumber(...args) | ||
} | ||
_isString(value) { | ||
@@ -69,14 +54,2 @@ return this._isType(value, 'string') | ||
_isNotString(...args) { | ||
return !this._isString(...args) | ||
} | ||
_isObject(value) { | ||
return this._isType(value, 'object') | ||
} | ||
_isNotObject(...args) { | ||
return !this._isObject(...args) | ||
} | ||
_isFunction(value) { | ||
@@ -86,14 +59,6 @@ return this._isType(value, 'function') | ||
_isNotFunction(...args) { | ||
return !this._isFunction(...args) | ||
_isSymbol(value) { | ||
return this._isType(value, 'symbol') | ||
} | ||
_isUndefined(value) { | ||
return this._isType(value, 'undefined') | ||
} | ||
_isNotUndefined(...args) { | ||
return !this._isUndefined(...args) | ||
} | ||
_isNull(value) { | ||
@@ -103,6 +68,2 @@ return value === null | ||
_isNotNull(...args) { | ||
return !this._isNull(...args) | ||
} | ||
_isInstance(value, instance) { | ||
@@ -112,6 +73,2 @@ return value instanceof instance | ||
_isNotInstance(...args) { | ||
return !this._isInstance(...args) | ||
} | ||
_isArray(value) { | ||
@@ -121,6 +78,2 @@ return Array.isArray(value) | ||
_isNotArray(...args) { | ||
return !this._isArray(...args) | ||
} | ||
_isBuffer(value) { | ||
@@ -130,6 +83,2 @@ return Buffer.isBuffer(value) | ||
_isNotBuffer(...args) { | ||
return !this._isBuffer(...args) | ||
} | ||
_isArrayBuffer(value) { | ||
@@ -139,12 +88,12 @@ return this._isInstance(value, ArrayBuffer) | ||
_isNotArrayBuffer(...args) { | ||
return !this._isArrayBuffer(...args) | ||
_isRegExp(value) { | ||
return this._isInstance(value, RegExp) | ||
} | ||
_isExists(value) { | ||
return this._isNotUndefined(value) && this._isNotNull(value) | ||
_isSet(value) { | ||
return this._isInstance(value, Set) | ||
} | ||
_isNotExists(...args) { | ||
return !this._isExists(...args) | ||
_isExists(value) { | ||
return !this._isUndefined(value) && !this._isNull(value) | ||
} | ||
@@ -156,6 +105,2 @@ | ||
_isNotNaN(...args) { | ||
return !this._isNaN(...args) | ||
} | ||
_isFinite(value) { | ||
@@ -165,6 +110,2 @@ return Number.isFinite(value) | ||
_isNotFinite(...args) { | ||
return !this._isFinite(...args) | ||
} | ||
_isInfinity(value) { | ||
@@ -174,25 +115,30 @@ return Math.abs(value) === Infinity | ||
_isNotInfinity(...args) { | ||
return !this._isInfinity(...args) | ||
_isMore(value, number) { | ||
return value > number | ||
} | ||
_isRegExp(value) { | ||
return this._isInstance(value, RegExp) | ||
_isLess(value, number) { | ||
return value < number | ||
} | ||
_isNotRegExp(...args) { | ||
return !this._isRegExp(...args) | ||
_isEqual(value1, value2) { | ||
return value1 === value2 | ||
} | ||
_isSet(value) { | ||
return this._isInstance(value, Set) | ||
_isMoreEqual(value, number) { | ||
return value >= number | ||
} | ||
_isNotSet(...args) { | ||
return !this._isSet(...args) | ||
_isLessEqual(value, number) { | ||
return value <= number | ||
} | ||
_valid(value) { | ||
this.isValid = this._isUndefined(this.isValid) ? value : | ||
this.equal ? this.isValid && value : this.isValid || value | ||
_isRange(value, min, max) { | ||
return this._isMoreEqual(value, min) && this._isLessEqual(value, max) | ||
} | ||
_valid(callback) { | ||
if (this._isUndefined(this.isValid) || this.isValid) { | ||
this.isValid = !!callback() | ||
} | ||
return this | ||
@@ -203,181 +149,126 @@ } | ||
if (args.length > 1) return this._isType(...args) | ||
return this._valid(this._isType(this.value, ...args)) | ||
return this._valid(() => this._isType(this.value, ...args)) | ||
} | ||
isNotType(...args) { | ||
if (args.length > 1) return this._isNotType(...args) | ||
return this._valid(this._isNotType(this.value, ...args)) | ||
isObject(...args) { | ||
if (args.length) return this._isObject(...args) | ||
return this._valid(() => this._isObject(this.value)) | ||
} | ||
isUndefined(...args) { | ||
if (args.length) return this._isUndefined(...args) | ||
return this._valid(() => this._isUndefined(this.value)) | ||
} | ||
isBoolean(...args) { | ||
if (args.length) return this._isBoolean(...args) | ||
return this._valid(this._isBoolean(this.value)) | ||
return this._valid(() => this._isBoolean(this.value)) | ||
} | ||
isNotBoolean(...args) { | ||
if (args.length) return this._isNotBoolean(...args) | ||
return this._valid(this._isNotBoolean(this.value)) | ||
} | ||
isNumber(...args) { | ||
if (args.length) return this._isNumber(...args) | ||
return this._valid(this._isNumber(this.value)) | ||
return this._valid(() => this._isNumber(this.value)) | ||
} | ||
isNotNumber(...args) { | ||
if (args.length) return this._isNotNumber(...args) | ||
return this._valid(this._isNotNumber(this.value)) | ||
} | ||
isString(...args) { | ||
if (args.length) return this._isString(...args) | ||
return this._valid(this._isString(this.value)) | ||
return this._valid(() => this._isString(this.value)) | ||
} | ||
isNotString(...args) { | ||
if (args.length) return this._isNotString(...args) | ||
return this._valid(this._isNotString(this.value)) | ||
} | ||
isObject(...args) { | ||
if (args.length) return this._isObject(...args) | ||
return this._valid(this._isObject(this.value)) | ||
} | ||
isNotObject(...args) { | ||
if (args.length) return this._isNotObject(...args) | ||
return this._valid(this._isNotObject(this.value)) | ||
} | ||
isFunction(...args) { | ||
if (args.length) return this._isFunction(...args) | ||
return this._valid(this._isFunction(this.value)) | ||
return this._valid(() => this._isFunction(this.value)) | ||
} | ||
isNotFunction(...args) { | ||
if (args.length) return this._isNotFunction(...args) | ||
return this._valid(this._isNotFunction(this.value)) | ||
isSymbol(...args) { | ||
if (args.length) return this._isSymbol(...args) | ||
return this._valid(() => this._isSymbol(this.value)) | ||
} | ||
isUndefined(...args) { | ||
if (args.length) return this._isUndefined(...args) | ||
return this._valid(this._isUndefined(this.value)) | ||
} | ||
isNotUndefined(...args) { | ||
if (args.length) return this._isNotUndefined(...args) | ||
return this._valid(this._isNotUndefined(this.value)) | ||
} | ||
isNull(...args) { | ||
if (args.length) return this._isNull(...args) | ||
return this._valid(this._isNull(this.value)) | ||
return this._valid(() => this._isNull(this.value)) | ||
} | ||
isNotNull(...args) { | ||
if (args.length) return this._isNotNull(...args) | ||
return this._valid(this._isNotNull(this.value)) | ||
} | ||
isInstance(...args) { | ||
if (args.length > 1) return this._isInstance(...args) | ||
return this._valid(this._isInstance(this.value, ...args)) | ||
return this._valid(() => this._isInstance(this.value, ...args)) | ||
} | ||
isNotInstance(...args) { | ||
if (args.length > 1) return this._isNotInstance(...args) | ||
return this._valid(this._isNotInstance(this.value, ...args)) | ||
} | ||
isArray(...args) { | ||
if (args.length) return this._isArray(...args) | ||
return this._valid(this._isArray(this.value)) | ||
return this._valid(() => this._isArray(this.value)) | ||
} | ||
isNotArray(...args) { | ||
if (args.length) return this._isNotArray(...args) | ||
return this._valid(this._isNotArray(this.value)) | ||
} | ||
isBuffer(...args) { | ||
if (args.length) return this._isBuffer(...args) | ||
return this._valid(this._isBuffer(this.value)) | ||
return this._valid(() => this._isBuffer(this.value)) | ||
} | ||
isNotBuffer(...args) { | ||
if (args.length) return this._isNotBuffer(...args) | ||
return this._valid(this._isNotBuffer(this.value)) | ||
} | ||
isArrayBuffer(...args) { | ||
if (args.length) return this._isArrayBuffer(...args) | ||
return this._valid(this._isArrayBuffer(this.value)) | ||
return this._valid(() => this._isArrayBuffer(this.value)) | ||
} | ||
isNotArrayBuffer(...args) { | ||
if (args.length) return this._isNotArrayBuffer(...args) | ||
return this._valid(this._isNotArrayBuffer(this.value)) | ||
isRegExp(...args) { | ||
if (args.length) return this._isRegExp(...args) | ||
return this._valid(() => this._isRegExp(this.value)) | ||
} | ||
isSet(...args) { | ||
if (args.length) return this._isSet(...args) | ||
return this._valid(() => this._isSet(this.value)) | ||
} | ||
isExists(...args) { | ||
if (args.length) return this._isExists(...args) | ||
return this._valid(this._isExists(this.value)) | ||
return this._valid(() => this._isExists(this.value)) | ||
} | ||
isNotExists(...args) { | ||
if (args.length) return this._isNotExists(...args) | ||
return this._valid(this._isNotExists(this.value)) | ||
} | ||
isNaN(...args) { | ||
if (args.length) return this._isNaN(...args) | ||
return this._valid(this._isNaN(this.value)) | ||
return this._valid(() => this._isNaN(this.value)) | ||
} | ||
isNotNaN(...args) { | ||
if (args.length) return this._isNotNaN(...args) | ||
return this._valid(this._isNotNaN(this.value)) | ||
} | ||
isFinite(...args) { | ||
if (args.length) return this._isFinite(...args) | ||
return this._valid(this._isFinite(this.value)) | ||
return this._valid(() => this._isFinite(this.value)) | ||
} | ||
isNotFinite(...args) { | ||
if (args.length) return this._isNotFinite(...args) | ||
return this._valid(this._isNotFinite(this.value)) | ||
} | ||
isInfinity(...args) { | ||
if (args.length) return this._isInfinity(...args) | ||
return this._valid(this._isInfinity(this.value)) | ||
return this._valid(() => this._isInfinity(this.value)) | ||
} | ||
isNotInfinity(...args) { | ||
if (args.length) return this._isNotInfinity(...args) | ||
return this._valid(this._isNotInfinity(this.value)) | ||
isMore(...args) { | ||
if (args.length > 1) return this._isMore(...args) | ||
return this._valid(() => this._isMore(this.value, ...args)) | ||
} | ||
isRegExp(...args) { | ||
if (args.length) return this._isRegExp(...args) | ||
return this._valid(this._isRegExp(this.value)) | ||
isLess(...args) { | ||
if (args.length > 1) return this._isLess(...args) | ||
return this._valid(() => this._isLess(this.value, ...args)) | ||
} | ||
isNotRegExp(...args) { | ||
if (args.length) return this._isNotRegExp(...args) | ||
return this._valid(this._isNotRegExp(this.value)) | ||
isEqual(...args) { | ||
if (args.length > 1) return this._isEqual(...args) | ||
return this._valid(() => this._isEqual(this.value, ...args)) | ||
} | ||
isSet(...args) { | ||
if (args.length) return this._isSet(...args) | ||
return this._valid(this._isSet(this.value)) | ||
isMoreEqual(...args) { | ||
if (args.length > 1) return this._isMoreEqual(...args) | ||
return this._valid(() => this._isMoreEqual(this.value, ...args)) | ||
} | ||
isNotSet(...args) { | ||
if (args.length) return this._isNotSet(...args) | ||
return this._valid(this._isNotSet(this.value)) | ||
isLessEqual(...args) { | ||
if (args.length > 1) return this._isLessEqual(...args) | ||
return this._valid(() => this._isLessEqual(this.value, ...args)) | ||
} | ||
isRange(...args) { | ||
if (args.length > 2) return this._isRange(...args) | ||
return this._valid(() => this._isRange(this.value, ...args)) | ||
} | ||
} | ||
module.exports = new Validator() |
{ | ||
"name": "@darkwolf/validator", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Validator Utility", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,52 +16,39 @@ # Validator Utility | ||
new Validator(69).isNumber().isString().isValid // false | ||
new Validator(69).isNumber().or.isString().isValid // true | ||
new Validator(69).isString().isValid // false | ||
new Validator(69).isNumber().isRange(0, 100).isValid // true | ||
new Validator('69') | ||
.isNumber() | ||
.call(validator => validator.isString()) | ||
.throw(new TypeError('Value must be a number')) // throw error | ||
.call(validator => validator) | ||
.throw(() => new TypeError('Value must be a number')) // throw error | ||
``` | ||
## Init | ||
### new Validator(value) | ||
### new Validator(value?) | ||
## Methods | ||
### setValue(value) | ||
### and | ||
### or | ||
### call(callback(validator)) | ||
### throw(e) | ||
### call(validator => validator) | ||
### throw(() => e) | ||
### isType(value?, type) | ||
### isNotType(value?, type) | ||
### isObject(value?) | ||
### isUndefined(value?) | ||
### isBoolean(value?) | ||
### isNotBoolean(value?) | ||
### isNumber(value?) | ||
### isNotNumber(value?) | ||
### isString(value?) | ||
### isNotString(value?) | ||
### isObject(value?) | ||
### isNotObject(value?) | ||
### isFunction(value?) | ||
### isNotFunction(value?) | ||
### isUndefined(value?) | ||
### isNotUndefined(value?) | ||
### isSymbol(value?) | ||
### isNull(value?) | ||
### isNotNull(value?) | ||
### isInstance(value?, instance) | ||
### isNotInstance(value?, instance) | ||
### isArray(value?) | ||
### isNotArray(value?) | ||
### isBuffer(value?) | ||
### isNotBuffer(value?) | ||
### isArrayBuffer(value?) | ||
### isNotArrayBuffer(value?) | ||
### isRegExp(value?) | ||
### isSet(value?) | ||
### isExists(value?) | ||
### isNotExists(value?) | ||
### isNaN(value?) | ||
### isNotNaN(value?) | ||
### isFinite(value?) | ||
### isNotFinite(value?) | ||
### isInfinity(value?) | ||
### isNotInfinity(value?) | ||
### isRegExp(value?) | ||
### isNotRegExp(value?) | ||
### isSet(value?) | ||
### isNotSet(value?) | ||
### isMore(value?) | ||
### isLess(value?) | ||
### isEqual(value?) | ||
### isMoreEqual(value?) | ||
### isLessEqual(value?) | ||
### isRange(value?, min, max) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8869
202
54