@lowdefy/helpers 4.6.0 → 4.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/serializer.js +41 -27
  2. package/package.json +2 -2
@@ -41,7 +41,7 @@ function propsToError(data) {
41
41
  }
42
42
  return error;
43
43
  }
44
- const makeReplacer = (customReplacer, isoStringDates)=>(key, value)=>{
44
+ const makeReplacer = (customReplacer, isoStringDates, skipMarkers)=>(key, value)=>{
45
45
  let dateReplacer = (date)=>({
46
46
  '~d': date.valueOf()
47
47
  });
@@ -69,29 +69,43 @@ const makeReplacer = (customReplacer, isoStringDates)=>(key, value)=>{
69
69
  newValue[k] = dateReplacer(newValue[k]);
70
70
  }
71
71
  });
72
- if (newValue['~r']) {
73
- Object.defineProperty(newValue, '~r', {
74
- value: newValue['~r'],
75
- enumerable: true,
76
- writable: true,
77
- configurable: true
78
- });
79
- }
80
- if (newValue['~k']) {
81
- Object.defineProperty(newValue, '~k', {
82
- value: newValue['~k'],
83
- enumerable: true,
84
- writable: true,
85
- configurable: true
86
- });
87
- }
88
- if (newValue['~l']) {
89
- Object.defineProperty(newValue, '~l', {
90
- value: newValue['~l'],
91
- enumerable: true,
92
- writable: true,
93
- configurable: true
94
- });
72
+ if (!skipMarkers) {
73
+ // Capture marker values before shallow copy (spread doesn't copy non-enumerable props)
74
+ const markerR = newValue['~r'];
75
+ const markerK = newValue['~k'];
76
+ const markerL = newValue['~l'];
77
+ if (markerR || markerK || markerL) {
78
+ // Shallow copy to avoid mutating the original object's property descriptors
79
+ if (newValue === value) {
80
+ newValue = {
81
+ ...newValue
82
+ };
83
+ }
84
+ if (markerR) {
85
+ Object.defineProperty(newValue, '~r', {
86
+ value: markerR,
87
+ enumerable: true,
88
+ writable: true,
89
+ configurable: true
90
+ });
91
+ }
92
+ if (markerK) {
93
+ Object.defineProperty(newValue, '~k', {
94
+ value: markerK,
95
+ enumerable: true,
96
+ writable: true,
97
+ configurable: true
98
+ });
99
+ }
100
+ if (markerL) {
101
+ Object.defineProperty(newValue, '~l', {
102
+ value: markerL,
103
+ enumerable: true,
104
+ writable: true,
105
+ configurable: true
106
+ });
107
+ }
108
+ }
95
109
  }
96
110
  return newValue;
97
111
  }
@@ -103,7 +117,7 @@ const makeReplacer = (customReplacer, isoStringDates)=>(key, value)=>{
103
117
  return item;
104
118
  });
105
119
  // Preserve ~l, ~k, ~r on arrays by wrapping in a marker object
106
- if (newValue['~l'] !== undefined || newValue['~k'] !== undefined || newValue['~r'] !== undefined) {
120
+ if (!skipMarkers && (newValue['~l'] !== undefined || newValue['~k'] !== undefined || newValue['~r'] !== undefined)) {
107
121
  const wrapper = {
108
122
  '~arr': mappedArray
109
123
  };
@@ -215,11 +229,11 @@ const serializeToString = (json, options = {})=>{
215
229
  }
216
230
  if (options.stable) {
217
231
  return stableStringify(json, {
218
- replacer: makeReplacer(options.replacer),
232
+ replacer: makeReplacer(options.replacer, undefined, options.skipMarkers),
219
233
  space: options.space
220
234
  });
221
235
  }
222
- return JSON.stringify(json, makeReplacer(options.replacer, options.isoStringDates), options.space);
236
+ return JSON.stringify(json, makeReplacer(options.replacer, options.isoStringDates, options.skipMarkers), options.space);
223
237
  };
224
238
  const deserialize = (json, options = {})=>{
225
239
  if (type.isUndefined(json)) return json;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/helpers",
3
- "version": "4.6.0",
3
+ "version": "4.7.1",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -31,7 +31,7 @@
31
31
  "dist/*"
32
32
  ],
33
33
  "dependencies": {
34
- "@lowdefy/errors": "4.6.0",
34
+ "@lowdefy/errors": "4.7.1",
35
35
  "lodash.merge": "4.6.2"
36
36
  },
37
37
  "devDependencies": {