array_surgeon

0.5.0 • Public • Published

array_surgeon

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

Similar, but different to: array_pinch

The npm package, array_pinch, lets you define only the start and end points of the sequence:

pinch = require "pinch"
arr = [1,2,3,4,5,6]
pinch(arr).remove [2,5]
# ===> [ 1, 6 ]

Installation & Usage

shell> npm install array_surgeon

surgeon = require 'array_surgeon'

hay = [ 1, 2, 3, 4, 5, 6 ]

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

surgeon(hay).replace [ 2, 5 ], "two", "->", "five"
# ==> [ 1, "two", "->", "five", 6 ]

You also get remove_all and replace_all:

hay = [ 1, 2, 3, 4, 1, 2, 3, 4 ]

surgeon(hay).remove_all [ 2, 3 ]
# ==> [ 1, 4, 1, 4 ]

surgeon(hay).replace_all [ 2, 3 ], "missing"
# ==> [ 1, "missing", 4, 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

Splats

is_4_to_6 = (v) ->
  v in [4, 5, 6]
  
is_4_to_6.is_splat = true

surgeon( [1, 2, 3, 4, 5, 6, 7] ).replace [ is_3, is_4_to_6 ], "taken"
# ==> [ 1, 2, "taken", 7]

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]
  indexs:      [1, 2]
}

When using splats:

finders = [ is_3, is_4_to_6 ]
hay     = [ 1, 2, 3, 4, 5, 6, 7]
# ==>
{
  start_index: 2
  end_index:   6
  length:      4
  slice:       [3, [4, 5, 6]]
  indexs:      [2, 3, 4, 5]
}

Readme

Keywords

none

Package Sidebar

Install

npm i array_surgeon

Weekly Downloads

2

Version

0.5.0

License

BSD

Last publish

Collaborators

  • da99