# johnson-trotter

> An implementation of the Johnson-Trotter permutation algorithm.

Latest version **0.1.1** (published 2015-03-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install johnson-trotter
pnpm add johnson-trotter
yarn add johnson-trotter
bun add johnson-trotter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2015-03-08 |
| First published | 2015-02-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Lim Yuan Qing |
| Maintainers | yuanqing |
| Keywords | array, combinations, combinatorics, iterate, iterator, permutate, permutation, permute, step, swap |

## Links

- npm: https://www.npmjs.com/package/johnson-trotter
- Repository: https://github.com/yuanqing/johnson-trotter
- Issues: https://github.com/yuanqing/johnson-trotter/issues
- npm.io page: https://npm.io/package/johnson-trotter

## Recent versions

- 0.1.1 (latest) — 2015-03-08
- 0.1.0 — 2015-02-23
- 0.0.0 — 2015-02-23

## README

# johnson-trotter.js [![npm Version](http://img.shields.io/npm/v/johnson-trotter.svg?style=flat)](https://npmjs.com/package/johnson-trotter) [![Build Status](https://img.shields.io/travis/yuanqing/johnson-trotter.svg?branch=master&style=flat)](https://travis-ci.org/yuanqing/johnson-trotter) [![Coverage Status](https://img.shields.io/coveralls/yuanqing/johnson-trotter.svg?style=flat)](https://coveralls.io/r/yuanqing/johnson-trotter)

> An implementation of the [Johnson-Trotter](http://en.wikipedia.org/wiki/Steinhaus-Johnson-Trotter_algorithm) permutation algorithm.

## Usage

To iterate over the permutations of a given array, simply do:

```js
var permute = require('johnson-trotter');

var arr = ['foo', 'bar', 'baz'];

var iter = permute(arr);
while (iter.hasNext()) {
  iter.next();
  //=> ['foo', 'bar', 'baz']
  //=> ['foo', 'baz', 'bar']
  //=> ['baz', 'foo', 'bar']
  //=> ['baz', 'bar', 'foo']
  //=> ['bar', 'baz', 'foo']
  //=> ['bar', 'foo', 'baz']
}
```

Note that:

- Permutations are generated on-the-fly on every call to `next`.
- Each permutation is a *new* array containing elements of the original array. The original array (ie. `arr`, in our example) is unmodified.

## API

```js
var permute = require('johnson-trotter');
```

### var iter = permute(arr)

Initialises the iterator.

- `arr` &mdash; The array to generate permutations for.

### iter.hasNext()

Returns `true` if there are more permutations in the iteration.

### iter.next()

Returns the next permutation in the iteration, or `null` if there are no more permutations in the iteration. The returned array is a *new* array containing elements of the original array.

### iter.reset()

Resets the iterator.

## Installation

Install via [npm](https://npmjs.com):

```
$ npm i --save johnson-trotter
```

## Changelog

- 0.1.0
  - Initial release

## License

[MIT](https://github.com/yuanqing/johnson-trotter/blob/master/LICENSE)

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