0.0.1 • Published 6 years ago

array-hand v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

array-hand

A hand for array manipulation, in a functional way.

const ah = require("array-hand");

No need to memorize Slice/Splice differences any more:

Cropping into a sub-array:

const squares = [1, 4, 9, 16, 25];

ah(squares)
  .startAt(2)   // start index.
  .endAt()      // end index.
  .crop()       // crop into a new array.
  .values()     // output: [9, 16, 25]
                // `squares` remain [1, 4, 9, 16, 25]

Erase part of the array:

ah(squares)
  .startAt(2)   // start index
  .select(1)    // select number of element(s)
  .erase()      // erase matching element(s)
  .values()     // output: [1, 4, 16, 25]

Insert new element(s):

ah(squares)
  .startAt(2)   // start index
  .insert([42]) // insert a new array to insert new element(s)
  .values()     // output: [1, 4, 9, 42, 16, 25]

Insert before an element

ah(squares)
  .startAt(0)   // start index, set to the head of array
  .insertBefore([42])  
                // insert a new element before start index
  .values()     // output: [42, 1, 4, 9, 16, 25]

Replace part of the array:

ah(squares)
  .startAt(2)   // start index
  .select(2)    // select number of element(s)
  .replaceEach(42)  // replace each element with a new value
  .values()     // output: [1, 4, 42, 42, 25]             

Return detailed result:

ah(squares)
  .startAt(2)   // start index
  .select(2)    // select number of element(s)
  .replace([42, 42])  // replace matching sub-array with a new one
  .details()    // output: an object with details

/**
{
  replaced: [9, 16],
  values: [1, 4, 42, 42, 25]
}
*/

should return error:

ah(squares)
  .startAt(0)   // start index, set to the head of array
  .select(2) 
  .insert([42]) // insert/erase/replace will change its structure. these two methods should clear selection. SHOULD WARN OF NO SELECTION
  .erase()
  .values()     // output: [42, 1, 4, 9, 16, 25]