Basic operatorsUsage

Basic operators

Usage

How to use basic operators when using Expry.


Overview

The core Expry package doesn't include built-in operators because its primary purpose is to enable the definition of operations that map JSON to JavaScript code.

However, since certain operations are frequently needed, we offer an additional package containing a set of basic, commonly used operators.

Installation

To install this package you have to run the following command.

Terminal
npm install @expry/basic

Usage

To use the basic operators we need to include them when creating the expry function.

TypeScript
import { createExpry } from "@expry/system";

import { basicOperations, BasicOperations } from "@expry/basic";

const expry = createExpry<[BasicOperations]>(basicOperations);

Then, we can use them as you can see here.

TSX
const expression: unknown = {
  name: { $concat: ["$name", " ", "$surname"] },
  mostFavoriteSports: {
    $filter: {
      input: "$sports",
      as: "sport",
      cond: { $gt: ["$$sport.rating", 8] },
    },
  },
};

const variables: Record<string, unknown> = {
  name: "John",
  surname: "Doe",
  sports: [
    { name: "football", rating: 9 },
    { name: "basketball", rating: 10 },
    { name: "tennis", rating: 5 },
    { name: "swimming", rating: 7 },
  ],
};

const result = expry(expression, variables);

console.log(result);