From 7d1a0c58c24d509a6ca0739964d3f17d60612b07 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sat, 6 Feb 2016 05:47:14 -0500 Subject: [PATCH] Use immutable sets/lists for projection results --- js/src/route.js | 19 ++++++++----------- js/test/test-route.js | 13 ++++++------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/js/src/route.js b/js/src/route.js index 1cca107..2f90b04 100644 --- a/js/src/route.js +++ b/js/src/route.js @@ -902,10 +902,10 @@ function project(m, compiledProjection) { } function trieKeys(m) { - if (is_emptyTrie(m)) return []; + if (is_emptyTrie(m)) return Immutable.Set(); var result = walkSeq(m, function (vss, vsk) { return vss; }); if (result === null) return null; - return result.map(function (vs) { return vs.toArray() }); + return Immutable.Set(result); function walk(m, k) { if (m instanceof $WildcardSequence) return null; @@ -968,16 +968,13 @@ function trieKeys(m) { function trieKeysToObjects(trieKeysResult, compiledProjection) { if (trieKeysResult === null) return null; - var result = []; - for (var i = 0; i < trieKeysResult.length; i++) { - var e = trieKeysResult[i]; + return trieKeysResult.toList().map(function (e) { var d = {}; - for (var j = 0; j < e.length; j++) { - d[compiledProjection.names[j] || ('$' + j)] = e[j]; - } - result.push(d); - } - return result; + e.forEach(function (key, index) { + d[compiledProjection.names[index] || ('$' + index)] = key; + }); + return d; + }); } function projectObjects(m, compiledProjection) { diff --git a/js/test/test-route.js b/js/test/test-route.js index 36cd714..49b1774 100644 --- a/js/test/test-route.js +++ b/js/test/test-route.js @@ -10,8 +10,7 @@ function checkPrettyTrie(m, expected) { } function checkTrieKeys(actual, expected) { - expect((Immutable.Set(actual).map(Immutable.List)) - .equals(Immutable.Set(expected).map(Immutable.List))).to.be(true); + expect(actual.equals(Immutable.Set(expected).map(Immutable.List))).to.be(true); } describe("basic pattern compilation", function () { @@ -282,9 +281,9 @@ describe("trieKeys using multiple-values in projections", function () { }); it("should be convertible into objects with $-indexed fields", function () { - expect(r.trieKeysToObjects(r.trieKeys(M2), proj)) + expect(r.trieKeysToObjects(r.trieKeys(M2), proj).toArray()) .to.eql([{'$0': 3, '$1': 4}, {'$0': 1, '$1': 2}, {'$0': 1, '$1': 3}]); - expect(r.projectObjects(M, proj)) + expect(r.projectObjects(M, proj).toArray()) .to.eql([{'$0': 3, '$1': 4}, {'$0': 1, '$1': 2}, {'$0': 1, '$1': 3}]); }); }); @@ -295,14 +294,14 @@ describe("trieKeys using multiple-values in projections, with names", function ( r.compilePattern(Immutable.Set(['B']), [3, 4])); it("should yield named fields", function () { - expect(r.projectObjects(M, r.compileProjection([r._$("fst"), r._$("snd")]))) + expect(r.projectObjects(M, r.compileProjection([r._$("fst"), r._$("snd")])).toArray()) .to.eql([{'fst': 3, 'snd': 4}, {'fst': 1, 'snd': 2}, {'fst': 1, 'snd': 3}]); }); it("should yield numbered fields where names are missing", function () { - expect(r.projectObjects(M, r.compileProjection([r._$, r._$("snd")]))) + expect(r.projectObjects(M, r.compileProjection([r._$, r._$("snd")])).toArray()) .to.eql([{'$0': 3, 'snd': 4}, {'$0': 1, 'snd': 2}, {'$0': 1, 'snd': 3}]); - expect(r.projectObjects(M, r.compileProjection([r._$("fst"), r._$]))) + expect(r.projectObjects(M, r.compileProjection([r._$("fst"), r._$])).toArray()) .to.eql([{'fst': 3, '$1': 4}, {'fst': 1, '$1': 2}, {'fst': 1, '$1': 3}]); }); });