Skip to content

Commit bf27317

Browse files
authored
feat: add an option to exclude object getters in affectedToPathList util (#48)
* fix: add an option to affectedToPathList * run apidoc * update CHANGELOG
1 parent 08c8fda commit bf27317

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44
### Added
55
- feat: better support of Object.hasOwn and "in" #47
6+
- feat: add an option to exclude object getters in affectedToPathList util #48
67

78
## [2.3.0] - 2022-08-13
89
### Added

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,22 @@ isChanged(original, { d: { e: "3" } }, affected) // true
185185

186186
Returns **any** No return.
187187

188+
### affectedToPathList
189+
190+
Convert `affected` to path list
191+
192+
`affected` is a weak map which is not printable.
193+
This function is can convert it to printable path list.
194+
It's for debugging purpose.
195+
196+
#### Parameters
197+
198+
* `obj` **any** An object that is used with `createProxy`.
199+
* `affected` **[WeakMap](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)<[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object), any>** A weak map that is used with `createProxy`.
200+
* `onlyWithValues` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** An optional boolean to exclude object getters.
201+
202+
Returns **any** An array of paths.
203+
188204
### replaceNewProxy
189205

190206
replace newProxy function.

src/index.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ export const getUntracked = <T>(obj: T): T | null => {
377377
*
378378
* @param obj - Object to mark as tracked or not.
379379
* @param mark - Boolean indicating whether you want to track this object or not.
380-
* @returns No return.
380+
* @returns - No return.
381381
*
382382
* @example
383383
* import { createProxy, markToTrack, isChanged } from 'proxy-compare';
@@ -399,10 +399,22 @@ export const markToTrack = (obj: object, mark = true) => {
399399
objectsToTrack.set(obj, mark);
400400
};
401401

402-
// convert affected to path list
402+
/**
403+
* Convert `affected` to path list
404+
*
405+
* `affected` is a weak map which is not printable.
406+
* This function is can convert it to printable path list.
407+
* It's for debugging purpose.
408+
*
409+
* @param obj - An object that is used with `createProxy`.
410+
* @param affected - A weak map that is used with `createProxy`.
411+
* @param onlyWithValues - An optional boolean to exclude object getters.
412+
* @returns - An array of paths.
413+
*/
403414
export const affectedToPathList = (
404415
obj: unknown,
405416
affected: WeakMap<object, unknown>,
417+
onlyWithValues?: boolean,
406418
) => {
407419
const list: (string | symbol)[][] = [];
408420
const seen = new WeakSet();
@@ -430,7 +442,9 @@ export const affectedToPathList = (
430442
});
431443
}
432444
used[KEYS_PROPERTY]?.forEach((key) => {
433-
walk((x as any)[key], path ? [...path, key] : [key]);
445+
if (!onlyWithValues || 'value' in (Object.getOwnPropertyDescriptor(x, key) || {})) {
446+
walk((x as any)[key], path ? [...path, key] : [key]);
447+
}
434448
});
435449
} else if (path) {
436450
list.push(path);

0 commit comments

Comments
 (0)