move-sort
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

move-sort Tests

Switch an array item's index and get a new sorted array.

Install

$ npm install move-sort --save

or include it in a <script> tag, hosted by unpkg.

<script src="//unpkg.com/move-sort" />

Usage

import moveSort from 'move-sort'

const items = ['a', 'b', 'c', 'd', 'e']
const sortedItems = moveSort(items, 4, 0)

console.log(sortedItems) // returns ["e", "a", "b", "c", "d"]

Note: move-sort always returns a new array, and does not mutate the original array you pass into it.

API

moveSort(items, startIndex, endIndex)

items

Type: T extends any[]

The original array you want to generate a sorted array for.

startIndex

Type: number

The index of the inital element in the array you want to move.

endIndex

Type: number

The target index of the inital element in the array you want to move it to.

Index constraints

The start and end index arguments are clamped to the bounds of the items array. Indexes less than 0 will become 0 and indexes greater than (items.length - 1) will become (items.length - 1).

Development

# To run the tests
$ pnpm test
# or
$ pnpm run test:watch

# To publish the dist files
$ pnpm run build

License

MIT © Collin Henderson

Readme

Keywords

none

Package Sidebar

Install

npm i move-sort

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

4.59 kB

Total Files

8

Last publish

Collaborators

  • syropian