Skip to content

Commit

Permalink
fix(analyze): Resolve issues with block and sprite counts
Browse files Browse the repository at this point in the history
Resolves issue where sprite counts were inflated due to global variables and lists as well as an
issue where block counts were deflated due to improper handling of nested block structures

GH-10
  • Loading branch information
thisandagain committed May 12, 2017
1 parent dd72cea commit bf56efd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
24 changes: 22 additions & 2 deletions lib/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ function extract (project, attribute, id, hash) {
return result;
}

/**
* Extract number of sprites from a project object. Will attempt to ignore
* "children" which are not sprites.
*
* @param {Object} input Project object
*
* @return {Object} Sprite information
*/
function sprites (input) {
var result = 0;

for (var i in input.children) {
if (input.children[i].hasOwnProperty('spriteInfo')) result++;
}

return { count: result };
}

/**
* Tallys term frequency from an array of strings.
*
Expand Down Expand Up @@ -118,7 +136,7 @@ function blocks (project) {
if (stack[i][0] === 'procDef') continue;

// Move to next item and walk
walk(stack[i][0].slice(1));
walk(stack[i].slice(1));
}
}
walk(flatten(project, 'scripts'));
Expand Down Expand Up @@ -169,7 +187,6 @@ function extensions (project) {
module.exports = function (project, callback) {
// Create metadata object
var meta = {
sprites: extract(project, 'children'),
scripts: extract(project, 'scripts'),
variables: extract(project, 'variables', 'name'),
lists: extract(project, 'lists', 'listName'),
Expand All @@ -178,6 +195,9 @@ module.exports = function (project, callback) {
costumes: extract(project, 'costumes', 'costumeName', 'baseLayerMD5')
};

// Sprites
meta.sprites = sprites(project);

// Blocks
meta.blocks = blocks(project);

Expand Down
Binary file modified test/fixtures/data/_example.sb2
Binary file not shown.
26 changes: 23 additions & 3 deletions test/unit/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,30 @@ test('empty project', function (t) {
});

test('example project', function (t) {
analyze(JSON.parse(data.example.json.toString()), function (err, project) {
analyze(JSON.parse(data.example.json.toString()), function (err, res) {
t.equal(err, null);
t.type(project, 'object');
t.type(project._meta, 'object');
t.type(res, 'object');
t.type(res._meta, 'object');
t.type(res._meta.sprites, 'object');
t.type(res._meta.scripts, 'object');
t.type(res._meta.variables, 'object');
t.type(res._meta.lists, 'object');
t.type(res._meta.comments, 'object');
t.type(res._meta.sounds, 'object');
t.type(res._meta.costumes, 'object');
t.type(res._meta.blocks, 'object');
t.type(res._meta.extensions, 'object');

t.equal(res._meta.sprites.count, 2, 'expected number of sprites');
t.equal(res._meta.scripts.count, 5, 'expected number of scripts');
t.equal(res._meta.variables.count, 2, 'expected number of variables');
t.equal(res._meta.lists.count, 2, 'expected number of lists');
t.equal(res._meta.comments.count, 1, 'expected number of comments');
t.equal(res._meta.sounds.count, 4, 'expected number of sounds');
t.equal(res._meta.costumes.count, 16, 'expected number of costumes');
t.equal(res._meta.blocks.count, 16, 'expected number of blocks');
t.equal(res._meta.blocks.unique, 11, 'exepected number of blocks');
t.equal(res._meta.extensions.count, 1, 'expected number of extensions');

t.end();
});
Expand Down

0 comments on commit bf56efd

Please sign in to comment.