Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jej2003 opened this issue Nov 21, 2023 · 0 comments
Open

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

jej2003 opened this issue Nov 21, 2023 · 0 comments

Comments

@jej2003
Copy link
Contributor

jej2003 commented Nov 21, 2023

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant