diff --git a/lib/sb2.js b/lib/sb2.js index 9902514..73ae754 100644 --- a/lib/sb2.js +++ b/lib/sb2.js @@ -1,3 +1,5 @@ +const utility = require('./utility'); + /** * Returns an array of items matching the specified attribute. * @param {object} project Project object (SB2 format) @@ -81,23 +83,6 @@ const sprites = function (input) { return {count: result}; }; -/** - * Tallys term frequency from an array of strings. - * @param {array} input Array of strings - * @return {object} Frequency information - */ -const frequency = function (input) { - const result = Object.create(null); - - for (let i in input) { - var term = input[i]; - if (typeof result[term] === 'undefined') result[term] = 0; - result[term]++; - } - - return result; -}; - /** * Extracts all blocks and generates a frequency count. * @param {object} project Project object (SB2 format) @@ -136,7 +121,7 @@ const blocks = function (project) { walk(flatten(project, 'scripts')); // Generate frequency count - const freq = frequency(result); + const freq = utility.frequency(result); // Build result and return return { diff --git a/lib/sb3.js b/lib/sb3.js index e4c2403..ba7b9db 100644 --- a/lib/sb3.js +++ b/lib/sb3.js @@ -9,7 +9,7 @@ const scripts = function (targets) { for (let b in targets[t].blocks) { if (targets[t].blocks[b].topLevel) occurances++; } - }; + } return { count: occurances @@ -27,7 +27,7 @@ const variables = function (targets, attribute) { occurances++; idList.push(variable[0]); } - }; + } return { count: occurances, @@ -49,7 +49,7 @@ const extract = function (targets, attribute, id, hash) { if (typeof id !== 'undefined') idList.push(asset[id]); if (typeof hash !== 'undefined') hashList.push(asset[hash]); } - }; + } const result = {count: occurances}; if (typeof id !== 'undefined') result.id = idList; @@ -61,7 +61,7 @@ const sprites = function (targets) { return { count: targets.length - 1 }; -} +}; const blocks = function (targets) { // Storage object @@ -86,10 +86,10 @@ const blocks = function (targets) { }; }; -const extensions = function (extensions) { +const extensions = function (list) { return { - count: extensions.length, - id: extensions + count: list.length, + id: list }; }; diff --git a/lib/utility.js b/lib/utility.js index 952b210..6ef2bb4 100644 --- a/lib/utility.js +++ b/lib/utility.js @@ -17,7 +17,7 @@ class Utility { } return result; - }; + } } module.exports = Utility; diff --git a/test/unit/sb2.js b/test/unit/sb2.js index f6ab269..261211c 100644 --- a/test/unit/sb2.js +++ b/test/unit/sb2.js @@ -189,60 +189,60 @@ test('complex (binary)', t => { t.equal(result.blocks.count, 34); t.equal(result.blocks.unique, 18); t.deepEqual(result.blocks.id, [ - "whenGreenFlag", - "doForever", - "changeGraphicEffect:by:", - "whenGreenFlag", - "deleteLine:ofList:", - "deleteLine:ofList:", - "doForever", - "forward:", - "turnRight:", - "randomFrom:to:", - "bounceOffEdge", - "whenGreenFlag", - "doForever", - "setGraphicEffect:to:", - "xpos", - "whenGreenFlag", - "doForever", - "call", - "randomFrom:to:", - "heading", - "randomFrom:to:", - "heading", - "procDef", - "setVar:to:", - "getParam", - "setVar:to:", - "getParam", - "append:toList:", - "getParam", - "append:toList:", - "getParam", - "LEGO WeDo 2.0\u001FwhenTilted", - "LEGO WeDo 2.0\u001FsetLED", - "randomFrom:to:" + 'whenGreenFlag', + 'doForever', + 'changeGraphicEffect:by:', + 'whenGreenFlag', + 'deleteLine:ofList:', + 'deleteLine:ofList:', + 'doForever', + 'forward:', + 'turnRight:', + 'randomFrom:to:', + 'bounceOffEdge', + 'whenGreenFlag', + 'doForever', + 'setGraphicEffect:to:', + 'xpos', + 'whenGreenFlag', + 'doForever', + 'call', + 'randomFrom:to:', + 'heading', + 'randomFrom:to:', + 'heading', + 'procDef', + 'setVar:to:', + 'getParam', + 'setVar:to:', + 'getParam', + 'append:toList:', + 'getParam', + 'append:toList:', + 'getParam', + 'LEGO WeDo 2.0\u001FwhenTilted', + 'LEGO WeDo 2.0\u001FsetLED', + 'randomFrom:to:' ]); t.deepEqual(result.blocks.frequency, { - "LEGO WeDo 2.0\u001FsetLED": 1, - "LEGO WeDo 2.0\u001FwhenTilted": 1, - "bounceOffEdge": 1, - "call": 1, - "changeGraphicEffect:by:": 1, - "doForever": 4, - "deleteLine:ofList:": 2, - "forward:": 1, - "getParam": 4, - "heading": 2, - "procDef": 1, - "append:toList:": 2, - "randomFrom:to:": 4, - "setGraphicEffect:to:": 1, - "setVar:to:": 2, - "turnRight:": 1, - "whenGreenFlag": 4, - "xpos": 1 + 'LEGO WeDo 2.0\u001FsetLED': 1, + 'LEGO WeDo 2.0\u001FwhenTilted': 1, + 'bounceOffEdge': 1, + 'call': 1, + 'changeGraphicEffect:by:': 1, + 'doForever': 4, + 'deleteLine:ofList:': 2, + 'forward:': 1, + 'getParam': 4, + 'heading': 2, + 'procDef': 1, + 'append:toList:': 2, + 'randomFrom:to:': 4, + 'setGraphicEffect:to:': 1, + 'setVar:to:': 2, + 'turnRight:': 1, + 'whenGreenFlag': 4, + 'xpos': 1 }); t.type(result.extensions, 'object'); diff --git a/test/unit/sb3.js b/test/unit/sb3.js index 0b8bf1f..8edf126 100644 --- a/test/unit/sb3.js +++ b/test/unit/sb3.js @@ -193,60 +193,60 @@ test('complex (binary)', t => { t.equal(result.blocks.count, 34); t.equal(result.blocks.unique, 18); t.deepEqual(result.blocks.id, [ - "event_whenflagclicked", - "control_forever", - "looks_changeeffectby", - "event_whenflagclicked", - "data_deleteoflist", - "data_deleteoflist", - "control_forever", - "motion_movesteps", - "motion_turnright", - "operator_random", - "motion_ifonedgebounce", - "event_whenflagclicked", - "control_forever", - "looks_seteffectto", - "motion_xposition", - "event_whenflagclicked", - "control_forever", - "procedures_call", - "operator_random", - "motion_direction", - "operator_random", - "motion_direction", - "procedures_definition", - "data_setvariableto", - "argument_reporter_string_number", - "data_setvariableto", - "argument_reporter_string_number", - "data_addtolist", - "argument_reporter_string_number", - "data_addtolist", - "argument_reporter_string_number", - "wedo2_whenTilted", - "wedo2_setLightHue", - "operator_random" + 'event_whenflagclicked', + 'control_forever', + 'looks_changeeffectby', + 'event_whenflagclicked', + 'data_deleteoflist', + 'data_deleteoflist', + 'control_forever', + 'motion_movesteps', + 'motion_turnright', + 'operator_random', + 'motion_ifonedgebounce', + 'event_whenflagclicked', + 'control_forever', + 'looks_seteffectto', + 'motion_xposition', + 'event_whenflagclicked', + 'control_forever', + 'procedures_call', + 'operator_random', + 'motion_direction', + 'operator_random', + 'motion_direction', + 'procedures_definition', + 'data_setvariableto', + 'argument_reporter_string_number', + 'data_setvariableto', + 'argument_reporter_string_number', + 'data_addtolist', + 'argument_reporter_string_number', + 'data_addtolist', + 'argument_reporter_string_number', + 'wedo2_whenTilted', + 'wedo2_setLightHue', + 'operator_random' ]); t.deepEqual(result.blocks.frequency, { - "argument_reporter_string_number": 4, - "control_forever": 4, - "data_addtolist": 2, - "data_deleteoflist": 2, - "data_setvariableto": 2, - "event_whenflagclicked": 4, - "looks_changeeffectby": 1, - "looks_seteffectto": 1, - "motion_direction": 2, - "motion_ifonedgebounce": 1, - "motion_movesteps": 1, - "motion_turnright": 1, - "motion_xposition": 1, - "operator_random": 4, - "procedures_call": 1, - "procedures_definition": 1, - "wedo2_setLightHue": 1, - "wedo2_whenTilted": 1 + argument_reporter_string_number: 4, + control_forever: 4, + data_addtolist: 2, + data_deleteoflist: 2, + data_setvariableto: 2, + event_whenflagclicked: 4, + looks_changeeffectby: 1, + looks_seteffectto: 1, + motion_direction: 2, + motion_ifonedgebounce: 1, + motion_movesteps: 1, + motion_turnright: 1, + motion_xposition: 1, + operator_random: 4, + procedures_call: 1, + procedures_definition: 1, + wedo2_setLightHue: 1, + wedo2_whenTilted: 1 }); t.type(result.extensions, 'object');