Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/syropian/move-sort
Switch an array item's index and get a new sorted array.
https://github.com/syropian/move-sort
Last synced: 22 days ago
JSON representation
Switch an array item's index and get a new sorted array.
- Host: GitHub
- URL: https://github.com/syropian/move-sort
- Owner: syropian
- License: mit
- Created: 2018-04-06T00:07:06.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2022-10-02T19:59:25.000Z (over 2 years ago)
- Last Synced: 2024-12-01T14:08:40.130Z (about 1 month ago)
- Language: TypeScript
- Size: 81.1 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# move-sort [![Tests](https://github.com/syropian/move-sort/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/syropian/move-sort/actions/workflows/test.yml)
> Switch an array item's index and get a new sorted array.
## Install
```
$ npm install move-sort --save
```**or** include it in a `` tag, hosted by [unpkg](https://unpkg.com).
```js
<script src="https://unpkg.com/move-sort/dist/move-sort.iife.js" />
```## Usage
```ts
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
```bash
# To run the tests
$ pnpm test
# or
$ pnpm run test:watch# To publish the dist files
$ pnpm run build
```## License
MIT © [Collin Henderson](https://github.com/syropian)