Skip to content

Commit af9d893

Browse files
authored
fix(kitsu-core): use typeof instead of constructor comparison for checking if Object (#654)
1 parent a477658 commit af9d893

5 files changed

Lines changed: 10 additions & 5 deletions

File tree

packages/kitsu-core/src/deattribute/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
export function deattribute (data) {
3030
if (typeof data === 'object' && data !== null) {
3131
if (Array.isArray(data)) data.map(el => deattribute(el))
32-
else if (data.attributes?.constructor === Object) {
32+
else if (typeof data.attributes === 'object' && !Array.isArray(data.attributes) && data.attributes !== null) {
3333
for (const key of Object.keys(data.attributes)) {
3434
// Hoist everything but attributes to parent to avoid issues with deleting
3535
// as can't delete data.attributes[key] as it will belete data[key] too

packages/kitsu-core/src/deserialise/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function deserialise (response) {
6161
if (Array.isArray(response.data)) response = deserialiseArray(response)
6262
// Single resource with included relationships
6363
else if (response.included) response.data = linkRelationships(response.data, response.included)
64-
else if (response.data?.constructor === Object) response.data = linkRelationships(response.data)
64+
else if (typeof response.data === 'object' && response.data !== null) response.data = linkRelationships(response.data)
6565

6666
delete response.included
6767

packages/kitsu-core/src/deserialise/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ describe('kitsu-core', () => {
489489
expect(deserialise([])).toEqual([])
490490
})
491491

492+
it('deserialises data correctly if null', () => {
493+
expect.assertions(1)
494+
expect(deserialise({ data: null })).toStrictEqual({ data: null })
495+
})
496+
492497
it('keeps all relationships', () => {
493498
expect.assertions(1)
494499
expect(deserialise({

packages/kitsu-core/src/linkRelationships/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function linkRelationships (data, included = [], previouslyLinked = {}) {
120120
}
121121
}
122122

123-
if (Object.keys(relationships || []).length === 0 && relationships?.constructor === Object) {
123+
if (Object.keys(relationships || []).length === 0 && typeof relationships === 'object' && !Array.isArray(relationships) && relationships !== null) {
124124
delete data.relationships
125125
}
126126

packages/kitsu-core/src/serialise/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function serialiseRelation (node, nodeType, key, data) {
106106
function serialiseAttr (node, key, data) {
107107
if (!data.attributes) data.attributes = {}
108108
if (key === 'links' && (typeof node.self === 'string' || typeof node.related === 'string')) data.links = node
109-
else if (key === 'meta' && node.constructor === Object) data.meta = node
109+
else if (key === 'meta' && typeof node === 'object' && !Array.isArray(node) && node !== null) data.meta = node
110110
else data.attributes[key] = node
111111
return data
112112
}
@@ -170,7 +170,7 @@ function serialiseRootObject (type, payload, method, options) {
170170
const node = payload[key]
171171
const nodeType = options.pluralTypes(options.camelCaseTypes(key))
172172
// 1. Skip null nodes, 2. Only grab objects, 3. Filter to only serialise relationable objects
173-
if (node !== null && node?.constructor === Object && hasID(node)) {
173+
if (typeof node === 'object' && !Array.isArray(node) && node !== null && hasID(node)) {
174174
data = serialiseRelation(node, nodeType, key, data)
175175
// 1. Don't place id/key inside attributes object
176176
} else if (key !== 'id' && key !== 'type') {

0 commit comments

Comments
 (0)