https://github.com/michaelzoidl/swap-array
Swaps the index / position of an array
https://github.com/michaelzoidl/swap-array
Last synced: 6 months ago
JSON representation
Swaps the index / position of an array
- Host: GitHub
- URL: https://github.com/michaelzoidl/swap-array
- Owner: michaelzoidl
- License: mit
- Created: 2016-04-20T06:22:04.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2022-01-06T03:47:02.000Z (over 4 years ago)
- Last Synced: 2025-02-02T00:32:00.577Z (about 1 year ago)
- Language: JavaScript
- Homepage: http://npmjs.com/package/swap-array
- Size: 7.81 KB
- Stars: 7
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-micro-npm-packages - array-swap - Swap position of two items in an array. (Modules / Array)
- awesome-micro-npm-packages-zh - array-swap - 在数组中交换两个值的位置. (模块 / 数组)
- awesome-micro-npm-packages - array-swap - Swap position of two items in an array. (Modules / Array)
- awesome-micro-npm-packages - array-swap - Swap position of two items in an array. (Modules / Array)
README
# Swap Array
Swap position of two items in array without changing the state of the passed array.
[](https://travis-ci.org/michaelzoidl/swap-array)
[](https://david-dm.org/michaelzoidl/babel-root-import#info=devDependencies)
## Install
```
$ npm install swap-array --save
```
## Usage
```js
import SwapArray from 'swap-array';
var SomeArray = ['thats','cool','dude'];
SwapArray(SomeArray, 0, 2);
// ['dude','thats','cool'];
```
## Does it change the passed array?
Nope, before it changes the positioning we clone the passed array and return just the new instance. Ideal for e.g. some redux-reducer where it's not allowed to change an existing state. If you want that the origin-array changes his state checkout the [array-swap](https://github.com/Marko-Jankovic/array-swap) repo from Marko Jankovic.
## FAQ
### How to import/require with old ES5 standard
Just import it usually but attend the default prop
```javascript
var SwapArray = require('swapt-array').default;
```
## Benchmark
```
1,149,543 op/s
```
## Contribute
```shell
// Run and Watch tests
$ npm run test -- -w
// Just run tests
$ npm run test
// Create new build
$ npm run build
// Run benchmark
$ npm run bench
```