Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jridgewell/path-sorter

Sort paths, giving precedence to sibling, descendant, then parent directories
https://github.com/jridgewell/path-sorter

Last synced: about 2 months ago
JSON representation

Sort paths, giving precedence to sibling, descendant, then parent directories

Awesome Lists containing this project

README

        

# @jridgewell/path-sorter

> Sort paths, giving precedence to sibling, descendant, then parent directories

## Installation

```sh
npm install @jridgewell/path-sorter
```

## Usage

```js
import pathSorter from '@jridgewell/path-sorter';

const paths = [
'../foo/baz',
'../bar/baz',
'../foo',
'../bar',
'./foo/baz',
'./bar/baz',
'./foo',
'./bar',
'foo',
'bar',
];

const sorted = paths.sort(pathSorter);
assert.deepEqual(sorted, [
// Non-relative paths are prioritized first
'bar',
'foo',

// relative siblings paths are next, giving precedence
// _this_ directory, then 1-deep, then 2-deep, etc.
'./bar',
'./foo',
'./bar/baz',
'./foo/baz',

// parent directories are last,
'../bar',
'../foo',
'../bar/baz',
'../foo/baz',
]);
```