From 29bdfb2d90637a4c0ae618efa4b0a9f315263b70 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 25 Feb 2022 11:19:12 +0000 Subject: [PATCH] _withContext() --- strudel.mjs | 4 ++++ test/pattern.test.mjs | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/strudel.mjs b/strudel.mjs index 044b7123..9d90bc57 100644 --- a/strudel.mjs +++ b/strudel.mjs @@ -312,6 +312,10 @@ class Pattern { return this._withEvent(event => event.setContext(context)) } + _withContext(func) { + return this._withEvent(event => event.setContext(func(event.context))) + } + withLocation(location) { return this.fmap(value => { value = typeof value === 'object' && !Array.isArray(value) ? value : { value }; diff --git a/test/pattern.test.mjs b/test/pattern.test.mjs index ae950321..5154ed86 100644 --- a/test/pattern.test.mjs +++ b/test/pattern.test.mjs @@ -353,6 +353,19 @@ describe('Pattern', function() { ) }) }) + describe('_withContext()', () => { + it('Can update the event context', () => { + assert.deepStrictEqual( + pure("a")._setContext([[[0,1],[1,2]]])._withContext(c => [...c,[[3,4],[3,4]]]).firstCycle, + [hap(ts(0,1), + ts(0,1), + "a", + [[[0,1],[1,2]],[[3,4],[3,4]]] + ) + ] + ) + }) + }) describe("apply", () => { it('Can apply a function', () => { assert.deepStrictEqual(