Skip to content

@sentrie/js

The @sentrie/js module provides access to JavaScript globals (Math, String, Number, Date, JSON, Array) as individual functions. This allows you to use standard JavaScript functions directly in your policies.

use { round, floor, ceil } from @sentrie/js as math
use { length, fromCharCode } from @sentrie/js as str
use { parse, stringify } from @sentrie/js as json

Returns the value of a number rounded to the nearest integer.

Example:

use { round } from @sentrie/js as math
let value = math.round(4.5) // 5

Returns the largest integer less than or equal to a number.

Example:

use { floor } from @sentrie/js as math
let value = math.floor(4.7) // 4

Returns the smallest integer greater than or equal to a number.

Example:

use { ceil } from @sentrie/js as math
let value = math.ceil(4.3) // 5

Returns the maximum value from a list of numbers.

Example:

use { max } from @sentrie/js as math
let value = math.max(1, 5, 3, 9, 2) // 9

Returns the minimum value from a list of numbers.

Example:

use { min } from @sentrie/js as math
let value = math.min(1, 5, 3, 9, 2) // 1

Returns the absolute value of a number.

Example:

use { abs } from @sentrie/js as math
let value = math.abs(-5) // 5

Returns the square root of a number.

Example:

use { sqrt } from @sentrie/js as math
let value = math.sqrt(16) // 4

pow(base: number, exponent: number): number

Section titled “pow(base: number, exponent: number): number”

Returns the value of base raised to the power of exponent.

Example:

use { pow } from @sentrie/js as math
let value = math.pow(2, 3) // 8

Returns e raised to the power of x (eˣ).

Example:

use { exp } from @sentrie/js as math
let value = math.exp(1) // ≈ 2.718

Returns the natural logarithm (base e) of a number.

Example:

use { log, E } from @sentrie/js as math
let value = math.log(math.E) // 1

Returns the base-10 logarithm of a number.

Example:

use { log10 } from @sentrie/js as math
let value = math.log10(100) // 2

Returns the base-2 logarithm of a number.

Example:

use { log2 } from @sentrie/js as math
let value = math.log2(8) // 3

All trigonometric functions work with angles in radians.

Returns the sine of an angle in radians.

Example:

use { sin, PI } from @sentrie/js as math
let value = math.sin(math.PI / 2) // 1

Returns the cosine of an angle in radians.

Example:

use { cos } from @sentrie/js as math
let value = math.cos(0) // 1

Returns the tangent of an angle in radians.

Example:

use { tan, PI } from @sentrie/js as math
let value = math.tan(math.PI / 4) // ≈ 1

Returns the arcsine (inverse sine) of a number in radians.

Example:

use { asin } from @sentrie/js as math
let value = math.asin(1) // ≈ 1.5708 (π/2)

Returns the arccosine (inverse cosine) of a number in radians.

Example:

use { acos } from @sentrie/js as math
let value = math.acos(1) // 0

Returns the arctangent (inverse tangent) of a number in radians.

Example:

use { atan } from @sentrie/js as math
let value = math.atan(1) // ≈ 0.7854 (π/4)

Returns the arctangent of the quotient of its arguments.

Example:

use { atan2, PI } from @sentrie/js as math
let value = math.atan2(1, 0) // ≈ 1.5708 (π/2)

Returns the hyperbolic sine of a number.

Returns the hyperbolic cosine of a number.

Returns the hyperbolic tangent of a number.

Returns a random number between 0 (inclusive) and 1 (exclusive).

Example:

use { random } from @sentrie/js as math
let value = math.random() // 0.0 to 1.0
  • E - Euler’s number (e ≈ 2.718281828459045)
  • PI - Pi (π ≈ 3.141592653589793)
  • LN2 - Natural logarithm of 2 (ln(2) ≈ 0.6931471805599453)
  • LN10 - Natural logarithm of 10 (ln(10) ≈ 2.302585092994046)
  • LOG2E - Base-2 logarithm of e (log₂(e) ≈ 1.4426950408889634)
  • LOG10E - Base-10 logarithm of e (log₁₀(e) ≈ 0.4342944819032518)
  • SQRT2 - Square root of 2 (√2 ≈ 1.4142135623730951)
  • SQRT1_2 - Square root of 0.5 (1/√2 ≈ 0.7071067811865476)
  • MAX_VALUE - Maximum finite value representable as a number
  • MIN_VALUE - Smallest positive non-zero value representable as a number

Returns the length of a string.

Example:

use { length } from @sentrie/js as str
let len = str.length("hello") // 5

Returns a string created from the specified sequence of UTF-16 code units.

Example:

use { fromCharCode } from @sentrie/js as str
let str = str.fromCharCode(72, 101, 108, 108, 111) // "Hello"

Determines whether a value is NaN (Not-a-Number).

Example:

use { isNaN } from @sentrie/js as num
let result = num.isNaN(NaN) // true

parseInt(str: string, radix?: number): number

Section titled “parseInt(str: string, radix?: number): number”

Parses a string and returns an integer.

Example:

use { parseInt } from @sentrie/js as num
let value = num.parseInt("123") // 123

Parses a string and returns a floating point number.

Example:

use { parseFloat } from @sentrie/js as num
let value = num.parseFloat("123.45") // 123.45

Determines whether a value is a finite number.

Example:

use { isFinite } from @sentrie/js as num
let result = num.isFinite(123) // true

Determines whether a value is an integer.

Example:

use { isInteger } from @sentrie/js as num
let result = num.isInteger(123) // true

Returns the number of milliseconds elapsed since January 1, 1970 UTC.

Example:

use { now } from @sentrie/js as date
let timestamp = date.now() // 1234567890123

Parses a date string and returns the number of milliseconds since January 1, 1970 UTC.

Example:

use { dateParse } from @sentrie/js as date
let timestamp = date.dateParse("2023-01-01") // 1672531200000

UTC(year: number, month: number, ...): number

Section titled “UTC(year: number, month: number, ...): number”

Returns the number of milliseconds in a Date object since January 1, 1970 UTC.

Example:

use { UTC } from @sentrie/js as date
let timestamp = date.UTC(2023, 0, 1) // 1672531200000

Parses a JSON string and returns the corresponding value.

Example:

use { parse } from @sentrie/js as json
let obj = json.parse('{"name": "John", "age": 30}')

Converts a value to a JSON string.

Example:

use { stringify } from @sentrie/js as json
let str = json.stringify({"name": "John", "age": 30}) // '{"name":"John","age":30}'

Determines whether a value is an array.

Example:

use { isArray } from @sentrie/js as arr
let result = arr.isArray([1, 2, 3]) // true

Creates a new array from an array-like or iterable object.

Example:

use { from } from @sentrie/js as arr
let newArray = arr.from("hello") // ["h", "e", "l", "l", "o"]

Creates a new array with the given elements.

Example:

use { of } from @sentrie/js as arr
let newArray = arr.of(1, 2, 3) // [1, 2, 3]
namespace com/example/mypolicy
policy mypolicy {
fact price!: number
fact data!: string
use { round, floor, ceil, max, min } from @sentrie/js as math
use { length, fromCharCode } from @sentrie/js as str
use { parse, stringify } from @sentrie/js as json
use { isValid } from @sentrie/json as jsonUtil
rule processData = default false {
let roundedPrice = math.round(price)
let strLength = str.length(data)
let parsed = json.parse(data)
let isValid = jsonUtil.isValid(data)
let maxPrice = math.max(price, 100)
yield roundedPrice > 0 and strLength > 0 and isValid
}
export decision of processData
}