Skip to content

Commit

Permalink
[Fix] Map/Set: work around core-js bug < v2.5.0
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
ljharb committed Jul 31, 2017
1 parent 7345a0a commit 94e6ca4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
16 changes: 16 additions & 0 deletions test-core-js.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 94e6ca4

Please sign in to comment.