UNPKG

deleight

Version:

A library with 9 modules for writing more expressive web applications with traditional HTML, CSS and JavaScript.

46 lines (45 loc) 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rev = exports.reversible = void 0; /** * Returns a function that will call * the reverse action automatically with the last argument called * with it before calling the action with the current argument. * * Suitable for things like tabs and selections... * * @example * import { rev } from 'deleight/function' * const hide = (dialog: HTMLDialogElement) => dialog.style.display = 'none'; * const show = rev( * (dialog: HTMLDialogElement) => dialog.style.display = null, * hide * ); * show(document.querySelector('#dialog1')); * show(document.querySelector('#dialog2')); // dialog 1 is hidden automatically * show(document.querySelector('#dialog3')); // dialog 2 is hidden automatically * * @param action * @param reverseAction */ function reversible(action, reverseAction, lateReverse) { let lastArg; return function (arg) { if (lastArg === arg) { reverseAction.call(this, lastArg); lastArg = null; } else { if (lastArg) reverseAction.call(this, lastArg); const result = action.call(this, arg); lastArg = arg; return result; } }; } exports.reversible = reversible; /** * Alias for {@link reversible} */ exports.rev = reversible;