Skip to content

Commit

Permalink
Merge pull request #48 from jej2003/47-string_number_object_clone
Browse files Browse the repository at this point in the history
added support for primitive wrappers to not be handled as objects
  • Loading branch information
briancavalier committed Dec 1, 2023
2 parents 880de3b + 19ef5f4 commit 035d5ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function clone(x) {
return x;
}

if (x instanceof String || x instanceof Number || x instanceof Boolean) {
return x.valueOf();
}

if(Array.isArray(x)) {
return cloneArray(x);
}
Expand Down
18 changes: 18 additions & 0 deletions test/jsonPatch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ buster.testCase('jsonPatch', {
assert.equals(result.value, 1);
},

'should replace String': function() {
var a = { value: 0 };
var result = jsonPatch.apply([{ op: 'add', path: '/value', value: new String("value") }], a);
assert.equals(result.value, "value");
},

'should replace Number': function() {
var a = { value: 0 };
var result = jsonPatch.apply([{ op: 'add', path: '/value', value: new Number(1) }], a);
assert.equals(result.value, 1);
},

'should replace Boolean': function() {
var a = { value: 0 };
var result = jsonPatch.apply([{ op: 'add', path: '/value', value: new Boolean(true) }], a);
assert.equals(result.value, true);
},

'should throw': {
'when path is invalid': function() {
assert.exception(function() {
Expand Down

0 comments on commit 035d5ac

Please sign in to comment.