Skip to content

Commit 54d6b52

Browse files
authored
Merge pull request #3139 from adumesny/master
fix Util:removeInternalForSave() to skip arrays
2 parents 79cec48 + 5b89b38 commit 54d6b52

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

doc/API.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,7 +2845,7 @@ new Utils(): Utils;
28452845
static addElStyles(el, styles): void;
28462846
```
28472847

2848-
Defined in: [utils.ts:690](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L690)
2848+
Defined in: [utils.ts:692](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L692)
28492849

28502850
###### Parameters
28512851

@@ -2864,7 +2864,7 @@ Defined in: [utils.ts:690](https://github.com/adumesny/gridstack.js/blob/master/
28642864
static appendTo(el, parent): void;
28652865
```
28662866

2867-
Defined in: [utils.ts:672](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L672)
2867+
Defined in: [utils.ts:674](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L674)
28682868

28692869
###### Parameters
28702870

@@ -2943,7 +2943,7 @@ const overlap = Utils.areaIntercept(
29432943
static canBeRotated(n): boolean;
29442944
```
29452945

2946-
Defined in: [utils.ts:793](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L793)
2946+
Defined in: [utils.ts:795](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L795)
29472947

29482948
true if the item can be rotated (checking for prop, not space available)
29492949

@@ -2963,7 +2963,7 @@ true if the item can be rotated (checking for prop, not space available)
29632963
static clone<T>(obj): T;
29642964
```
29652965

2966-
Defined in: [utils.ts:635](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L635)
2966+
Defined in: [utils.ts:637](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L637)
29672967

29682968
single level clone, returning a new object with same top fields. This will share sub objects and arrays
29692969

@@ -2989,7 +2989,7 @@ single level clone, returning a new object with same top fields. This will share
29892989
static cloneDeep<T>(obj): T;
29902990
```
29912991

2992-
Defined in: [utils.ts:651](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L651)
2992+
Defined in: [utils.ts:653](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L653)
29932993

29942994
Recursive clone version that returns a full copy, checking for nested objects and arrays ONLY.
29952995
Note: this will use as-is any key starting with double __ (and not copy inside) some lib have circular dependencies.
@@ -3016,7 +3016,7 @@ Note: this will use as-is any key starting with double __ (and not copy inside)
30163016
static cloneNode(el): HTMLElement;
30173017
```
30183018

3019-
Defined in: [utils.ts:666](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L666)
3019+
Defined in: [utils.ts:668](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L668)
30203020

30213021
deep clone the given HTML node, removing teh unique id field
30223022

@@ -3226,7 +3226,7 @@ const fromShadow = Utils.getElements('.item', shadowRoot);
32263226
static getValuesFromTransformedElement(parent): DragTransform;
32273227
```
32283228

3229-
Defined in: [utils.ts:750](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L750)
3229+
Defined in: [utils.ts:752](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L752)
32303230

32313231
defines an element that is used to get the offset and scale from grid transforms
32323232
returns the scale and offsets from said element
@@ -3247,7 +3247,7 @@ returns the scale and offsets from said element
32473247
static initEvent<T>(e, info): T;
32483248
```
32493249

3250-
Defined in: [utils.ts:707](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L707)
3250+
Defined in: [utils.ts:709](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L709)
32513251

32523252
###### Type Parameters
32533253

@@ -3420,7 +3420,7 @@ removes field from the first object if same as the second objects (like diffing)
34203420
static removeInternalForSave(n, removeEl): void;
34213421
```
34223422

3423-
Defined in: [utils.ts:509](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L509)
3423+
Defined in: [utils.ts:511](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L511)
34243424

34253425
removes internal fields '_' and default values for saving
34263426

@@ -3441,7 +3441,7 @@ removes internal fields '_' and default values for saving
34413441
static removePositioningStyles(el): void;
34423442
```
34433443

3444-
Defined in: [utils.ts:542](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L542)
3444+
Defined in: [utils.ts:544](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L544)
34453445

34463446
###### Parameters
34473447

@@ -3565,7 +3565,7 @@ static simulateMouseEvent(
35653565
target?): void;
35663566
```
35673567

3568-
Defined in: [utils.ts:723](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L723)
3568+
Defined in: [utils.ts:725](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L725)
35693569

35703570
copies the MouseEvent (or convert Touch) properties and sends it as another event to the given target
35713571

@@ -3620,7 +3620,7 @@ static swap(
36203620
b): void;
36213621
```
36223622

3623-
Defined in: [utils.ts:774](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L774)
3623+
Defined in: [utils.ts:776](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L776)
36243624

36253625
swap the given object 2 field values
36263626

@@ -3642,7 +3642,7 @@ swap the given object 2 field values
36423642
static throttle(func, delay): () => void;
36433643
```
36443644

3645-
Defined in: [utils.ts:532](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L532)
3645+
Defined in: [utils.ts:534](https://github.com/adumesny/gridstack.js/blob/master/src/utils.ts#L534)
36463646

36473647
delay calling the given function for given delay, preventing new calls from happening while waiting
36483648

doc/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Change log
55
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
66
**Table of Contents** *generated with [DocToc](http://doctoc.herokuapp.com/)*
77

8+
- [12.3.2-dev (TBD)](#1232-dev-tbd)
89
- [12.3.2 (2025-08-12)](#1232-2025-08-12)
910
- [12.3.1 (2025-08-11)](#1231-2025-08-11)
1011
- [12.3.0 (2025-08-10)](#1230-2025-08-10)
@@ -133,6 +134,9 @@ Change log
133134

134135
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
135136

137+
## 12.3.2-dev (TBD)
138+
* fix: [#3139](https://github.com/gridstack/gridstack.js/pull/3139) `Utils:removeInternalForSave()` to skip arrays
139+
136140
## 12.3.2 (2025-08-12)
137141
* fix: [#3136](https://github.com/gridstack/gridstack.js/pull/3136) more tweaks to save(columnCount) option.
138142

spec/utils-spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,31 @@ describe('gridstack utils', () => {
225225
expect(f).toEqual({first: 1, _dontskip: {second: 2}});
226226
expect(z).toEqual({first: 1, _dontskip: {second: 'two'}});
227227
});
228-
});
228+
});
229+
describe('removeInternalAndSame', () => {
230+
it('should remove internal and same', () => {
231+
const a = {first: 1, second: 'text', _skip: {second: 2}, arr: [1, 'second', 3]};
232+
const b = {first: 1, second: 'text'};
233+
Utils.removeInternalAndSame(a, b);
234+
expect(a).toEqual({arr: [1, 'second', 3]});
235+
});
236+
it('should not remove items in an array', () => {
237+
const a = {arr: [1, 2, 3]};
238+
const b = {arr: [1, 3]};
239+
Utils.removeInternalAndSame(a, b);
240+
expect(a).toEqual({arr: [1, 2, 3]});
241+
});
242+
it('should remove nested object, and make empty', () => {
243+
const a = {obj1: {first: 1, nested: {second: 2}}, obj2: {first: 1, second: 2}};
244+
const b = {obj1: {first: 1, nested: {second: 2}}, obj2: {first: 1, second: 2}};
245+
Utils.removeInternalAndSame(a, b);
246+
expect(a).toEqual({});
247+
});
248+
it('should remove nested object, and make empty - part 2', () => {
249+
const a = {obj1: {first: 1, nested: {second: 2}}, obj2: {first: 1, second: 2}};
250+
const b = {obj1: {first: 1}, obj2: {first: 1, second: 2}};
251+
Utils.removeInternalAndSame(a, b);
252+
expect(a).toEqual({obj1: {nested: {second: 2}}});
253+
});
254+
});
229255
});

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ export class Utils {
493493
/** removes field from the first object if same as the second objects (like diffing) and internal '_' for saving */
494494
static removeInternalAndSame(a: unknown, b: unknown):void {
495495
if (typeof a !== 'object' || typeof b !== 'object') return;
496+
// skip arrays as we don't know how to hydrate them (unlike object spread operator)
497+
if (Array.isArray(a) || Array.isArray(b)) return;
496498
for (let key in a) {
497499
const aVal = a[key];
498500
const bVal = b[key];

0 commit comments

Comments
 (0)