Skip to content

Commit bf56efd

Browse files
committed
fix(analyze): Resolve issues with block and sprite counts
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
1 parent dd72cea commit bf56efd

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

lib/analyze.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ function extract (project, attribute, id, hash) {
6969
return result;
7070
}
7171

72+
/**
73+
* Extract number of sprites from a project object. Will attempt to ignore
74+
* "children" which are not sprites.
75+
*
76+
* @param {Object} input Project object
77+
*
78+
* @return {Object} Sprite information
79+
*/
80+
function sprites (input) {
81+
var result = 0;
82+
83+
for (var i in input.children) {
84+
if (input.children[i].hasOwnProperty('spriteInfo')) result++;
85+
}
86+
87+
return { count: result };
88+
}
89+
7290
/**
7391
* Tallys term frequency from an array of strings.
7492
*
@@ -118,7 +136,7 @@ function blocks (project) {
118136
if (stack[i][0] === 'procDef') continue;
119137

120138
// Move to next item and walk
121-
walk(stack[i][0].slice(1));
139+
walk(stack[i].slice(1));
122140
}
123141
}
124142
walk(flatten(project, 'scripts'));
@@ -169,7 +187,6 @@ function extensions (project) {
169187
module.exports = function (project, callback) {
170188
// Create metadata object
171189
var meta = {
172-
sprites: extract(project, 'children'),
173190
scripts: extract(project, 'scripts'),
174191
variables: extract(project, 'variables', 'name'),
175192
lists: extract(project, 'lists', 'listName'),
@@ -178,6 +195,9 @@ module.exports = function (project, callback) {
178195
costumes: extract(project, 'costumes', 'costumeName', 'baseLayerMD5')
179196
};
180197

198+
// Sprites
199+
meta.sprites = sprites(project);
200+
181201
// Blocks
182202
meta.blocks = blocks(project);
183203

test/fixtures/data/_example.sb2

2.51 KB
Binary file not shown.

test/unit/analyze.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,30 @@ test('empty project', function (t) {
1212
});
1313

1414
test('example project', function (t) {
15-
analyze(JSON.parse(data.example.json.toString()), function (err, project) {
15+
analyze(JSON.parse(data.example.json.toString()), function (err, res) {
1616
t.equal(err, null);
17-
t.type(project, 'object');
18-
t.type(project._meta, 'object');
17+
t.type(res, 'object');
18+
t.type(res._meta, 'object');
19+
t.type(res._meta.sprites, 'object');
20+
t.type(res._meta.scripts, 'object');
21+
t.type(res._meta.variables, 'object');
22+
t.type(res._meta.lists, 'object');
23+
t.type(res._meta.comments, 'object');
24+
t.type(res._meta.sounds, 'object');
25+
t.type(res._meta.costumes, 'object');
26+
t.type(res._meta.blocks, 'object');
27+
t.type(res._meta.extensions, 'object');
28+
29+
t.equal(res._meta.sprites.count, 2, 'expected number of sprites');
30+
t.equal(res._meta.scripts.count, 5, 'expected number of scripts');
31+
t.equal(res._meta.variables.count, 2, 'expected number of variables');
32+
t.equal(res._meta.lists.count, 2, 'expected number of lists');
33+
t.equal(res._meta.comments.count, 1, 'expected number of comments');
34+
t.equal(res._meta.sounds.count, 4, 'expected number of sounds');
35+
t.equal(res._meta.costumes.count, 16, 'expected number of costumes');
36+
t.equal(res._meta.blocks.count, 16, 'expected number of blocks');
37+
t.equal(res._meta.blocks.unique, 11, 'exepected number of blocks');
38+
t.equal(res._meta.extensions.count, 1, 'expected number of extensions');
1939

2040
t.end();
2141
});

0 commit comments

Comments
 (0)