A minimalistic library for writing safer code. It came out of a few years of programming JavaScript and TypeScript where I wrote these functions over and over to ensure code reliability.
It has a solid and minimalistic API surface that provides useful functions for basic type checking.
true
or false
(none of them throw
s in any condition)jty
helps verify function/method contracts and fail early with good error messages instead of continuing on wrong assumption and producing wrong results (which is hard to debug due to implicit type conversion quirks)jty
helps guarantee type safely when called from JavaScript code (also provides reliability against abusing TypeScript's escape hatches like as
and any
)jty
helps verify the shape of the object inside the code without having to write a schema.$ npm i jty
// In your JS file
const { isStr } = require('jty')
if (isStr('Hello world!', 3)) {
console.log('Success')
} else {
throw new TypeError('Expected an string with at least 3 characters')
}
If you use TypeScript, many of these functions work as guards:
const a = { foo: 13 }
if (hasPath(a, 'bar', 'baz')) {
// `a.foo` is valid, as well as `a.bar` and `a.baz`
}
Let's say you have a function that is supposed to double a number:
function double(n) {
return n + n
}
double(1) // 2
double(13) // 26
But this function happily accepts strings which is not desired:
double('13') // '1313'
Using jty
we can verify the input before using it:
const { isNum } = require('jty')
function double(n) {
if (isNum(n)) {
return n + n
}
throw new TypeError(`Expected a number but got ${n}`)
}
double(13) // 26
double('13') // throws 'Expected a number but got 13'
double(NaN) // throws 'Expected a number but got NaN'
You can also use the assertion library of your choice to make the code shorter and more readable:
// Node assert: https://nodejs.org/api/assert.html
const assert = require('assert')
const { isNum } = require('jty')
function double(n) {
assert(isNum(n))
return n + n
}
['possibleValue1', 'possibleValue2'].includes(x)
to check if x
is any of the possible values (kinda like TypeScript's union types)someArray.every(item => checkItemFormat(item))
to check that all elements of an array have a certain shapeisStr(x) || isInt(x)
check OR to ensure that a value is of either typesa instanceOf A
ensure that a
is an instance of the class A
in
operator with caution: user in objectWithOneKeyPerUser
returns true
if the user
is 'constructor'
(use hasOProp()
instead)console.assert()
function f(...props) { if (Array.isArr(props)) ... }
, props
is guaranteed to be an array per JavaScript language specification.Error
subclass:TypeError
when a value has an unexpected typeReferenceError
when a property is missing from an objectRangeError
when a value is outside the expected rangeSyntaxError
when there is a syntax error (usually comes handy when parsing a string, using regular expressions or validating JSON)Made in Sweden 🇸🇪 by Alex Ewerlöf
Similar to hasPath but only works for own properties (not inherited properties)
a value that may possibly have some properties
one or more property names
Same as hasProp but checks for own properties (not inherited properties)
an object
one or more property names
Checks if the provided value has the a path of properties
a value that may possibly have some properties
one or more property names
Checks if x is a non-null object that has all the provided properties
an object
one or more property names
Checks if a provided value is an instance of the provided class
This does not throw for some cases where JavaScript chokes ()
possibly an instance of a class
a class constructor (usually starts with big letter!)
Checks if the provided value is an array and optionally checks whether its length is in a boundary
possibly a string
minimum possible length (inclusive)
maximum possible length (inclusive)
Checks if the provided value is boolean (basically true
or false
)
This is exactly typeof x === "boolean"
but a bit shorter
possibly a boolean value
Checks if the provided value is defined
This is exactly x !== undefined
but a bit shorter
Also
any value
Checks if a value is a function
This is exactly typeof x === "function"
but a bit shorter
If you are using TypeScript and you know the function signature, you can provide the generic T
for your guard.
possibly a function (including static methods but not getters or setters)
true if the value is a function, false otherwise
Checks if a value is a finite integer number and optionally bound by a min and max.
possibly an integer number
the minimum possible value (inclusive). If this is not a finite number, the lower bound will not be checked
the maximum possible value (inclusive). If this is not a finite number, the upper bound will not be checked
Checks if a value is a finite number and optionally bound by a min and max
possibly a number
the minimum possible value (inclusive). If this is not a finite number, the lower bound will not be checked
the maximum possible value (inclusive). If this is not a finite number, the upper bound will not be checked
Checks if a value is a non-null object
possibly an object
true if the value is an non-null object, false otherwise
Checks if the provided value is a string and optionally checks whether its length is in a boundary
possibly a string
minimum possible length (inclusive)
maximum possible length (inclusive)
Checks if the provided value is a symbol
This is exactly x === "symbol"
but a bit shorter
possibly a symbol
Checks if the provided value is defined
This is exactly x === undefined
but a bit shorter
Also see isDef
any value
Acceptable types for object property names