From b861c78086faccf682d7eb2fddc3ed142137edba Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 31 Mar 2018 13:25:50 -0300 Subject: [PATCH 1/2] Implement 'get item # of (thing) in (list)' --- src/blocks/scratch3_data.js | 29 ++++++++++++++++++++++ test/unit/blocks_data.js | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 test/unit/blocks_data.js diff --git a/src/blocks/scratch3_data.js b/src/blocks/scratch3_data.js index 0a382e28..a181206b 100644 --- a/src/blocks/scratch3_data.js +++ b/src/blocks/scratch3_data.js @@ -24,6 +24,7 @@ class Scratch3DataBlocks { data_insertatlist: this.insertAtList, data_replaceitemoflist: this.replaceItemOfList, data_itemoflist: this.getItemOfList, + data_itemnumoflist: this.getItemNumOfList, data_lengthoflist: this.lengthOfList, data_listcontainsitem: this.listContainsItem }; @@ -122,6 +123,34 @@ class Scratch3DataBlocks { return list.value[index - 1]; } + getItemNumOfList (args, util) { + const item = args.ITEM; + const list = util.target.lookupOrCreateList( + args.LIST.id, args.LIST.name); + + // Go through the list items one-by-one using Cast.compare. This is for + // cases like checking if 123 is contained in a list [4, 7, '123'] -- + // Scratch considers 123 and '123' to be equal. + for (let i = 0; i < list.value.length; i++) { + if (Cast.compare(list.value[i], item) === 0) { + return i + 1; + } + } + + // We don't bother using .indexOf() at all, because it would end up with + // edge cases such as the index of '123' in [4, 7, 123, '123', 9]. + // If we use indexOf(), this block would return 4 instead of 3, because + // indexOf() sees the first occurence of the string 123 as the fourth + // item in the list. With Scratch, this would be confusing -- after all, + // '123' and 123 look the same, so one would expect the block to say + // that the first occurrence of '123' (or 123) to be the third item. + + // Default to 0 if there's no match. Since Scratch lists are 1-indexed, + // we don't have to worry about this conflicting with the "this item is + // the first value" number (in JS that is 0, but in Scratch it's 1). + return 0; + } + lengthOfList (args, util) { const list = util.target.lookupOrCreateList( args.LIST.id, args.LIST.name); diff --git a/test/unit/blocks_data.js b/test/unit/blocks_data.js new file mode 100644 index 00000000..ad77207b --- /dev/null +++ b/test/unit/blocks_data.js @@ -0,0 +1,49 @@ +const test = require('tap').test; +const Data = require('../../src/blocks/scratch3_data'); + +const blocks = new Data(); + +const lists = {}; +const util = { + target: { + lookupOrCreateList(id, name) { + if (!(name in lists)) { + lists[name] = {value: []}; + } + return lists[name]; + } + } +}; + +test('getItemNumOfList returns the index of an item (basic)', t => { + lists.list = {value: ['apple', 'taco', 'burrito', 'extravaganza']}; + const args = {ITEM: 'burrito', LIST: {name: 'list'}}; + const index = blocks.getItemNumOfList(args, util); + t.strictEqual(index, 3); + t.end(); +}); + +test('getItemNumOfList returns 0 when an item is not found', t => { + lists.list = {value: ['aaaaapple', 'burrito']}; + const args = {ITEM: 'jump', LIST: {name: 'list'}}; + const index = blocks.getItemNumOfList(args, util); + t.strictEqual(index, 0); + t.end(); +}); + +test('getItemNumOfList uses Scratch comparison', t => { + lists.list = {value: ['jump', 'Jump', '123', 123, 800]}; + const args = {LIST: {name: 'list'}}; + + // Be case-insensitive: + args.ITEM = 'Jump'; + t.strictEqual(blocks.getItemNumOfList(args, util), 1); + + // Be type-insensitive: + args.ITEM = 123; + t.strictEqual(blocks.getItemNumOfList(args, util), 3); + args.ITEM = '800'; + t.strictEqual(blocks.getItemNumOfList(args, util), 5); + + t.end(); +}); From fd188741ba87ab3d624cfb62b0901ec740965e34 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 31 Mar 2018 13:41:39 -0300 Subject: [PATCH 2/2] Lint --- test/unit/blocks_data.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/blocks_data.js b/test/unit/blocks_data.js index ad77207b..01980434 100644 --- a/test/unit/blocks_data.js +++ b/test/unit/blocks_data.js @@ -6,7 +6,7 @@ const blocks = new Data(); const lists = {}; const util = { target: { - lookupOrCreateList(id, name) { + lookupOrCreateList (id, name) { if (!(name in lists)) { lists[name] = {value: []}; }