JavaScript: List of files and directories

Last update: 26 Mar 23:20
0
Students

ls.js

Implement and export as default a function that takes a path (absolute or relative) and returns information about files and directories found there. The data is returned as an array of objects, each containing details on a single file: a path and permissions (stat.mode). The objects in the array must be sorted by file name.

Examples

import ls from '../ls.js';

await ls('/var');
// [
//   { filepath: '/var/local', mode: 17917 },
//   { filepath: '/var/lock', mode: 17407 },
//   { filepath: '/var/log', mode: 16877 },
// ];

await ls('/etc/passwd');
// [{ filepath: '/etc/passwd', mode: 33188 }];

await ls('../../../../etc/passwd');
// [{ filepath: '/etc/passwd', mode: 33188 }];

Along with processing directories, this function must be able to handle files as well. In this scenario, an array with one object – information about the current file — is returned.

Tips

  • readdir() reads directory
  • stat() provides a file info. isFile() checks if it's a file, mode gives a permission description
  • zipWith() is a helper function from lodash

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests