Optional standard-library plugins for TraSH. This package does not include the parser or virtual machine.
npm install @tardo/trash @tardo/trash-stdlibImport only the registrars your program needs, then install them in TraSH's virtual machine:
import {Interpreter, VMachine} from '@tardo/trash';
import {registerMath, registerStr} from '@tardo/trash-stdlib';
const interpreter = new Interpreter();
const vmachine = new VMachine({processCommandJob: async () => null});
vmachine.use(registerMath);
vmachine.use(registerStr);
const program = interpreter.parse("str_upper (fixed 3.14 1)", {
registeredCmds: vmachine.getRegisteredCmds(),
});
await vmachine.execute(program); // '3.1'| Register | Functions |
|---|---|
registerArr |
arr_clone, arr_append, arr_prepend, arr_join, arr_map, arr_filter, arr_reduce, arr_slice, arr_includes, arr_reverse, arr_unique |
registerDict |
dict_keys, dict_values, dict_entries, dict_has, dict_get, dict_set, dict_remove, dict_merge, dict_clone, dict_size |
registerStr |
str_split, str_upper, str_lower, str_trim, str_replace, str_slice, str_includes, str_starts, str_ends |
registerMath |
floor, ceil, round, trunc, fixed, rand, abs, pow |
registerEnde |
encode, decode |
registerTime |
sleep, pnow |
registerNet |
fetch |
fetch returns the native Fetch Response object.
arr_slice [10, 20, 30, 40] 1 3 // [20, 30]
arr_slice [10, 20, 30] -2 // [20, 30]
arr_includes [10, 20, 30] 20 // true
arr_reverse [10, 20, 30] // [30, 20, 10]
arr_unique [20, 10, 20] // [20, 10]
ceil -1.8 // -1
round -1.5 // -1
trunc -1.8 // -1
arr_slice -a/--arr -b/--begin [-e/--end]excludes the end index; omitting it takes the rest of the array. Negative indices count from the end.arr_includes -a/--arr -v/--valueandarr_unique -a/--arruse JavaScript's SameValueZero equality: no type coercion,NaNequalsNaN, and objects compare by identity rather than contents.arr_uniquepreserves first-occurrence order.arr_slice,arr_reverse -a/--arr, andarr_uniquereturn new shallow arrays without modifying the input.ceil,round, andtruncaccept-n/--num. They round toward positive infinity, to the nearest integer (ties toward positive infinity), and toward zero, respectively, following JavaScript'sMathfunctions.