Skip to content

Commit 8d83fb9

Browse files
authored
Merge pull request #95 from pi0/fix/null-proto-class
fix: handle custom classes with null proto as pojo
2 parents 13c6521 + 2dfa504 commit 8d83fb9

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

.changeset/sweet-jeans-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"devalue": patch
3+
---
4+
5+
fix: handle custom classes with null proto as pojo

src/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function is_plain_object(thing) {
4141
return (
4242
proto === Object.prototype ||
4343
proto === null ||
44+
Object.getPrototypeOf(proto) === null ||
4445
Object.getOwnPropertyNames(proto).sort().join('\0') === object_proto_names
4546
);
4647
}

test/test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ class Custom {
99
}
1010
}
1111

12+
const NullObject = (() => {
13+
const C = function () {};
14+
C.prototype = Object.create(null);
15+
return C;
16+
})()
17+
1218
const node_version = +process.versions.node.split('.')[0];
1319

1420
const fixtures = {
@@ -320,6 +326,21 @@ const fixtures = {
320326
};
321327
})(Object.create(null)),
322328

329+
((obj) => {
330+
obj.self = obj;
331+
return {
332+
name: 'Object with null prototype class',
333+
value: obj,
334+
js: '(function(a){a.foo="bar";a.self=a;return a}({}))',
335+
json: '[{"foo":1,"self":0},"bar"]',
336+
validate: (value) => {
337+
assert.is(value.foo, "bar")
338+
assert.is(value.self, value);
339+
340+
}
341+
};
342+
})(Object.assign(new NullObject(), { foo: 'bar' })),
343+
323344
((first, second) => {
324345
first.second = second;
325346
second.first = first;

0 commit comments

Comments
 (0)