Skip to content

Latest commit

 

History

History
533 lines (428 loc) · 12.8 KB

number.md

File metadata and controls

533 lines (428 loc) · 12.8 KB

Number Functions

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

The number.isFinite function checks if a number is finite. It takes one parameter: the number to be checked.

Example

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
}

number.isOdd

The number.isOdd function checks if a number is odd. It takes one parameter: the number to be checked.

Example

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
}

number.isEven

The number.isEven function checks if a number is even. It takes one parameter: the number to be checked.

Example

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
}

number.isInteger

The number.isInteger function checks if a number is an integer. It takes one parameter: the number to be checked.

Example

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
}

number.toExponential

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.

Example

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"
}

number.toPrecision

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.

Example

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"
}

number.parseInt

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.

Example

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
}

number.parseFloat

The number.parseFloat function parses a string and returns a floating-point number. It takes a single parameter: the string to be parsed.

Example

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
}

number.toFixed

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.

Example

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"
}

number.toString

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.

Example

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"
}

number.isNaN

The number.isNaN function checks if the given value is Not-a-Number (NaN). It takes one parameter: the value to be checked.

Example

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
}

number.add

The number.add function sums all the numbers passed as arguments. It can take any number of arguments, and all arguments should be numbers.

Example

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
}

number.subtract

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.

Example

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
}

number.multiply

The number.multiply function multiplies all the numbers passed as arguments. It can take any number of arguments, and all arguments should be numbers.

Example

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
}

number.divide

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.

Example

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
}