Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-jeans-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"devalue": patch
---

fix: handle custom classes with null proto as pojo
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function is_plain_object(thing) {
return (
proto === Object.prototype ||
proto === null ||
Object.getPrototypeOf(proto) === null ||
Object.getOwnPropertyNames(proto).sort().join('\0') === object_proto_names
);
}
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class Custom {
}
}

const NullObject = (() => {
const C = function () {};
C.prototype = Object.create(null);
return C;
})()

const node_version = +process.versions.node.split('.')[0];

const fixtures = {
Expand Down Expand Up @@ -320,6 +326,21 @@ const fixtures = {
};
})(Object.create(null)),

((obj) => {
obj.self = obj;
return {
name: 'Object with null prototype class',
value: obj,
js: '(function(a){a.foo="bar";a.self=a;return a}({}))',
json: '[{"foo":1,"self":0},"bar"]',
validate: (value) => {
assert.is(value.foo, "bar")
assert.is(value.self, value);

}
};
})(Object.assign(new NullObject(), { foo: 'bar' })),

((first, second) => {
first.second = second;
second.first = first;
Expand Down