Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New hierarchy find functions #4603

Closed
wants to merge 26 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
db2c2e0
Add functions to find parents
NewboO Aug 31, 2022
6028413
Add functions to find script instances and components
NewboO Aug 31, 2022
d0722b9
Unit test for new graph node functions
NewboO Aug 31, 2022
9a6cbbe
Unit test for new find component functions
NewboO Aug 31, 2022
6b3ece6
Unit test for new find script functions
NewboO Aug 31, 2022
3f2c647
graph-node.js lint fix
NewboO Aug 31, 2022
9505c6d
entity.test.mjs lint fix
NewboO Aug 31, 2022
c3f30c2
Fix doc
NewboO Aug 31, 2022
56577ec
Fix doc
NewboO Aug 31, 2022
f9ee083
Const and array spreading in all find functions
NewboO Aug 31, 2022
4c803a9
Further refactoring of find functions
NewboO Sep 1, 2022
edb8038
Another refactoring in findOne
NewboO Sep 1, 2022
dc35892
Avoid creating and pushing empty arrays
NewboO Sep 1, 2022
90fa55e
Pluralize and make non-recursive when possible
NewboO Sep 1, 2022
b4ecb30
Allow looking for falsey values
NewboO Sep 2, 2022
4b4636a
Renaming, factorizing and adding forEachParent
NewboO Sep 2, 2022
cffd4dc
Simplifying a bit more some find functions
NewboO Sep 2, 2022
582f2d4
More simplifications
NewboO Sep 4, 2022
8419b28
Always ignore self node + renaming parents to ancestors
NewboO Sep 6, 2022
5ef124b
Let the user choose to include self node
NewboO Sep 7, 2022
78b52ca
Doc
NewboO Sep 7, 2022
6e4597b
Tests descriptions
NewboO Sep 7, 2022
f126819
Specific functions for descendants only search
NewboO Sep 12, 2022
fdd3f98
Doc
NewboO Sep 12, 2022
1c08147
Doc
NewboO Sep 13, 2022
ee4f7ad
Update src/scene/graph-node.js
NewboO Sep 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions src/scene/graph-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,20 +488,19 @@ class GraphNode extends EventHandler {
* var entities = parent.find('name', 'Test');
*/
find(attr, value) {
let result, results = [];
const results = [];
const len = this._children.length;

if (attr instanceof Function) {
const fn = attr;

result = fn(this);
if (result)
if (fn(this))
results.push(this);

for (let i = 0; i < len; i++) {
const descendants = this._children[i].find(fn);
if (descendants.length)
results = results.concat(descendants);
results.push(...descendants);
}
} else {
let testValue;
Expand All @@ -519,7 +518,7 @@ class GraphNode extends EventHandler {
for (let i = 0; i < len; ++i) {
const descendants = this._children[i].find(attr, value);
if (descendants.length)
results = results.concat(descendants);
results.push(...descendants);
NewboO marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -557,8 +556,7 @@ class GraphNode extends EventHandler {
if (attr instanceof Function) {
const fn = attr;

result = fn(this);
if (result)
if (fn(this))
return this;

for (let i = 0; i < len; i++) {
Expand All @@ -581,7 +579,7 @@ class GraphNode extends EventHandler {

for (let i = 0; i < len; i++) {
result = this._children[i].findOne(attr, value);
if (result !== null)
if (result)
return result;
}
}
Expand Down Expand Up @@ -613,17 +611,16 @@ class GraphNode extends EventHandler {
* var entities = entity.findInParent('name', 'Test');
*/
findInParent(attr, value) {
let result, results = [];
const results = [];

if (attr instanceof Function) {
const fn = attr;

result = fn(this);
if (result)
if (fn(this))
results.push(this);

if (this._parent) {
results = results.concat(this._parent.findInParent(fn));
results.push(...this._parent.findInParent(fn));
}
} else {
let testValue;
Expand All @@ -639,7 +636,7 @@ class GraphNode extends EventHandler {
}

if (this._parent) {
results = results.concat(this._parent.findInParent(attr, value));
results.push(...this._parent.findInParent(attr, value));
}
}

Expand Down Expand Up @@ -671,13 +668,10 @@ class GraphNode extends EventHandler {
* var node = parent.findOneInParent('name', 'Test');
*/
findOneInParent(attr, value) {
let result = null;

if (attr instanceof Function) {
const fn = attr;

result = fn(this);
if (result)
if (fn(this))
return this;

if (this._parent) {
Expand Down