From ece3c11ec349552cfde981301a20da7797e046e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Sun, 21 Aug 2022 06:56:15 +0100 Subject: [PATCH] CustomCategory: move parsing category to a separate function --- src/customCategory.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/customCategory.js b/src/customCategory.js index d6bb6f43..238ca65d 100644 --- a/src/customCategory.js +++ b/src/customCategory.js @@ -30,34 +30,42 @@ class CustomCategoryRepository { getCategory (id, options, callback) { if (id in this.cache) { - return callback(null, yaml.load(this.cache[id]), this.cache[id]) + const data = this.parseCategory(id, this.cache[id]) + return callback(null, data, this.cache[id]) } fetch('customCategory.php?id=' + id) .then(res => res.text()) .then(content => { - let data this.cache[id] = content - try { - data = yaml.load(content) - } - catch (e) { - return global.alert(e) - } - - if (data && typeof data !== 'object') { - callback(new Error('Data can not be parsed into an object')) - } - - if (!data.name) { - data.name = 'Custom ' + id.substr(0, 6) - } + const data = this.parseCategory(id, content) callback(null, data, content) }) } + parseCategory (id, content) { + let data + + try { + data = yaml.load(content) + } + catch (e) { + return global.alert(e) + } + + if (data && typeof data !== 'object') { + callback(new Error('Data can not be parsed into an object')) + } + + if (!data.name) { + data.name = 'Custom ' + id.substr(0, 6) + } + + return data + } + saveCategory (body, options, callback) { const id = md5(body) this.cache[id] = body