Skip to content

Commit

Permalink
Mutate variable instead of copying to new variable
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Apr 23, 2021
1 parent 653ce38 commit 0d40211
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@ export function persistResumableFields(id: string, options?: PersistOptions): vo
}
}

const fields = resumables.filter(field => shouldResumeField(field)).map(field => [field.id, field.value])
let fields = resumables.filter(field => shouldResumeField(field)).map(field => [field.id, field.value])

if (fields.length) {
try {
const previouslyStoredFieldsJson = sessionStorage.getItem(key)
let allFields: string[][] = fields

if (previouslyStoredFieldsJson !== null) {
const previouslyStoredFields: string[][] = JSON.parse(previouslyStoredFieldsJson)
const fieldsNotReplaced: string[][] = previouslyStoredFields.filter(function (oldField) {
return !allFields.some(field => field[0] === oldField[0])
return !fields.some(field => field[0] === oldField[0])
})
allFields = allFields.concat(fieldsNotReplaced)
fields = fields.concat(fieldsNotReplaced)
}

sessionStorage.setItem(key, JSON.stringify(allFields))
sessionStorage.setItem(key, JSON.stringify(fields))
} catch {
// Ignore browser private mode error.
}
Expand Down

0 comments on commit 0d40211

Please sign in to comment.