Setup eslint and enforce rules
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -9,200 +9,207 @@ export abstract class CLIOption<T> {
|
||||
* @type {boolean}
|
||||
* @private
|
||||
*/
|
||||
protected _useWhitelist: boolean = false
|
||||
protected useWhitelist = false
|
||||
|
||||
/**
|
||||
* Do we use the blacklist?
|
||||
* @type {boolean}
|
||||
* @private
|
||||
*/
|
||||
protected _useBlacklist: boolean = false
|
||||
protected useBlacklist = false
|
||||
|
||||
/**
|
||||
* Do we use the less-than comparison?
|
||||
* @type {boolean}
|
||||
* @private
|
||||
*/
|
||||
protected _useLessThan: boolean = false
|
||||
protected useLessThan = false
|
||||
|
||||
/**
|
||||
* Do we use the greater-than comparison?
|
||||
* @type {boolean}
|
||||
* @private
|
||||
*/
|
||||
protected _useGreaterThan: boolean = false
|
||||
protected useGreaterThan = false
|
||||
|
||||
/**
|
||||
* Do we use the equality operator?
|
||||
* @type {boolean}
|
||||
* @private
|
||||
*/
|
||||
protected _useEquality: boolean = false
|
||||
protected useEquality = false
|
||||
|
||||
/**
|
||||
* Is this option optional?
|
||||
* @type {boolean}
|
||||
* @private
|
||||
*/
|
||||
protected _optional: boolean = false
|
||||
protected isOptional = false
|
||||
|
||||
/**
|
||||
* Whitelisted values.
|
||||
* @type {Array<*>}
|
||||
* @private
|
||||
*/
|
||||
protected _whitelist: T[] = []
|
||||
protected whitelistItems: T[] = []
|
||||
|
||||
/**
|
||||
* Blacklisted values.
|
||||
* @type {Array<*>}
|
||||
* @private
|
||||
*/
|
||||
protected _blacklist: T[] = []
|
||||
protected blacklistItems: T[] = []
|
||||
|
||||
/**
|
||||
* Value to be compared in less than.
|
||||
* @type {*}
|
||||
* @private
|
||||
*/
|
||||
protected _lessThanValue?: T
|
||||
protected lessThanValue?: T
|
||||
|
||||
/**
|
||||
* If true, the less than will be less than or equal to.
|
||||
* @type {boolean}
|
||||
* @private
|
||||
*/
|
||||
protected _lessThanBit: boolean = false
|
||||
protected lessThanBit = false
|
||||
|
||||
/**
|
||||
* Value to be compared in greater than.
|
||||
* @type {*}
|
||||
* @private
|
||||
*/
|
||||
protected _greaterThanValue?: T
|
||||
protected greaterThanValue?: T
|
||||
|
||||
/**
|
||||
* If true, the greater than will be greater than or equal to.
|
||||
* @type {boolean}
|
||||
* @private
|
||||
*/
|
||||
protected _greateerThanBit: boolean = false
|
||||
protected greaterThanBit = false
|
||||
|
||||
/**
|
||||
* The value to be used to check equality.
|
||||
* @type {*}
|
||||
* @private
|
||||
*/
|
||||
protected _equalityValue?: T
|
||||
protected equalityValue?: T
|
||||
|
||||
/**
|
||||
* Whitelist the specified item or items and enable the whitelist.
|
||||
* @param {...*} items - the items to whitelist
|
||||
*/
|
||||
whitelist(...items: T[]) {
|
||||
this._useWhitelist = true
|
||||
items.forEach(item => this._whitelist.push(item))
|
||||
whitelist(...items: T[]): this {
|
||||
this.useWhitelist = true
|
||||
items.forEach(item => this.whitelistItems.push(item))
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Blacklist the specified item or items and enable the blacklist.
|
||||
* @param {...*} items - the items to blacklist
|
||||
*/
|
||||
blacklist(...items: T[]) {
|
||||
this._useBlacklist = true
|
||||
items.forEach(item => this._blacklist.push(item))
|
||||
blacklist(...items: T[]): this {
|
||||
this.useBlacklist = true
|
||||
items.forEach(item => this.blacklistItems.push(item))
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the value to be used in less-than comparison and enables less-than comparison.
|
||||
* @param {*} value
|
||||
*/
|
||||
lessThan(value: T) {
|
||||
this._useLessThan = true
|
||||
this._lessThanValue = value
|
||||
lessThan(value: T): this {
|
||||
this.useLessThan = true
|
||||
this.lessThanValue = value
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the value to be used in less-than or equal-to comparison and enables that comparison.
|
||||
* @param {*} value
|
||||
*/
|
||||
lessThanOrEqualTo(value: T) {
|
||||
this._lessThanBit = true
|
||||
lessThanOrEqualTo(value: T): this {
|
||||
this.lessThanBit = true
|
||||
this.lessThan(value)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the value to be used in greater-than comparison and enables that comparison.
|
||||
* @param {*} value
|
||||
*/
|
||||
greaterThan(value: T) {
|
||||
this._useGreaterThan = true
|
||||
this._greaterThanValue = value
|
||||
greaterThan(value: T): this {
|
||||
this.useGreaterThan = true
|
||||
this.greaterThanValue = value
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the value to be used in greater-than or equal-to comparison and enables that comparison.
|
||||
* @param {*} value
|
||||
*/
|
||||
greaterThanOrEqualTo(value: T) {
|
||||
this._greateerThanBit = true
|
||||
greaterThanOrEqualTo(value: T): this {
|
||||
this.greaterThanBit = true
|
||||
this.greaterThan(value)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the value to be used in equality comparison and enables that comparison.
|
||||
* @param {*} value
|
||||
*/
|
||||
equals(value: T) {
|
||||
this._useEquality = true
|
||||
this._equalityValue = value
|
||||
equals(value: T): this {
|
||||
this.useEquality = true
|
||||
this.equalityValue = value
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the specified value passes the configured comparisons.
|
||||
* @param {*} value
|
||||
* @param value
|
||||
* @returns {boolean}
|
||||
*/
|
||||
validate(value: any) {
|
||||
let is_valid = true
|
||||
if ( this._useEquality ) {
|
||||
is_valid = is_valid && (this._equalityValue === value)
|
||||
validate(value: T): boolean {
|
||||
let isValid = true
|
||||
if ( this.useEquality ) {
|
||||
isValid = isValid && (this.equalityValue === value)
|
||||
}
|
||||
|
||||
if ( this._useLessThan && typeof this._lessThanValue !== 'undefined' ) {
|
||||
if ( this._lessThanBit ) {
|
||||
is_valid = is_valid && (value <= this._lessThanValue)
|
||||
if ( this.useLessThan && typeof this.lessThanValue !== 'undefined' ) {
|
||||
if ( this.lessThanBit ) {
|
||||
isValid = isValid && (value <= this.lessThanValue)
|
||||
} else {
|
||||
is_valid = is_valid && (value < this._lessThanValue)
|
||||
isValid = isValid && (value < this.lessThanValue)
|
||||
}
|
||||
}
|
||||
|
||||
if ( this._useGreaterThan && typeof this._greaterThanValue !== 'undefined' ) {
|
||||
if ( this._greateerThanBit ) {
|
||||
is_valid = is_valid && (value >= this._greaterThanValue)
|
||||
if ( this.useGreaterThan && typeof this.greaterThanValue !== 'undefined' ) {
|
||||
if ( this.greaterThanBit ) {
|
||||
isValid = isValid && (value >= this.greaterThanValue)
|
||||
} else {
|
||||
is_valid = is_valid && (value > this._greaterThanValue)
|
||||
isValid = isValid && (value > this.greaterThanValue)
|
||||
}
|
||||
}
|
||||
|
||||
if ( this._useWhitelist ) {
|
||||
is_valid = is_valid && this._whitelist.some(x => {
|
||||
if ( this.useWhitelist ) {
|
||||
isValid = isValid && this.whitelistItems.some(x => {
|
||||
return x === value
|
||||
})
|
||||
}
|
||||
|
||||
if ( this._useBlacklist ) {
|
||||
is_valid = is_valid && !(this._blacklist.some(x => x === value))
|
||||
if ( this.useBlacklist ) {
|
||||
isValid = isValid && !(this.blacklistItems.some(x => x === value))
|
||||
}
|
||||
|
||||
return is_valid
|
||||
return isValid
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Option as optional.
|
||||
*/
|
||||
optional() {
|
||||
this._optional = true
|
||||
optional(): this {
|
||||
this.isOptional = true
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -216,27 +223,27 @@ export abstract class CLIOption<T> {
|
||||
* Get an array of strings denoting the human-readable requirements for this option to be valid.
|
||||
* @returns {Array<string>}
|
||||
*/
|
||||
getRequirementDisplays() {
|
||||
getRequirementDisplays(): string[] {
|
||||
const clauses = []
|
||||
|
||||
if ( this._useBlacklist ) {
|
||||
clauses.push(`must not be one of: ${this._blacklist.map(x => String(x)).join(', ')}`)
|
||||
if ( this.useBlacklist ) {
|
||||
clauses.push(`must not be one of: ${this.blacklistItems.map(x => String(x)).join(', ')}`)
|
||||
}
|
||||
|
||||
if ( this._useWhitelist ) {
|
||||
clauses.push(`must be one of: ${this._whitelist.map(x => String(x)).join(', ')}`)
|
||||
if ( this.useWhitelist ) {
|
||||
clauses.push(`must be one of: ${this.whitelistItems.map(x => String(x)).join(', ')}`)
|
||||
}
|
||||
|
||||
if ( this._useGreaterThan ) {
|
||||
clauses.push(`must be greater than${this._greateerThanBit ? ' or equal to' : ''}: ${String(this._greaterThanValue)}`)
|
||||
if ( this.useGreaterThan ) {
|
||||
clauses.push(`must be greater than${this.greaterThanBit ? ' or equal to' : ''}: ${String(this.greaterThanValue)}`)
|
||||
}
|
||||
|
||||
if ( this._useLessThan ) {
|
||||
clauses.push(`must be less than${this._lessThanBit ? ' or equal to' : ''}: ${String(this._lessThanValue)}`)
|
||||
if ( this.useLessThan ) {
|
||||
clauses.push(`must be less than${this.lessThanBit ? ' or equal to' : ''}: ${String(this.lessThanValue)}`)
|
||||
}
|
||||
|
||||
if ( this._useEquality ) {
|
||||
clauses.push(`must be equal to: ${String(this._equalityValue)}`)
|
||||
if ( this.useEquality ) {
|
||||
clauses.push(`must be equal to: ${String(this.equalityValue)}`)
|
||||
}
|
||||
|
||||
return clauses
|
||||
|
||||
Reference in New Issue
Block a user