Giter Club home page Giter Club logo

readdir-sorted's Introduction

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

readdir-sorted's People

Contributors

shinnn avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.