UNPKG

@azure-tools/linq

Version:

LINQ-like functionality for Typescript.

75 lines 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.references = exports.refCount = exports.visitor = void 0; function* _visitor(instance, visited) { if (instance === null || instance === undefined || visited.has(instance)) { return; } visited.add(instance); if (instance instanceof Set || Array.isArray(instance)) { let ndx = 0; for (const each of instance) { if (typeof each === 'object') { yield* _visitor(each, visited); } // yield the member after visiting children yield { index: ndx++, parent: instance, instance: each }; } return; } if (instance instanceof Map) { // walk thru map members. for (const [key, value] of instance.entries()) { if (typeof value === 'object') { yield* _visitor(value, visited); } // yield the member after visiting children yield { index: key, parent: instance, instance: value }; } return; } // objects for (const key of Object.keys(instance)) { const value = instance[key]; if (typeof value === 'object') { yield* _visitor(value, visited); } // yield the member after visiting children yield { index: key, parent: instance, instance: value }; } } function visitor(instance) { return _visitor(instance, new WeakSet()); } exports.visitor = visitor; function refCount(instance, target) { let count = 0; for (const each of visitor(instance)) { if (target === each.instance) { count++; } } return count; } exports.refCount = refCount; function* references(instance, target) { for (const each of visitor(instance)) { if (each === instance.instance) { yield instance; } } } exports.references = references; //# sourceMappingURL=visitor.js.map