Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

readdir-sorted

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

readdir-sorted

Like `fs.promise.readdir()`, but sorts the result based on `String#localeCompare()`

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
261
decreased by-1.51%
Maintainers
1
Weekly downloads
 
Created
Source

readdir-sorted

npm version Build Status codecov

Like fs.promise.readdir() but sorts the result based on String#localeCompare()

const readdirSorted = require('readdir-sorted');

(async () => {
  await readdirSorted('.'); /* => [
    '.editorconfig',
    '.gitattributes',
    '.gitignore',
    '.travis.yml',
    'index.js',
    'LICENSE',
    'package.json',
    'README.md',
    'test'
  ] */
})();

Installation

Use npm.

npm install readdir-sorted

API

const readdirSorted = require('readdir-sorted');

readdirSorted(path [, options])

path: string | Buffer | URL (directory path)
options: Object
Return: Promise<string[]|Buffer[]|fs.Dirent[]>

It returns a result of fs.promises.readdir() with sorting entries based on String#localeCompare().

Options
options.encoding and options.withFileTypes

Both will be passed to fs.promises.readdir().

(async () => {
  await readdirSorted('example');
  //=> ['directory', 'file.txt', 'symlink']

  await readdirSorted('example', {
    encoding: 'base64',
    withFileTypes: true
  });
  /* => [
    Dirent { name: 'ZGlyZWN0b3J5', [Symbol(type)]: 2 },
    Dirent { name: 'aW5kZXguanM=', [Symbol(type)]: 1 },
    Dirent { name: 'c3ltbGluaw==', [Symbol(type)]: 3 }
  ] */
})();
Other options

locale property will be passed to the second argument of String#localeCompare(), and the rest will be used in the third argument.

(async () => {
  await readdirSorted('/path/to/dir');
  //=> ['10', '2', 'ä', 'z']

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

License

ISC License © 2017 - 2019 Watanabe Shinnosuke

Keywords

FAQs

Package last updated on 07 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc