Installation via NPM is very simple
npm install datautils
Once you've done that, you can access this using the
var datautils = require('datautils');
The data methods all work in fundamentally the same way - you pass in some raw input as the first parameter and a default value as the second. If the first parameter fulfils the criteria of the method, it return that otherwise it returns the default value.
Ensures that the input is an array
Ensures the the input is a boolean (true/false). It also casts it to a boolean if the following criteria:
Ensures that the input is an instance of the Date object. Can take in either a Date object or a string that matches the ISO8601 format.
Ensures that input is a floating point number. This can receive either a number or a numerical string.
Ensures that the input a function
Ensures that the input is an instance of the instance. The instance must be a function. This function can be injected into this method.
Makes sure that the input matches the given regular expression. It also forces the output to be a string, so be careful.
Ensures that the input is an object. Although this is designed to receive key/value pairs, but it will allow other objects (eg, Date). It will not allow arrays or null values however.
Ensures that the input is a string. If it is a number, this is cast to a string. If you wish to specify a series of values, this can be done by passing in some values.
var values = [ 'val1', 'val2', 'val3' ]; val1 = datautils.data.setString('val1', null, values); // 'val1' val2 = datautils.data.setString('val4', null, values); // null
The model stuff primarily all works in the same way - if it passes the test it
true, if it fails the test or the wrong input is entered it throws an
a maximum of three parameters - the
message (as the Error object has by default),
value (the value passed in) and
params (an array of any other paramaters passed
For instance, the function
validation.greaterThan(8, 10); throws an error
(because 8 is less than 10).
err.message equals VALUE_NOT_GREATER_THAN_TARGET,
err.value = 8 and
err.params = [ 10 ].
Checks if the given string validates as an email address. THIS DOES NOT CHECK IF THE EMAIL IS ACTUALLY VALID!!!
This tests if the two variables are equal. If the variables are of a complex nature (eg, objects), then it will match those too.
Does a numerical test on the variable, to see if it is greater than the given value.
Does a numerical test on the variable, to see if it is greater than or equal to the given value.
Makes sure that the value matches the length.
Checks if the value is between the two lengths.
Does a numerical test on the variable, to see if it is less than the given value.
Does a numerical test on the variable, to see if it is less than or equal to the given value.
Ensures that the value is no longer than the given max length.
Ensures that the value fulfils the given minimum length.
Matches the given value against the given regular expression. It will allow
either a string or an instance of the RegExp object (by either
new RegExp() or
If it's a truthy value, 0 or false, it is ok. Otherwise, it fails the test