From 93d151b943872f2c8f14dd51ea6aca55b828fdcb Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 17 Apr 2024 12:37:19 +0200 Subject: [PATCH] anonymous patterns + muting with _ --- packages/core/repl.mjs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/core/repl.mjs b/packages/core/repl.mjs index 3a2e6bd6a..5b7bcd71b 100644 --- a/packages/core/repl.mjs +++ b/packages/core/repl.mjs @@ -54,10 +54,12 @@ export function repl({ const scheduler = sync && typeof SharedWorker != 'undefined' ? new NeoCyclist(schedulerOptions) : new Cyclist(schedulerOptions); let pPatterns = {}; + let anonymousIndex = 0; let allTransform; const hush = function () { pPatterns = {}; + anonymousIndex = 0; allTransform = undefined; return silence; }; @@ -82,6 +84,15 @@ export function repl({ // set pattern methods that use this repl via closure const injectPatternMethods = () => { Pattern.prototype.p = function (id) { + if (id.startsWith('_') || id.endsWith('_')) { + // allows muting a pattern x with x_ or _x + return silence; + } + if (id === '$') { + // allows adding anonymous patterns with $: + id = `$${anonymousIndex}`; + anonymousIndex++; + } pPatterns[id] = this; return this; };