From 549c2217f14c0cfb140cc848aee0b2cf2691a484 Mon Sep 17 00:00:00 2001 From: lajohnston Date: Sat, 3 Mar 2018 17:10:19 +0000 Subject: [PATCH] Add a self-reference to entity so it can be accessed when destructuring --- dist/eco.min.js | 4 ++-- spec/e2e/iterators.spec.js | 5 ++++- spec/unit/abstractEntity.spec.js | 5 +++++ src/abstractEntity.js | 9 +++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/dist/eco.min.js b/dist/eco.min.js index 5e48348..73bc893 100644 --- a/dist/eco.min.js +++ b/dist/eco.min.js @@ -1,10 +1,10 @@ /*! * eco.js - v0.4.0-alpha - * Built Fri, 2nd Mar 2018 21:03:56 UTC + * Built Sat, 3rd Mar 2018 17:05:23 UTC * * https://github.com/lajohnston/eco * * eco.js is licensed under the MIT License. * http://www.opensource.org/licenses/mit-license */ - !function(){"use strict";function t(t,e){this.entity=t,this.component=e,this.next=void 0}function e(t,e){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:[],n=function(t){function e(){return o(this,e),c(this,(e.__proto__||Object.getPrototypeOf(e)).apply(this,arguments))}return s(e,t),e}(t.Entity);return e.forEach(function(t){return n.defineComponent(t)}),new t.Eco(function(t){return new n(t)},t.entityCollection(),t.createIterator)},entityCollection:function(){return new t.EntityCollection},createIterator:function(e,n,i){return new t.Iterator(e,n,i)}};return t}function i(t){return n().create(t)}var o=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},r=function(){function t(t,e){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:[],n=function(t){function e(){return o(this,e),c(this,(e.__proto__||Object.getPrototypeOf(e)).apply(this,arguments))}return s(e,t),e}(t.Entity);return e.forEach(function(t){return n.defineComponent(t)}),new t.Eco(function(t){return new n(t)},t.entityCollection(),t.createIterator)},entityCollection:function(){return new t.EntityCollection},createIterator:function(e,n,i){return new t.Iterator(e,n,i)}};return t}function i(t){return n().create(t)}var o=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},r=function(){function t(t,e){for(var n=0;n { 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(); }); diff --git a/spec/unit/abstractEntity.spec.js b/spec/unit/abstractEntity.spec.js index c74bd0b..5889beb 100644 --- a/spec/unit/abstractEntity.spec.js +++ b/spec/unit/abstractEntity.spec.js @@ -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); + }); }); diff --git a/src/abstractEntity.js b/src/abstractEntity.js index 313afdc..21a7ad2 100644 --- a/src/abstractEntity.js +++ b/src/abstractEntity.js @@ -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 */