From 94e6ca4ea447a0c347e3c190c153d7a16b1ec266 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 31 Jul 2017 14:43:39 -0700 Subject: [PATCH] [Fix] Map/Set: work around core-js bug < v2.5.0 Fixes #9 --- index.js | 16 +++++++++++++--- package.json | 2 ++ test-core-js.js | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test-core-js.js diff --git a/index.js b/index.js index d86c6e6..47b7631 100644 --- a/index.js +++ b/index.js @@ -41,7 +41,7 @@ module.exports = function inspect_ (obj, opts, depth, seen) { else if (indexOf(seen, obj) >= 0) { return '[Circular]'; } - + function inspect (value, from) { if (from) { seen = seen.slice(); @@ -154,7 +154,12 @@ function isMap (x) { } try { mapSize.call(x); - return true; + try { + setSize.call(x); + } catch (s) { + return true; + } + return x instanceof Map; // core-js workaround, pre-v2.5.0 } catch (e) {} return false; } @@ -165,7 +170,12 @@ function isSet (x) { } try { setSize.call(x); - return true; + try { + mapSize.call(x); + } catch (m) { + return true; + } + return x instanceof Set; // core-js workaround, pre-v2.5.0 } catch (e) {} return false; } diff --git a/package.json b/package.json index b2b5945..aaa1b16 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,12 @@ "description": "string representations of objects in node and the browser", "main": "index.js", "devDependencies": { + "core-js": "^2.4.1", "tape": "^4.7.0" }, "scripts": { "test": "npm run tests-only", + "pretests-only": "node test-core-js", "tests-only": "tape test/*.js" }, "testling": { diff --git a/test-core-js.js b/test-core-js.js new file mode 100644 index 0000000..12c4e2a --- /dev/null +++ b/test-core-js.js @@ -0,0 +1,16 @@ +'use strict'; + +require('core-js'); + +var inspect = require('./'); +var test = require('tape'); + +test('Maps', function (t) { + t.equal(inspect(new Map([[1, 2]])), 'Map (1) {1 => 2}'); + t.end(); +}); + +test('Sets', function (t) { + t.equal(inspect(new Set([[1, 2]])), 'Set (1) {[ 1, 2 ]}'); + t.end(); +});