Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
NPM package to find a sequence of values in an Array and replace/remove them.
CoffeeScript
Tag: v0.2.3

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
src
test
.gitignore
README.md
package.json

README.md

array_surgeon

Find a consecutive group of elements in an array and remove or replace them.

Installation & Usage

shell> npm install array_surgeon

surgeon = require 'array_surgeon'

hay = [ 1, 2, 3, 4 ]

// You can use a regular array
surgeon(hay).remove [ 2, 3 ]
# ==> [ 1, 4 ]

surgeon(hay).replace [ 2, 3 ], "missing"
# ==> [ 1, "missing", 4 ]

Using Functions for Comparison

You can also use a function for comparision:

is_2 = (val) ->
  val is 2

is_3 = (val) ->
  val is 3

surgeon(hay).remove [ is_2, is_3 ]
# ==> [ 1,  4 ]

surgeon(hay).replace [ is_2, is_3 ], "missing"
# ==> [ 1, "missing", 4 ]

The comparison function also accepts two other arguments:

  • arr_i: the position of the value within the array.
  • finder_i: the position of the comparison function within the array of finders.

Example:

is_2 = (val, arr_i, finder_i) ->
  console.log "Checking #{val} at position #{arr_i} with finder at #{finder_i}"
  val is 2

Get Info on a Slice

You can also get info on the first slice that matches your slice:

surgeon(hay).describe_slice [ is_2, is_3 ]

# ==> 
{ 
  start_index: 1, 
  end_index:   3, 
  length:      2,
  slice:       [2, 3]
}
Something went wrong with that request. Please try again.