← 输入框|github.com|刷新
Skip to content

About

Optional standard-library plugins for TraSH

Topics

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

TraSH standard library

Optional standard-library plugins for TraSH. This package does not include the parser or virtual machine.

Installation

npm install @tardo/trash @tardo/trash-stdlib

Usage

Import 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.

Array and rounding helpers

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/--value and arr_unique -a/--arr use JavaScript's SameValueZero equality: no type coercion, NaN equals NaN, and objects compare by identity rather than contents. arr_unique preserves first-occurrence order.
  • arr_slice, arr_reverse -a/--arr, and arr_unique return new shallow arrays without modifying the input.
  • ceil, round, and trunc accept -n/--num. They round toward positive infinity, to the nearest integer (ties toward positive infinity), and toward zero, respectively, following JavaScript's Math functions.

License

MIT

About

Optional standard-library plugins for TraSH

Topics

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages