In this section, various Number functions will be explored, which are available for use in PubSubDB mapping rules. These functions are designed to facilitate the manipulation and transformation of numbers during the mapping process. They are inspired by and should be familiar to JavaScript developers. The key principle to keep in mind is that each transformation is a function that expects one or more input parameters from the prior row in the @pipe structure.
Table of Contents
- number.isFinite: Check if a number is finite
- number.isEven: Check if a number is even
- number.isOdd: Check if a number is odd
- number.isInteger: Check if a number is an integer
- number.isNaN: Check if a value is NaN
- number.parseFloat: Parse a string and return a floating-point number
- number.parseInt: Parse a string and return an integer
- number.toExponential: Return a string representing the number in exponential notation
- number.toFixed: Return a string representing the number in fixed-point notation
- number.toPrecision: Return a string representing the number to a specified precision
- number.add: Sums all the numbers passed as arguments
- number.subtract: Subtracts all the numbers passed as arguments in the order they are given
- number.multiply: Multiplies all the numbers passed as arguments
- number.divide: Divides all the numbers passed as arguments in the order they are given
The number.isFinite
function checks if a number is finite. It takes one parameter: the number to be checked.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"value": 42
}
}
}
The goal is to create a new object with a boolean field indicating if the value
field is a finite number. The number.isFinite
function can be used in the mapping rules as follows:
is_finite:
"@pipe":
- ["{a.output.data.value}"]
- ["{@number.isFinite}"]
After executing the mapping rules, the resulting JSON object will be:
{
"is_finite": true
}
The number.isOdd
function checks if a number is odd. It takes one parameter: the number to be checked.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"value": 42
}
}
}
The goal is to create a new object with a boolean field indicating if the value
field is an odd number. The number.isOdd
function can be used in the mapping rules as follows:
is_odd:
"@pipe":
- ["{a.output.data.value}"]
- ["{@number.isOdd}"]
After executing the mapping rules, the resulting JSON object will be:
{
"is_odd": false
}
The number.isEven
function checks if a number is even. It takes one parameter: the number to be checked.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"value": 42
}
}
}
The goal is to create a new object with a boolean field indicating if the value
field is an even number. The number.isEven
function can be used in the mapping rules as follows:
is_even:
"@pipe":
- ["{a.output.data.value}"]
- ["{@number.isEven}"]
After executing the mapping rules, the resulting JSON object will be:
{
"is_even": true
}
The number.isInteger
function checks if a number is an integer. It takes one parameter: the number to be checked.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"value": 42
}
}
}
The goal is to create a new object with a boolean field indicating if the value
field is an integer. The number.isInteger
function can be used in the mapping rules as follows:
is_integer:
"@pipe":
- ["{a.output.data.value}"]
- ["{@number.isInteger}"]
After executing the mapping rules, the resulting JSON object will be:
{
"is_integer": true
}
The number.toExponential
function formats a number using exponential notation, rounding if necessary. It takes two parameters: the number to be formatted and the number of digits after the decimal point.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"value": 123456
}
}
}
The goal is to create a new object with the value
field represented in exponential notation with 2 decimal places. The number.toExponential
function can be used in the mapping rules as follows:
exponential_value:
"@pipe":
- ["{a.output.data.value}", 2]
- ["{@number.toExponential}"]
After executing the mapping rules, the resulting JSON object will be:
{
"exponential_value": "1.23e+5"
}
The number.toPrecision
function formats a number to a specified precision (total number of significant digits) using either fixed-point or exponential notation, depending on the value. It takes two parameters: the number to be formatted and the precision.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"value": 42.12345
}
}
}
The goal is to create a new object with the value
field formatted to a precision of 4 significant digits. The number.toPrecision
function can be used in the mapping rules as follows:
precise_value:
"@pipe":
- ["{a.output.data.value}", 4]
- ["{@number.toPrecision}"]
After executing the mapping rules, the resulting JSON object will be:
{
"precise_value": "42.12"
}
The number.parseInt
function parses a string and returns an integer of the specified radix (base). It takes two parameters: the string to be parsed and the radix.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"hex_value": "1a"
}
}
}
The goal is to create a new object with the hex_value
field converted to a decimal integer. The number.parseInt
function can be used in the mapping rules as follows:
decimal_value:
"@pipe":
- ["{a.output.data.hex_value}", 16]
- ["{@number.parseInt}"]
After executing the mapping rules, the resulting JSON object will be:
{
"decimal_value": 26
}
The number.parseFloat
function parses a string and returns a floating-point number. It takes a single parameter: the string to be parsed.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"string_value": "3.14159"
}
}
}
The goal is to create a new object with the string_value
field converted to a floating-point number. The number.parseFloat
function can be used in the mapping rules as follows:
float_value:
"@pipe":
- ["{a.output.data.string_value}"]
- ["{@number.parseFloat}"]
After executing the mapping rules, the resulting JSON object will be:
{
"float_value": 3.14159
}
The number.toFixed
function formats a number using fixed-point notation, with a specified number of digits after the decimal point. It takes two parameters: the number to be formatted and the number of digits after the decimal point.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"value": 123.45678
}
}
}
The goal is to create a new object with the value
field formatted with 2 digits after the decimal point. The number.toFixed
function can be used in the mapping rules as follows:
fixed_value:
"@pipe":
- ["{a.output.data.value}", 2]
- ["{@number.toFixed}"]
After executing the mapping rules, the resulting JSON object will be:
{
"fixed_value": "123.46"
}
The number.toString
function converts a number to a string representation. It takes two parameters: the number to be converted, and the radix (optional, default is 10) to which the number should be converted.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"value": 42
}
}
}
The goal is to create a new object with the value
field converted to a string representation. The number.toString
function can be used in the mapping rules as follows:
string_value:
"@pipe":
- ["{a.output.data.value}"]
- ["{@number.toString}"]
After executing the mapping rules, the resulting JSON object will be:
{
"string_value": "42"
}
The number.isNaN
function checks if the given value is Not-a-Number (NaN). It takes one parameter: the value to be checked.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"value1": "NaN",
"value2": 42
}
}
}
The goal is to create a new object with two boolean fields, is_value1_nan
and is_value2_nan
, indicating whether the respective values are NaN or not. The number.isNaN
function can be used in the mapping rules as follows:
is_value1_nan:
"@pipe":
- ["{a.output.data.value1}"]
- ["{@number.isNaN}"]
is_value2_nan:
"@pipe":
- ["{a.output.data.value2}"]
- ["{@number.isNaN}"]
After executing the mapping rules, the resulting JSON object will be:
{
"is_value1_nan": true,
"is_value2_nan": false
}
The number.add
function sums all the numbers passed as arguments. It can take any number of arguments, and all arguments should be numbers.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"values": [1, 2, 3, 4, 5]
}
}
}
The goal is to create a new object with the sum
field calculated by summing all the numbers in the values
array. The number.add
function can be used in the mapping rules as follows:
sum:
"@pipe":
- ["{a.output.data.values}"]
- ["{@number.add}"]
After executing the mapping rules, the resulting JSON object will be:
{
"sum": 15
}
The number.subtract
function subtracts all the numbers passed as arguments in the order they are given. It can take any number of arguments, and all arguments should be numbers. If only one argument is provided, it will return the negative of that number.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"values": [20, 5, 3]
}
}
}
The goal is to create a new object with the difference
field calculated by subtracting all the numbers in the values
array in the order they are given. The number.subtract
function can be used in the mapping rules as follows:
difference:
"@pipe":
- ["{a.output.data.values}"]
- ["{@number.subtract}"]
After executing the mapping rules, the resulting JSON object will be:
{
"difference": 12
}
The number.multiply
function multiplies all the numbers passed as arguments. It can take any number of arguments, and all arguments should be numbers.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"values": [2, 3, 4]
}
}
}
The goal is to create a new object with the product
field calculated by multiplying all the numbers in the values
array and then multiplying by 5
. The number.multiply
function can be used in the mapping rules as follows:
product:
"@pipe":
- ["{a.output.data.values}", 5]
- ["{@number.multiply}"]
After executing the mapping rules, the resulting JSON object will be:
{
"product": 120
}
The number.divide
function divides all the numbers passed as arguments in the order they are given. It can take any number of arguments, and all arguments should be numbers. Division by zero is not allowed.
Suppose there are the following input JSON objects:
Object A:
{
"output": {
"data": {
"values": [20, 5, 2]
}
}
}
The goal is to create a new object with the quotient
field calculated by dividing all the numbers in the values
array in the order they are given. The number.divide
function can be used in the mapping rules as follows:
quotient:
"@pipe":
- ["{a.output.data.values}"]
- ["{@number.divide}"]
After executing the mapping rules, the resulting JSON object will be:
{
"quotient": 2
}