rjrodger / patrun

A fast pattern matcher on JavaScript object properties.
MIT License
143 stars 19 forks source link

remove should pass through the value #3

Open mcollina opened 10 years ago

mcollina commented 10 years ago

As for supporting the 'list' patrun as depicted in the README, it's not currently possible to remove elements in sparse order.

var patrun = require('patrun')
  , many = patrun( function(pat,data){
      var items = this.find(pat,true) || []
      items.push(data)

      return {
        find: function(args,data){
          console.log('find', arguments)
          return 0 < items.length ? items : null
        },
        remove: function(args,data){
          // data is always 'A'
          items.pop()
          return 0 == items.length;
        }
      }
    })

many.add( {a:1}, 'A' )
many.add( {a:1}, 'B' )

many.remove( {a:1}, 'A' )
console.log(many.find( {a:1} )) // should be [ 'B' ], but is 'A'

I suggest that the custom remove should include a way for passing the dest value through the remove call.

Moreover, I think the following line https://github.com/rjrodger/patrun/blob/master/patrun.js#L92 should be changed: it's impossible to remove the original data through the custom remove function.