diff --git a/lib/analyze.js b/lib/analyze.js index 044ab567..3db958f1 100644 --- a/lib/analyze.js +++ b/lib/analyze.js @@ -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. * @@ -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')); @@ -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'), @@ -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); diff --git a/test/fixtures/data/_example.sb2 b/test/fixtures/data/_example.sb2 index 9fa2631d..7d207d04 100644 Binary files a/test/fixtures/data/_example.sb2 and b/test/fixtures/data/_example.sb2 differ diff --git a/test/unit/analyze.js b/test/unit/analyze.js index 9c030b44..561cc95a 100644 --- a/test/unit/analyze.js +++ b/test/unit/analyze.js @@ -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(); });