Standard Library Functions

Overview

Clevero scripting gives you access to a collection of handy JavaScript libraries that make it easy to build powerful workflows and scripts. Whether you need to encrypt data, handle authentication, parse XML, or work with currencies, these libraries have you covered with ready-to-use functions.

Here's what you can use:

LibraryWhat it does
CryptoHandle all your security needs. From hashing, HMAC, generating random bytes, and encoding/decoding with Base64 URLs.
JWTWork with JSON Web Tokens. Create them, verify them, and decode them.
Query String (QS)Turn query strings into objects and vice versa, perfect for URL handling.
XML to JSONEasily convert between XML and JSON formats.
Currency.jsDo currency math without worrying about decimal precision issues.
MomentParse, validate, manipulate, and format dates and times with ease.
LodashSimplify working with arrays, objects, strings, and other JavaScript data.

The examples on this page show common ways to use the available libraries in Clevero scripting.

🪙Crypto

The crypto library provides functions for hashing, HMAC generation, random byte generation, timing-safe comparisons, and Base64 URL encoding and decoding.

HMAC SHA-256

Use hmacSha256() to create an HMAC SHA-256 hash.

async function script() {
  const result = crypto.hmacSha256("secret", "hello");
  return result;
}

You can also specify base64url as the output format:

async function script() {
  const result = crypto.hmacSha256("secret", "hello", "base64url");
  return result;
}

SHA-256

Use sha256() to create a SHA-256 hash.

By default, the result is returned as a hexadecimal string:

async function script() {
  const hash = crypto.sha256("hello");
  return hash;
}

You can also return the result as a buffer:

async function script() {
  const result = crypto.sha256(Buffer.from("hello"), "buffer");
  return result;
}

Random Bytes

Use randomBytes() to generate random bytes.

async function script() {
  const result = crypto.randomBytes(16);
  return result;
}

Timing-Safe Comparison

Use timingSafeEqual() to compare two buffers without exposing timing differences that could be used in certain security attacks.

async function script() {
  const result = crypto.timingSafeEqual(
    Buffer.from("abc"),
    Buffer.from("abc")
  );

  return result;
}

The function returns true when the values are equal and false when they are not.

Base64 URL Encoding and Decoding

Use crypto.base64url.encode() and crypto.base64url.decode() to encode and decode Base64 URL values.

async function script() {
  const encoded = crypto.base64url.encode("Hello");
  const decoded = crypto.base64url.decode(encoded);

  return {
    encoded,
    decoded: decoded.toString()
  };
}

🔑JSON Web Token (JWT)

The jwt library provides functions for creating, verifying, and decoding JSON Web Tokens.

Sign a Token

Use jwt.sign() to create a token.

async function script() {
  const token = jwt.sign(
    { user: "ipsa" },
    "secret123",
    {
      alg: "HS256",
      issuer: "me",
      audience: "you",
      expiresIn: "1h"
    }
  );

  return token;
}

Verify a Token

Use jwt.verify() to verify a token and return its payload.

async function script() {
  const token = jwt.sign(
    { user: "ipsa" },
    "secret123",
    {
      alg: "HS256",
      issuer: "me",
      audience: "you",
      expiresIn: "1h"
    }
  );

  const verified = jwt.verify(
    token,
    "secret123",
    {
      alg: "HS256",
      issuer: "me",
      audience: "you"
    }
  );

  return {
    user: verified.payload.user
  };
}

Decode a Token

Use jwt.decode() to decode a token without verifying its signature.

async function script() {
  const decoded = jwt.decode(token);
  return decoded;
}
⚠️

Important

jwt.decode() does not verify the token's signature. Do not use the decoded values as trusted data unless the token has been verified with jwt.verify().


💻Query String (QS)

The qs library provides functions for parsing query strings into objects and converting objects into query strings.

Parse a Query String

Use qs.parse() to convert a query string into an object.

async function script() {
  const result = qs.parse("name=ipsa&age=21");
  return result;
}

This returns an object similar to:

{
  name: "ipsa",
  age: "21"
}

A leading ? is also supported:

async function script() {
  const result = qs.parse("?q=test");
  return result;
}

Convert an Object to a Query String

Use qs.stringify() to convert an object into a query string.

async function script() {
  const query = qs.stringify({
    name: "ipsa",
    age: 21
  });

  return query;
}

For example, an object containing an array can be converted into a query string:

async function script() {
  const query = qs.stringify({
    tag: ["a", "b"]
  });

  return query;
}

➡️XML to JSON

The XML library provides functions for converting XML to JSON and JSON to XML.

Parse XML

Use xml.parse() to convert XML into a JavaScript object.

async function script() {
  const result = xml.parse(
    "<user><name>ipsa</name></user>"
  );

  return result;
}

Build XML

Use xml.build() to convert a JavaScript object into XML.

async function script() {
  const result = xml.build(
    {
      root: {
        a: "1"
      }
    },
    {
      rootName: "root"
    }
  );

  return result;
}
📘

Info

The XML parser has limits on input size, nesting depth, and the number of child elements. These limits help prevent excessively large or deeply nested XML data from being processed.


💰Currency.js

The money library provides functions for performing currency calculations and formatting monetary values.

Create a Currency Value

Use money.from() to create a currency value.

async function script() {
  const amount = money.from(100.25);

  return {
    value: amount.value(),
    cents: amount.toCents()
  };
}

Add and Subtract

Use add() and subtract() to perform calculations.

async function script() {
  const a = money.from(100);
  const b = money.from(50);

  return {
    add: a.add(b).value(),
    subtract: a.subtract(b).value()
  };
}

Multiply and Divide

Use multiply() and divide() for calculations.

async function script() {
  const amount = money.from(100);

  return {
    multiply: amount.multiply(2).value(),
    divide: amount.divide(4).value()
  };
}

Allocate an Amount

Use allocate() to split an amount into multiple parts while accounting for rounding.

async function script() {
  const parts = money.from(100).allocate([1, 1, 2]);

  return parts.map(part => part.value());
}

Convert to Cents

Use toCents() to return the amount as an integer representing the smallest currency unit.

async function script() {
  const amount = money.from(1.23);

  return amount.toCents();
}

Format a Currency Value

Use format() to format a currency value.

async function script() {
  const amount = money.from(150.75);

  return amount.format({
    symbol: "$"
  });
}

⏲️Moment

The moment library provides functions for working with dates and times, including parsing, formatting, comparing, and manipulating dates.

For example:

async function script() {
  const date = moment();

  return date.format("YYYY-MM-DD");
}

You can also add or subtract time:

async function script() {
  const date = moment().add(7, "days");

  return date.format("YYYY-MM-DD");
}

Lodash

The lodash library provides utility functions for working with JavaScript data, including arrays and objects.

For example, use _.get() to safely access a nested property:

async function script() {
  const value = _.get(
    {
      user: {
        name: "ipsa"
      }
    },
    "user.name"
  );

  return value;
}

You can also use functions such as _.filter() to filter an array:

async function script() {
  const users = [
    { name: "Alice", active: true },
    { name: "Bob", active: false },
    { name: "Charlie", active: true }
  ];

  return _.filter(users, { active: true });
}

📌 Need Help?

If you require assistance or encounter any issues, please don't hesitate to contact us for further support.