Guide

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. let and const are 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...of and for...in are refused.
  • Join text with +; template literals are refused.
  • Assign the outcome to result. Use return only inside function expressions, never at the top level.

What a script can use

KindAvailable
Array methodsmap, filter, reduce, forEach, sort, find, findIndex, some, every, flat, flatMap, push, pop, shift, unshift, join, reverse, splice, indexOf, lastIndexOf, includes, concat, slice
Text methodstoLowerCase, toUpperCase, trim, trimStart, trimEnd, charAt, charCodeAt, substring, split, startsWith, endsWith, replace, padStart, padEnd, repeat
NumberstoFixed, toString, Number, parseInt, parseFloat
MathMath.abs, ceil, floor, round, max, min, pow, sqrt, log, exp, sin, cos, tan, atan2, random
Objects and dataObject.keys, Object.values, Object.entries, JSON.parse, JSON.stringify, Array.isArray, String, Boolean
Neblex helpersdql(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, or attributes['Name with spaces'].
  • dql() returns an array of rows; read a value with rows[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 an attributes wrapper.
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

EffectShape
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' } }