From 9bc38ac6ff1c2ce63de224686eb286bb3c789544 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 21 Oct 2018 13:33:18 +0100 Subject: [PATCH] Minor refactoring: abstract away from direct usage of .get --- src/skeleton.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/skeleton.js b/src/skeleton.js index 344bf45..a5f0522 100644 --- a/src/skeleton.js +++ b/src/skeleton.js @@ -40,8 +40,22 @@ function Handler(cachedCaptures) { this.callbacks = Immutable.Set(); } +function classOf(v) { + if (v instanceof Struct.Structure) { + return v.meta; + } else if (v instanceof Immutable.List) { + return v.size; + } else { + throw new Error("Assertion contains unsupported data type: " + v.toString()); + } +} + +function step(v, index) { + return v.get(index); +} + function projectPath(v, path) { - path.forEach((index) => { v = v.get(index); return true; }); + path.forEach((index) => { v = step(v, index); return true; }); return v; } @@ -49,14 +63,6 @@ function projectPaths(v, paths) { return paths.map((path) => { return projectPath(v, path) }); } -function classOf(v) { - if (v instanceof Struct.Structure) { - return v.meta; - } else { - return v.size; - } -} - Node.prototype.extend = function(skeleton) { function walkNode(path, node, popCount, index, skeleton) { if (skeleton === null) { @@ -159,7 +165,7 @@ Node.prototype.modify = function(outerValue, m_cont, m_leaf, m_handler) { let i = selector.popCount; while (i--) { mutable.pop(); } }); - let nextValue = nextStack.first().get(selector.index); + let nextValue = step(nextStack.first(), selector.index); let cls = classOf(nextValue); let nextNode = table.get(cls, false); if (nextNode) {