{"size": 1215, "ext": "js", "max_stars_repo_path": "src/enumerables/PathNodesToEnumerable.js", "max_stars_repo_name": "wm123450405/linqjs", "max_stars_repo_stars_event_min_datetime": "2015-05-20T03:30:24.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-31T04:13:07.000Z", "max_issues_repo_path": "src/enumerables/PathNodesToEnumerable.js", "max_issues_repo_name": "wm123450405/linqjs", "max_issues_repo_issues_event_min_datetime": "2015-05-23T08:12:20.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-27T15:15:58.000Z", "max_forks_repo_path": "src/enumerables/PathNodesToEnumerable.js", "max_forks_repo_name": "wm123450405/linqjs", "max_forks_repo_forks_event_min_datetime": "2016-09-18T09:20:19.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-08T11:04:26.000Z"}
{"max_stars_count": 20, "max_issues_count": 3, "max_forks_count": 6, "avg_line_length": 29.6341463415, "max_line_length": 84, "alphanum_fraction": 0.4674897119}
'use strict';

const IEnumerable = require('../IEnumerable');

const ITree = require('./ITree');

const core = require('../core/core');

const NotAncestorOfException = require('../core/exceptions/NotAncestorOfException');

class PathNodesToEnumerable extends IEnumerable {
    constructor(root, node) {
        super([]);
        core.defineProperty(this, Symbol.iterator, function* PathNodesToIterator() {
            let search = (result, current) => {
                result.push(current);
                if (ITree.isSameNode(current, node)) {
                    return result;
                } else {
                    for (let child of current) {
                        if (search(result, child)) {
                            return result;
                        }
                    }
                    result.pop();
                    return false;
                }
            };
            let result = search([], root);
            if (result) {
                for (let n of result) {
                    yield n;
                }
            } else {
                throw new NotAncestorOfException(root, node);
            }
        });
    }
}

module.exports = PathNodesToEnumerable;