array-transfer
v0.1.1
Published
Immutably transfer value from an array to another array
Downloads
3
Readme
array-transfer
Immutably transfer value from an array to another array
Installation
npm i array-transfer --save
Usage
const transfer = require('array-transfer');
const {source, destination} = transfer(['rupee', 'doggie'], ['macky'], 1, 0);
console.log(source, destination) // ['rupee'], ['doggie', 'macky']
Without ES6 destructuring
If you're within a project that doesn't use a transpiler like Babel, and would like to support legacy browsers, no worries!
const set = transfer(['rupee', 'doggie'], ['macky'], 1, 0);
console.log(set.source, set.destination) // ['rupee'], ['doggie', 'macky']
transfer(src, dest, start, end)
src
- The array where the value will be taken fromdest
- The array where the value will be transferred tostart
- Which index insrc
the value will be taken fromend
- Which index inend
the value will be taken to
Use case
This is a typical use case for Trello-like apps where you want to move a task from its original list to another list. You may also find this useful if you're dealing with tree nodes where you want to transfer a node to another parent.
Running tests
npm test