hlpjs
hlpjs
is a simple NodeJS / Javascript backend validators and asserter module.
Installation
Run:
npm install --save hlpjs
Then, in your javascript file:
const hlp = ;
Contributing
See CONTRIBUTING.md
Documentations & Methods
For all of the following methods, assume we called const hlp = require('hlpjs')
. For each method, I listed the purpose along with the return type (or {void}
) in bold brackets ({return type}
), then parameters, then the usage of the method, and sometimes an example. In case a method throws error, I indicated it with [throws]
.
A couple of definitions:
Any
: This is any type. It can be aFunction
,Object
,Array
,Number
, etc.Primitive
: A primitive type. These arenull
,undefined
,String
, andNumber
. They are "primitive" because they can be checked for equality with===
against another primitive.
NOTE: The documentation below will offer examples. However, the documentation in the code is very much more detailed about what the requirements / outcomes of the methods will be. Using an IDE such as IDEA PHPStorm or WebStorm helps.
Table of Contents for Documentations
This will be added in the future if REALLY needed
Regular Methods
throw
{void}
Throw an error with constructor ErrorInstance with the given message.
message
: String.ErrorInstance
: Error constructor to throw.
hlp
isBoolean
{Boolean}
Check if the arg
is a Boolean.
arg
: Any.
hlp
isNumber
{Boolean}
Check if the arg
is a usable Javascript Number
that is not NaN
.
arg
: Any.
hlp
isInteger
{Boolean}
Check if the argument is an integer.
arg
: Any.
hlp
isNull
{Boolean}
Check if the argument is null
.
arg
: Any.
hlp
isUndefined
{Boolean}
Check if the argument is undefined
.
arg
: Any.
hlp
isString
{Boolean}
Check if the argument is a String
.
arg
: Any.
hlp
isArray
{Boolean}
Check if the argument is an Array
.
arg
: Any.
hlp
isPrimitive
{Boolean}
Check if the argument is an Primitive
.
arg
: Any.
hlp
Primitive
{void}
[throws]
Create an instance of Primitive
type. It will throw an error if arg
is not Primitive
. This is not meant to be used. It was created to be used on jsdocs.
arg
: Any.
hlp
hasStringLength
{Boolean}
[throws]
Checks if str
has the length exp_len
. This throws if any of the parameter types is invalid.
str
: Stringexp_len
: Integer
hlp
extendString
{String}
[throws]
Takes str_or_num
, converts it into String
, then expands it (if needed) so that the length of the string is equal to desired_length
by appending additional_character
at the beginning of the string multiple times. This throws if any of the parameter types is invalid.
str_or_num
: String | Numberdesired_length
: Integeraddition_character
: String, this must be of length 1.
hlp
Examples:
hlp; // ' con'hlp; // '0cone'hlp; // 'cone'hlp; // 'conecodecode'hlp; // 'oooe'
containsNonNumeric
{Boolean}
[throws]
Checks if str
contains non-numeric characters (anything other than 0,1,2,3,4,5,6,7,8,9). This throws if any of the parameter types is invalid.
str
: String
hlp
Examples:
hlp; // truehlp; // falsehlp; // true
convertStringToNumber
{Number}
[throws]
Converts str
into a number. This throws if the string contains non-numeric characters (as that cannot be converted) except for up to one point (i.e. .
). This throws if any of the parameter types is invalid.
str
: string
hlp
isPrimitiveArgInArray
{Boolean}
[throws]
Checks if arg
is in arr
. arr
needs to be an array of Primitive
. This throws if any of the parameter types is invalid.
primitive
: Primitivearr
: Array
hlp
Examples:
hlp; // truehlp; // falsehlp; // throws errorhlp; // throws error
arePrimitiveArgsEqual
{Boolean}
[throws]
Checks if the two primitives are equal. This throws if any of the parameter types is invalid.
primitive1
: Primitiveprimitive2
: Primitive
hlp
Asserters
These method throw errors when the condition to be asserted is not met. These methods are found under the key assert
. For example,
const hlp = ;hlpassert; // throws an error
Most assert methods have 3 arguments while some of a 4 arguments parameters. All asserters' last 2 parameters are message
and ErrorInstance
.
message
: The message you want to be printed out in case the assertion failsErrorInstance
: The Error instance to be thrown in case the assertion fails. So, you have the flexibility of choosing what type of error to throw with what message. The only catch is thatErrorInstance.prototype
must be an instance of JavaScript'sError
constructor. I.e., the checkErrorInstance.prototype instanceof Error
is made prior to throwing this. In case it's not, it will fall back toError
.
All asserter methods throw an error when the expected argument type is invalid.
Assuming we called const hlp = require('hlpjs')
, here are the methods:
argIsNumber
{void}
[throws]
Throws when arg
is not a number.
arg
: Any
hlpassert
argIsInteger
{void}
[throws]
Throws when arg
is not an integer as defined in hlp.isInteger(...)
.
arg
: Any
hlpassert
argIsNull
{void}
[throws]
Throws when arg
is not null.
arg
: Any
hlpassert
argIsNotNull
{void}
[throws]
Throws when arg
is null.
arg
: Any
hlpassert
argIsNotUndefined
{void}
[throws]
Throws when arg
is undefined.
arg
: Any
hlpassert
argIsString
{Boolean}
[throws]
Throws when arg
is not a String.
arg
: Any
hlpassert
argIsArray
{void}
[throws]
Throws when arg
is not an Array.
arg
: Any
hlpassert
argIsPrimitive
{void}
[throws]
Throws when arg
is not primitive as defined with hlp.isPrimitive(...)
.
arg
: Any
hlpassert
stringHasLength
{void}
[throws]
Throws when str
does not have length exp_len
.
str
: Stringexp_len
: Integer
hlpassert
stringContainsOnlyNumeric
{void}
[throws]
Throws when str
contains non-numeric characters.
str
: String
hlpassert
primitivesAreEqual
{void}
[throws]
Throws when prim1
does not equal prim2
.
prim1
: Primitiveprim2
: Primitive
hlpassert