-
Notifications
You must be signed in to change notification settings - Fork 3
StringVariable
Represents a sequence of characters.
These methods are unique to StringVariable
.
exceedsMaxLength(n: NumberVariable) ⇒ Proposition
lacksMinLength(n: NumberVariable) ⇒ Proposition
isEmpty() ⇒ Proposition
isNotEmpty() ⇒ Proposition
isOneOf(array: ArrayVariable) ⇒ Proposition
endsWith(s: StringVariable) ⇒ Proposition
includes(s: StringVariable) ⇒ Proposition
matches(regExp: StringVariable) ⇒ Proposition
startsWith(s: StringVariable) ⇒ Proposition
Determines whether the current value is less than or equal to a specific character count.
throws ArgumentError
.
Example:
/* eslint-disable max-len */
/* eslint-disable sonarjs/no-duplicate-string */
const ArrayVariable = require('../array-variable')
const {
NumberVariable,
StringVariable
} = require('archetypes-rules')
describe('exceedsMaxLength', () => {
it('determines whether the current value is less than or equal to a specific character count', () => {
const usaStateAbbrev = new StringVariable('usa-state-abbrev', 'TX')
const TWO = 2
const maxCharCount = new NumberVariable('max-chars-allowed', TWO)
let proposition = variable.exceedsMaxLength(maxCharCount)
expect(proposition.value).toBe(false)
variable.value = 'Texas'
proposition = variable.exceedsMaxLength(maxCharCount)
expect(proposition.value).toBe(true)
})
describe('when given anything other than a NumberVariable', () => {
it('throws an ArgumentError', () => {
expect(() => {
const thrower = new StringVariable('thrower')
thrower.exceedsMaxLength(
new StringVariable('this-variable-should-be', 'of-type-number-variable')
)
}).toThrow()
})
})
})
Determines whether the current value is greater than or equal to a given character count.
throws ArgumentError
Example:
const {
NumberVariable,
StringVariable
} = require('archetypes-rules')
describe('lacksMinLength', () => {
it('determines whether the current value is greater than or equal to a given character count', () => {
const password = new StringVariable(
'password',
'this-is-a-good-password...compelling-and-RICH'
)
const MINIMUM = 8
const minNumberOfChars = new NumberVariable(
'min-number-characters-allowed',
MINIMUM
)
let proposition = variable.lacksMinLength(minNumberOfChars)
expect(proposition.value).toBe(false)
variable.value = 'hi'
proposition = variable.lacksMinLength(minNumberOfChars)
expect(proposition.value).toBe(true)
})
describe('when given anything other than a NumberVariable', () => {
it('throws an ArgumentError', () => {
expect(() => {
const thrower = new StringVariable('thrower')
thrower.lacksMinLength(
new StringVariable('should-be', 'a-number-variable')
)
}).toThrow()
})
})
})
Determines whether this instance's value has zero (0) characters.
Example:
const { StringVariable } = require('archetypes-rules')
describe('isEmpty', () => {
it("determines whether this instance's value has zero (0) characters", () => {
const variable = new StringVariable('empty-string-variable', '')
expect(variable.isEmpty().value).toBe(true)
variable.value = null
expect(variable.isEmpty().value).toBe(false)
variable.value = ' '
expect(variable.isEmpty().value).toBe(false)
})
})
Determines whether a string has more then zero (0) characters.
Example:
const { StringVariable } = require('archetypes-rules')
describe('isNotEmpty', () => {
it('determines whether a string has more then zero (0) chars', () => {
const variable = new StringVariable(
'non-empty-string-variable',
'non-empty-string-variable'
)
let proposition = variable.isNotEmpty()
expect(proposition.value).toBe(true)
variable.value = ' '
expect(variable.isNotEmpty().value).toBe(true)
variable.value = null
expect(variable.isNotEmpty().value).toBe(false)
variable.value = ''
expect(variable.isNotEmpty().value).toBe(false)
})
})
Determines whether a string is in an ArrayVariable.
throws ArgumentError
const {
ArrayVariable,
StringVariable
} = require('archetypes-rules')
describe('isOneOf', () => {
it('determines whether a string is in an ArrayVariable', () => {
let specimen = new StringVariable('specimen', 'password')
const suspiciousWords = new ArrayVariable('array-variable', [
'password',
'token'
])
let proposition = specimen.isOneOf(suspiciousWords)
expect(proposition.value).toBe(true)
specimen.value = 'safe-word'
proposition = specimen.isOneOf(suspiciousWords)
expect(proposition.value).toBe(false)
})
describe('when given anything other than an ArrayVariable', () => {
it('throws an ArgumentError', () => {
expect(() => {
const thrower = new StringVariable('thrower')
thrower.isOneOf(
new StringVariable('should-be', 'an-array-variable')
)
}).toThrow()
})
})
})
Determines whether a string ends with another string.
throws ArgumentError
const {
NumberVariable,
StringVariable
} = require('archetypes-rules')
describe('endsWith', () => {
it('determines whether a string ends with another string', () => {
const variable = new StringVariable('test-ends-with', 'test-ends-with')
const characters = new StringVariable('ends-with', 'with')
expect(variable.endsWith(characters).value).toBe(true)
characters.value = 'without'
expect(variable.endsWith(characters).value).toBe(false)
})
describe('when given anything other than a StringVariable', () => {
it('throws an ArgumentError', () => {
expect(() => {
const thrower = new StringVariable('thrower')
thrower.endsWith(
new NumberVariable('should-be-string-variable')
)
}).toThrow()
})
})
})
Determines whether a string may be found within another string.
throws ArgumentError
Example:
const {
NumberVariable,
StringVariable
} = require('archetypes-rules')
describe('includes', () => {
it('determines whether a string may be found within another string', () => {
const sentence = new StringVariable(
'sentence',
'The quick brown fox jumps over the lazy dog.'
)
const word = new StringVariable('word', 'fox')
expect(sentence.includes(word).value).toBe(true)
word.value = 'dolphin'
expect(sentence.includes(word).value).toBe(false)
})
describe('when given anything other than a StringVariable', () => {
it('throws an ArgumentError', () => {
expect(() => {
const thrower = new StringVariable('thrower')
thrower.includes(
new NumberVariable('only-accepts-string-variables')
)
}).toThrow()
})
})
})
Determines whether a string may be found within another string.
throws ArgumentError
Example:
const { StringVariable } = require('archetypes-rules')
describe('matches', () => {
it('determines whether a string matches a regular expression', () => {
const sentence = new StringVariable(
'sentence',
'The quick brown fox jumps over the lazy dog. It barked.'
)
const anyCapitalLetters = new StringVariable('capital-letters', /[A-Z]/g)
expect(sentence.matches(anyCapitalLetters).value).toBe(true)
sentence.value = sentence.value.toLowerCase()
expect(sentence.matches(anyCapitalLetters).value).toBe(false)
})
describe('when given anything other than a StringVariable with a regular expression value', () => {
it('throws an ArgumentError', () => {
expect(() => {
// Create a nullStringVariable
const thrower = new StringVariable('thrower')
thrower.matches(
new StringVariable('should-be', 'regex')
)
}).toThrow()
})
})
})
Determines whether a string begins with the characters of another string.
throws ArgumentError
Example:
const {
NumberVariable,
StringVariable
} = require('archetypes-rules')
describe('startsWith', () => {
it('determines whether a string begins with the characters of another', () => {
const sentence = new StringVariable(
'sentence',
'The quick brown fox jumps over the lazy dog. It barked.'
)
const theseWords = new StringVariable(
'starts-with-the-words',
'The quick brown fox '
)
expect(sentence.startsWith(theseWords).value).toBe(true)
// Backwards masking! 😈
theseWords.value = [...theseWords.value].reverse().join('')
expect(sentence.startsWith(theseWords).value).toBe(false)
})
describe('when given anything other than a StringVariable', () => {
it('throws an ArgumentError', () => {
expect(() => {
const thrower = new StringVariable('thrower')
thrower.startsWith(new NumberVariable('string-variables-only-please'))
}).toThrow()
})
})
})