Skip to content

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

Description

@jej2003
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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions