Skip to content

A pure function to swap the position of two elements in the array

License

Notifications You must be signed in to change notification settings

alferov/pure-swap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pure-swap

Build Status Dependency Status

A pure function to swap the position of two elements in the array

Installation

$ npm install pure-swap --save

Browser support

  • IE 9+
  • Chrome 5+
  • Opera 10.5+
  • Safari 5+
  • FireFox 4+

Usage

var pureSwap = require('pure-swap');
var array = [1,2,3,4];
pureSwap(array, 1, 3); // => [1,4,3,2]
pureSwap(array, 3, 1); // => [1,4,3,2]

// It doesn't modify the original array
console.log(array) // => [1,2,3,4]

API

pureSwap(data, indexToSwap, indexToBeSwapped)

A pure function to swap the position of two elements in the array

Parameters

  • Array array: An array of data
  • Number indexToSwap An index of array element to swap
  • Number indexToBeSwapped An index of array element to be swapped

Return

  • Array: Result

License

MIT © Philipp Alferov

About

A pure function to swap the position of two elements in the array

Resources

License

Stars

Watchers

Forks

Packages

No packages published