Skip to content

Commit

Permalink
Fix: change for loop to filter/map instead
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderleegs committed Nov 25, 2020
1 parent 2e79c88 commit fb58d14
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
30 changes: 17 additions & 13 deletions classes/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ class Collection {
const { content:navContent, sha:navSha } = await nav.read(NAV_FILE_NAME)
const navContentObject = yaml.safeLoad(base64.decode(navContent))

for (let i = 0; i < navContentObject.links.length; i++) {
if (navContentObject.links[i].collection === collectionName) {
navContentObject.links.splice(i,1)
const newNavContent = base64.encode(yaml.safeDump(navContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)
break
}
const newNavLinks = navContentObject.links.filter(link => link.collection !== collectionName)
const newNavContentObject = {
...navContentObject,
links: newNavLinks,
}
const newNavContent = base64.encode(yaml.safeDump(newNavContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)

// Get all collectionPages
const IsomerFile = new File(this.accessToken, this.siteName)
Expand Down Expand Up @@ -130,17 +129,22 @@ class Collection {
const { content:navContent, sha:navSha } = await nav.read(NAV_FILE_NAME)
const navContentObject = yaml.safeLoad(base64.decode(navContent))

for (let i = 0; i < navContentObject.links.length; i++) {
if (navContentObject.links[i].collection === oldCollectionName) {
navContentObject.links[i] = {
const newNavLinks = navContentObject.links.map(link => {
if (link.collection === oldCollectionName) {
return {
title: deslugifyCollectionName(newCollectionName),
collection: newCollectionName
}
const newNavContent = base64.encode(yaml.safeDump(navContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)
break
} else {
return link
}
})
const newNavContentObject = {
...navContentObject,
links: newNavLinks,
}
const newNavContent = base64.encode(yaml.safeDump(newNavContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)

// Get all collectionPages
const OldIsomerFile = new File(this.accessToken, this.siteName)
Expand Down
33 changes: 18 additions & 15 deletions classes/ResourceRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,22 @@ class ResourceRoom {
const { content:navContent, sha:navSha } = await nav.read(NAV_FILE_NAME)
const navContentObject = yaml.safeLoad(base64.decode(navContent))

for (let i = 0; i < navContentObject.links.length; i++) {
if (navContentObject.links[i].resource_room === true) {
// Assumption: only a single resource room exists
navContentObject.links[i] = {
const newNavLinks = navContentObject.links.map(link => {
if (link.resource_room === true) {
return {
title: deslugifyCollectionName(newResourceRoom),
resource_room: true
}
const newNavContent = base64.encode(yaml.safeDump(navContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)
break
} else {
return link
}
})
const newNavContentObject = {
...navContentObject,
links: newNavLinks,
}
const newNavContent = base64.encode(yaml.safeDump(newNavContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)

// Delete all resources and resourcePages
const IsomerResource = new Resource(this.accessToken, this.siteName)
Expand Down Expand Up @@ -154,15 +158,14 @@ class ResourceRoom {
const { content:navContent, sha:navSha } = await nav.read(NAV_FILE_NAME)
const navContentObject = yaml.safeLoad(base64.decode(navContent))

for (let i = 0; i < navContentObject.links.length; i++) {
if (navContentObject.links[i].resource_room === true) {
// Assumption: only a single resource room exists
navContentObject.links.splice(i,1)
const newNavContent = base64.encode(yaml.safeDump(navContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)
break
}
// Assumption: only a single resource room exists
const newNavLinks = navContentObject.links.filter(link => link.resource_room !== true)
const newNavContentObject = {
...navContentObject,
links: newNavLinks,
}
const newNavContent = base64.encode(yaml.safeDump(newNavContentObject))
await nav.update(NAV_FILE_NAME, newNavContent, navSha)

// Delete all resources and resourcePages
const IsomerResource = new Resource(this.accessToken, this.siteName)
Expand Down

0 comments on commit fb58d14

Please sign in to comment.