Skip to content

Commit

Permalink
refactor: extract function for key check
Browse files Browse the repository at this point in the history
  • Loading branch information
markwhitfeld committed Apr 27, 2023
1 parent d486007 commit ad3a297
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ defaults = require('./defaults').defaults
isEmpty = (thing) ->
return typeof thing is "object" && thing? && Object.keys(thing).length is 0

isValidKey = (key) ->
return key != '__proto__' && key != 'constructor' && key != 'prototype'

processItem = (processors, item, key) ->
item = process(item, key) for process in processors
return item
Expand Down Expand Up @@ -52,8 +55,7 @@ class exports.Parser extends events
@emit err

assignOrPush: (obj, key, newValue) =>
return if key == '__proto__'
return if key == 'constructor'
return if not isValidKey(key)
if key not of obj
if not @options.explicitArray
obj[key] = newValue
Expand Down Expand Up @@ -112,9 +114,10 @@ class exports.Parser extends events
obj[attrkey] = {}
newValue = if @options.attrValueProcessors then processItem(@options.attrValueProcessors, node.attributes[key], key) else node.attributes[key]
processedKey = if @options.attrNameProcessors then processItem(@options.attrNameProcessors, key) else key
if @options.mergeAttrs
@assignOrPush obj, processedKey, newValue
else
if isValidKey(processedKey)
if @options.mergeAttrs
@assignOrPush obj, processedKey, newValue
else
@assignOrPush obj[attrkey], processedKey, newValue

# need a place to store the node name
Expand Down

0 comments on commit ad3a297

Please sign in to comment.