Skip to content

Commit

Permalink
Add a self-reference to entity so it can be accessed when destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
lajohnston committed Mar 3, 2018
1 parent 3d5fc09 commit 549c221
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions dist/eco.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion spec/e2e/iterators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ describe("Iterators", () => {
matching.foo = "foo";
matching.bar = "bar";

filter.forEach((entity, index, array) => {
filter.forEach(({ entity, foo, bar }, index, array) => {
expect(entity).toBe(matching);
expect(foo).toBe(matching.foo);
expect(bar).toBe(matching.bar);

expect(array).toEqual([matching]);
done();
});
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/abstractEntity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,9 @@ describe("Entity", () => {
entity.enabled = false;
expect(entity.enabled).toBe(false);
});

it("should hold a property that references itself, for use when destructuring", () => {
const entity = new AbstractEntity();
expect(entity.entity).toBe(entity);
});
});
9 changes: 9 additions & 0 deletions src/abstractEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export default class AbstractEntity {
this._enabled = true;
}

/**
* Returns a reference to this entity to allow it to be accessed with destructuring
*
* @type {Entity} the entity
*/
get entity() {
return this;
}

/**
* @type {boolean} true if the entity is enabled, otherwise false
*/
Expand Down

0 comments on commit 549c221

Please sign in to comment.