From ad3a29729658528b14c6dc42c260b52b8c76d990 Mon Sep 17 00:00:00 2001 From: markwhitfeld Date: Thu, 27 Apr 2023 13:09:33 +0200 Subject: [PATCH] refactor: extract function for key check --- src/parser.coffee | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/parser.coffee b/src/parser.coffee index 2aeb4ab6..36d9ac8c 100644 --- a/src/parser.coffee +++ b/src/parser.coffee @@ -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 @@ -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 @@ -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