or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-1/

Directory Content Lister

Build a utility that reads and returns the contents of directories in an in-memory filesystem.

Requirements

Implement two functions that work with an in-memory filesystem instance:

  1. listDirectoryContents(fs, dirPath) - Returns an array of entry names (files and subdirectories) from the specified directory path
  2. countEntriesInDirectory(fs, dirPath) - Returns the total count of entries in the specified directory path

Both functions should:

  • Accept a filesystem instance and a directory path as parameters
  • Handle non-existent directories appropriately (let errors propagate naturally)

Test Cases

  • Given an empty directory "/test-empty", listing its contents returns an empty array @test
  • Given a directory "/test-files" containing 3 files (c.txt, a.txt, b.txt), listing returns all three filenames @test
  • Given a directory "/test-mixed" with files (file.txt, another.txt) and a subdirectory (subdir), listing returns all three entry names @test
  • Counting entries in a directory with 4 items returns 4 @test
  • Attempting to list a non-existent directory "/does-not-exist" throws an error @test

Implementation

@generates

API

/**
 * Lists all entries in the specified directory path.
 * @param {Object} fs - The memory filesystem instance
 * @param {string} dirPath - Path to the directory
 * @returns {string[]} Array of entry names sorted alphabetically
 * @throws {Error} Throws error if directory does not exist
 */
function listDirectoryContents(fs, dirPath) {
  // IMPLEMENTATION HERE
}

/**
 * Counts the number of entries in a directory.
 * @param {Object} fs - The memory filesystem instance
 * @param {string} dirPath - Path to the directory
 * @returns {number} Number of entries in the directory
 * @throws {Error} Throws error if directory does not exist
 */
function countEntriesInDirectory(fs, dirPath) {
  // IMPLEMENTATION HERE
}

module.exports = {
  listDirectoryContents,
  countEntriesInDirectory,
};

Dependencies { .dependencies }

metro-memory-fs { .dependency }

Provides in-memory filesystem operations for testing.

@satisfied-by