Skip to content

Instantly share code, notes, and snippets.

@swashcap
Last active June 3, 2016 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swashcap/5584b8ae3b2abbfc0ef331478f0ea44e to your computer and use it in GitHub Desktop.
Save swashcap/5584b8ae3b2abbfc0ef331478f0ea44e to your computer and use it in GitHub Desktop.
Directory Parsing Exercise

Instructions

Background

An API endpoint represents a directory structure of “cool images:” http://mrn-creed.cloudant.com/cool-images/3f6adfcb7cb3d0117e14062810715ece

Files look like:

{
  "name": "dance", // {string} file’s name
  "ext": "gif", // {string} file’s extension
  "size": 615 // {number} file’s approximate size in kB
}

Directories look like:

{
  "name": "favorite-images" // {string} directory’s name
  "files": [ // {Object[]} collection of file/directory objects
    // ...
  ]
}

Tasks

Create a program that downloads and parses the data. It should output the following:

  1. List of names of all GIFs and JPEGs, including extensions
  2. List of the names of the largest and smallest file, including their size and extension
  3. List the names of all GIFs greater than 500 in size, including their path and extension

Bonus: create a tool that outputs the visual structure of the directories (like tree).

{
"_id": "look-at-me-im-couch-db",
"_rev": "2",
"data": [{
"name": "cats",
"files": [{
"name": "bueno",
"ext": "jpeg",
"size": 88
}, {
"name": "catrusii",
"ext": "gif",
"size": 1844
}, {
"name": "cat-iron",
"ext": "png",
"size": 225
}]
}, {
"name": "dogs",
"files": [{
"name": "noidea-professor",
"ext": "jpg",
"size": 40
}, {
"name": "noidea",
"ext": "jpg",
"size": 55
}, {
"name": "partygod",
"ext": "gif",
"size": 596
}]
}, {
"name": "picardpalm",
"ext": "jpg",
"size": 57
}, {
"name": "best-gifs",
"files": [{
"name": "swag",
"ext": "gif",
"size": 447
}, {
"name": "out",
"ext": "gif",
"size": 481
}, {
"name": "secrets",
"files": [{
"name": "sigh",
"ext": "gif",
"size": 926
}, {
"name": "pie",
"ext": "jpg",
"size": 88
}, {
"name": "jimmy-cage",
"ext": "tiff",
"size": 1055
}]
}]
}]
}
'use strict';
/**
* Directory parsing exercise test.
*
* Tests for @{link https://gist.github.com/swashcap/5584b8ae3b2abbfc0ef331478f0ea44e}
*
* *Warning!* This script executes a passed file. This could potentially mess
* with your system! Examine the executable and make sure it doesn't do anything
* fishy.
*
* @example
* node test.js ./path/to/executable.js
*/
const assert = require('assert');
const spawn = require('child_process').spawn;
const executablePath = process.argv[2];
const command = spawn(executablePath);
/**
* Test 1 output.
*
* Collection of output strings for the first test, “list all JPEGs and GIFs
* with extensions.”
*
* @const {string[]}
*/
const test1Output = [
'bueno.jpeg',
'catrusii.gif',
'noidea-professor.jpg',
'noidea.jpg',
'partygod.gif',
'picardpalm.jpg',
'swag.gif',
'out.gif',
'sigh.gif',
'pie.jpg',
];
/**
* Test 2 output.
*
* Collection of two-dimensional arrays containing file name and size (in that
* order). for the second test, “list largest and smallest images.”
*
* @const {Array[]}
*/
const test2Output = [
['catrussi.gif', 1844],
['noidea-professor.jpg', 40],
];
/**
* Test 3 output.
*
* Collection of output strings for the third test, “list full paths and
* extensions of GIFs >500 kB in size.”
*
* @const {string[]}
*/
const test3Output = [
'cats/catrusii.gif',
'dogs/partygod.gif',
'best-gifs/secrets/sigh.gif',
];
let errorData;
let outData;
command.stdout.on('data', data => {
outData += data.toString();
});
command.stderr.on('data', data => {
errorData += data.toString();
});
command.on('close', (code) => {
assert.equal(code, 0, 'exits with non-error code');
assert.ok(!errorData, 'doesn’t output to stderr');
const outPieces = outData.trim().split('\n\n');
assert.ok(
test1Output.every(f => outPieces[0].indexOf(f) > -1),
'Test 1 contains every GIF and JPEG'
);
assert.ok(
test2Output.every(set => outPieces[1].indexOf(set[0]) > -1),
'Test 2 contains biggest and smallest image'
);
assert.ok(
test2Output.every(set => {
return outPieces[1].split('\n').some(line => {
return line.indexOf(set[0]) > -1 && line.indexOf(set[1]) > -1;
});
}),
'Test 2 outputs file sizes'
);
assert.ok(
test3Output.every(f => outPieces[2].indexOf(f) > -1),
'Test 3 contains every GIF >500'
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment