Pure Conditions

Utility methods for simple conditions.

Installation

npm install pure-conditions

endsWith (string1, string2)

Checks if first string ends with the second string.

endsWith("ban qux"m "qux")
<= true

exists (object)

Checks if object is defined.

exists(myVariable)
<= false
exists({})
<= true

hasDuplicates (array)

Checks if the array contains duplicate values.

hasDuplicates(["foo", "foo"])
<= true

hasExtension (string1, string2)

Checks if the string is a name of audio file.

hasExtension("foo.txt", "txt")
<= true

hasKeys (object)

Checks if the passed object has any key.

hasKeys({ foo: "bar" })
<= true

hasLengthOf (value, number)

Checks if the length of value is equal the number.

hasLengthOf([1, 2], 2)
<= true

hasLengthOfAtLeast (value, number)

Checks if the length of value is greater or equal the number.

hasLengthOfAtLeast([1, 2], 2)
<= true
hasLengthOfAtLeast([1, 2, 3], 2)
<= true

hasLengthOfAtMost (value, number)

Checks if the length of value is smaller or equal the number.

hasLengthOfAtMost([1, 2, 3, 4, 5], 10)
<= true
hasLengthOfAtMost([1, 2, 3], 2)
<= false

hasNewLine (value)

Checks if the value contains the new line char.

hasNewLine("foo bar")
<= true

hasNumber (value)

Checks if the value contains at least one number.

hasNumber([1, 2])
<= true
hasNumber({ foo: 1, bar: "baz" })
<= true

hasNumbers (value)

Checks if the value contains more than one number.

hasNumber([1, 2])
<= true
hasNumber({ foo: 1, bar: "baz", ban: 2 })
<= true

hasTypeOf (value, string)

Checks the type of passed value.

hasTypeOf(32, "number")
<= true

hasWhitespace (string)

Checks if the string contains whitespaces.

hasWhitespace("foo&nbsp;bar")
<= true
hasWhitespace("foo bar")
<= true

hasWords (string, number)

Checks if the string contains particular number of words.

hasWords("foo bar baz", 3)
<= true
hasWords(myVariable)
<= true

have (value, number)

Checks if the length of value is equal the number.

have([1, 2, 3, 4, 5], 5)
<= true

haveLessThan (value, number)

Checks if the length of value is smaller than the number.

haveLessThan([1, 2, 3], 10)
<= true

haveMany (array)

Checks if the array have more than one element.

haveMany([1, 2])
<= true

haveMoreThan (value, number)

Checks if the length of value is greater than number.

haveMoreThan([1, 2, 3, 4, 5], 4)
<= true

includes (value1, value2)

Checks if first value includes second value.

includes("lorem ipsum", lorem)
<= true

isAlpha (string)

Checks if the string contains only letters.

isAlpha("foo")
<= true
isAlpha("foo123")
<= false

isAlphaNumeric (string)

Checks if the string contains only letters and numbers.

isAlphaNumeric("foo")
<= true
isAlphaNumeric("foo123")
<= true

isAlternative (value1, value2)

Returns true when one value from passed values is truthy.

isAlternative(0, 1)
<= true
isAlternative(1, 1)
<= true

isArray (value)

Checks if the value is an array.

isArray([])
<= true

isAudio (string)

Checks if the string is a name of audio file.

isAudio("foo.mp3")
<= true

isBetween (number1, number2, number3)

Checks if first is between second and third number.

isBetween(10, 5, 20)
<= true

isBitwiseAlternative (value1, value2)

Returns boolean value of bitwise alternative.

isBitwiseAlternative(1, 0)
<= true
isBitwiseAlternative(1, 1)
<= true

isBitwiseAlternativeNegation (value1, value2)

Returns boolean value of bitwise alternative negation.

isBitwiseAlternativeNegation(1, 0)
<= true
isBitwiseAlternativeNegation(0, 1)
<= true

isBitwiseConjunction (value1, value2)

Returns boolean value of bitwise conjunction.

isBitwiseConjunction(1, 1)
<= true

isBitwiseNegation (value)

Returns boolean value of bitwise negation.

isBitwiseNegation(0)
<= true

isBoolean (value)

Checks if the value is an boolean.

isBoolean(true)
<= true
isBoolean(1)
<= false

isComputer ()

Checks if device is a computer (The viewport is at least 992 pixels wide).

isComputer()
<= true

isConjunction (value1, value2)

Returns true when both values from passed values are truthy.

isConjunction(1, 1)
<= true

isDate (value)

Checks if the value is an date.

isDate(new Date())
<= true

isDecimal (value)

Checks if the value is an decimal.

isDecimal(2.5)
<= true

isDigit (value)

Checks if the value is an digit.

isDigit(2)
<= true
isDigit("2")
<= true

isDivisible (number1, number2)

Checks if one number is divisible by second number.

isDivisible(10, 5)
<= true

isEmail (string)

Checks if the string is an email.

isEmail("foobar@gmail.com")
<= true

isEmpty (value)

Checks if the value is empty.

isEmpty([])
<= true
isEmpty({})
<= true

isEmptyArray (array)

Checks if the array is empty.

isEmptyArray([])
<= true

isEmptyObject (object)

Checks if the object is empty.

isEmptyObject({})
<= true

isEmptySet (set)

Checks if the set is empty.

isEmptySet(new Set([]))
<= true

isEqual (value1, value2)

Checks if values are equal.

isEqual("foo", "foo")
<= true
isEqual([1, 3, 2], [1, 2, 3])
<= true
isEqual([1, 2, 3], [1, 2, 3])
<= true
isEqual([1, 3, 2], [1, 2, 3], true)
<= false
isEqual([1, 2, 3], [1, 2, 3], true)
<= true

isError (object)

Checks if passed object is an error object.

isError(new Error("ValidationError"))
<= true

isEven (number)

Checks if the number is even.

isEven(2)
<= true

isExclusiveAlternative (value1, value2)

Returns true when one value from passed values is truthy and second is falsy.

isExclusiveAlternative(1, 0)
<= true
isExclusiveAlternative(0, 1)
<= true

isExtensible (object)

Determines if an object can be extend by new properties.

isExtensible({})
<= true

isFalsy (value)

Checks if passed value is an falsy value.

isFalsy(false)
<= true
isFalsy(null)
<= true

isFinite (number)

Checks if the number is finite.

isFinite(100)
<= true

isFrozen (object)

Checks if tje object is frozen (cannot add, remove, change object properties).

isFrozen(Object.freeze({}))
<= true

isFunction (value)

Checks if passed value is a function.

isFunction(function () {})
<= true

isGreaterThan (number1, number2)

Checks if first number is greater than second.

isGreaterThan(10, 5)
<= true

isGreaterThanOrEqual (value1, value2)

Checks if first value is greater or equal second value.

isGreaterThanOrEqual(42, 42)
<= true

isImage (string)

Checks if the string is a name of image file.

isImage("foo.jpg")
<= true

isIn (value1, value2)

Checks if the first value includes second value.

isIn(5, [5, 10, 15])
<= true
isIn("5", "12345")
<= true

isInfinite (number)

Checks if the number is infinite.

isInfinite(Infinity)
<= true

isInstanceOf (object, constructor)

Checks if the prototype property of a constructor exists in the prototype chain of an object.

isInstanceOf(["foo", "bar"], Array)
<= true

isLaterThan (date1, date2)

Checks if one date is later than second date.

isLaterThan(new Date(2018, 4, 11), new Date(2018, 4, 1))
<= true

isLessThan (number1, number2)

Checks if first number is smaller than second.

isLessThan(2, 5)
<= true

isLessThanOrEqual (number1, number2)

Checks if first number is smaller or equal second.

isLessThanOrEqual(3,5)
<= true

isMap (value)

Checks if the value is a map.

isMap(new Map([ ["foo", "bar"] ]))
<= true

isMissing (object)

Checks if object is undefined.

isMissing(myVariable)
<= true
isMissing({})
<= true

isMobile ()

Checks if device is a smartphone (The viewport is at most 767 pixels wide).

isMobile()
<= true

isMultiple (numbe1, number2)

Checks if one number is multiple of second number.

isMultiple(200, 100)
<= true

isNaN (value)

Checks if passed value is NaN and the type of value is Number.

isNaN(NaN)
<= true

isNegative (number)

Checks if the number is negative.

isNegative(-100)
<= true

isNull (value)

Checks if the value is null.

isNull(null)
<= true
isNull(0)
<= false

isNumber (value)

Checks if the value is an number.

isNumber(2)
<= true

isNumeric (value)

Checks if the value is an numeric (a number or string that could be converted to number).

isNumeric(2)
<= true
isNumeric("2")
<= true

isObject (value)

Checks if the value is an object.

isObject({})
<= true
isObject(function () {})
<= true

isOdd (number)

Checks if the number is odd.

isOdd(1)
<= true

isPalindrome (string)

Checks if the value is palindrome.

isPalindrome("devil lived")
<= true

isPhone (string)

Checks if the string is a valid phone number.

isPhone("+48 123 345 67")
<= true

isPlainObject (value)

Checks if passed value is an object.

isPlainObject({})
<= true
isPlainObject([])
<= false

isPositive (number)

Checks if the number is positive.

isPositive(100)
<= true

isPresent (value)

Checks if the value is present (different than undefined).

isPresent({ foo: "bar" })
<= true
isPresent(null)
<= true

isPrime (number)

Checks if the number is prime.

isPrime(13)
<= true

isRegExp (value)

Checks if the value is an regural expression.

isRegExp(/foo/)
<= true

isSealed (object)

Checks if the object is frozen (cannot add and remove object properties).

isSealed(Object.seal({}))
<= true

isSet (value)

Checks if the value is an set.

isSet(new Set())
<= true

isSoonerThan (date1, date2)

Checks if one date is sooner than second date.

isSoonerThan(new Date(2018, 4, 1), new Date(2018, 4, 11))
<= true

isString (value)

Checks if the value is an string.

isString("foo")
<= true

isSymbol (value)

Checks if the value is an symbol.

isSymbol(Symbol("foo"))
<= true

isTablet ()

Checks if device is a tablet (The viewport width is between 768px and 991px).

isTablet()
<= true

isTruthy (value)

Checks if passed value is an truthy value.

isTruthy({})
<= true
isTruthy([])
<= true

isUndefined (value)

Checks if the value is undefined.

isUndefined(undefined)
<= true

isUrl (string)

Checks if the string is an url.

isUrl("https://buxlabs.pl/narzędzia/js")
<= true

isVideo (string)

Checks if the string is a name of video file.

isVideo("foo.mkv")
<= true

isWeakMap (value)

Checks if the value is an weakmap.

isWeakMap(new WeakMap())
<= true

isWeakSet (value)

Checks if the value is an weakset.

isWeakSet(new WeakSet())
<= true

matches (string, regExp)

Checks if string matches passed regExp.

matches("lorem ipsum", /ipsum/)
<= true

notEqual (value1, value2)

Checks if values are different.

notEqual("foo", "bar")
<= true

respondsTo (object, string)

Checks if object has function with the name given in string.

respondsTo({ foo: function () {} }, "foo")
<= true

startsWith (string1, string2)

Checks if first string starts with the second string.

startsWith("ban qux", "ban")
<= true