Skip to content

Commit

Permalink
refactor(method-count): remove extra functions
Browse files Browse the repository at this point in the history
- Remove ArrayVariable.prototype.isNonEmpty
- Rename Proposition results' names to match their
  operator method names.

#103
  • Loading branch information
gregswindle committed Sep 7, 2019
1 parent 035db0f commit c1e412f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 58 deletions.
30 changes: 19 additions & 11 deletions lib/variable/__tests__/array-variable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ describe('ArrayVariable', () => {
})
})

describe('empty', () => {
it('is an alias for #isEmpty', () => {
variable.isEmpty = jest.fn()
variable.empty()
expect(variable.isEmpty).toHaveBeenCalled()
})
})

describe('endsWith', () => {
it('tests an array to end with a specific value by identity', () => {
expect(variable.endsWith(someOtherVariable).value).toBe(false)
Expand All @@ -88,19 +96,11 @@ describe('ArrayVariable', () => {
})
})

describe('isNonEmpty', () => {
describe('isNotEmpty', () => {
it('declares true when an ArrayVariable has one or more values', () => {
expect(variable.isNonEmpty().value).toBe(true)
expect(variable.isNotEmpty().value).toBe(true)
variable.value.length = 0
expect(variable.isNonEmpty().value).toBe(false)
})
})

describe('isNotEmpty', () => {
it('is an alias for #nonEmpty', () => {
variable.nonEmpty = jest.fn()
variable.isNotEmpty()
expect(variable.nonEmpty).toHaveBeenCalled()
expect(variable.isNotEmpty().value).toBe(false)
})
})

Expand Down Expand Up @@ -280,6 +280,14 @@ describe('ArrayVariable', () => {
)
})

describe('nonEmpty', () => {
it('is an alias for #isNotEmpty', () => {
variable.isNotEmpty = jest.fn()
variable.nonEmpty()
expect(variable.isNotEmpty).toHaveBeenCalled()
})
})

describe('notEqualTo', () => {
it('evaluates whether it is not equal to another variable *by identity*', () => {
variable = new ArrayVariable('variable', [2, 3])
Expand Down
82 changes: 35 additions & 47 deletions lib/variable/array-variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,35 @@ const Proposition = require('../proposition/proposition')
const Variable = require('./variable')
const camelCase = require('lodash.camelcase')
const dataTypePredicateMap = require('./helpers/data-type-predicate-map')
const debug = require('debug')('@archetypes/rules/variable/ArrayVariable')
const getValidatedNumberValue = require('./helpers/get-validated-number-value')
const ow = require('ow')

/**
* @classdesc
*
*
* @class ArrayVariable
* @extends {Variable}
* @private
*/

const getValidatedNumberValue = (variable) => {
const val = variable.valueOf()
debug(`getValidatedNumberValue: variable.valueOf(): ${val}`)
ow(val, ow.any(ow.array, ow.number, ow.string))
if (Array.isArray(val) || ow.isValid(val, ow.string)) {
debug(`getValidatedNumberValue => ${val.length}`)
return val.length
}
debug(`getValidatedNumberValue => ${val}`)
return val
}

const getVariableType = (variable) => {
ow(variable, ow.any(ow.object, ow.string))
if (ow.isValid(variable, ow.string)) {
return variable
}
return camelCase(variable.type
.split('/')
.pop()
.replace('Variable', ''))
return camelCase(
variable.type
.split('/')
.pop()
.replace('Variable', '')
)
}

/**
* @classdesc
*
*
* @class ArrayVariable
* @extends {Variable}
*/

class ArrayVariable extends Variable {
constructor (name, value = []) {
super(name, value, ow.array)
Expand All @@ -50,10 +45,7 @@ class ArrayVariable extends Variable {
*/

empty () {
return new Proposition(
`(IS_EMPTY ${this.name})`,
ow.isValid(this.value, ow.array.empty)
)
return this.isEmpty()
}

/**
Expand Down Expand Up @@ -87,15 +79,15 @@ class ArrayVariable extends Variable {
greaterThan (variable) {
const number = getValidatedNumberValue(variable)
return new Proposition(
`(${this.name} HAS_LENGTH_GREATER_THAN ${number})`,
`(${this.name} GREATER_THAN ${number})`,
this.value.length > number
)
}

greaterThanOrEqualTo (variable) {
const number = getValidatedNumberValue(variable)
return new Proposition(
`(${this.name} HAS_LENGTH_GREATER_THAN_OR_EQUAL_TO ${number})`,
`(${this.name} GREATER_THAN_OR_EQUAL_TO ${number})`,
this.value.length >= number
)
}
Expand Down Expand Up @@ -129,23 +121,21 @@ class ArrayVariable extends Variable {
*/

isEmpty () {
return this.empty()
}

/**
* Determine whether the ArrayVariable's value has one or more elements.
*/

isNonEmpty () {
return this.nonEmpty()
return new Proposition(
`(IS_EMPTY ${this.name})`,
ow.isValid(this.value, ow.array.empty)
)
}

/**
* Determine whether the ArrayVariable's value has one or more elements.
*/

isNotEmpty () {
return this.nonEmpty()
return new Proposition(
`(IS_NOT_EMPTY ${this.name})`,
ow.isValid(this.value, ow.array.nonEmpty)
)
}

/**
Expand All @@ -155,23 +145,23 @@ class ArrayVariable extends Variable {
length (variable) {
const number = getValidatedNumberValue(variable)
return new Proposition(
`(${this.name} HAS_LENGTH_OF ${number})`,
`(${this.name} LENGTH ${number})`,
ow.isValid(this.value, ow.array.length(number))
)
}

lessThan (variable) {
const number = getValidatedNumberValue(variable)
return new Proposition(
`(${this.name} HAS_LENGTH_LESS_THAN ${number})`,
`(${this.name} LESS_THAN ${number})`,
this.value.length < number
)
}

lessThanOrEqualTo (variable) {
const number = getValidatedNumberValue(variable)
return new Proposition(
`(${this.name} HAS_LENGTH_LESS_THAN_OR_EQUAL_TO ${number})`,
`(${this.name} LESS_THAN_OR_EQUAL ${number})`,
this.value.length <= number
)
}
Expand All @@ -183,7 +173,7 @@ class ArrayVariable extends Variable {
maxLength (variable) {
const number = getValidatedNumberValue(variable)
return new Proposition(
`(${this.name} HAS_MAX_LENGTH_OF ${number})`,
`(${this.name} MAX_LENGTH ${number})`,
ow.isValid(this.value, ow.array.maxLength(number))
)
}
Expand All @@ -195,7 +185,7 @@ class ArrayVariable extends Variable {
minLength (variable) {
const number = getValidatedNumberValue(variable)
return new Proposition(
`(${this.name} HAS_MIN_LENGTH_OF ${number})`,
`(${this.name} MIN_LENGTH ${number})`,
ow.isValid(this.value, ow.array.minLength(number))
)
}
Expand All @@ -205,21 +195,19 @@ class ArrayVariable extends Variable {
*/

nonEmpty () {
return new Proposition(
`(IS_NOT_EMPTY ${this.name})`,
ow.isValid(this.value, ow.array.nonEmpty)
)
return this.isNotEmpty()
}

/**
* Test all elements in the array to match to provided predicate.
*/

ofType (variable) {
ow(variable, ow.any(ow.object.instanceOf(Variable), ow.string))
const dataType = getVariableType(variable)
const predicate = dataTypePredicateMap.get(dataType)
return new Proposition(
`(${this.name} IS_OF_TYPE ${dataType})`,
`(${this.name} OF_TYPE ${dataType})`,
ow.isValid(this.value, ow.array.ofType(predicate))
)
}
Expand Down
31 changes: 31 additions & 0 deletions lib/variable/helpers/get-validated-number-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const debug = require('debug')(
'@archetypes/rules/variable/helpers/get-validated-number-value'
)
const ow = require('ow')

/**
* Returns a Number.
*
* @private
*
* @param {@archetypes/rules/variable|number|string} variable - A valid
* - Instance of @archetypes/rules/variable
* - ECMAScript Number
* - ECMAScript String
* @throws {ArgumentError}
* @returns {number} - The length of an ArrayVariable or string.
*/

const getValidatedNumberValue = (variable) => {
const val = variable.valueOf()
debug(`getValidatedNumberValue: variable.valueOf(): ${val}`)
ow(val, ow.any(ow.array, ow.number, ow.string))
if (Array.isArray(val) || ow.isValid(val, ow.string)) {
debug(`getValidatedNumberValue => ${val.length}`)
return val.length
}
debug(`getValidatedNumberValue => ${val}`)
return val
}

module.exports = getValidatedNumberValue

0 comments on commit c1e412f

Please sign in to comment.