Skip to content

Commit 568eff5

Browse files
committed
refactor(kitsu-core): cleanup linkRelationships + use optional chaining
1 parent 43d5d4b commit 568eff5

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

  • packages/kitsu-core/src/linkRelationships

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { filterIncludes } from '../filterIncludes'
1111
* @param {Object} included The response included object
1212
* @private
1313
*/
14-
function link ({ id, type, meta, links }, included) {
14+
function link ({ id, type, meta }, included) {
1515
const filtered = filterIncludes(included, { id, type })
1616
if (filtered.relationships) linkRelationships(filtered, included)
1717
if (meta) filtered.meta = meta
@@ -56,11 +56,10 @@ function linkObject (data, included, key) {
5656
* Helper function for relationships with no data
5757
*
5858
* @param {Object} data The response data object
59-
* @param {Object} included The response included object
6059
* @param {string} key Name of the relationship item
6160
* @private
6261
*/
63-
function linkAttr (data, included, key) {
62+
function linkAttr (data, key) {
6463
data[key] = {}
6564
if (data.relationships[key].links) data[key].links = data.relationships[key].links
6665
delete data.relationships[key]
@@ -100,17 +99,17 @@ export function linkRelationships (data, included = []) {
10099

101100
for (const key in relationships) {
102101
// Relationship contains collection of resources
103-
if (relationships[key].data && relationships[key].data.constructor === Array) {
102+
if (Array.isArray(relationships[key]?.data)) {
104103
linkArray(data, included, key)
105104
// Relationship contains a single resource
106105
} else if (relationships[key].data) {
107106
linkObject(data, included, key)
108107
} else {
109-
linkAttr(data, included, key)
108+
linkAttr(data, key)
110109
}
111110
}
112111

113-
if (relationships && Object.keys(relationships).length === 0 && relationships.constructor === Object) {
112+
if (Object.keys(relationships)?.length === 0 && relationships?.constructor === Object) {
114113
delete data.relationships
115114
}
116115

0 commit comments

Comments
 (0)