diff --git a/extensions/vercte/dictionaries.js b/extensions/vercte/dictionaries.js index 83cedcd972..f6b03c141b 100644 --- a/extensions/vercte/dictionaries.js +++ b/extensions/vercte/dictionaries.js @@ -2,9 +2,11 @@ // ID: verctedictionaries // Description: Use the power of dictionaries in your project. // By: Vercte -// License: MIT +// By: LSPECTRONIZTAR +// License: MPL-2.0 -(function (Scratch) { +/* generated l10n code */ Scratch.translate(); +/* end generated l10n code */ (function (Scratch) { "use strict"; let dictionaries = new Map(); @@ -29,6 +31,7 @@ { opcode: "dict_stringify", blockType: Scratch.BlockType.REPORTER, + hideFromPalette: true, text: Scratch.translate("stringify dictionary [DICT] into JSON"), arguments: { DICT: { @@ -37,10 +40,27 @@ }, }, }, + { + opcode: "dict_stringify_better", + blockType: Scratch.BlockType.REPORTER, + text: Scratch.translate( + "get dictionary [DICT] as JSON [STRINGIFIED]" + ), + arguments: { + DICT: { + type: Scratch.ArgumentType.STRING, + defaultValue: "foo", + }, + STRINGIFIED: { + type: Scratch.ArgumentType.STRING, + menu: "stringify", + }, + }, + }, { opcode: "dict_parse", blockType: Scratch.BlockType.COMMAND, - text: Scratch.translate("parse JSON [OBJ] into dictionary [DICT]"), + text: Scratch.translate("set dictionary [DICT] to JSON [OBJ]"), arguments: { OBJ: { type: Scratch.ArgumentType.STRING, @@ -132,6 +152,12 @@ }, }, ], + menus: { + stringify: { + acceptReporters: true, + items: ["stringified", "parsed"], + }, + }, }; } @@ -147,13 +173,26 @@ }, {}); }; if (!dictionaries.get(DICT)) return "{}"; - return JSON.stringify(mapToObj(dictionaries.get(DICT))); + const rObj = mapToObj(dictionaries.get(DICT)); + return JSON.stringify(rObj); + } + + dict_stringify_better({ DICT, STRINGIFIED }) { + const mapToObj = (m) => { + return Array.from(m).reduce((obj, [key, value]) => { + obj[key] = value; + return obj; + }, {}); + }; + if (!dictionaries.get(DICT)) return "{}"; + const rObj = mapToObj(dictionaries.get(DICT)); + return STRINGIFIED == "stringified" ? JSON.stringify(rObj) : rObj; } dict_parse({ OBJ, DICT }) { let dict = null; try { - dict = JSON.parse(OBJ); + dict = typeof OBJ === "object" ? OBJ : JSON.parse(OBJ); } catch (e) { dict = { error: String(e) }; }