Skip to content

Commit

Permalink
fix(kitsu-core): use typeof instead of constructor comparison for che…
Browse files Browse the repository at this point in the history
…cking if Object (#654)
  • Loading branch information
wopian authored Mar 4, 2022
1 parent a477658 commit af9d893
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/kitsu-core/src/deattribute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
export function deattribute (data) {
if (typeof data === 'object' && data !== null) {
if (Array.isArray(data)) data.map(el => deattribute(el))
else if (data.attributes?.constructor === Object) {
else if (typeof data.attributes === 'object' && !Array.isArray(data.attributes) && data.attributes !== null) {
for (const key of Object.keys(data.attributes)) {
// Hoist everything but attributes to parent to avoid issues with deleting
// as can't delete data.attributes[key] as it will belete data[key] too
Expand Down
2 changes: 1 addition & 1 deletion packages/kitsu-core/src/deserialise/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function deserialise (response) {
if (Array.isArray(response.data)) response = deserialiseArray(response)
// Single resource with included relationships
else if (response.included) response.data = linkRelationships(response.data, response.included)
else if (response.data?.constructor === Object) response.data = linkRelationships(response.data)
else if (typeof response.data === 'object' && response.data !== null) response.data = linkRelationships(response.data)

delete response.included

Expand Down
5 changes: 5 additions & 0 deletions packages/kitsu-core/src/deserialise/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,11 @@ describe('kitsu-core', () => {
expect(deserialise([])).toEqual([])
})

it('deserialises data correctly if null', () => {
expect.assertions(1)
expect(deserialise({ data: null })).toStrictEqual({ data: null })
})

it('keeps all relationships', () => {
expect.assertions(1)
expect(deserialise({
Expand Down
2 changes: 1 addition & 1 deletion packages/kitsu-core/src/linkRelationships/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function linkRelationships (data, included = [], previouslyLinked = {}) {
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions packages/kitsu-core/src/serialise/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function serialiseRelation (node, nodeType, key, data) {
function serialiseAttr (node, key, data) {
if (!data.attributes) data.attributes = {}
if (key === 'links' && (typeof node.self === 'string' || typeof node.related === 'string')) data.links = node
else if (key === 'meta' && node.constructor === Object) data.meta = node
else if (key === 'meta' && typeof node === 'object' && !Array.isArray(node) && node !== null) data.meta = node
else data.attributes[key] = node
return data
}
Expand Down Expand Up @@ -170,7 +170,7 @@ function serialiseRootObject (type, payload, method, options) {
const node = payload[key]
const nodeType = options.pluralTypes(options.camelCaseTypes(key))
// 1. Skip null nodes, 2. Only grab objects, 3. Filter to only serialise relationable objects
if (node !== null && node?.constructor === Object && hasID(node)) {
if (typeof node === 'object' && !Array.isArray(node) && node !== null && hasID(node)) {
data = serialiseRelation(node, nodeType, key, data)
// 1. Don't place id/key inside attributes object
} else if (key !== 'id' && key !== 'type') {
Expand Down

0 comments on commit af9d893

Please sign in to comment.