Immutably transfer value from an array to another array
If both source and destination array are the same (e.g., you're moving an item in the same array), please use array-move
instead.
npm i array-transfer --save
const transfer = require('array-transfer');
const {source, destination} = transfer(['rupee', 'doggie'], ['macky'], 1, 0);
console.log(source, destination) // ['rupee'], ['doggie', 'macky']
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']
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
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.
npm test