Skip to content

Commit 6c7b4e8

Browse files
committed
Warn when array props are set
1 parent c808a85 commit 6c7b4e8

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/jsonpatcherproxy.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ const JSONPatcherProxy = (function() {
134134
} else {
135135
if (Array.isArray(target) && !Number.isInteger(+key.toString())) {
136136
/* array props (as opposed to indices) don't emit any patches, to avoid needless `length` patches */
137+
if(key !== 'length') {
138+
console.warn('JSONPatcherProxy noticed a non-integer prop was set for an array. This will not emit a patch');
139+
}
137140
return Reflect.set(target, key, newValue);
138141
}
139142
operation.op = 'add';

test/spec/proxySpec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,35 @@ describe('proxy', function() {
551551
expect(observedObj).toEqualInJson(obj2);
552552
});
553553

554+
it('should not generate a patch when array props are added or replaced', function() {
555+
var obj = [];
556+
var jsonPatcherProxy = new JSONPatcherProxy(obj);
557+
var observedObj = jsonPatcherProxy.observe(true);
558+
559+
observedObj.lastName = 'Wester';
560+
561+
var patches = jsonPatcherProxy.generate();
562+
expect(patches).toReallyEqual([]);
563+
564+
observedObj.lastName = 'Wester Jr.';
565+
566+
var patches = jsonPatcherProxy.generate();
567+
expect(patches).toReallyEqual([]);
568+
});
569+
570+
it('should not generate a patch when array props are added or replaced - and log a warning', function() {
571+
572+
var obj = [];
573+
var jsonPatcherProxy = new JSONPatcherProxy(obj);
574+
var observedObj = jsonPatcherProxy.observe(true);
575+
576+
spyOn(console, 'warn');
577+
578+
observedObj.lastName = 'Wester';
579+
580+
expect(console.warn).toHaveBeenCalledWith('JSONPatcherProxy noticed a non-integer prop was set for an array. This will not emit a patch');
581+
});
582+
554583
it('should not generate the same patch twice (replace)', function() {
555584
var obj = {
556585
lastName: 'Einstein'

0 commit comments

Comments
 (0)