Search…
⌃K
Links

Inbuilt Libraries

A summary of external helper libraries available in the tests
There are many additional external helper functions available in the scope of the test body. Let’s have a short look at them.

Chance.js

Chance.js is a random content generation library which is quite useful to generate data. For example, if you wanted to generate a random street address, you can use chance.js:
state['street'] = chance.address();
state['city'] = chance.city();
state['zip'] = chance.zip();
return JSON.stringify(state);
And voila, you have a completely random fake street address.

Day.js

Day.js is another useful library, especially to manipulate time. In the example given below, we will calculate the elapsed time since the simulation started (iteration 0)
{
if (state['start'] === undefined) {
state['start'] = dayjs();
}
state['elapsed'] = dayjs(state['start']).fromNow();
return JSON.stringify(state);
}

Underscore.js/Lodash

Underscore.js is another popular library that is a must-have for serious javascript programmers. Here is an example of filtering even values in an array:
{
var evens = _.filter([1, 2, 3, 4, 5, 6], function(num) {
return num % 2 == 0;
});
return JSON.stringify(evens);
}

DeAsync.js

DeAsync.js can be used to create synchronous behaviour out of async or callback-based functions. Though async functions should be used as much as possible, in some cases, DeAsync could make life easier. For Example, in the code snippet given below, we are waiting for the callback function to finish while calling setTimeout() async function.
{
var ret;
setTimeout(function() {
ret = "hello";
}, 3000);
while (ret === undefined) {
deasync.sleep(100);
}
}

JSON Web Tokens

JSON web tokens are gaining popularity as an authorization mechanism. IOTIFY tests support creating JWTs by integrating the jsonwebtoken NPM library.
{
var token = jwt.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256' });
}

Geolib

Geolib is a useful library for geospatial operations or for manipulating locations. For example, to get the distance from Delhi to a specified coordinate, use the following code snippet:
{
var distance = geolib.getDistance(
location('Delhi'),
{latitude: 51.519475, longitude: 7.46694444});
}

Geohash

Geohash allows the conversion of Latitude and Longitude pairs to a geohash and vice versa.
{
let hash = geohash.encode(37.8324, 112.5584);
var latlon = geohash.decode('ww8p1r4t8');
}

Native built-in objects

There are several built-in modules available in the function body. Math is one of them which is quite useful for arithmetic operations.