@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 mathuse { length, fromCharCode } from @sentrie/js as struse { parse, stringify } from @sentrie/js as jsonMath Functions
Section titled “Math Functions”Basic Operations
Section titled “Basic Operations”round(x: number): number
Section titled “round(x: number): number”Returns the value of a number rounded to the nearest integer.
Example:
use { round } from @sentrie/js as mathlet value = math.round(4.5) // 5floor(x: number): number
Section titled “floor(x: number): number”Returns the largest integer less than or equal to a number.
Example:
use { floor } from @sentrie/js as mathlet value = math.floor(4.7) // 4ceil(x: number): number
Section titled “ceil(x: number): number”Returns the smallest integer greater than or equal to a number.
Example:
use { ceil } from @sentrie/js as mathlet value = math.ceil(4.3) // 5max(...values: number[]): number
Section titled “max(...values: number[]): number”Returns the maximum value from a list of numbers.
Example:
use { max } from @sentrie/js as mathlet value = math.max(1, 5, 3, 9, 2) // 9min(...values: number[]): number
Section titled “min(...values: number[]): number”Returns the minimum value from a list of numbers.
Example:
use { min } from @sentrie/js as mathlet value = math.min(1, 5, 3, 9, 2) // 1abs(x: number): number
Section titled “abs(x: number): number”Returns the absolute value of a number.
Example:
use { abs } from @sentrie/js as mathlet value = math.abs(-5) // 5Exponential and Logarithmic Functions
Section titled “Exponential and Logarithmic Functions”sqrt(x: number): number
Section titled “sqrt(x: number): number”Returns the square root of a number.
Example:
use { sqrt } from @sentrie/js as mathlet value = math.sqrt(16) // 4pow(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 mathlet value = math.pow(2, 3) // 8exp(x: number): number
Section titled “exp(x: number): number”Returns e raised to the power of x (eˣ).
Example:
use { exp } from @sentrie/js as mathlet value = math.exp(1) // ≈ 2.718log(x: number): number
Section titled “log(x: number): number”Returns the natural logarithm (base e) of a number.
Example:
use { log, E } from @sentrie/js as mathlet value = math.log(math.E) // 1log10(x: number): number
Section titled “log10(x: number): number”Returns the base-10 logarithm of a number.
Example:
use { log10 } from @sentrie/js as mathlet value = math.log10(100) // 2log2(x: number): number
Section titled “log2(x: number): number”Returns the base-2 logarithm of a number.
Example:
use { log2 } from @sentrie/js as mathlet value = math.log2(8) // 3Trigonometric Functions
Section titled “Trigonometric Functions”All trigonometric functions work with angles in radians.
sin(x: number): number
Section titled “sin(x: number): number”Returns the sine of an angle in radians.
Example:
use { sin, PI } from @sentrie/js as mathlet value = math.sin(math.PI / 2) // 1cos(x: number): number
Section titled “cos(x: number): number”Returns the cosine of an angle in radians.
Example:
use { cos } from @sentrie/js as mathlet value = math.cos(0) // 1tan(x: number): number
Section titled “tan(x: number): number”Returns the tangent of an angle in radians.
Example:
use { tan, PI } from @sentrie/js as mathlet value = math.tan(math.PI / 4) // ≈ 1asin(x: number): number
Section titled “asin(x: number): number”Returns the arcsine (inverse sine) of a number in radians.
Example:
use { asin } from @sentrie/js as mathlet value = math.asin(1) // ≈ 1.5708 (π/2)acos(x: number): number
Section titled “acos(x: number): number”Returns the arccosine (inverse cosine) of a number in radians.
Example:
use { acos } from @sentrie/js as mathlet value = math.acos(1) // 0atan(x: number): number
Section titled “atan(x: number): number”Returns the arctangent (inverse tangent) of a number in radians.
Example:
use { atan } from @sentrie/js as mathlet value = math.atan(1) // ≈ 0.7854 (π/4)atan2(y: number, x: number): number
Section titled “atan2(y: number, x: number): number”Returns the arctangent of the quotient of its arguments.
Example:
use { atan2, PI } from @sentrie/js as mathlet value = math.atan2(1, 0) // ≈ 1.5708 (π/2)Hyperbolic Functions
Section titled “Hyperbolic Functions”sinh(x: number): number
Section titled “sinh(x: number): number”Returns the hyperbolic sine of a number.
cosh(x: number): number
Section titled “cosh(x: number): number”Returns the hyperbolic cosine of a number.
tanh(x: number): number
Section titled “tanh(x: number): number”Returns the hyperbolic tangent of a number.
Random Number Generation
Section titled “Random Number Generation”random(): number
Section titled “random(): number”Returns a random number between 0 (inclusive) and 1 (exclusive).
Example:
use { random } from @sentrie/js as mathlet value = math.random() // 0.0 to 1.0Math Constants
Section titled “Math Constants”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 numberMIN_VALUE- Smallest positive non-zero value representable as a number
String Functions
Section titled “String Functions”length(str: string): number
Section titled “length(str: string): number”Returns the length of a string.
Example:
use { length } from @sentrie/js as strlet len = str.length("hello") // 5fromCharCode(...codes: number[]): string
Section titled “fromCharCode(...codes: number[]): string”Returns a string created from the specified sequence of UTF-16 code units.
Example:
use { fromCharCode } from @sentrie/js as strlet str = str.fromCharCode(72, 101, 108, 108, 111) // "Hello"Number Functions
Section titled “Number Functions”isNaN(value: any): boolean
Section titled “isNaN(value: any): boolean”Determines whether a value is NaN (Not-a-Number).
Example:
use { isNaN } from @sentrie/js as numlet result = num.isNaN(NaN) // trueparseInt(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 numlet value = num.parseInt("123") // 123parseFloat(str: string): number
Section titled “parseFloat(str: string): number”Parses a string and returns a floating point number.
Example:
use { parseFloat } from @sentrie/js as numlet value = num.parseFloat("123.45") // 123.45isFinite(value: any): boolean
Section titled “isFinite(value: any): boolean”Determines whether a value is a finite number.
Example:
use { isFinite } from @sentrie/js as numlet result = num.isFinite(123) // trueisInteger(value: any): boolean
Section titled “isInteger(value: any): boolean”Determines whether a value is an integer.
Example:
use { isInteger } from @sentrie/js as numlet result = num.isInteger(123) // trueDate Functions
Section titled “Date Functions”now(): number
Section titled “now(): number”Returns the number of milliseconds elapsed since January 1, 1970 UTC.
Example:
use { now } from @sentrie/js as datelet timestamp = date.now() // 1234567890123dateParse(dateString: string): number
Section titled “dateParse(dateString: string): number”Parses a date string and returns the number of milliseconds since January 1, 1970 UTC.
Example:
use { dateParse } from @sentrie/js as datelet timestamp = date.dateParse("2023-01-01") // 1672531200000UTC(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 datelet timestamp = date.UTC(2023, 0, 1) // 1672531200000JSON Functions
Section titled “JSON Functions”parse(text: string): any
Section titled “parse(text: string): any”Parses a JSON string and returns the corresponding value.
Example:
use { parse } from @sentrie/js as jsonlet obj = json.parse('{"name": "John", "age": 30}')stringify(value: any): string
Section titled “stringify(value: any): string”Converts a value to a JSON string.
Example:
use { stringify } from @sentrie/js as jsonlet str = json.stringify({"name": "John", "age": 30}) // '{"name":"John","age":30}'Array Functions
Section titled “Array Functions”isArray(value: any): boolean
Section titled “isArray(value: any): boolean”Determines whether a value is an array.
Example:
use { isArray } from @sentrie/js as arrlet result = arr.isArray([1, 2, 3]) // truefrom(arrayLike: any): any[]
Section titled “from(arrayLike: any): any[]”Creates a new array from an array-like or iterable object.
Example:
use { from } from @sentrie/js as arrlet newArray = arr.from("hello") // ["h", "e", "l", "l", "o"]of(...elements: any[]): any[]
Section titled “of(...elements: any[]): any[]”Creates a new array with the given elements.
Example:
use { of } from @sentrie/js as arrlet newArray = arr.of(1, 2, 3) // [1, 2, 3]Complete Example
Section titled “Complete Example”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}