Reference
Function scripting reference
2 min readOpen in Neblex
Functions and script values in rules run in a sandbox with a deliberately small language. This chapter lists what scripts can use. For how functions run and what they return, see Functions.
Language rules
- Declare variables with
var.letandconstare refused. - Write functions as expressions:
var double = function (x) { return x * 2; };. Function declarations and arrow functions are refused. - Loop with classic
for (var i = 0; i < list.length; i++)or with array methods;for...ofandfor...inare refused. - Join text with
+; template literals are refused. - Assign the outcome to
result. Usereturnonly inside function expressions, never at the top level.
What a script can use
| Kind | Available |
|---|---|
| Array methods | map, filter, reduce, forEach, sort, find, findIndex, some, every, flat, flatMap, push, pop, shift, unshift, join, reverse, splice, indexOf, lastIndexOf, includes, concat, slice |
| Text methods | toLowerCase, toUpperCase, trim, trimStart, trimEnd, charAt, charCodeAt, substring, split, startsWith, endsWith, replace, padStart, padEnd, repeat |
| Numbers | toFixed, toString, Number, parseInt, parseFloat |
| Math | Math.abs, ceil, floor, round, max, min, pow, sqrt, log, exp, sin, cos, tan, atan2, random |
| Objects and data | Object.keys, Object.values, Object.entries, JSON.parse, JSON.stringify, Array.isArray, String, Boolean |
| Neblex helpers | dql(query), apiCall(name, params), log(...) |
What a script cannot use
No dates or clock (new Date(), Date.now()): pass a date in through an attribute instead. No async, await or promises, no constructors other than Number, String and Boolean, no this, and no fetch, timers, eval, require or import. Outside systems are reached only through apiCall().
Inputs and outputs
- Read inputs from
attributes.Name, orattributes['Name with spaces']. dql()returns an array of rows; read a value withrows[0]['table.column']. Put attribute values into the query as${attributes.Name}(or${attributes['Name with spaces']}inside a double-quoted string), never by joining text.- Test values are plain attribute names:
{ "Flow": 40, "Head": 32 }, without anattributeswrapper.
var rows = dql('SELECT c_price FROM pump_frames WHERE c_frame = ${attributes.Frame}');
var price = rows.length ? Number(rows[0]['pump_frames.c_price']) : 0;
result = {
set: { 'attributes.FramePrice': price },
prices: price ? [{ value: price, method: 'delta', name: 'Frame' }] : []
};Effect shapes
| Effect | Shape |
|---|---|
| Set values | { set: { 'attributes.Weight': 42 } } |
| Recommend | { recommend: { 'attributes.Cooling': 'Fan' } } |
| Hide and require | { hidden: ['attributes.Ramp'], required: ['attributes.Axle'] } |
| Hide sections | { hiddenSections: ['Compliance'] } |
| Messages and violations | { messages: [{ message: '...' }], violations: [{ message: '...' }] } |
| Prices | { prices: [{ value: 45, method: 'delta', name: 'Caster upgrade' }] } |
| Bill of materials | { bom: [{ partNumber: 'TR-RAMP', quantity: 1 }] } |
| Lead time | { leadTimeDays: 5 } adds days |
| Values for other functions | { derived: { 'derived.shippingClass': 'Heavy' } } |