How to use
Tutorial
Follow this tutorial to learn everything you need to know.
Overview
In programming, expressions are vital building blocks, seamlessly combining operations and variables to produce specific outcomes. Shaped by your programming language's syntax, these expressions serve as the cornerstone of coding endeavors.
However, a challenge emerges when you want to store, send, or save these expressions in files or databases. That's where Mongu steps in. With Mongu, crafting expressions that can be easily stored and shared becomes as simple as defining a JSON.
Installation
To install this package you have to run the following command.
npm install mongu
Basic usage
To use this package you have to import the mongu
function and call this function passing as arguments an expression and an object that defines variables.
const { mongu } = require('mongu')
const expr = {
fullName: {
$concat: [{ $toLower: '$name' }, ' ', { $toLower: '$surname' }],
},
isAdult: { $gte: ['$age', 18] },
}
const vars = { name: 'Marti', surname: 'Serra', age: 24 }
console.log(mongu(expr, vars)) // { fullName: "marti serra", isAdult: true }
In the expression you can reference the variables, and you can use operators to define how the values have to be transformed.
Omit variables
The variables argument is optional, so you can omit it.
const { mongu } = require('mongu')
const expr = {
fullName: {
$concat: [{ $toLower: 'Marti' }, ' ', { $toLower: 'Serra' }],
},
isAdult: { $gte: [24, 18] },
}
console.log(mongu(expr)) // { fullName: "marti serra", isAdult: true }
Operators
When you create an expression you can use operators to define operations. There is a great variety of operators, and each of them is defined as an object with a key that starts with a dollar sign ($).
const { mongu } = require('mongu')
const expr = { $add: [5, 10] }
console.log(mongu(expr)) // 15
Variables
The second argument of the mongu function is the variables. These variables are defined as an object in which each key corresponds to a variable.
const { mongu } = require('mongu')
const expr = { $add: ['$a', '$b'] }
const vars = { a: 5, b: 10 }
console.log(mongu(expr, vars)) // 15
Variables can be of any type and to access a variable in an expression you just have to reference it with a string that starts with a dollar sign ($).
Object variables
When a variable is an object you can access the properties using the dot notation.
const { mongu } = require('mongu')
const expr = { $add: ['$numbers.a', '$numbers.b'] }
const vars = { numbers: { a: 5, b: 10 } }
console.log(mongu(expr, vars)) // 15
Array variables
When a variable is an array you can access the items using the dot notation.
const { mongu } = require('mongu')
const expr = { $add: ['$numbers.0', '$numbers.1'] }
const vars = { numbers: [5, 10] }
console.log(mongu(expr, vars)) // 15
Underscore (_)
When you want to treat the dollar sign ($) as a normal character you have to use the underscore character (_).
const { mongu } = require('mongu')
const expr = { _$add: ['_$a', '_$b'] }
const vars = { a: 5, b: 10 }
console.log(mongu(expr, vars)) // { $add: ["$a", "$b"] }