From 3a221c20543a0db0d1fd34c636513f86998d2fc1 Mon Sep 17 00:00:00 2001 From: Alexander Jones Date: Thu, 19 Oct 2023 10:37:01 -0500 Subject: [PATCH] Split BidsSidecar.parseHedStrings for Code Climate reasons --- bids/types.js | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/bids/types.js b/bids/types.js index 0498d8c1..c3ce965d 100644 --- a/bids/types.js +++ b/bids/types.js @@ -252,27 +252,34 @@ export class BidsSidecar extends BidsJsonFile { */ parseHedStrings(hedSchemas) { this.parsedHedData = new Map() - let issues = [] - for (const [key, strings] of this.hedData) { - if (typeof strings === 'string') { - const [parsedString, parsingIssues] = parseHedString(strings, hedSchemas) - this.parsedHedData.set(key, parsedString) - issues = issues.concat(...Object.values(parsingIssues)) - } else if (strings === Object(strings)) { - const keyMap = new Map() - for (const [value, string] of Object.entries(strings)) { - const [parsedString, parsingIssues] = parseHedString(string, hedSchemas) - keyMap.set(value, parsedString) - issues = issues.concat(...Object.values(parsingIssues)) - } - this.parsedHedData.set(key, keyMap) - } else { - issues.push(generateIssue('illegalSidecarHedType', { key: key, file: this.name })) - } + const issues = [] + for (const [key, value] of this.hedData) { + issues.push(...this._parseSidecarKey(key, value, hedSchemas)) + } + return issues + } + + _parseSidecarKey(key, data, hedSchemas) { + if (typeof data === 'string') { + return this._parseHedString(this.parsedHedData, key, data, hedSchemas) + } else if (data !== Object(data)) { + return [generateIssue('illegalSidecarHedType', { key: key, file: this.name })] + } + const issues = [] + const keyMap = new Map() + for (const [value, string] of Object.entries(data)) { + issues.push(...this._parseHedString(keyMap, value, string, hedSchemas)) } + this.parsedHedData.set(key, keyMap) return issues } + _parseHedString(map, key, string, hedSchemas) { + const [parsedString, parsingIssues] = parseHedString(string, hedSchemas) + map.set(key, parsedString) + return Object.values(parsingIssues).flat() + } + /** * The extracted HED strings. * @returns {string[]}