cujojs / jiff

JSON Patch and diff based on rfc6902
Other
627 stars 41 forks source link

String/Number object clone results in strange patch results #47

Open jej2003 opened 11 months ago

jej2003 commented 11 months ago
let patch:jiff.JSONPatch = [{
    'op': 'add',
    'path': '/test/321/prop1',
    'value': new String('value'),
    'context': { id : '321' }
  }]
  let doc = {test: [{
    id: '321',
    prop1: 'old_value'
  }]}
  let result = jiff.patch(patch, doc, {
    findContext: (index, array, context) => {
      return array.findIndex((value, index, array) => value["id"] === context.id);
    }
  })

which results in

{
  "test":[{
    "id":"321",
    "prop1":{
      "0":"v",
      "1":"a",
      "2":"l",
      "3":"u",
      "4":"e"
    }
  }]
}"

ideally the clone logic would have a special case for String objects if possible resulting in

{
  "test":[{
    "id":"321",
    "prop1":"value"
  }]
}

something like the following in clone could work

function clone(x) {
    if(x == null || typeof x !== 'object' || x instanceof String || x instanceof Number) {
        return x;
    }

    if(Array.isArray(x)) {
        return cloneArray(x);
    }

    return cloneObject(x);
}