MCPcopy
hub / github.com/TypeStrong/ts-node / resetObject

Function resetObject

src/test/helpers.ts:292–331  ·  view source on GitHub ↗
(
  object: any,
  state: ReturnType<typeof captureObjectState>,
  doNotDeleteTheseKeys: string[] = [],
  doNotSetTheseKeys: true | string[] = [],
  avoidSetterIfUnchanged: string[] = [],
  reorderProperties = false
)

Source from the content-addressed store, hash-verified

290}
291// Redefine all property descriptors and delete any new properties
292function resetObject(
293 object: any,
294 state: ReturnType<typeof captureObjectState>,
295 doNotDeleteTheseKeys: string[] = [],
296 doNotSetTheseKeys: true | string[] = [],
297 avoidSetterIfUnchanged: string[] = [],
298 reorderProperties = false
299) {
300 const currentDescriptors = Object.getOwnPropertyDescriptors(object);
301 for (const key of Object.keys(currentDescriptors)) {
302 if (doNotDeleteTheseKeys.includes(key)) continue;
303 if (has(state.descriptors, key)) continue;
304 delete object[key];
305 }
306 // Trigger nyc's setter functions
307 for (const [key, value] of Object.entries(state.values)) {
308 try {
309 if (doNotSetTheseKeys === true || doNotSetTheseKeys.includes(key))
310 continue;
311 if (avoidSetterIfUnchanged.includes(key) && object[key] === value)
312 continue;
313 state.descriptors[key].set?.call(object, value);
314 } catch {}
315 }
316 // Reset descriptors
317 Object.defineProperties(object, state.descriptors);
318
319 if (reorderProperties) {
320 // Delete and re-define each property so that they are in original order
321 const originalOrder = Object.keys(state.descriptors);
322 const properties = Object.getOwnPropertyDescriptors(object);
323 const sortedKeys = sortBy(Object.keys(properties), (name) =>
324 originalOrder.includes(name) ? originalOrder.indexOf(name) : 999
325 );
326 for (const key of sortedKeys) {
327 delete object[key];
328 Object.defineProperty(object, key, properties[key]);
329 }
330 }
331}
332
333//#endregion
334

Callers 1

resetNodeEnvironmentFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…