A minimal type checking library that I developed over a few years of programming JavaScript. It has a solid and minimalistic API surface that provides useful functions for basic type checking.
- All functions return
trueorfalse(no onethrows) - It is resistent to monkey patching or malicious prototype overriding
- It comes with TypeScript support out of the box.
- Thoroughly tested for edge cases
- Works in Node and Browsers (CommonJS out of the box)
Returns true if x is a non-null object.
Returns true if x is a function (including class methods, arrow functions, result of new Function(), etc.).
Returns true if x is a finite number.
- If
minis a number, it'll also check thatx.length >= min - If
maxis a number, it'll also check thatx.length <= max(note that it is an inclusive range)
Returns true if x is an integer number.
- If
minis a number, it'll also check thatx.length >= min - If
maxis a number, it'll also check thatx.length <= max(note that it is an inclusive range)
Returns true if x is a boolean
Returns true if x is a string.
- If
minLengthis a number, it'll also check thatx.length >= minLength - If
maxLengthis a number, it'll also check thatx.length <= maxLength(note that it is an inclusive range)
Returns true if x is an array (Array.isArray()).
- If
minLengthis a number, it'll also check thatx.length >= minLength - If
maxLengthis a number, it'll also check thatx.length <= maxLength(note that it is an inclusive range)
Returns true if x is an array or string and idx represents a valid index to it (0 <= idx && idx < x.length)
Returns true if x is defined (x !== undefined)
The opposite of isDef(). Returns true if x === undefined
Reutnrs true if x is an object and the property path specified in propNames exists.
const a = {
b: {
c: [0, 1, 2]
}
}
hasProp(a, 'b', 'c', 0) // returns true
hasProp(a, 'b', 'c', '1') // returns true
hasProp(a, 'b', 'c', 'd') // returns falseSame as hasProp() but only returns true if all prop names are own properties (hence the O in the name). This is the safer way to check the existence of usernames in an object without mistakenly returning true for constructor or other prototypically-inherited properties.
const users = {
'alex': '123456'
}
const name = 'alex'
hasProp(a, name) // returns true
hasProp(a, 'constructor') // returns true
hasOProp(a, 'constructor') // returns falseMade in Sweden 🇸🇪 by Alex Ewerlöf