Skip to content

Commit

Permalink
Fix toString as '[Object object]' in a vm (#281)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Dalgleish <[email protected]>
Co-authored-by: Daniel Cousens <[email protected]>
  • Loading branch information
3 people authored Sep 13, 2022
1 parent 2a42583 commit f169072
Show file tree
Hide file tree
Showing 6 changed files with 2,418 additions and 2,196 deletions.
55 changes: 47 additions & 8 deletions benchmarks/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions bind.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
Copyright (c) 2018 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
Copyright (c) 2018 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/
/* global define */

Expand All @@ -24,14 +24,15 @@
} else if (Array.isArray(arg)) {
classes.push(classNames.apply(this, arg));
} else if (argType === 'object') {
if (arg.toString === Object.prototype.toString) {
for (var key in arg) {
if (hasOwn.call(arg, key) && arg[key]) {
classes.push(this && this[key] || key);
}
}
} else {
if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) {
classes.push(arg.toString());
continue;
}

for (var key in arg) {
if (hasOwn.call(arg, key) && arg[key]) {
classes.push(this && this[key] || key);
}
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions dedupe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
Copyright (c) 2018 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
Copyright (c) 2018 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/
/* global define */

Expand Down Expand Up @@ -29,16 +29,17 @@
}

function _parseObject (resultSet, object) {
if (object.toString === Object.prototype.toString) {
for (var k in object) {
if (hasOwn.call(object, k)) {
// set value to false instead of deleting it to avoid changing object structure
// https://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/#de-referencing-misconceptions
resultSet[k] = !!object[k];
}
}
} else {
if (object.toString !== Object.prototype.toString && !object.toString.toString().includes('[native code]')) {
resultSet[object.toString()] = true;
return;
}

for (var k in object) {
if (hasOwn.call(object, k)) {
// set value to false instead of deleting it to avoid changing object structure
// https://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/#de-referencing-misconceptions
resultSet[k] = !!object[k];
}
}
}

Expand Down
22 changes: 12 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*!
Copyright (c) 2018 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
Copyright (c) 2018 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/
/* global define */

(function () {
'use strict';

var hasOwn = {}.hasOwnProperty;
var nativeCodeString = '[native code]';

function classNames() {
var classes = [];
Expand All @@ -29,14 +30,15 @@
}
}
} else if (argType === 'object') {
if (arg.toString === Object.prototype.toString) {
for (var key in arg) {
if (hasOwn.call(arg, key) && arg[key]) {
classes.push(key);
}
}
} else {
if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes('[native code]')) {
classes.push(arg.toString());
continue;
}

for (var key in arg) {
if (hasOwn.call(arg, key) && arg[key]) {
classes.push(key);
}
}
}
}
Expand Down
Loading

0 comments on commit f169072

Please sign in to comment.