From b861c78086faccf682d7eb2fddc3ed142137edba Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 31 Mar 2018 13:25:50 -0300 Subject: [PATCH 1/3] 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 0a382e280..a181206b6 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 000000000..ad77207b2 --- /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/3] 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 ad77207b2..01980434d 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: []}; } From 8e252f2d7dd6eb98a44f1f5b419624926458c8df Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Thu, 3 May 2018 18:53:56 -0400 Subject: [PATCH 3/3] Use arraybuffer-loader in place of buffer-loader --- package.json | 2 +- src/extensions/scratch3_music/index.js | 2 +- src/extensions/scratch3_music/manifest.js | 124 +++++++++++----------- 3 files changed, 65 insertions(+), 63 deletions(-) diff --git a/package.json b/package.json index b39779db8..697122d84 100644 --- a/package.json +++ b/package.json @@ -29,11 +29,11 @@ }, "devDependencies": { "adm-zip": "0.4.7", + "arraybuffer-loader": "^1.0.3", "babel-core": "^6.24.1", "babel-eslint": "^7.1.1", "babel-loader": "^7.0.0", "babel-preset-es2015": "^6.24.1", - "buffer-loader": "0.0.1", "canvas-toBlob": "1.0.0", "copy-webpack-plugin": "4.2.1", "decode-html": "2.0.0", diff --git a/src/extensions/scratch3_music/index.js b/src/extensions/scratch3_music/index.js index 03f175c75..bfa55bee3 100644 --- a/src/extensions/scratch3_music/index.js +++ b/src/extensions/scratch3_music/index.js @@ -115,7 +115,7 @@ class Scratch3MusicBlocks { if (!assetData[fullPath]) return; // The sound buffer has already been downloaded via the manifest file required above. - const soundBuffer = assetData[fullPath].buffer; + const soundBuffer = assetData[fullPath]; return this._decodeSound(soundBuffer).then(buffer => { bufferArray[index] = buffer; diff --git a/src/extensions/scratch3_music/manifest.js b/src/extensions/scratch3_music/manifest.js index 017da03b9..be166a76c 100644 --- a/src/extensions/scratch3_music/manifest.js +++ b/src/extensions/scratch3_music/manifest.js @@ -1,63 +1,65 @@ module.exports = { - 'drums/1-snare.mp3': require('!buffer-loader!./assets/drums/1-snare.mp3'), - 'drums/2-bass-drum.mp3': require('!buffer-loader!./assets/drums/2-bass-drum.mp3'), - 'drums/3-side-stick.mp3': require('!buffer-loader!./assets/drums/3-side-stick.mp3'), - 'drums/4-crash-cymbal.mp3': require('!buffer-loader!./assets/drums/4-crash-cymbal.mp3'), - 'drums/5-open-hi-hat.mp3': require('!buffer-loader!./assets/drums/5-open-hi-hat.mp3'), - 'drums/6-closed-hi-hat.mp3': require('!buffer-loader!./assets/drums/6-closed-hi-hat.mp3'), - 'drums/7-tambourine.mp3': require('!buffer-loader!./assets/drums/7-tambourine.mp3'), - 'drums/8-hand-clap.mp3': require('!buffer-loader!./assets/drums/8-hand-clap.mp3'), - 'drums/9-claves.mp3': require('!buffer-loader!./assets/drums/9-claves.mp3'), - 'drums/10-wood-block.mp3': require('!buffer-loader!./assets/drums/10-wood-block.mp3'), - 'drums/11-cowbell.mp3': require('!buffer-loader!./assets/drums/11-cowbell.mp3'), - 'drums/12-triangle.mp3': require('!buffer-loader!./assets/drums/12-triangle.mp3'), - 'drums/13-bongo.mp3': require('!buffer-loader!./assets/drums/13-bongo.mp3'), - 'drums/14-conga.mp3': require('!buffer-loader!./assets/drums/14-conga.mp3'), - 'drums/15-cabasa.mp3': require('!buffer-loader!./assets/drums/15-cabasa.mp3'), - 'drums/16-guiro.mp3': require('!buffer-loader!./assets/drums/16-guiro.mp3'), - 'drums/17-vibraslap.mp3': require('!buffer-loader!./assets/drums/17-vibraslap.mp3'), - 'drums/18-cuica.mp3': require('!buffer-loader!./assets/drums/18-cuica.mp3'), - 'instruments/1-piano/24.mp3': require('!buffer-loader!./assets/instruments/1-piano/24.mp3'), - 'instruments/1-piano/36.mp3': require('!buffer-loader!./assets/instruments/1-piano/36.mp3'), - 'instruments/1-piano/48.mp3': require('!buffer-loader!./assets/instruments/1-piano/48.mp3'), - 'instruments/1-piano/60.mp3': require('!buffer-loader!./assets/instruments/1-piano/60.mp3'), - 'instruments/1-piano/72.mp3': require('!buffer-loader!./assets/instruments/1-piano/72.mp3'), - 'instruments/1-piano/84.mp3': require('!buffer-loader!./assets/instruments/1-piano/84.mp3'), - 'instruments/1-piano/96.mp3': require('!buffer-loader!./assets/instruments/1-piano/96.mp3'), - 'instruments/1-piano/108.mp3': require('!buffer-loader!./assets/instruments/1-piano/108.mp3'), - 'instruments/2-electric-piano/60.mp3': require('!buffer-loader!./assets/instruments/2-electric-piano/60.mp3'), - 'instruments/3-organ/60.mp3': require('!buffer-loader!./assets/instruments/3-organ/60.mp3'), - 'instruments/4-guitar/60.mp3': require('!buffer-loader!./assets/instruments/4-guitar/60.mp3'), - 'instruments/5-electric-guitar/60.mp3': require('!buffer-loader!./assets/instruments/5-electric-guitar/60.mp3'), - 'instruments/6-bass/36.mp3': require('!buffer-loader!./assets/instruments/6-bass/36.mp3'), - 'instruments/6-bass/48.mp3': require('!buffer-loader!./assets/instruments/6-bass/48.mp3'), - 'instruments/7-pizzicato/60.mp3': require('!buffer-loader!./assets/instruments/7-pizzicato/60.mp3'), - 'instruments/8-cello/36.mp3': require('!buffer-loader!./assets/instruments/8-cello/36.mp3'), - 'instruments/8-cello/48.mp3': require('!buffer-loader!./assets/instruments/8-cello/48.mp3'), - 'instruments/8-cello/60.mp3': require('!buffer-loader!./assets/instruments/8-cello/60.mp3'), - 'instruments/9-trombone/36.mp3': require('!buffer-loader!./assets/instruments/9-trombone/36.mp3'), - 'instruments/9-trombone/48.mp3': require('!buffer-loader!./assets/instruments/9-trombone/48.mp3'), - 'instruments/9-trombone/60.mp3': require('!buffer-loader!./assets/instruments/9-trombone/60.mp3'), - 'instruments/10-clarinet/48.mp3': require('!buffer-loader!./assets/instruments/10-clarinet/48.mp3'), - 'instruments/10-clarinet/60.mp3': require('!buffer-loader!./assets/instruments/10-clarinet/60.mp3'), - 'instruments/11-saxophone/36.mp3': require('!buffer-loader!./assets/instruments/11-saxophone/36.mp3'), - 'instruments/11-saxophone/60.mp3': require('!buffer-loader!./assets/instruments/11-saxophone/60.mp3'), - 'instruments/11-saxophone/84.mp3': require('!buffer-loader!./assets/instruments/11-saxophone/84.mp3'), - 'instruments/12-flute/60.mp3': require('!buffer-loader!./assets/instruments/12-flute/60.mp3'), - 'instruments/12-flute/72.mp3': require('!buffer-loader!./assets/instruments/12-flute/72.mp3'), - 'instruments/13-wooden-flute/60.mp3': require('!buffer-loader!./assets/instruments/13-wooden-flute/60.mp3'), - 'instruments/13-wooden-flute/72.mp3': require('!buffer-loader!./assets/instruments/13-wooden-flute/72.mp3'), - 'instruments/14-bassoon/36.mp3': require('!buffer-loader!./assets/instruments/14-bassoon/36.mp3'), - 'instruments/14-bassoon/48.mp3': require('!buffer-loader!./assets/instruments/14-bassoon/48.mp3'), - 'instruments/14-bassoon/60.mp3': require('!buffer-loader!./assets/instruments/14-bassoon/60.mp3'), - 'instruments/15-choir/48.mp3': require('!buffer-loader!./assets/instruments/15-choir/48.mp3'), - 'instruments/15-choir/60.mp3': require('!buffer-loader!./assets/instruments/15-choir/60.mp3'), - 'instruments/15-choir/72.mp3': require('!buffer-loader!./assets/instruments/15-choir/72.mp3'), - 'instruments/16-vibraphone/60.mp3': require('!buffer-loader!./assets/instruments/16-vibraphone/60.mp3'), - 'instruments/16-vibraphone/72.mp3': require('!buffer-loader!./assets/instruments/16-vibraphone/72.mp3'), - 'instruments/17-music-box/60.mp3': require('!buffer-loader!./assets/instruments/17-music-box/60.mp3'), - 'instruments/18-steel-drum/60.mp3': require('!buffer-loader!./assets/instruments/18-steel-drum/60.mp3'), - 'instruments/19-marimba/60.mp3': require('!buffer-loader!./assets/instruments/19-marimba/60.mp3'), - 'instruments/20-synth-lead/60.mp3': require('!buffer-loader!./assets/instruments/20-synth-lead/60.mp3'), - 'instruments/21-synth-pad/60.mp3': require('!buffer-loader!./assets/instruments/21-synth-pad/60.mp3') + 'drums/1-snare.mp3': require('!arraybuffer-loader!./assets/drums/1-snare.mp3'), + 'drums/2-bass-drum.mp3': require('!arraybuffer-loader!./assets/drums/2-bass-drum.mp3'), + 'drums/3-side-stick.mp3': require('!arraybuffer-loader!./assets/drums/3-side-stick.mp3'), + 'drums/4-crash-cymbal.mp3': require('!arraybuffer-loader!./assets/drums/4-crash-cymbal.mp3'), + 'drums/5-open-hi-hat.mp3': require('!arraybuffer-loader!./assets/drums/5-open-hi-hat.mp3'), + 'drums/6-closed-hi-hat.mp3': require('!arraybuffer-loader!./assets/drums/6-closed-hi-hat.mp3'), + 'drums/7-tambourine.mp3': require('!arraybuffer-loader!./assets/drums/7-tambourine.mp3'), + 'drums/8-hand-clap.mp3': require('!arraybuffer-loader!./assets/drums/8-hand-clap.mp3'), + 'drums/9-claves.mp3': require('!arraybuffer-loader!./assets/drums/9-claves.mp3'), + 'drums/10-wood-block.mp3': require('!arraybuffer-loader!./assets/drums/10-wood-block.mp3'), + 'drums/11-cowbell.mp3': require('!arraybuffer-loader!./assets/drums/11-cowbell.mp3'), + 'drums/12-triangle.mp3': require('!arraybuffer-loader!./assets/drums/12-triangle.mp3'), + 'drums/13-bongo.mp3': require('!arraybuffer-loader!./assets/drums/13-bongo.mp3'), + 'drums/14-conga.mp3': require('!arraybuffer-loader!./assets/drums/14-conga.mp3'), + 'drums/15-cabasa.mp3': require('!arraybuffer-loader!./assets/drums/15-cabasa.mp3'), + 'drums/16-guiro.mp3': require('!arraybuffer-loader!./assets/drums/16-guiro.mp3'), + 'drums/17-vibraslap.mp3': require('!arraybuffer-loader!./assets/drums/17-vibraslap.mp3'), + 'drums/18-cuica.mp3': require('!arraybuffer-loader!./assets/drums/18-cuica.mp3'), + 'instruments/1-piano/24.mp3': require('!arraybuffer-loader!./assets/instruments/1-piano/24.mp3'), + 'instruments/1-piano/36.mp3': require('!arraybuffer-loader!./assets/instruments/1-piano/36.mp3'), + 'instruments/1-piano/48.mp3': require('!arraybuffer-loader!./assets/instruments/1-piano/48.mp3'), + 'instruments/1-piano/60.mp3': require('!arraybuffer-loader!./assets/instruments/1-piano/60.mp3'), + 'instruments/1-piano/72.mp3': require('!arraybuffer-loader!./assets/instruments/1-piano/72.mp3'), + 'instruments/1-piano/84.mp3': require('!arraybuffer-loader!./assets/instruments/1-piano/84.mp3'), + 'instruments/1-piano/96.mp3': require('!arraybuffer-loader!./assets/instruments/1-piano/96.mp3'), + 'instruments/1-piano/108.mp3': require('!arraybuffer-loader!./assets/instruments/1-piano/108.mp3'), + 'instruments/2-electric-piano/60.mp3': require('!arraybuffer-loader!./assets/instruments/2-electric-piano/60.mp3'), + 'instruments/3-organ/60.mp3': require('!arraybuffer-loader!./assets/instruments/3-organ/60.mp3'), + 'instruments/4-guitar/60.mp3': require('!arraybuffer-loader!./assets/instruments/4-guitar/60.mp3'), + 'instruments/5-electric-guitar/60.mp3': require( + '!arraybuffer-loader!./assets/instruments/5-electric-guitar/60.mp3' + ), + 'instruments/6-bass/36.mp3': require('!arraybuffer-loader!./assets/instruments/6-bass/36.mp3'), + 'instruments/6-bass/48.mp3': require('!arraybuffer-loader!./assets/instruments/6-bass/48.mp3'), + 'instruments/7-pizzicato/60.mp3': require('!arraybuffer-loader!./assets/instruments/7-pizzicato/60.mp3'), + 'instruments/8-cello/36.mp3': require('!arraybuffer-loader!./assets/instruments/8-cello/36.mp3'), + 'instruments/8-cello/48.mp3': require('!arraybuffer-loader!./assets/instruments/8-cello/48.mp3'), + 'instruments/8-cello/60.mp3': require('!arraybuffer-loader!./assets/instruments/8-cello/60.mp3'), + 'instruments/9-trombone/36.mp3': require('!arraybuffer-loader!./assets/instruments/9-trombone/36.mp3'), + 'instruments/9-trombone/48.mp3': require('!arraybuffer-loader!./assets/instruments/9-trombone/48.mp3'), + 'instruments/9-trombone/60.mp3': require('!arraybuffer-loader!./assets/instruments/9-trombone/60.mp3'), + 'instruments/10-clarinet/48.mp3': require('!arraybuffer-loader!./assets/instruments/10-clarinet/48.mp3'), + 'instruments/10-clarinet/60.mp3': require('!arraybuffer-loader!./assets/instruments/10-clarinet/60.mp3'), + 'instruments/11-saxophone/36.mp3': require('!arraybuffer-loader!./assets/instruments/11-saxophone/36.mp3'), + 'instruments/11-saxophone/60.mp3': require('!arraybuffer-loader!./assets/instruments/11-saxophone/60.mp3'), + 'instruments/11-saxophone/84.mp3': require('!arraybuffer-loader!./assets/instruments/11-saxophone/84.mp3'), + 'instruments/12-flute/60.mp3': require('!arraybuffer-loader!./assets/instruments/12-flute/60.mp3'), + 'instruments/12-flute/72.mp3': require('!arraybuffer-loader!./assets/instruments/12-flute/72.mp3'), + 'instruments/13-wooden-flute/60.mp3': require('!arraybuffer-loader!./assets/instruments/13-wooden-flute/60.mp3'), + 'instruments/13-wooden-flute/72.mp3': require('!arraybuffer-loader!./assets/instruments/13-wooden-flute/72.mp3'), + 'instruments/14-bassoon/36.mp3': require('!arraybuffer-loader!./assets/instruments/14-bassoon/36.mp3'), + 'instruments/14-bassoon/48.mp3': require('!arraybuffer-loader!./assets/instruments/14-bassoon/48.mp3'), + 'instruments/14-bassoon/60.mp3': require('!arraybuffer-loader!./assets/instruments/14-bassoon/60.mp3'), + 'instruments/15-choir/48.mp3': require('!arraybuffer-loader!./assets/instruments/15-choir/48.mp3'), + 'instruments/15-choir/60.mp3': require('!arraybuffer-loader!./assets/instruments/15-choir/60.mp3'), + 'instruments/15-choir/72.mp3': require('!arraybuffer-loader!./assets/instruments/15-choir/72.mp3'), + 'instruments/16-vibraphone/60.mp3': require('!arraybuffer-loader!./assets/instruments/16-vibraphone/60.mp3'), + 'instruments/16-vibraphone/72.mp3': require('!arraybuffer-loader!./assets/instruments/16-vibraphone/72.mp3'), + 'instruments/17-music-box/60.mp3': require('!arraybuffer-loader!./assets/instruments/17-music-box/60.mp3'), + 'instruments/18-steel-drum/60.mp3': require('!arraybuffer-loader!./assets/instruments/18-steel-drum/60.mp3'), + 'instruments/19-marimba/60.mp3': require('!arraybuffer-loader!./assets/instruments/19-marimba/60.mp3'), + 'instruments/20-synth-lead/60.mp3': require('!arraybuffer-loader!./assets/instruments/20-synth-lead/60.mp3'), + 'instruments/21-synth-pad/60.mp3': require('!arraybuffer-loader!./assets/instruments/21-synth-pad/60.mp3') };