1.6.6 • Published 1 year ago

guardex v1.6.6

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Guardex

A very small library to create type guards for your project, it can be used either for typescript or javascript.

To install the guardex library, run the following command in the console.

  npm install guardex --save-dev

Note: All of the examples in this document uses ECMA syntax to import or export code from the library, but this supports CommonJS syntax as well.

// ECMA Syntax
import is from "guardex";

// CommonJS Syntax
const is = require("guardex");

If you are using ESM with TypeScript consider reading the following information links:

Functions

These are the functions from the is exported object that can be used as type guards.

functionguards to
undefinedundefined
nullnull
trivialnull, undefined
value{}
object{} or T
json{} or T
rgb{ red: number, green: number, blue: number }
functionFunction
symbolSymbol
stringstring
numbernumber, bigint
zero0
positivenumber, bigint, not 0
negativenumber, bigint, not 0
betweennumber, bigint
insidenumber, bigint
smallernumber, bigint
smallerOrEqualnumber, bigint
biggernumber, bigint
biggerOrEqualnumber, bigint
arrayArray (Exotic object)
truetrue
truthyOther than false, 0, "", null, undefined
falsefalse
falsyfalse, 0, "", null, undefined
booleanboolean
truetrue
falsefalse
primitiveFunction, Symbol, string, number, bigint, boolean
promisePromise, Custom Promise (Read Promises)

Descriptions

Undefined

Checks if some value is undefined..

Null

Checks if some value is null..

Trivial

Checks if some value is null or undefined..

Value

Checks if some value is not null or undefined..

Object

Checks if some value is a function.

Symbol

Checks if some value is a string.

Number

Checks if some value is a number.

Zero

Checks if some value is a number and zero.

Positive

Checks if some value is a number and is positive.

Note Includes 0 as positive value

Negative

Checks if some value is a number and is negative.

Note Not includes 0 as negative value

Between

Checks if is a number and if it is between the upper bound and lower bound.

Inside

Checks if is a number and if it is between the upper bound and lower bound or is one of those values.

Smaller

Checks if is a number and if it is before the bound.

Smaller Or Equal

Checks if is a number and if it is before the bound or is equal/

Bigger

Checks if is a number and if it is beyond the bound.

Bigger Or Equal

Checks if is a number and if it is beyond the bound or is equal.

Boolean

Checks if some value is a boolean.

True

Checks if some value is a boolean and is true.

Truthy

Checks if some value is truthy.

Note Any other value than false, 0, "", null, undefined is considered truthy.

False

Checks if some value is a boolean and is false.

Falsy

Checks if some value is falsy.

Note Any value of false, 0, "", null, undefined is considered falsy.

Primitive

Checks if some value is a JavaScript primitive.

Array

Checks a value is a valid array.

Note Only works for the exotic object Array.

Object

Checks if some value is not a primitive.

Note If using typescript the generic T works only as a type guard and not as an instance checker, for more information go to https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates

JSON

Checks if the value descends from the protoype of the {} object and is not a primitive value.

Note If using typescript the generic T works only as a type guard and not as an instance checker, for more information go to https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates

Instance Of

Checks if the passed value is a valid instance of the parent.

RGB

Checks if an object is a valid RGB where the passed object should contain the red, green, blue properties where each one should be a number from 0 to 255.

RGB String

Checks if the string starts with the character # and if after that is followed by 6 characters where each: is a value from A to F (casing is ignored) or any number from 0 to 9.

// This is a valid RGB string
const tangerine = "#e1442A";

Promises

Promises were while ago a feature only accessible with polyfills. This was somehing new that helped a lot to run in syntactic sugar way callbacks. Currently is a fully supported feature in every modern browser or any modern JavaScript interpreter with the standarization of ES6 (https://caniuse.com/?search=es6) with even its own syntactic sugar as the async/await of JavaScript. Because of this, when the global Promise class is found the only comparison that will be done is that is a non trivial value and the instanceof operator.

In case this function runs in an obsolete environment (i.e. Internet Explorer) that ussually uses polyfill libraries, the method will check:

  • That is an object (not trivial)
  • If the then, catch and finally function are available
  • If its part of the protoype chain of a Promise prototype function
  • If it contains the Symbol.toStringTag equals to "[object Promise]"

The custom Promise may look like the following example:

// Parent function
function Promise() {}

// All of the three standard methods
Promise.then = function () {};
Promise.catch = function (error) {};
Promise.finally = function () {};

// The symbol tag to be represented as a promise
Promise[Symbol.toStringTag] = "Promise";

// The prototype and constructor assignation
Promise.prototype = Promise;
Promise.constructor = Promise;
1.6.4

1 year ago

1.6.3

1 year ago

1.6.2

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.6.6

1 year ago

1.6.5

1 year ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago