From 8c51e2049922a726ec0e32278296cb9d219b8a09 Mon Sep 17 00:00:00 2001 From: Skyhigh173 <100467674+Skyhigh173@users.noreply.github.com> Date: Fri, 22 Dec 2023 20:57:36 +0800 Subject: [PATCH 1/7] v2.6 --- extensions/Skyhigh173/json.js | 408 +++++++++++++++++++--------------- 1 file changed, 224 insertions(+), 184 deletions(-) diff --git a/extensions/Skyhigh173/json.js b/extensions/Skyhigh173/json.js index 3b0065c317..a51552be5c 100644 --- a/extensions/Skyhigh173/json.js +++ b/extensions/Skyhigh173/json.js @@ -4,33 +4,32 @@ // By: Skyhigh173 (function (Scratch) { - "use strict"; + 'use strict'; /* - * JSON extension v2.5 by skyhigh173 (English Version) + * JSON extension v2.6 by skyhigh173 (English Version) * Do not remove this comment */ const vm = Scratch.vm; - const hasOwn = (obj, property) => - Object.prototype.hasOwnProperty.call(obj, property); + const hasOwn = (obj, property) => Object.prototype.hasOwnProperty.call(obj, property); const makeLabel = (text) => ({ - blockType: "label", + blockType: 'label', text: text, }); class JSONS { getInfo() { return { - id: "skyhigh173JSON", - name: "JSON", - color1: "#3271D0", + id: 'skyhigh173JSON', + name: 'JSON', + color1: '#3271D0', blocks: [ - makeLabel("General Utils"), + makeLabel('General Utils'), { - opcode: "json_is_valid", + opcode: 'json_is_valid', blockType: Scratch.BlockType.BOOLEAN, - text: "is JSON [json] valid?", + text: 'is JSON [json] valid?', arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -39,9 +38,9 @@ }, }, { - opcode: "json_is", + opcode: 'json_is', blockType: Scratch.BlockType.BOOLEAN, - text: "is [json] [types]?", + text: 'is [json] [types]?', arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -49,20 +48,20 @@ }, types: { type: Scratch.ArgumentType.STRING, - defaultValue: "Object", - menu: "types", + defaultValue: 'Object', + menu: 'types', }, }, }, - "---", + '---', { - opcode: "json_get_all", + opcode: 'json_get_all', blockType: Scratch.BlockType.REPORTER, - text: "all [Stype] of [json]", + text: 'all [Stype] of [json]', arguments: { Stype: { type: Scratch.ArgumentType.STRING, - menu: "get_all", + menu: 'get_all', }, json: { type: Scratch.ArgumentType.STRING, @@ -71,26 +70,26 @@ }, }, { - opcode: "json_new", + opcode: 'json_new', blockType: Scratch.BlockType.REPORTER, - text: "new [json]", + text: 'new [json]', arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: "Object", - menu: "types", + defaultValue: 'Object', + menu: 'types', }, }, }, - "---", + '---', { - opcode: "json_has_key", + opcode: 'json_has_key', blockType: Scratch.BlockType.BOOLEAN, - text: "[json] contains key [key]?", + text: '[json] contains key [key]?', arguments: { key: { type: Scratch.ArgumentType.STRING, - defaultValue: "key2", + defaultValue: 'key2', }, json: { type: Scratch.ArgumentType.STRING, @@ -99,13 +98,13 @@ }, }, { - opcode: "json_has_value", + opcode: 'json_has_value', blockType: Scratch.BlockType.BOOLEAN, - text: "[json] contains value [value]?", + text: '[json] contains value [value]?', arguments: { value: { type: Scratch.ArgumentType.STRING, - defaultValue: "scratch", + defaultValue: 'scratch', }, json: { type: Scratch.ArgumentType.STRING, @@ -114,9 +113,9 @@ }, }, { - opcode: "json_equal", + opcode: 'json_equal', blockType: Scratch.BlockType.BOOLEAN, - text: "[json1] [equal] [json2]", + text: '[json1] [equal] [json2]', arguments: { json1: { type: Scratch.ArgumentType.STRING, @@ -128,16 +127,28 @@ }, equal: { type: Scratch.ArgumentType.STRING, - defaultValue: "=", - menu: "equal", + defaultValue: '=', + menu: 'equal', }, }, }, - makeLabel("JSON Strings"), + '---', { - opcode: "json_jlength", + opcode: 'json_minify', blockType: Scratch.BlockType.REPORTER, - text: "length of json [json]", + text: 'minify JSON [json]', + arguments: { + json: { + type: Scratch.ArgumentType.STRING, + defaultValue: ' {"key" : "value" } ', + }, + }, + }, + makeLabel('JSON Strings'), + { + opcode: 'json_jlength', + blockType: Scratch.BlockType.REPORTER, + text: 'length of json [json]', arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -146,13 +157,13 @@ }, }, { - opcode: "json_get", + opcode: 'json_get', blockType: Scratch.BlockType.REPORTER, - text: "value of [item] in [json]", + text: 'value of [item] in [json]', arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: "key", + defaultValue: 'key', }, json: { type: Scratch.ArgumentType.STRING, @@ -161,17 +172,17 @@ }, }, { - opcode: "json_set", + opcode: 'json_set', blockType: Scratch.BlockType.REPORTER, - text: "set [item] in [json] to [value]", + text: 'set [item] in [json] to [value]', arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: "key", + defaultValue: 'key', }, value: { type: Scratch.ArgumentType.STRING, - defaultValue: "new value", + defaultValue: 'new value', }, json: { type: Scratch.ArgumentType.STRING, @@ -180,13 +191,13 @@ }, }, { - opcode: "json_delete", + opcode: 'json_delete', blockType: Scratch.BlockType.REPORTER, - text: "delete [item] in [json]", + text: 'delete [item] in [json]', arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: "key2", + defaultValue: 'key2', }, json: { type: Scratch.ArgumentType.STRING, @@ -194,22 +205,22 @@ }, }, }, - makeLabel("Array"), + makeLabel('Array'), { - opcode: "json_length", + opcode: 'json_length', blockType: Scratch.BlockType.REPORTER, - text: "length of array [json]", + text: 'length of array [json]', arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: "[1,2,3]", + defaultValue: '[1,2,3]', }, }, }, { - opcode: "json_array_get", + opcode: 'json_array_get', blockType: Scratch.BlockType.REPORTER, - text: "item [item] of array [json]", + text: 'item [item] of array [json]', arguments: { item: { type: Scratch.ArgumentType.NUMBER, @@ -222,13 +233,13 @@ }, }, { - opcode: "json_array_push", + opcode: 'json_array_push', blockType: Scratch.BlockType.REPORTER, - text: "add [item] to array [json]", + text: 'add [item] to array [json]', arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: "TurboWarp", + defaultValue: 'TurboWarp', }, json: { type: Scratch.ArgumentType.STRING, @@ -237,13 +248,13 @@ }, }, { - opcode: "json_array_set", + opcode: 'json_array_set', blockType: Scratch.BlockType.REPORTER, - text: "replace item [pos] of [json] with [item]", + text: 'replace item [pos] of [json] with [item]', arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: "fav", + defaultValue: 'fav', }, pos: { type: Scratch.ArgumentType.NUMBER, @@ -256,13 +267,13 @@ }, }, { - opcode: "json_array_insert", + opcode: 'json_array_insert', blockType: Scratch.BlockType.REPORTER, - text: "insert [item] at [pos] of array [json]", + text: 'insert [item] at [pos] of array [json]', arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: "fav", + defaultValue: 'fav', }, pos: { type: Scratch.ArgumentType.NUMBER, @@ -274,11 +285,11 @@ }, }, }, - "---", + '---', { - opcode: "json_array_delete", + opcode: 'json_array_delete', blockType: Scratch.BlockType.REPORTER, - text: "delete item [item] of array [json]", + text: 'delete item [item] of array [json]', arguments: { item: { type: Scratch.ArgumentType.NUMBER, @@ -291,13 +302,13 @@ }, }, { - opcode: "json_array_remove_all", + opcode: 'json_array_remove_all', blockType: Scratch.BlockType.REPORTER, - text: "delete all [item] in array [json]", + text: 'delete all [item] in array [json]', arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: "a", + defaultValue: 'a', }, json: { type: Scratch.ArgumentType.STRING, @@ -305,15 +316,15 @@ }, }, }, - "---", + '---', { - opcode: "json_array_itemH", + opcode: 'json_array_itemH', blockType: Scratch.BlockType.REPORTER, - text: "item # of [item] in array [json]", + text: 'item # of [item] in array [json]', arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: "scratch", + defaultValue: 'scratch', }, json: { type: Scratch.ArgumentType.STRING, @@ -321,26 +332,26 @@ }, }, }, - makeLabel("Advanced"), + makeLabel('Advanced'), { - opcode: "json_array_from", + opcode: 'json_array_from', blockType: Scratch.BlockType.REPORTER, - text: "array from text [json]", + text: 'array from text [json]', arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: "abcd", + defaultValue: 'abcd', }, }, }, { - opcode: "json_array_fromto", + opcode: 'json_array_fromto', blockType: Scratch.BlockType.REPORTER, - text: "items [item] to [item2] of array [json]", + text: 'items [item] to [item2] of array [json]', arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: "[1,2,3,4]", + defaultValue: '[1,2,3,4]', }, item: { type: Scratch.ArgumentType.NUMBER, @@ -353,9 +364,9 @@ }, }, { - opcode: "json_array_reverse", + opcode: 'json_array_reverse', blockType: Scratch.BlockType.REPORTER, - text: "reverse array [json]", + text: 'reverse array [json]', arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -364,13 +375,13 @@ }, }, { - opcode: "json_array_flat", + opcode: 'json_array_flat', blockType: Scratch.BlockType.REPORTER, - text: "flat array [json] by depth [depth]", + text: 'flat array [json] by depth [depth]', arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: "[[1],2,[3,4],[5,[6]]]", + defaultValue: '[[1],2,[3,4],[5,[6]]]', }, depth: { type: Scratch.ArgumentType.NUMBER, @@ -379,9 +390,9 @@ }, }, { - opcode: "json_array_concat", + opcode: 'json_array_concat', blockType: Scratch.BlockType.REPORTER, - text: "array concat [json] [json2]", + text: 'concatenate array [json] [json2]', arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -394,13 +405,13 @@ }, }, { - opcode: "json_array_filter", + opcode: 'json_array_filter', blockType: Scratch.BlockType.REPORTER, - text: "get all values with key [key] in array [json]", + text: 'get all values with key [key] in array [json]', arguments: { key: { type: Scratch.ArgumentType.STRING, - defaultValue: "id", + defaultValue: 'id', }, json: { type: Scratch.ArgumentType.STRING, @@ -409,9 +420,9 @@ }, }, { - opcode: "json_array_setlen", + opcode: 'json_array_setlen', blockType: Scratch.BlockType.REPORTER, - text: "set length of array [json] to [len]", + text: 'set length of array [json] to [len]', arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -423,26 +434,26 @@ }, }, }, - "---", + '---', { - opcode: "json_array_create", + opcode: 'json_array_create', blockType: Scratch.BlockType.REPORTER, - text: "create array by [text] with delimiter [d]", + text: 'create array by [text] with delimiter [d]', arguments: { text: { type: Scratch.ArgumentType.STRING, - defaultValue: "a,b,c", + defaultValue: 'a,b,c', }, d: { type: Scratch.ArgumentType.STRING, - defaultValue: ",", + defaultValue: ',', }, }, }, { - opcode: "json_array_join", + opcode: 'json_array_join', blockType: Scratch.BlockType.REPORTER, - text: "join string by array [json] with delimiter [d]", + text: 'join string by array [json] with delimiter [d]', arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -450,48 +461,66 @@ }, d: { type: Scratch.ArgumentType.STRING, - defaultValue: ",", + defaultValue: ',', }, }, }, - "---", + '---', { - opcode: "json_array_sort", + opcode: 'json_array_sort', blockType: Scratch.BlockType.REPORTER, - text: "sort array [list] in [order] order", + text: 'sort array [list] in [order] order', disableMonitor: true, arguments: { list: { type: Scratch.ArgumentType.STRING, defaultValue: - "[5.23, 214, 522, 61, 5.24, 62.2, 1, 51212, 0, 0]", + '[5.23, 214, 522, 61, 5.24, 62.2, 1, 51212, 0, 0]', }, order: { type: Scratch.ArgumentType.STRING, - menu: "sort_order", + menu: 'sort_order', }, }, }, - makeLabel("Lists"), + makeLabel('Variables & Lists'), { - opcode: "json_vm_getlist", + opcode: 'json_vm_export_var', blockType: Scratch.BlockType.REPORTER, - text: "get list [list] as array", + text: 'export all variables', + disableMonitor: true, + }, + { + opcode: 'json_vm_import_var', + blockType: Scratch.BlockType.COMMAND, + text: 'import all variables from [json]', + arguments: { + json: { + type: Scratch.ArgumentType.STRING, + defaultValue: '{"my variable": 1}', + }, + }, + }, + '---', + { + opcode: 'json_vm_getlist', + blockType: Scratch.BlockType.REPORTER, + text: 'get list [list] as array', arguments: { list: { type: Scratch.ArgumentType.STRING, - menu: "get_list", + menu: 'get_list', }, }, }, { - opcode: "json_vm_setlist", + opcode: 'json_vm_setlist', blockType: Scratch.BlockType.COMMAND, - text: "set list [list] to [json]", + text: 'set list [list] to [json]', arguments: { list: { type: Scratch.ArgumentType.STRING, - menu: "get_list", + menu: 'get_list', }, json: { type: Scratch.ArgumentType.STRING, @@ -502,22 +531,22 @@ ], menus: { get_all: { - items: ["keys", "values", "datas"], + items: ['keys', 'values', 'datas'], }, get_list: { acceptReporters: true, - items: "getLists", + items: 'getLists', }, types: { acceptReporters: true, - items: ["Object", "Array"], + items: ['Object', 'Array'], }, equal: { acceptReporters: true, - items: ["=", "≠"], + items: ['=', '≠'], }, sort_order: { - items: ["ascending", "descending"], + items: ['ascending', 'descending'], acceptReporters: true, }, }, @@ -527,16 +556,16 @@ getLists() { const globalLists = Object.values( vm.runtime.getTargetForStage().variables - ).filter((x) => x.type == "list"); + ).filter((x) => x.type == 'list'); const localLists = Object.values(vm.editingTarget.variables).filter( - (x) => x.type == "list" + (x) => x.type == 'list' ); const uniqueLists = [...new Set([...globalLists, ...localLists])]; if (uniqueLists.length === 0) { return [ { - text: "select a list", - value: "select a list", + text: 'select a list', + value: 'select a list', }, ]; } @@ -548,10 +577,10 @@ lookupList(list, util) { const byId = util.target.lookupVariableById(list); - if (byId && byId.type === "list") { + if (byId && byId.type === 'list') { return byId; } - const byName = util.target.lookupVariableByNameAndType(list, "list"); + const byName = util.target.lookupVariableByNameAndType(list, 'list'); if (byName) { return byName; } @@ -559,12 +588,7 @@ } json_is_valid({ json }) { - if (typeof json != "string") { - return false; - } else if ( - (json.slice(0, 1) != "[" || json.slice(-1) != "]") && - (json.slice(0, 1) != "{" || json.slice(-1) != "}") - ) { + if (typeof json != 'string') { return false; } else { try { @@ -578,12 +602,7 @@ // return object if its json else string json_valid_return(json) { - if (typeof json != "string") { - return json; - } else if ( - (json.slice(0, 1) != "[" || json.slice(-1) != "]") && - (json.slice(0, 1) != "{" || json.slice(-1) != "}") - ) { + if (typeof json != 'string') { return json; } else { try { @@ -599,9 +618,9 @@ try { json = JSON.parse(json); switch (types) { - case "Object": + case 'Object': return !Array.isArray(json); - case "Array": + case 'Array': return Array.isArray(json); default: return false; @@ -611,23 +630,31 @@ } } + json_minify({ json }) { + try { + return JSON.stringify(JSON.parse(json)); + } catch { + return ''; + } + } + json_length({ json }) { try { json = JSON.parse(json); return Object.keys(json).length; } catch { - return " "; + return ' '; } } json_new({ json }) { switch (json) { - case "Object": - return "{}"; - case "Array": - return "[]"; + case 'Object': + return '{}'; + case 'Array': + return '[]'; default: - return ""; + return ''; } } @@ -662,8 +689,8 @@ const result = keys1.length === keys2.length && Object.keys(json1).every((key) => json1[key] === json2[key]); - if (equal === "=") return result; - if (equal === "≠") return !result; + if (equal === '=') return result; + if (equal === '≠') return !result; } catch { // ignore } @@ -674,19 +701,17 @@ try { json = JSON.parse(json); switch (Stype) { - case "keys": - return JSON.stringify(Object.keys(json).map((key) => key)); - case "values": - return JSON.stringify(Object.keys(json).map((key) => json[key])); - case "datas": - return JSON.stringify( - Object.keys(json).map((key) => [key, json[key]]) - ); + case 'keys': + return JSON.stringify(Object.keys(json)); + case 'values': + return JSON.stringify(Object.values(json)); + case 'datas': + return JSON.stringify(Object.entries(json)); default: - return ""; + return ''; } } catch { - return ""; + return ''; } } @@ -695,7 +720,7 @@ json = JSON.parse(json); if (hasOwn(json, item)) { const result = json[item]; - if (typeof result === "object") { + if (typeof result === 'object') { return JSON.stringify(result); } else { return result; @@ -704,14 +729,14 @@ } catch { // ignore } - return ""; + return ''; } _fixInvalidJSONValues(value) { // JSON does not support these values, so convert to string. - if (Number.isNaN(value)) return "NaN"; - if (value === Infinity) return "Infinity"; - if (value === -Infinity) return "-Infinity"; + if (Number.isNaN(value)) return 'NaN'; + if (value === Infinity) return 'Infinity'; + if (value === -Infinity) return '-Infinity'; return value; } @@ -723,7 +748,7 @@ json[item] = value; return JSON.stringify(json); } catch { - return ""; + return ''; } } @@ -733,7 +758,7 @@ delete json[item]; return JSON.stringify(json); } catch { - return ""; + return ''; } } @@ -746,7 +771,7 @@ // 1...length : array content, -1...-length : reverse array content, 0 : ERROR try { item = Scratch.Cast.toNumber(item); - if (item == 0) return ""; + if (item == 0) return ''; if (item > 0) { item--; } @@ -757,13 +782,13 @@ } else { result = json[json.length + item]; } - if (typeof result == "object") { + if (typeof result == 'object') { return JSON.stringify(result); } else { return result; } } catch { - return ""; + return ''; } } @@ -774,7 +799,7 @@ let result = JSON.stringify(json.indexOf(item) + 1); return result; } catch { - return ""; + return ''; } } @@ -782,7 +807,7 @@ try { return JSON.stringify(Array.from(String(json))); } catch { - return ""; + return ''; } } @@ -792,7 +817,7 @@ json2 = JSON.parse(json2); return JSON.stringify(json.concat(json2)); } catch { - return ""; + return ''; } } @@ -803,7 +828,7 @@ json.push(item); return JSON.stringify(json); } catch { - return ""; + return ''; } } @@ -814,7 +839,7 @@ json.splice(pos - 1, 0, item); return JSON.stringify(json); } catch { - return ""; + return ''; } } @@ -826,7 +851,7 @@ ); return JSON.stringify(json); } catch { - return ""; + return ''; } } @@ -836,7 +861,7 @@ json.splice(item - 1, 1); return JSON.stringify(json); } catch { - return ""; + return ''; } } @@ -854,7 +879,7 @@ } return JSON.stringify(json); } catch { - return ""; + return ''; } } @@ -862,7 +887,7 @@ try { return JSON.stringify(JSON.parse(json).slice(item - 1, item2)); } catch { - return ""; + return ''; } } @@ -870,7 +895,7 @@ try { return JSON.stringify(JSON.parse(json).reverse()); } catch { - return ""; + return ''; } } @@ -878,7 +903,7 @@ try { return JSON.stringify(JSON.parse(json).flat(depth)); } catch { - return ""; + return ''; } } @@ -890,7 +915,7 @@ try { return JSON.parse(json).join(d); } catch { - return ""; + return ''; } } @@ -906,7 +931,7 @@ }) ); } catch (e) { - return ""; + return ''; } } @@ -916,7 +941,7 @@ json.length = len; return JSON.stringify(json); } catch { - return ""; + return ''; } } @@ -929,7 +954,7 @@ } catch (e) { // ignore } - return ""; + return ''; } json_vm_setlist({ list, json }, util) { try { @@ -938,7 +963,7 @@ const array = JSON.parse(json); if (Array.isArray(array)) { const safeArray = array.map((i) => { - if (typeof i === "object") return JSON.stringify(i); + if (typeof i === 'object') return JSON.stringify(i); return i; }); listVariable.value = safeArray; @@ -947,7 +972,7 @@ } catch (e) { // ignore } - return ""; + return ''; } json_array_sort(args) { @@ -955,15 +980,30 @@ try { list = JSON.parse(args.list); } catch { - return ""; + return ''; } if (!Array.isArray(list)) { - return ""; + return ''; } list.sort(Scratch.Cast.compare); - if (args.order === "descending") list.reverse(); + if (args.order === 'descending') list.reverse(); return JSON.stringify(list); } + + json_vm_export_var(_, util) { + const global = Object.fromEntries(Object.values(vm.runtime.getTargetForStage().variables).filter(x => x.type === '').map(x => [x.name, x.value])); + const local = Object.fromEntries(Object.values(util.target.variables).filter(x => x.type === '').map(x => [x.name, x.value])); + return JSON.stringify({...global, ...local}); + } + + json_vm_import_var({ json }, util) { + try { + json = JSON.parse(json); + Object.entries(json).forEach(([k, v]) => { + util.target.lookupVariableByNameAndType(k, '').value = v; + }); + } catch {} + } } Scratch.extensions.register(new JSONS()); })(Scratch); From 364d1ca98fdb303357602681abb926b477f0e116 Mon Sep 17 00:00:00 2001 From: Skyhigh173 <100467674+Skyhigh173@users.noreply.github.com> Date: Mon, 25 Dec 2023 20:56:51 +0800 Subject: [PATCH 2/7] v2.6 - part 2 --- extensions/Skyhigh173/json.js | 78 +++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/extensions/Skyhigh173/json.js b/extensions/Skyhigh173/json.js index a51552be5c..72b08db236 100644 --- a/extensions/Skyhigh173/json.js +++ b/extensions/Skyhigh173/json.js @@ -144,6 +144,17 @@ }, }, }, + { + opcode: 'json_flip', + blockType: Scratch.BlockType.REPORTER, + text: 'flip key-value pair in JSON [json]', + arguments: { + json: { + type: Scratch.ArgumentType.STRING, + defaultValue: '{"key":"value","key2":"value2"}', + } + }, + }, makeLabel('JSON Strings'), { opcode: 'json_jlength', @@ -434,6 +445,31 @@ }, }, }, + { + // requested by sharkpool, I will improve it when theres lambda function + opcode: 'json_array_textfilter', + blockType: Scratch.BlockType.REPORTER, + text: 'filter array [list] if text [options] [text] and return [type]', + arguments: { + list: { + type: Scratch.ArgumentType.STRING, + defaultValue: + '["abc","bac","cab"]', + }, + options: { + type: Scratch.ArgumentType.STRING, + menu: 'filter_options', + }, + text: { + type: Scratch.ArgumentType.STRING, + defaultValue: 'a', + }, + type: { + type: Scratch.ArgumentType.STRING, + menu: 'types', + } + }, + }, '---', { opcode: 'json_array_create', @@ -549,6 +585,9 @@ items: ['ascending', 'descending'], acceptReporters: true, }, + filter_options: { + items: ['includes', 'starts with', 'ends with'] + } }, }; } @@ -679,6 +718,16 @@ } } + json_flip({ json }) { + try { + json = Object.entries(JSON.parse(json)); + json = json.map(x => [x[1], x[0]]); + return JSON.stringify(Object.fromEntries(json)); + } catch { + return ''; + } + } + json_equal({ json1, equal, json2 }) { try { json1 = JSON.parse(json1); @@ -975,6 +1024,35 @@ return ''; } + json_array_textfilter(args) { + try { + const option = args.options; + let list = JSON.parse(args.list); + // check type + const isArray = Array.isArray(list); + // [[0, a], [1, b], [2, c]] + list = Object.entries(list); + const text = Scratch.Cast.toString(args.text); + const out = args.type; + + switch (option) { + case 'includes': list = list.filter(x => typeof x[1] === 'string' && x[1].includes(text)); break; + case 'starts with': list = list.filter(x => typeof x[1] === 'string' && x[1].startsWith(text)); break; + case 'ends with': list = list.filter(x => typeof x[1] === 'string' && x[1].endsWith(text)); break; + default: return ''; // shouldn't happen + } + if (out === 'Object') { + // if array, convert to scratch index + if (isArray) list = list.map(x => [Number(x[0]) + 1, x[1]]); + return JSON.stringify(Object.fromEntries(list)); + } + if (out === 'Array') return JSON.stringify(list.map(x => x[1])); + } catch { + // ignore + } + return ''; + } + json_array_sort(args) { let list; try { From 9886a7185a169955d0e2f4db20a81e22cc7d209f Mon Sep 17 00:00:00 2001 From: Skyhigh173 <100467674+Skyhigh173@users.noreply.github.com> Date: Mon, 25 Dec 2023 21:22:43 +0800 Subject: [PATCH 3/7] better format --- extensions/Skyhigh173/json.js | 88 ++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/extensions/Skyhigh173/json.js b/extensions/Skyhigh173/json.js index 72b08db236..6661852422 100644 --- a/extensions/Skyhigh173/json.js +++ b/extensions/Skyhigh173/json.js @@ -18,6 +18,15 @@ text: text, }); + const sampleJSON = { + keyval: '{"key":"value"}', + mulkeyval: '{"key":"value","key2":"value2"}', + list: '["scratch","TurboWarp"]', + longList: '["apple","banana","orange"]', + intList: '[1,2,3,4]', + floatList: '[5.23, 214, 522, 61, 5.24, 62.2, 1, 51212, 0, 0]' + }; + class JSONS { getInfo() { return { @@ -33,7 +42,7 @@ arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '{"key":"value"}', + defaultValue: sampleJSON.keyval, }, }, }, @@ -44,7 +53,7 @@ arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '{"key":"value"}', + defaultValue: sampleJSON.keyval, }, types: { type: Scratch.ArgumentType.STRING, @@ -65,7 +74,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '{"key":"value","key2":"value2"}', + defaultValue: sampleJSON.mulkeyval, }, }, }, @@ -93,7 +102,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '{"key":"value"}', + defaultValue: sampleJSON.keyval, }, }, }, @@ -239,7 +248,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["scratch","TurboWarp"]', + defaultValue: sampleJSON.list, }, }, }, @@ -273,7 +282,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["love","heart","follow"]', + defaultValue: sampleJSON.longList, }, }, }, @@ -284,7 +293,7 @@ arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: 'fav', + defaultValue: 'pear', }, pos: { type: Scratch.ArgumentType.NUMBER, @@ -292,7 +301,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["love","follow"]', + defaultValue: sampleJSON.longList, }, }, }, @@ -308,7 +317,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["scratch","a","TurboWarp"]', + defaultValue: sampleJSON.longList, }, }, }, @@ -339,7 +348,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["scratch","TurboWarp"]', + defaultValue: sampleJSON.list, }, }, }, @@ -362,7 +371,7 @@ arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '[1,2,3,4]', + defaultValue: sampleJSON.intList, }, item: { type: Scratch.ArgumentType.NUMBER, @@ -381,7 +390,7 @@ arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["a","b","c","d","e","f"]', + defaultValue: sampleJSON.intList, }, }, }, @@ -407,11 +416,11 @@ arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["a","b"]', + defaultValue: sampleJSON.list, }, json2: { type: Scratch.ArgumentType.STRING, - defaultValue: '["c","d"]', + defaultValue: sampleJSON.longList, }, }, }, @@ -437,11 +446,11 @@ arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["a","b","c"]', + defaultValue: sampleJSON.list, }, len: { type: Scratch.ArgumentType.NUMBER, - defaultValue: 2, + defaultValue: 1, }, }, }, @@ -453,8 +462,7 @@ arguments: { list: { type: Scratch.ArgumentType.STRING, - defaultValue: - '["abc","bac","cab"]', + defaultValue: sampleJSON.longList, }, options: { type: Scratch.ArgumentType.STRING, @@ -462,7 +470,7 @@ }, text: { type: Scratch.ArgumentType.STRING, - defaultValue: 'a', + defaultValue: 'e', }, type: { type: Scratch.ArgumentType.STRING, @@ -510,8 +518,7 @@ arguments: { list: { type: Scratch.ArgumentType.STRING, - defaultValue: - '[5.23, 214, 522, 61, 5.24, 62.2, 1, 51212, 0, 0]', + defaultValue: sampleJSON.floatList, }, order: { type: Scratch.ArgumentType.STRING, @@ -560,7 +567,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["apple","banana"]', + defaultValue: sampleJSON.longList, }, }, }, @@ -582,8 +589,8 @@ items: ['=', '≠'], }, sort_order: { - items: ['ascending', 'descending'], acceptReporters: true, + items: ['ascending', 'descending'], }, filter_options: { items: ['includes', 'starts with', 'ends with'] @@ -661,12 +668,11 @@ return !Array.isArray(json); case 'Array': return Array.isArray(json); - default: - return false; } } catch { - return false; + // ignore } + return false; } json_minify({ json }) { @@ -721,7 +727,7 @@ json_flip({ json }) { try { json = Object.entries(JSON.parse(json)); - json = json.map(x => [x[1], x[0]]); + json = json.map(([k, v]) => [v, k]); return JSON.stringify(Object.fromEntries(json)); } catch { return ''; @@ -1036,14 +1042,21 @@ const out = args.type; switch (option) { - case 'includes': list = list.filter(x => typeof x[1] === 'string' && x[1].includes(text)); break; - case 'starts with': list = list.filter(x => typeof x[1] === 'string' && x[1].startsWith(text)); break; - case 'ends with': list = list.filter(x => typeof x[1] === 'string' && x[1].endsWith(text)); break; - default: return ''; // shouldn't happen + case 'includes': + list = list.filter(x => typeof x[1] === 'string' && x[1].includes(text)); + break; + case 'starts with': + list = list.filter(x => typeof x[1] === 'string' && x[1].startsWith(text)); + break; + case 'ends with': + list = list.filter(x => typeof x[1] === 'string' && x[1].endsWith(text)); + break; + default: + return ''; // shouldn't happen } if (out === 'Object') { // if array, convert to scratch index - if (isArray) list = list.map(x => [Number(x[0]) + 1, x[1]]); + if (isArray) list = list.map(([k, v]) => [Number(k) + 1, v]); return JSON.stringify(Object.fromEntries(list)); } if (out === 'Array') return JSON.stringify(list.map(x => x[1])); @@ -1060,17 +1073,18 @@ } catch { return ''; } - if (!Array.isArray(list)) { - return ''; - } + if (!Array.isArray(list)) return ''; + list.sort(Scratch.Cast.compare); if (args.order === 'descending') list.reverse(); return JSON.stringify(list); } json_vm_export_var(_, util) { - const global = Object.fromEntries(Object.values(vm.runtime.getTargetForStage().variables).filter(x => x.type === '').map(x => [x.name, x.value])); - const local = Object.fromEntries(Object.values(util.target.variables).filter(x => x.type === '').map(x => [x.name, x.value])); + const getVariables = x => Object.fromEntries(Object.values(x.variables).filter(x => x.type === '').map(x => [x.name, x.value])); + + const global = getVariables(vm.runtime.getTargetForStage()); + const local = getVariables(util.target); return JSON.stringify({...global, ...local}); } From abe616b3f3d6ab442e7d09b58fdd516a3e38122c Mon Sep 17 00:00:00 2001 From: Muffin Date: Fri, 23 Feb 2024 23:34:09 -0600 Subject: [PATCH 4/7] npm run format --- extensions/Skyhigh173/json.js | 414 +++++++++++++++++----------------- 1 file changed, 213 insertions(+), 201 deletions(-) diff --git a/extensions/Skyhigh173/json.js b/extensions/Skyhigh173/json.js index 6661852422..47704890ba 100644 --- a/extensions/Skyhigh173/json.js +++ b/extensions/Skyhigh173/json.js @@ -4,17 +4,18 @@ // By: Skyhigh173 (function (Scratch) { - 'use strict'; + "use strict"; /* * JSON extension v2.6 by skyhigh173 (English Version) * Do not remove this comment */ const vm = Scratch.vm; - const hasOwn = (obj, property) => Object.prototype.hasOwnProperty.call(obj, property); + const hasOwn = (obj, property) => + Object.prototype.hasOwnProperty.call(obj, property); const makeLabel = (text) => ({ - blockType: 'label', + blockType: "label", text: text, }); @@ -23,22 +24,22 @@ mulkeyval: '{"key":"value","key2":"value2"}', list: '["scratch","TurboWarp"]', longList: '["apple","banana","orange"]', - intList: '[1,2,3,4]', - floatList: '[5.23, 214, 522, 61, 5.24, 62.2, 1, 51212, 0, 0]' + intList: "[1,2,3,4]", + floatList: "[5.23, 214, 522, 61, 5.24, 62.2, 1, 51212, 0, 0]", }; class JSONS { getInfo() { return { - id: 'skyhigh173JSON', - name: 'JSON', - color1: '#3271D0', + id: "skyhigh173JSON", + name: "JSON", + color1: "#3271D0", blocks: [ - makeLabel('General Utils'), + makeLabel("General Utils"), { - opcode: 'json_is_valid', + opcode: "json_is_valid", blockType: Scratch.BlockType.BOOLEAN, - text: 'is JSON [json] valid?', + text: "is JSON [json] valid?", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -47,9 +48,9 @@ }, }, { - opcode: 'json_is', + opcode: "json_is", blockType: Scratch.BlockType.BOOLEAN, - text: 'is [json] [types]?', + text: "is [json] [types]?", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -57,20 +58,20 @@ }, types: { type: Scratch.ArgumentType.STRING, - defaultValue: 'Object', - menu: 'types', + defaultValue: "Object", + menu: "types", }, }, }, - '---', + "---", { - opcode: 'json_get_all', + opcode: "json_get_all", blockType: Scratch.BlockType.REPORTER, - text: 'all [Stype] of [json]', + text: "all [Stype] of [json]", arguments: { Stype: { type: Scratch.ArgumentType.STRING, - menu: 'get_all', + menu: "get_all", }, json: { type: Scratch.ArgumentType.STRING, @@ -79,26 +80,26 @@ }, }, { - opcode: 'json_new', + opcode: "json_new", blockType: Scratch.BlockType.REPORTER, - text: 'new [json]', + text: "new [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: 'Object', - menu: 'types', + defaultValue: "Object", + menu: "types", }, }, }, - '---', + "---", { - opcode: 'json_has_key', + opcode: "json_has_key", blockType: Scratch.BlockType.BOOLEAN, - text: '[json] contains key [key]?', + text: "[json] contains key [key]?", arguments: { key: { type: Scratch.ArgumentType.STRING, - defaultValue: 'key2', + defaultValue: "key2", }, json: { type: Scratch.ArgumentType.STRING, @@ -107,13 +108,13 @@ }, }, { - opcode: 'json_has_value', + opcode: "json_has_value", blockType: Scratch.BlockType.BOOLEAN, - text: '[json] contains value [value]?', + text: "[json] contains value [value]?", arguments: { value: { type: Scratch.ArgumentType.STRING, - defaultValue: 'scratch', + defaultValue: "scratch", }, json: { type: Scratch.ArgumentType.STRING, @@ -122,9 +123,9 @@ }, }, { - opcode: 'json_equal', + opcode: "json_equal", blockType: Scratch.BlockType.BOOLEAN, - text: '[json1] [equal] [json2]', + text: "[json1] [equal] [json2]", arguments: { json1: { type: Scratch.ArgumentType.STRING, @@ -136,16 +137,16 @@ }, equal: { type: Scratch.ArgumentType.STRING, - defaultValue: '=', - menu: 'equal', + defaultValue: "=", + menu: "equal", }, }, }, - '---', + "---", { - opcode: 'json_minify', + opcode: "json_minify", blockType: Scratch.BlockType.REPORTER, - text: 'minify JSON [json]', + text: "minify JSON [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -154,21 +155,21 @@ }, }, { - opcode: 'json_flip', + opcode: "json_flip", blockType: Scratch.BlockType.REPORTER, - text: 'flip key-value pair in JSON [json]', + text: "flip key-value pair in JSON [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, defaultValue: '{"key":"value","key2":"value2"}', - } + }, }, }, - makeLabel('JSON Strings'), + makeLabel("JSON Strings"), { - opcode: 'json_jlength', + opcode: "json_jlength", blockType: Scratch.BlockType.REPORTER, - text: 'length of json [json]', + text: "length of json [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -177,13 +178,13 @@ }, }, { - opcode: 'json_get', + opcode: "json_get", blockType: Scratch.BlockType.REPORTER, - text: 'value of [item] in [json]', + text: "value of [item] in [json]", arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: 'key', + defaultValue: "key", }, json: { type: Scratch.ArgumentType.STRING, @@ -192,17 +193,17 @@ }, }, { - opcode: 'json_set', + opcode: "json_set", blockType: Scratch.BlockType.REPORTER, - text: 'set [item] in [json] to [value]', + text: "set [item] in [json] to [value]", arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: 'key', + defaultValue: "key", }, value: { type: Scratch.ArgumentType.STRING, - defaultValue: 'new value', + defaultValue: "new value", }, json: { type: Scratch.ArgumentType.STRING, @@ -211,13 +212,13 @@ }, }, { - opcode: 'json_delete', + opcode: "json_delete", blockType: Scratch.BlockType.REPORTER, - text: 'delete [item] in [json]', + text: "delete [item] in [json]", arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: 'key2', + defaultValue: "key2", }, json: { type: Scratch.ArgumentType.STRING, @@ -225,22 +226,22 @@ }, }, }, - makeLabel('Array'), + makeLabel("Array"), { - opcode: 'json_length', + opcode: "json_length", blockType: Scratch.BlockType.REPORTER, - text: 'length of array [json]', + text: "length of array [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '[1,2,3]', + defaultValue: "[1,2,3]", }, }, }, { - opcode: 'json_array_get', + opcode: "json_array_get", blockType: Scratch.BlockType.REPORTER, - text: 'item [item] of array [json]', + text: "item [item] of array [json]", arguments: { item: { type: Scratch.ArgumentType.NUMBER, @@ -253,13 +254,13 @@ }, }, { - opcode: 'json_array_push', + opcode: "json_array_push", blockType: Scratch.BlockType.REPORTER, - text: 'add [item] to array [json]', + text: "add [item] to array [json]", arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: 'TurboWarp', + defaultValue: "TurboWarp", }, json: { type: Scratch.ArgumentType.STRING, @@ -268,13 +269,13 @@ }, }, { - opcode: 'json_array_set', + opcode: "json_array_set", blockType: Scratch.BlockType.REPORTER, - text: 'replace item [pos] of [json] with [item]', + text: "replace item [pos] of [json] with [item]", arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: 'fav', + defaultValue: "fav", }, pos: { type: Scratch.ArgumentType.NUMBER, @@ -287,13 +288,13 @@ }, }, { - opcode: 'json_array_insert', + opcode: "json_array_insert", blockType: Scratch.BlockType.REPORTER, - text: 'insert [item] at [pos] of array [json]', + text: "insert [item] at [pos] of array [json]", arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: 'pear', + defaultValue: "pear", }, pos: { type: Scratch.ArgumentType.NUMBER, @@ -305,11 +306,11 @@ }, }, }, - '---', + "---", { - opcode: 'json_array_delete', + opcode: "json_array_delete", blockType: Scratch.BlockType.REPORTER, - text: 'delete item [item] of array [json]', + text: "delete item [item] of array [json]", arguments: { item: { type: Scratch.ArgumentType.NUMBER, @@ -322,13 +323,13 @@ }, }, { - opcode: 'json_array_remove_all', + opcode: "json_array_remove_all", blockType: Scratch.BlockType.REPORTER, - text: 'delete all [item] in array [json]', + text: "delete all [item] in array [json]", arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: 'a', + defaultValue: "a", }, json: { type: Scratch.ArgumentType.STRING, @@ -336,15 +337,15 @@ }, }, }, - '---', + "---", { - opcode: 'json_array_itemH', + opcode: "json_array_itemH", blockType: Scratch.BlockType.REPORTER, - text: 'item # of [item] in array [json]', + text: "item # of [item] in array [json]", arguments: { item: { type: Scratch.ArgumentType.STRING, - defaultValue: 'scratch', + defaultValue: "scratch", }, json: { type: Scratch.ArgumentType.STRING, @@ -352,22 +353,22 @@ }, }, }, - makeLabel('Advanced'), + makeLabel("Advanced"), { - opcode: 'json_array_from', + opcode: "json_array_from", blockType: Scratch.BlockType.REPORTER, - text: 'array from text [json]', + text: "array from text [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: 'abcd', + defaultValue: "abcd", }, }, }, { - opcode: 'json_array_fromto', + opcode: "json_array_fromto", blockType: Scratch.BlockType.REPORTER, - text: 'items [item] to [item2] of array [json]', + text: "items [item] to [item2] of array [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -384,9 +385,9 @@ }, }, { - opcode: 'json_array_reverse', + opcode: "json_array_reverse", blockType: Scratch.BlockType.REPORTER, - text: 'reverse array [json]', + text: "reverse array [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -395,13 +396,13 @@ }, }, { - opcode: 'json_array_flat', + opcode: "json_array_flat", blockType: Scratch.BlockType.REPORTER, - text: 'flat array [json] by depth [depth]', + text: "flat array [json] by depth [depth]", arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '[[1],2,[3,4],[5,[6]]]', + defaultValue: "[[1],2,[3,4],[5,[6]]]", }, depth: { type: Scratch.ArgumentType.NUMBER, @@ -410,9 +411,9 @@ }, }, { - opcode: 'json_array_concat', + opcode: "json_array_concat", blockType: Scratch.BlockType.REPORTER, - text: 'concatenate array [json] [json2]', + text: "concatenate array [json] [json2]", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -425,13 +426,13 @@ }, }, { - opcode: 'json_array_filter', + opcode: "json_array_filter", blockType: Scratch.BlockType.REPORTER, - text: 'get all values with key [key] in array [json]', + text: "get all values with key [key] in array [json]", arguments: { key: { type: Scratch.ArgumentType.STRING, - defaultValue: 'id', + defaultValue: "id", }, json: { type: Scratch.ArgumentType.STRING, @@ -440,9 +441,9 @@ }, }, { - opcode: 'json_array_setlen', + opcode: "json_array_setlen", blockType: Scratch.BlockType.REPORTER, - text: 'set length of array [json] to [len]', + text: "set length of array [json] to [len]", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -456,9 +457,9 @@ }, { // requested by sharkpool, I will improve it when theres lambda function - opcode: 'json_array_textfilter', + opcode: "json_array_textfilter", blockType: Scratch.BlockType.REPORTER, - text: 'filter array [list] if text [options] [text] and return [type]', + text: "filter array [list] if text [options] [text] and return [type]", arguments: { list: { type: Scratch.ArgumentType.STRING, @@ -466,38 +467,38 @@ }, options: { type: Scratch.ArgumentType.STRING, - menu: 'filter_options', + menu: "filter_options", }, text: { type: Scratch.ArgumentType.STRING, - defaultValue: 'e', + defaultValue: "e", }, type: { type: Scratch.ArgumentType.STRING, - menu: 'types', - } + menu: "types", + }, }, }, - '---', + "---", { - opcode: 'json_array_create', + opcode: "json_array_create", blockType: Scratch.BlockType.REPORTER, - text: 'create array by [text] with delimiter [d]', + text: "create array by [text] with delimiter [d]", arguments: { text: { type: Scratch.ArgumentType.STRING, - defaultValue: 'a,b,c', + defaultValue: "a,b,c", }, d: { type: Scratch.ArgumentType.STRING, - defaultValue: ',', + defaultValue: ",", }, }, }, { - opcode: 'json_array_join', + opcode: "json_array_join", blockType: Scratch.BlockType.REPORTER, - text: 'join string by array [json] with delimiter [d]', + text: "join string by array [json] with delimiter [d]", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -505,15 +506,15 @@ }, d: { type: Scratch.ArgumentType.STRING, - defaultValue: ',', + defaultValue: ",", }, }, }, - '---', + "---", { - opcode: 'json_array_sort', + opcode: "json_array_sort", blockType: Scratch.BlockType.REPORTER, - text: 'sort array [list] in [order] order', + text: "sort array [list] in [order] order", disableMonitor: true, arguments: { list: { @@ -522,21 +523,21 @@ }, order: { type: Scratch.ArgumentType.STRING, - menu: 'sort_order', + menu: "sort_order", }, }, }, - makeLabel('Variables & Lists'), + makeLabel("Variables & Lists"), { - opcode: 'json_vm_export_var', + opcode: "json_vm_export_var", blockType: Scratch.BlockType.REPORTER, - text: 'export all variables', + text: "export all variables", disableMonitor: true, }, { - opcode: 'json_vm_import_var', + opcode: "json_vm_import_var", blockType: Scratch.BlockType.COMMAND, - text: 'import all variables from [json]', + text: "import all variables from [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -544,26 +545,26 @@ }, }, }, - '---', + "---", { - opcode: 'json_vm_getlist', + opcode: "json_vm_getlist", blockType: Scratch.BlockType.REPORTER, - text: 'get list [list] as array', + text: "get list [list] as array", arguments: { list: { type: Scratch.ArgumentType.STRING, - menu: 'get_list', + menu: "get_list", }, }, }, { - opcode: 'json_vm_setlist', + opcode: "json_vm_setlist", blockType: Scratch.BlockType.COMMAND, - text: 'set list [list] to [json]', + text: "set list [list] to [json]", arguments: { list: { type: Scratch.ArgumentType.STRING, - menu: 'get_list', + menu: "get_list", }, json: { type: Scratch.ArgumentType.STRING, @@ -574,27 +575,27 @@ ], menus: { get_all: { - items: ['keys', 'values', 'datas'], + items: ["keys", "values", "datas"], }, get_list: { acceptReporters: true, - items: 'getLists', + items: "getLists", }, types: { acceptReporters: true, - items: ['Object', 'Array'], + items: ["Object", "Array"], }, equal: { acceptReporters: true, - items: ['=', '≠'], + items: ["=", "≠"], }, sort_order: { acceptReporters: true, - items: ['ascending', 'descending'], + items: ["ascending", "descending"], }, filter_options: { - items: ['includes', 'starts with', 'ends with'] - } + items: ["includes", "starts with", "ends with"], + }, }, }; } @@ -602,16 +603,16 @@ getLists() { const globalLists = Object.values( vm.runtime.getTargetForStage().variables - ).filter((x) => x.type == 'list'); + ).filter((x) => x.type == "list"); const localLists = Object.values(vm.editingTarget.variables).filter( - (x) => x.type == 'list' + (x) => x.type == "list" ); const uniqueLists = [...new Set([...globalLists, ...localLists])]; if (uniqueLists.length === 0) { return [ { - text: 'select a list', - value: 'select a list', + text: "select a list", + value: "select a list", }, ]; } @@ -623,10 +624,10 @@ lookupList(list, util) { const byId = util.target.lookupVariableById(list); - if (byId && byId.type === 'list') { + if (byId && byId.type === "list") { return byId; } - const byName = util.target.lookupVariableByNameAndType(list, 'list'); + const byName = util.target.lookupVariableByNameAndType(list, "list"); if (byName) { return byName; } @@ -634,7 +635,7 @@ } json_is_valid({ json }) { - if (typeof json != 'string') { + if (typeof json != "string") { return false; } else { try { @@ -648,7 +649,7 @@ // return object if its json else string json_valid_return(json) { - if (typeof json != 'string') { + if (typeof json != "string") { return json; } else { try { @@ -664,9 +665,9 @@ try { json = JSON.parse(json); switch (types) { - case 'Object': + case "Object": return !Array.isArray(json); - case 'Array': + case "Array": return Array.isArray(json); } } catch { @@ -679,7 +680,7 @@ try { return JSON.stringify(JSON.parse(json)); } catch { - return ''; + return ""; } } @@ -688,18 +689,18 @@ json = JSON.parse(json); return Object.keys(json).length; } catch { - return ' '; + return " "; } } json_new({ json }) { switch (json) { - case 'Object': - return '{}'; - case 'Array': - return '[]'; + case "Object": + return "{}"; + case "Array": + return "[]"; default: - return ''; + return ""; } } @@ -730,7 +731,7 @@ json = json.map(([k, v]) => [v, k]); return JSON.stringify(Object.fromEntries(json)); } catch { - return ''; + return ""; } } @@ -744,8 +745,8 @@ const result = keys1.length === keys2.length && Object.keys(json1).every((key) => json1[key] === json2[key]); - if (equal === '=') return result; - if (equal === '≠') return !result; + if (equal === "=") return result; + if (equal === "≠") return !result; } catch { // ignore } @@ -756,17 +757,17 @@ try { json = JSON.parse(json); switch (Stype) { - case 'keys': + case "keys": return JSON.stringify(Object.keys(json)); - case 'values': + case "values": return JSON.stringify(Object.values(json)); - case 'datas': + case "datas": return JSON.stringify(Object.entries(json)); default: - return ''; + return ""; } } catch { - return ''; + return ""; } } @@ -775,7 +776,7 @@ json = JSON.parse(json); if (hasOwn(json, item)) { const result = json[item]; - if (typeof result === 'object') { + if (typeof result === "object") { return JSON.stringify(result); } else { return result; @@ -784,14 +785,14 @@ } catch { // ignore } - return ''; + return ""; } _fixInvalidJSONValues(value) { // JSON does not support these values, so convert to string. - if (Number.isNaN(value)) return 'NaN'; - if (value === Infinity) return 'Infinity'; - if (value === -Infinity) return '-Infinity'; + if (Number.isNaN(value)) return "NaN"; + if (value === Infinity) return "Infinity"; + if (value === -Infinity) return "-Infinity"; return value; } @@ -803,7 +804,7 @@ json[item] = value; return JSON.stringify(json); } catch { - return ''; + return ""; } } @@ -813,7 +814,7 @@ delete json[item]; return JSON.stringify(json); } catch { - return ''; + return ""; } } @@ -826,7 +827,7 @@ // 1...length : array content, -1...-length : reverse array content, 0 : ERROR try { item = Scratch.Cast.toNumber(item); - if (item == 0) return ''; + if (item == 0) return ""; if (item > 0) { item--; } @@ -837,13 +838,13 @@ } else { result = json[json.length + item]; } - if (typeof result == 'object') { + if (typeof result == "object") { return JSON.stringify(result); } else { return result; } } catch { - return ''; + return ""; } } @@ -854,7 +855,7 @@ let result = JSON.stringify(json.indexOf(item) + 1); return result; } catch { - return ''; + return ""; } } @@ -862,7 +863,7 @@ try { return JSON.stringify(Array.from(String(json))); } catch { - return ''; + return ""; } } @@ -872,7 +873,7 @@ json2 = JSON.parse(json2); return JSON.stringify(json.concat(json2)); } catch { - return ''; + return ""; } } @@ -883,7 +884,7 @@ json.push(item); return JSON.stringify(json); } catch { - return ''; + return ""; } } @@ -894,7 +895,7 @@ json.splice(pos - 1, 0, item); return JSON.stringify(json); } catch { - return ''; + return ""; } } @@ -906,7 +907,7 @@ ); return JSON.stringify(json); } catch { - return ''; + return ""; } } @@ -916,7 +917,7 @@ json.splice(item - 1, 1); return JSON.stringify(json); } catch { - return ''; + return ""; } } @@ -934,7 +935,7 @@ } return JSON.stringify(json); } catch { - return ''; + return ""; } } @@ -942,7 +943,7 @@ try { return JSON.stringify(JSON.parse(json).slice(item - 1, item2)); } catch { - return ''; + return ""; } } @@ -950,7 +951,7 @@ try { return JSON.stringify(JSON.parse(json).reverse()); } catch { - return ''; + return ""; } } @@ -958,7 +959,7 @@ try { return JSON.stringify(JSON.parse(json).flat(depth)); } catch { - return ''; + return ""; } } @@ -970,7 +971,7 @@ try { return JSON.parse(json).join(d); } catch { - return ''; + return ""; } } @@ -986,7 +987,7 @@ }) ); } catch (e) { - return ''; + return ""; } } @@ -996,7 +997,7 @@ json.length = len; return JSON.stringify(json); } catch { - return ''; + return ""; } } @@ -1009,7 +1010,7 @@ } catch (e) { // ignore } - return ''; + return ""; } json_vm_setlist({ list, json }, util) { try { @@ -1018,7 +1019,7 @@ const array = JSON.parse(json); if (Array.isArray(array)) { const safeArray = array.map((i) => { - if (typeof i === 'object') return JSON.stringify(i); + if (typeof i === "object") return JSON.stringify(i); return i; }); listVariable.value = safeArray; @@ -1027,7 +1028,7 @@ } catch (e) { // ignore } - return ''; + return ""; } json_array_textfilter(args) { @@ -1042,28 +1043,34 @@ const out = args.type; switch (option) { - case 'includes': - list = list.filter(x => typeof x[1] === 'string' && x[1].includes(text)); + case "includes": + list = list.filter( + (x) => typeof x[1] === "string" && x[1].includes(text) + ); break; - case 'starts with': - list = list.filter(x => typeof x[1] === 'string' && x[1].startsWith(text)); + case "starts with": + list = list.filter( + (x) => typeof x[1] === "string" && x[1].startsWith(text) + ); break; - case 'ends with': - list = list.filter(x => typeof x[1] === 'string' && x[1].endsWith(text)); + case "ends with": + list = list.filter( + (x) => typeof x[1] === "string" && x[1].endsWith(text) + ); break; default: - return ''; // shouldn't happen + return ""; // shouldn't happen } - if (out === 'Object') { + if (out === "Object") { // if array, convert to scratch index if (isArray) list = list.map(([k, v]) => [Number(k) + 1, v]); return JSON.stringify(Object.fromEntries(list)); } - if (out === 'Array') return JSON.stringify(list.map(x => x[1])); + if (out === "Array") return JSON.stringify(list.map((x) => x[1])); } catch { // ignore } - return ''; + return ""; } json_array_sort(args) { @@ -1071,28 +1078,33 @@ try { list = JSON.parse(args.list); } catch { - return ''; + return ""; } - if (!Array.isArray(list)) return ''; + if (!Array.isArray(list)) return ""; list.sort(Scratch.Cast.compare); - if (args.order === 'descending') list.reverse(); + if (args.order === "descending") list.reverse(); return JSON.stringify(list); } json_vm_export_var(_, util) { - const getVariables = x => Object.fromEntries(Object.values(x.variables).filter(x => x.type === '').map(x => [x.name, x.value])); + const getVariables = (x) => + Object.fromEntries( + Object.values(x.variables) + .filter((x) => x.type === "") + .map((x) => [x.name, x.value]) + ); const global = getVariables(vm.runtime.getTargetForStage()); const local = getVariables(util.target); - return JSON.stringify({...global, ...local}); + return JSON.stringify({ ...global, ...local }); } json_vm_import_var({ json }, util) { try { json = JSON.parse(json); Object.entries(json).forEach(([k, v]) => { - util.target.lookupVariableByNameAndType(k, '').value = v; + util.target.lookupVariableByNameAndType(k, "").value = v; }); } catch {} } From a961688017411a6f921db2c91cae7bd1c5e94c43 Mon Sep 17 00:00:00 2001 From: Skyhigh173 <100467674+Skyhigh173@users.noreply.github.com> Date: Sun, 24 Mar 2024 17:14:27 +0800 Subject: [PATCH 5/7] more blocks --- extensions/Skyhigh173/json.js | 156 +++++++++++++++++++++++++++++----- 1 file changed, 133 insertions(+), 23 deletions(-) diff --git a/extensions/Skyhigh173/json.js b/extensions/Skyhigh173/json.js index ca0221442a..251bbca66c 100644 --- a/extensions/Skyhigh173/json.js +++ b/extensions/Skyhigh173/json.js @@ -7,7 +7,7 @@ (function (Scratch) { "use strict"; /* - * JSON extension v2.6 by skyhigh173 (English Version) + * JSON extension v2.6 by skyhigh173 * Do not remove this comment */ @@ -119,7 +119,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '["TurboWarp","scratch"]', + defaultValue: sampleJSON.list, }, }, }, @@ -147,7 +147,7 @@ { opcode: "json_minify", blockType: Scratch.BlockType.REPORTER, - text: "minify JSON [json]", + text: "minify [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -158,14 +158,49 @@ { opcode: "json_flip", blockType: Scratch.BlockType.REPORTER, - text: "flip key-value pair in JSON [json]", + text: "flip key-value pair in [json]", arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '{"key":"value","key2":"value2"}', + defaultValue: sampleJSON.mulkeyval, }, }, }, + "---", + { + opcode: "json_get_path", + blockType: Scratch.BlockType.REPORTER, + text: "get value by path [path] in [json]", + arguments: { + path: { + type: Scratch.ArgumentType.STRING, + defaultValue: '["key","inner",1]', + }, + json: { + type: Scratch.ArgumentType.STRING, + defaultValue: '{"key":{"inner":[true,false]}}', + }, + }, + }, + { + opcode: "json_set_path", + blockType: Scratch.BlockType.REPORTER, + text: "set value by path [path] in [json] to [data]", + arguments: { + path: { + type: Scratch.ArgumentType.STRING, + defaultValue: '["key","inner",1]', + }, + json: { + type: Scratch.ArgumentType.STRING, + defaultValue: '{"key":{"inner":[true,false]}}', + }, + data: { + type: Scratch.ArgumentType.STRING, + defaultValue: 'value' + } + }, + }, makeLabel("JSON Strings"), { opcode: "json_jlength", @@ -174,7 +209,7 @@ arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: '{"key":"value","key2":"value2"}', + defaultValue: sampleJSON.mulkeyval, }, }, }, @@ -189,7 +224,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '{"key":"value"}', + defaultValue: sampleJSON.keyval, }, }, }, @@ -208,7 +243,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '{"key":"value"}', + defaultValue: sampleJSON.keyval, }, }, }, @@ -223,7 +258,7 @@ }, json: { type: Scratch.ArgumentType.STRING, - defaultValue: '{"key":"value","key2":"value2"}', + defaultValue: sampleJSON.mulkeyval, }, }, }, @@ -235,7 +270,7 @@ arguments: { json: { type: Scratch.ArgumentType.STRING, - defaultValue: "[1,2,3]", + defaultValue: sampleJSON.intList, }, }, }, @@ -648,7 +683,11 @@ } } - // return object if its json else string + /** + * Checks if the input is valid JSON or not, then return it + * @param {*} json + * @returns {Object|Any} + */ json_valid_return(json) { if (typeof json != "string") { return json; @@ -729,8 +768,33 @@ json_flip({ json }) { try { json = Object.entries(JSON.parse(json)); - json = json.map(([k, v]) => [v, k]); - return JSON.stringify(Object.fromEntries(json)); + let result = {}; + let store = (key,val) => { + // invalid key + if (typeof key === "object") return; + if (typeof result[key] == "undefined") { + // new key + result[key] = val; + } else { + // multiple key exists + if (Array.isArray(result[key])) { + result[key] = result[key].push(val); + } else { + result[key] = [result[key], val]; + } + } + }; + for (const i of json) { + if (Array.isArray(i[1])) { + // map to different keys + for (const key of i[1]) { + store(key, i[0]); + } + } else { + store(i[1], i[0]); + } + } + return JSON.stringify(result); } catch { return ""; } @@ -824,21 +888,67 @@ return this.json_length({ json: json }); } + json_get_path({ path, json }) { + try { + path = JSON.parse(path); + json = JSON.parse(json); + for (let key of path) { + if (Array.isArray(json)) { + if (key == 0) return ""; + if (key > 0) key--; + key += key < 0 ? json.length : 0; + if (key >= json.length || key < 0) return ""; + } + json = json[key]; + } + if (typeof json == "object") { + return JSON.stringify(json); + } else { + return json; + } + } catch { + return ""; + } + } + + json_set_path({ path, json, data }) { + try { + path = JSON.parse(path); + json = JSON.parse(json); + let obj = json; + data = this.json_valid_return(data); + let count = path.length; + for (let key of path) { + if (Array.isArray(obj)) { + if (key == 0) return ""; + if (key > 0) key--; + key += key < 0 ? obj.length : 0; + if (key >= obj.length || key < 0) return ""; + } + count--; + if (count == 0) { + obj[key] = data; + } else { + obj = obj[key]; + } + } + return JSON.stringify(json); + } catch { + return ""; + } + } + json_array_get({ item, json }) { // 1...length : array content, -1...-length : reverse array content, 0 : ERROR try { + json = JSON.parse(json); item = Scratch.Cast.toNumber(item); if (item == 0) return ""; - if (item > 0) { - item--; - } - json = JSON.parse(json); - let result; - if (item >= 0) { - result = json[item]; - } else { - result = json[json.length + item]; - } + if (item > 0) item--; + item += item < 0 ? json.length : 0; + if (item >= json.length || item < 0) return ""; + + let result = json[item]; if (typeof result == "object") { return JSON.stringify(result); } else { From 720fe4ed676cfd3365d8df0fbd87b7162cd788d7 Mon Sep 17 00:00:00 2001 From: Cubester Date: Fri, 12 Jul 2024 21:07:40 -0400 Subject: [PATCH 6/7] Formatting and Translations --- extensions/Skyhigh173/json.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/extensions/Skyhigh173/json.js b/extensions/Skyhigh173/json.js index 33f9488492..e57b7b413b 100644 --- a/extensions/Skyhigh173/json.js +++ b/extensions/Skyhigh173/json.js @@ -147,7 +147,7 @@ { opcode: "json_minify", blockType: Scratch.BlockType.REPORTER, - text: "minify [json]", + text: Scratch.translate("minify [json]"), arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -158,7 +158,7 @@ { opcode: "json_flip", blockType: Scratch.BlockType.REPORTER, - text: "flip key-value pair in [json]", + text: Scratch.translate("flip key-value pair in [json]"), arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -170,7 +170,7 @@ { opcode: "json_get_path", blockType: Scratch.BlockType.REPORTER, - text: "get value by path [path] in [json]", + text: Scratch.translate("value by path [path] in [json]"), arguments: { path: { type: Scratch.ArgumentType.STRING, @@ -185,7 +185,9 @@ { opcode: "json_set_path", blockType: Scratch.BlockType.REPORTER, - text: "set value by path [path] in [json] to [data]", + text: Scratch.translate( + "set value by path [path] in [json] to [data]" + ), arguments: { path: { type: Scratch.ArgumentType.STRING, @@ -197,8 +199,8 @@ }, data: { type: Scratch.ArgumentType.STRING, - defaultValue: 'value' - } + defaultValue: "value", + }, }, }, makeLabel("JSON Strings"), @@ -497,7 +499,9 @@ // requested by sharkpool, I will improve it when theres lambda function opcode: "json_array_textfilter", blockType: Scratch.BlockType.REPORTER, - text: "filter array [list] if text [options] [text] and return [type]", + text: Scratch.translate( + "filter array [list] if text [options] [text] and return [type]" + ), arguments: { list: { type: Scratch.ArgumentType.STRING, @@ -573,13 +577,13 @@ { opcode: "json_vm_export_var", blockType: Scratch.BlockType.REPORTER, - text: "export all variables", + text: Scratch.translate("export all variables"), disableMonitor: true, }, { opcode: "json_vm_import_var", blockType: Scratch.BlockType.COMMAND, - text: "import all variables from [json]", + text: Scratch.translate("import all variables from [json]"), arguments: { json: { type: Scratch.ArgumentType.STRING, @@ -667,7 +671,11 @@ ], }, filter_options: { - items: ["includes", "starts with", "ends with"], + items: [ + { text: Scratch.translate("includes"), value: "includes" }, + { text: Scratch.translate("starts with"), value: "starts with" }, + { text: Scratch.translate("ends with"), value: "ends with" }, + ], }, }, }; @@ -722,7 +730,7 @@ /** * Checks if the input is valid JSON or not, then return it - * @param {*} json + * @param {*} json * @returns {Object|Any} */ json_valid_return(json) { @@ -806,7 +814,7 @@ try { json = Object.entries(JSON.parse(json)); let result = {}; - let store = (key,val) => { + let store = (key, val) => { // invalid key if (typeof key === "object") return; if (typeof result[key] == "undefined") { From 603f6b66a5159715500911c9774d98df7a1d7020 Mon Sep 17 00:00:00 2001 From: Skyhigh173 <100467674+Skyhigh173@users.noreply.github.com> Date: Mon, 30 Dec 2024 19:04:39 +0800 Subject: [PATCH 7/7] try to resolve conflicts --- extensions/Skyhigh173/json.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/extensions/Skyhigh173/json.js b/extensions/Skyhigh173/json.js index e57b7b413b..c05f7bb5fa 100644 --- a/extensions/Skyhigh173/json.js +++ b/extensions/Skyhigh173/json.js @@ -718,6 +718,13 @@ json_is_valid({ json }) { if (typeof json != "string") { return false; + } + json = json.trim(); + if ( + (json.slice(0, 1) != "[" || json.slice(-1) != "]") && + (json.slice(0, 1) != "{" || json.slice(-1) != "}") + ) { + return false; } else { try { JSON.parse(json);