senecajs / seneca-entity

Entity plugin for seneca
MIT License
13 stars 15 forks source link

match$ method; true if entA subset of entB #50

Open rjrodger opened 6 years ago

rjrodger commented 6 years ago

draft impl:

function match$(sub_ent, super_ent, why) {
  var why = { match: false }
  if( (null != super_ent.id || null != super_ent.id$) && null == sub_ent.id ) {
    why.no_id = true
    return why
  }

  if( (null == super_ent.id && null == super_ent.id$) && null != sub_ent.id )  {
    why.has_id = true
    return why
  }

  if( !sub_ent.is$(super_ent.canon$()) ) {
    why.canon = true
    return why
  }

  // FIX: this will need to be deepEqual
  why.match = true
  Object.keys(sub_ent.data$()).forEach(function(sub_field) {
    if( 'id' != sub_field && sub_ent[sub_field] !== super_ent[sub_field] ) {
      why.match = false
      why.field = sub_field
      why.values = [sub_ent[sub_field], super_ent[sub_field]]
    }
  })

  return why
}