UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

30 lines (29 loc) 874 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.arrayMachine = void 0; /** * Treats array like memory or tape for state-based operations. * @author @dailker * @param {any[]} array * @param {{op: string, [key: string]: any}[]} instructions * @returns {any[]} */ function arrayMachine(array, instructions) { const arr = array.slice(); for (const ins of instructions) { switch (ins.op) { case 'swap': [arr[ins.i], arr[ins.j]] = [arr[ins.j], arr[ins.i]]; break; case 'add': arr[ins.i] += ins.value; break; case 'set': arr[ins.i] = ins.value; break; // Add more ops as needed } } return arr; } exports.arrayMachine = arrayMachine;