From cc5cba684b6b16e16e9f1df841d2d642e597d6d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Hillerstro=CC=88m?= Date: Wed, 21 Nov 2018 17:42:55 +0200 Subject: [PATCH] Fix inserting falsy values as data --- lib/treap.js | 2 +- test/tests.js | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/treap.js b/lib/treap.js index 1364ae8..fee017b 100644 --- a/lib/treap.js +++ b/lib/treap.js @@ -18,7 +18,7 @@ var treap = (typeof exports === 'undefined') ? {} : exports; // Create the node and randomly assign it a priority var node = {key: key, priority: Math.random(), size: 1}; - if (data) node.data = data; + if (data !== undefined) node.data = data; // Perform the insertion of the node as for a normal BST var parentNode = this._search(key); diff --git a/test/tests.js b/test/tests.js index 23f1914..e759e0a 100644 --- a/test/tests.js +++ b/test/tests.js @@ -25,7 +25,6 @@ describe('performing insertions', function() { }); }); - describe('performing lookups', function() { var t = treap.create(); @@ -120,3 +119,30 @@ describe('splitting and merging', function() { mergedTreap.find(50).key.should.equal(50) }); }); + +describe('data', function() { + var t = treap.create(); + + it('inserts falsy data values', function() { + // https://developer.mozilla.org/en-US/docs/Glossary/Falsy + var falsyValues = [ + false, + null, + undefined, // ok to include + 0, + NaN, + '', + "", + ``, + ]; + var found; + falsyValues.map(function(value, key) { + t.insert(key, value) + }); + + found = t.findRange(0, falsyValues.length); + found.map(function(node) { + return node.data; + }).should.eql(falsyValues); + }); +})