lstat-dir

Run `fs.lstat()` for all contents in a given directory

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
lstat-dir
402.1.14 years ago6 years agoMinified + gzip package size for lstat-dir in KB

Readme

lstat-dir
npm version Build Status Coverage Status
Run fs.lstat() for all contents in a given directory
const lstatDir = require('lstat-dir');

(async () => {
  await lstatDir('node_modules/lstat-dir'); /*=> Map {
    '/Users/example/node_modules/lstat-dir/LICENSE' => {mode: 33188, size: 1086, ...},
    '/Users/example/node_modules/lstat-dir/README.md' => {mode: 33188, size: 2060, ...}
    '/Users/example/node_modules/lstat-dir/index.js' => {mode: 33188, size: 124, ...}
    '/Users/example/node_modules/lstat-dir/package.json' => {mode: 33188, size: 922, ...}
  } */
})();

Installation

Use npm.
npm install lstat-dir

API

const lstatDir = require('lstat-dir');

lstatDir(dir, options)

dir: string (directory path)
options: Object (readdir-sorted options)
Return: Promise<Map>
The returned Promise is fulfilled with a Map, whose keys are absolute paths of contents in the directory, and whose values are fs.Stats of contents.
/* my-dir
   ├── file.txt
   ├── symlink => file.txt
   └── tmp
       └── debug.log
*/

(async () => {
  const stats = await lstatDir('my-dir');

  stats instanceof Map; //=> true

  stats.keys(); /*=> MapIterator {
    '/Users/example/my-dir/file.txt',
    '/Users/example/my-dir/symlink',
    '/Users/example/my-dir/tmp'
  } */

  stats.get('/Users/example/my-dir/file.txt').isFile(); //=> true
  stats.get('/Users/example/my-dir/symlink').isSymbolicLink(); //=> true
  stats.get('/Users/example/my-dir/tmp').isDirectory()(); //=> true
})();

Options are directly passed to the underlying readdir-sorted to control the order of keys.
(async() => {
  [...(await lstatDir('/path/dir')).keys()];
  // => ['/path/dir/10.txt', '/path/dir/2.txt', '/path/dir/ä.txt', '/path/dir/z.txt']

  [...(await lstatDir('/path/dir', {
    locale: 'sv',
    numeric: true
  })).keys()];
  //=> ['/path/dir/2.txt', '/path/dir/10.txt', '/path/dir/z.txt', '/path/dir/ä.txt']
})();

License

ISC License © 2017 - 2018 Shinnosuke Watanabe