# @overlook/router-ordered

> Overlook framework ordered router

Latest version **0.3.2** (published 2020-01-23) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @overlook/router-ordered
pnpm add @overlook/router-ordered
yarn add @overlook/router-ordered
bun add @overlook/router-ordered
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2020-01-23 |
| First published | 2019-05-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 0 |
| Unpacked size | 9.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Overlook Motel |
| Maintainers | overlookmotel |
| Keywords | overlook, overlook-router, ordered |

## Links

- npm: https://www.npmjs.com/package/@overlook/router-ordered
- Repository: https://github.com/overlookjs/plugin-ordered
- Homepage: https://github.com/overlookjs/plugin-ordered#readme
- Issues: https://github.com/overlookjs/plugin-ordered/issues
- npm.io page: https://npm.io/package/@overlook/router-ordered

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.3.2 (latest) — 2020-01-23
- 0.3.1 — 2019-06-01
- 0.3.0 — 2019-05-31
- 0.2.3 — 2019-05-27
- 0.2.2 — 2019-05-27
- 0.2.1 — 2019-05-27
- 0.2.0 — 2019-05-26
- 0.1.1 — 2019-05-26
- 0.1.0 — 2019-05-26

## README

[![NPM version](https://img.shields.io/npm/v/@overlook/router-ordered.svg)](https://www.npmjs.com/package/@overlook/router-ordered)

# DEPRECATED

This package has been renamed [@overlook/plugin-ordered](https://www.npmjs.com/package/@overlook/plugin-ordered). Please use that instead.

# Overlook framework ordered router

Part of the [Overlook framework](https://overlookjs.github.io/).

## Abstract

Route class extension for routes which need to be in a certain order relative to their siblings in their mutual parent's array of children.

e.g. For path-matching routes, `/photos/new` needs to be before `/photos/:id` so it gets a chance to be matched first.

## Usage

### Where to use it

This extension should be on the routes which need to be ordered, not the parent containing them. i.e. on `/photos/new` and `/photos/:id`, not `/photos`.

### Defining order

Each route can say that it needs to be before or after any other of its siblings.

It can do this by extending the `[IS_BEFORE]()` method.

`[IS_BEFORE]()` will be called with each of the route's siblings. It can return:

* `true` if needs to be before that sibling
* `false` if needs to be after that sibling
* `null` if no preference

The default `[IS_BEFORE]()` method provided by the extension returns `null` (i.e. no preference).

```js
const Route = require('@overlook/route');
const orderedExtension = require('@overlook/router-ordered');
const {IS_BEFORE} = orderedExtension;
const RouteOrdered = Route.extend( orderedExtension );

class MyOrderedRoute extends RouteOrdered {
  [IS_BEFORE](sibling) {
    // If super method returns a result, use it
    const before = super[IS_BEFORE](sibling);
    if (before !== null) return before;

    // Sort in alphabetical order
    if (this.name === sibling.name) return null;
    return this.name < sibling.name ? true : false;
  }
}
```

### Conflicts

Ordering occurs in the `init` phase.

Every sibling will be asked where it wants to be relative to every other sibling.

Conflicts can occur if A says it's before B and B says it's before A, or there's a circular relationship (A before B, B before C, C before A).

In these cases an error will be thrown.

### Extending

The extension also exposes an `[ORDER]()` method.

If you want to take some action before/after ordering, extend this method.

NB After `[ORDER]()`, *this* route will be in correct order as per its preferences, but all its siblings are not neccesarily in their final order. It's possible a later sibling may switch positions with another sibling once it's `[ORDER]()` method has been called.

## Tests

Use `npm test` to run the tests. Use `npm run cover` to check coverage.

## Changelog

See [changelog.md](https://github.com/overlookjs/router-ordered/blob/master/changelog.md)

## Issues

If you discover a bug, please raise an issue on Github. https://github.com/overlookjs/router-ordered/issues

## Contribution

Pull requests are very welcome. Please:

* ensure all tests pass before submitting PR
* add tests for new features
* document new functionality/API additions in README
* do not add an entry to Changelog (Changelog is created when cutting releases)

---
_Source: https://npm.io/package/@overlook/router-ordered · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
