From bc6502951ed26e8fe448379078190fe3f922be2a Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 28 Jul 2021 20:43:34 +0200 Subject: [PATCH] Add promise support --- src/motdparsernode.js | 106 +++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 49 deletions(-) diff --git a/src/motdparsernode.js b/src/motdparsernode.js index 3440f16..9c37957 100644 --- a/src/motdparsernode.js +++ b/src/motdparsernode.js @@ -65,65 +65,73 @@ function parseJsonToHTML(jsonPart) { } function jsonToHtml(json, callback) { - json = JSON.parse(JSON.stringify(json).split('\\n').join("
")); - let motd = parseJsonToHTML(json); - motd = "
" + motd + "
"; - callback(null, motd); + const promise = new Promise((resolve, _reject) => { + json = JSON.parse(JSON.stringify(json).split('\\n').join("
")); + let motd = parseJsonToHTML(json); + motd = "
" + motd + "
"; + resolve(motd) + }) + + if (callback && typeof callback === 'function') { + promise.then(callback.bind(null, null), callback); + } + return promise; } function textToJson(text, callback) { - let jsonObj = { text: "", extra: [] }; - let curObj = jsonObj; - let arr = text.split(""); - for (let i = 0; i < arr.length; i++) { - if (arr[i] != '§') { - curObj.text += arr[i]; - } else if (arr[i + 1] == 'r') { - let innerObj = { text: "", extra: [] }; - jsonObj.extra.push(innerObj); - curObj = innerObj; - i++; - } else { - let codeStr = '§' + arr[i + 1]; - let innerObj = { text: "", extra: [] }; - if (colors.hasOwnProperty(codeStr)) { - innerObj.color = colors[codeStr]; - } - if (extras.hasOwnProperty(codeStr)) { - innerObj[extras[codeStr]] = true; + const promise = new Promise((resolve, _reject) => { + let jsonObj = { text: "", extra: [] }; + let curObj = jsonObj; + let arr = text.split(""); + for (let i = 0; i < arr.length; i++) { + if (arr[i] != '§') { + curObj.text += arr[i]; + } else if (arr[i + 1] == 'r') { + let innerObj = { text: "", extra: [] }; + jsonObj.extra.push(innerObj); + curObj = innerObj; + i++; + } else { + let codeStr = '§' + arr[i + 1]; + let innerObj = { text: "", extra: [] }; + if (colors.hasOwnProperty(codeStr)) { + innerObj.color = colors[codeStr]; + } + if (extras.hasOwnProperty(codeStr)) { + innerObj[extras[codeStr]] = true; + } + curObj.extra.push(innerObj); + curObj = innerObj; + i++; } - curObj.extra.push(innerObj); - curObj = innerObj; - i++; } + resolve(jsonObj); + }) + + if (callback && typeof callback === 'function') { + promise.then(callback.bind(null, null), callback); } - callback(null, jsonObj); + return promise; } function toHtml(motd, callback) { - if (typeof motd === 'object') { - jsonToHtml(motd, (err, res) => { - if (err) { - callback(err); - return; - } - callback(null, res); - }); - } else if (typeof motd === 'string') { - textToJson(motd, (err, json) => { - if (err) { - callback(err); - return; - } - jsonToHtml(json, (err, res) => { - if (err) { - callback(err); - return; - } - callback(null, res); - }); - }); + const promise = new Promise((resolve, reject) => { + if (typeof motd === 'object') { + return jsonToHtml(motd) + .then(resolve) + .catch(reject) + } else if (typeof motd === 'string') { + return textToJson(motd) + .then(jsonToHtml) + .then(resolve) + .catch(reject) + } + }) + + if (callback && typeof callback === 'function') { + promise.then(callback.bind(null, null), callback); } + return promise; } //var text = "§aHypixel Network §7§c1.8/1.9/1.10/1.11/1.12 §e§lNEW PTL GAME:§b§l THE BRIDGE";