ecsjs 1.0.0-beta.6 → 1.0.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ecs.js +1 -1
- package/dist/ecs.js.map +3 -3
- package/dist/types/src/entity-map.d.ts +34 -2
- package/dist/types/src/entity-map.d.ts.map +1 -1
- package/dist/types/src/key-map.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/entity-map.ts +88 -6
- package/src/key-map.ts +4 -6
package/dist/ecs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var a=class extends Map{constructor(e=[]){super(e)}firstEntry(){return this.size===0?void 0:this.entries().next().value}firstKey(){return this.size===0?void 0:this.keys().next().value}firstValue(){return this.size===0?void 0:this.values().next().value}toJSON(){return{__KeyMap__:1,iterable:[...this.entries()]}}toTable(e=!1){let t=[];for(let[n,o]of this.entries()){let s=o,r={};e==!1&&(r["entity.key"]=n),r["entity.type"]=s.constructor.name,t.push({...r,...s})}return t}printTable(e=!1){console.table(this.toTable(e))}entries(){return super.entries().filter(e=>e!=null)}keys(){return super.keys().filter(e=>e!=null)}values(){return super.values().filter(e=>e!=null)}static get[Symbol.species](){return Map}};var p=class i{componentMaps=new a;nextId=0;constructor(){}register(...e){for(let t of e){let n=t.name;if(n===void 0)throw new ReferenceError("The component class does not have a name defined. i.e. a constructor name");let o=new a;this.componentMaps.set(n,o)}return this}getMap(e){return this.componentMaps.get(e.name)}firstEntry(e){return this.getMap(e)?.firstEntry()}firstKey(e){return this.getMap(e)?.firstKey()}firstValue(e){return this.getMap(e)?.firstValue()}get(e,t){let n=this.componentMaps.get(t.name);if(n===void 0)throw new ReferenceError(`"${t}" component class does not exist in the componentMaps`);return n.get(e)}getValues(e,...t){return t.map(n=>this.get(e,n))}getByKey(e,t){let n=this.componentMaps.get(t);if(n===void 0)throw new ReferenceError(`"${t}" component does not exist in the componentMaps`);return n.get(e)}has(e,t){let n=this.componentMaps.get(t.name);return n===void 0?!1:n.has(e)}set(e,t){let n=this.componentMaps.get(t.constructor.name);if(n===void 0)throw new ReferenceError(`Component map does not exist using the name: ${t.constructor.name}`);return n.set(e,t),t}setValues(e,...t){return t.map(n=>this.set(e,n))}remove(e,t){return this.removeByKey(e,t.name)}removeByKey(e,t){let n=this.componentMaps.get(t);if(n===void 0)throw new ReferenceError(`A registered entity map does not exist for the given key: ${t}`);return n.get(e)===void 0?!1:n.delete(e)}destroyEntity(e){this.componentMaps.forEach(t=>{t.has(e)&&t.delete(e)})}getNextId(){return this.nextId++,this.nextId}clear(){return this.componentMaps.clear(),this}printTable(){return this.componentMaps.forEach(e=>console.table(e.toTable(!0))),this}printEntity(e){for(let t of this.componentMaps.values()){if(t.has(e)===!1)continue;let n=t.get(e),o={name:n.constructor.name,...n};console.table({[e]:o})}return this}static parse(e){let t=new i,n=JSON.parse(e,function(o,s){return s.hasOwnProperty("componentMaps")?(Reflect.setPrototypeOf(s,i.prototype),s):s.hasOwnProperty("__KeyMap__")?new a(s.iterable):this[o]});return t.componentMaps=n.componentMaps,t.nextId=n.nextId,t}static createWithTracing(e){let t={get(n,o){let s=n[o];return typeof s=="function"&&(e.length===0||e.includes(o))?function(...r){return console.groupCollapsed("ecs trace",o,r),console.trace(),console.groupEnd(),s.apply(this,r)}:s}};return new Proxy(new i,t)}};var u=new p;typeof window<"u"?window.ecs=u:typeof module<"u"&&module!==null&&(module.exports={ecs:u});export{u as ecs};
|
|
2
2
|
//# sourceMappingURL=ecs.js.map
|
package/dist/ecs.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/key-map.ts", "../src/entity-map.ts", "../src/ecs.ts"],
|
|
4
|
-
"sourcesContent": ["/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\nimport type { KeyCollection } from './definitions.js';\n\n/**\n * Class for storing keys and values\n */\nexport class KeyMap<TKey, TValue> extends Map<TKey, TValue> {\n\n constructor(entries: any[] = []) {\n super(entries)\n }\n\n firstEntry(): [key: TKey, value: TValue] | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.entries();\n const result = iterator.next();\n return result.value;\n }\n\n firstKey(): TKey | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.keys();\n const result = iterator.next();\n return result.value;\n }\n\n firstValue(): TValue | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.values();\n const result = iterator.next();\n return result.value;\n }\n\n toJSON() {\n return { __KeyMap__: 1, iterable: [...this.entries()] }\n }\n\n toTable(excludeKeys = false): any[] {\n const table = []\n for (const value of this.entries()) {\n let entity = value[1] as { new(): TValue }\n let meta: KeyCollection<any> = {}\n\n if (excludeKeys == false) meta[\"entity.key\"] = value[0]\n meta[\"entity.type\"] = entity.constructor.name\n\n table.push({ ...meta, ...entity })\n }\n return table\n }\n\n /**\n * Outputs keys and values in a tabular format to the console\n */\n printTable(excludeKeys = false): void {\n console.table(this.toTable(excludeKeys))\n }\n\n entries(): MapIterator<[TKey, TValue]> {\n return super.entries().filter(x => x !== undefined && x !== null)\n }\n\n keys(): MapIterator<TKey> {\n return super.keys().filter(x => x !== undefined && x !== null)\n }\n\n values(): MapIterator<TValue> {\n return super.values().filter(x => x !== undefined && x !== null)\n }\n\n static get [Symbol.species]() { return Map; }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * @module ecsjs\n */\nimport {\n type ComponentDataMap,\n type ComponentClass,\n type ComponentMap,\n type IComponent\n} from './definitions.js';\nimport { KeyMap } from './key-map.js'\n\n/**\n * Class for storing entities and their relationships\n * \n * @class EntityMap\n */\nexport class EntityMap {\n\n // entity class maps\n private componentMaps: ComponentMap = new KeyMap()\n\n private nextId: number = 0\n\n constructor() { }\n\n /**\n * Registers a component class\n * \n * @example\n *\n * ```javascript\n * // component class\n * class MyComponent {\n * constructor(x) {\n * this.x = x;\n * }\n * }\n *\n * ecs.register(MyComponent);\n * // or\n * ecs.register(MyComponent1, MyComponent2);\n * ```\n */\n register(...component: ComponentClass<any>[]) {\n for (const definition of component) {\n const componentName = definition.name;\n if (componentName === undefined)\n throw new ReferenceError(\"The component class does not have a name defined. i.e. a constructor name\");\n\n // create the component map\n const componentDataMap: ComponentDataMap<any> = new KeyMap();\n this.componentMaps.set(componentName, componentDataMap);\n }\n\n // chain\n return this;\n }\n\n /**\n * Retrieves an entity map by it's registered type\n */\n getMap<T extends IComponent>(component: ComponentClass<T>): ComponentDataMap<T> | undefined {\n return this.componentMaps.get(component.name);\n }\n\n /**\n * Get the first entity entry for a component class\n * \n * @example\n *\n * ```javascript\n * const [entityId, player] = ecs.firstEntry(PlayerComponent) ?? [];\n * ```\n */\n firstEntry<T extends IComponent>(component: ComponentClass<T>) {\n return this.getMap(component)?.firstEntry();\n }\n\n /**\n * Get the first entity id for a component class\n * \n * @example\n *\n * ```javascript\n * const entityId = ecs.firstKey(PlayerComponent);\n * ```\n */\n firstKey<T extends IComponent>(component: ComponentClass<T>) {\n return this.getMap(component)?.firstKey();\n }\n\n /**\n * Get the first entity component value for a component class\n * \n * @example\n *\n * ```javascript\n * const player = ecs.firstValue(PlayerComponent);\n * ```\n */\n firstValue<T extends IComponent>(component: ComponentClass<T>) {\n return this.getMap(component)?.firstValue();\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.get(entityId, Components.PositionComponent);\n * ```\n */\n get<T extends IComponent>(entityId: number, component: ComponentClass<T>): T {\n let cm = this.componentMaps.get(component.name);\n if (cm === undefined)\n throw new ReferenceError(`\"${component}\" component class does not exist in the componentMaps`);\n\n return cm.get(entityId);\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.getByKey(entityId, \"PositionComponent\");\n * ```\n */\n getByKey(entityId: number, componentName: string) {\n // get the component map\n let map = this.componentMaps.get(componentName);\n if (map === undefined)\n throw new ReferenceError(`\"${componentName}\" component does not exist in the componentMaps`);\n\n return map.get(entityId);\n }\n\n /**\n * Checks if the entity exists in it's registed component map\n * The entity type must be registered before calling this method\n * \n * @example\n *\n * ```javascript\n * // get an entity\n * let exists = ecs.has(entityId, PositionComponent);\n * ```\n */\n has<T extends IComponent>(entityId: number, component: ComponentClass<T>): boolean {\n // get the component map\n let map = this.componentMaps.get(component.name);\n if (map === undefined) return false\n\n return map.has(entityId);\n }\n\n /**\n * Adds or updates an entity to it's registered entity map\n *\n * @example\n *\n * ```javascript\n * // add/update an entity\n * let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));\n * ```\n */\n set<T extends IComponent>(entityId: number, componentData: T): T {\n // get the component map\n let map = this.componentMaps.get(componentData.constructor.name);\n if (map === undefined)\n throw new ReferenceError(`Component map does not exist using the name: ${componentData.constructor.name}`);\n\n // set the entity on the entity map\n map.set(entityId, componentData);\n\n // return instance\n return componentData as T;\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.remove(entityId, Components.PositionComponent);\n * ```\n */\n remove<T extends IComponent>(entityId: number, component: ComponentClass<T>) {\n return this.removeByKey(entityId, component.name);\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.removeByKey(entityId, \"PositionComponent\");\n * ```\n */\n removeByKey(entityId: number, componentName: string) {\n // get the entity map\n let entityMap = this.componentMaps.get(componentName);\n\n // ensure the map is defined\n if (entityMap === undefined)\n throw new ReferenceError(`A registered entity map does not exist for the given key: ${componentName}`);\n\n // get the entity\n let entity = entityMap.get(entityId);\n if (entity === undefined) return false;\n\n // remove the entity from the entity map\n return entityMap.delete(entityId);\n }\n\n /**\n * Destroys an entity across all component maps\n */\n destroyEntity(entityId: number) {\n this.componentMaps.forEach((dataMap: ComponentDataMap<any>) => {\n if (dataMap.has(entityId)) dataMap.delete(entityId);\n });\n }\n\n // TODO generate a non repeatable id for network play?\n getNextId() {\n this.nextId++;\n return this.nextId;\n }\n\n clear() {\n this.componentMaps.clear();\n return this;\n }\n\n /**\n * Prints all entity maps in a tabular format to the console\n */\n printTable() {\n this.componentMaps.forEach(map => console.table(map.toTable(true)));\n return this\n }\n\n /**\n * Parse's the JSON and returns an EntityMap object\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * const json = JSON.stringfy(ecs);\n * const restoredMap = EntityMap.parse(json);\n * ```\n */\n static parse(json: string) {\n const ecs = new EntityMap();\n\n const restored = JSON.parse(json, function (key: string, value: any) {\n\n if (value.hasOwnProperty('componentMaps')) {\n Reflect.setPrototypeOf(value, EntityMap.prototype);\n return value;\n }\n\n if (value.hasOwnProperty('__KeyMap__')) {\n return new KeyMap(value.iterable);\n }\n\n return this[key];\n });\n\n ecs.componentMaps = restored.componentMaps;\n ecs.nextId = restored.nextId;\n return ecs;\n }\n\n /**\n * A tracing method used for debugging.\n * Intercepts all functions specified and logs each call to the console.\n * \n * @method createWithTracing\n * @static\n * @param {Array} funcFilter A list of function names you want to intercept. If no function names are specified then will log all functions called\n * @return {EntityMap} A new entity map with tracing enabled\n */\n static createWithTracing(funcFilter: any) {\n let traceHandler = {\n get(target: any, propKey: string) {\n const targetValue = target[propKey]\n\n if (typeof targetValue === 'function' && (funcFilter.length === 0 || funcFilter.includes(propKey))) {\n return function (this: any, ...args: any[]) {\n console.groupCollapsed('ecs trace', propKey, args);\n console.trace();\n console.groupEnd();\n return targetValue.apply(this, args);\n }\n }\n\n return targetValue;\n }\n }\n\n return new Proxy(new EntityMap(), traceHandler)\n }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * ### An entity component system library for JavaScript\n *\n * @module ecsjs\n * @main ecsjs\n */\nimport { EntityMap } from './entity-map.js'\n\nexport const ecs = new EntityMap()\n\nif (typeof window !== 'undefined') {\n // @ts-ignore: exports to window\n window.ecs = ecs\n} else if (typeof module !== 'undefined' && module !== null) {\n // exports to nodejs\n module.exports = { ecs };\n}"],
|
|
5
|
-
"mappings": "AAsBO,IAAMA,EAAN,cAAmC,GAAkB,CAE1D,YAAYC,EAAiB,CAAC,EAAG,CAC/B,MAAMA,CAAO,CACf,CAEA,YAAqD,CACnD,OAAI,KAAK,OAAS,EAAG,OAEJ,KAAK,QAAQ,EACN,KAAK,EACf,KAChB,CAEA,UAA6B,CAC3B,OAAI,KAAK,OAAS,EAAG,OAEJ,KAAK,KAAK,EACH,KAAK,EACf,KAChB,CAEA,YAAiC,CAC/B,OAAI,KAAK,OAAS,EAAG,OAEJ,KAAK,OAAO,EACL,KAAK,EACf,KAChB,CAEA,QAAS,CACP,MAAO,CAAE,WAAY,EAAG,SAAU,CAAC,GAAG,KAAK,QAAQ,CAAC,CAAE,CACxD,CAEA,QAAQC,EAAc,GAAc,CAClC,IAAMC,EAAQ,CAAC,EACf,
|
|
6
|
-
"names": ["KeyMap", "entries", "excludeKeys", "table", "value", "entity", "meta", "x", "EntityMap", "_EntityMap", "KeyMap", "component", "definition", "componentName", "componentDataMap", "entityId", "cm", "map", "componentData", "entityMap", "dataMap", "json", "ecs", "restored", "key", "value", "funcFilter", "traceHandler", "target", "propKey", "targetValue", "args", "ecs", "EntityMap"]
|
|
4
|
+
"sourcesContent": ["/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\nimport type { KeyCollection } from './definitions.js';\n\n/**\n * Class for storing keys and values\n */\nexport class KeyMap<TKey, TValue> extends Map<TKey, TValue> {\n\n constructor(entries: any[] = []) {\n super(entries)\n }\n\n firstEntry(): [key: TKey, value: TValue] | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.entries();\n const result = iterator.next();\n return result.value;\n }\n\n firstKey(): TKey | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.keys();\n const result = iterator.next();\n return result.value;\n }\n\n firstValue(): TValue | undefined {\n if (this.size === 0) return undefined;\n\n const iterator = this.values();\n const result = iterator.next();\n return result.value;\n }\n\n toJSON() {\n return { __KeyMap__: 1, iterable: [...this.entries()] }\n }\n\n toTable(excludeKeys = false): any[] {\n const table = []\n for (const [key, value] of this.entries()) {\n const entity = value as { new(): TValue }\n const meta: KeyCollection<any> = {}\n if (excludeKeys == false) meta[\"entity.key\"] = key\n meta[\"entity.type\"] = entity.constructor.name\n table.push({ ...meta, ...entity })\n }\n return table\n }\n\n /**\n * Outputs keys and values in a tabular format to the console\n */\n printTable(excludeKeys = false): void {\n console.table(this.toTable(excludeKeys))\n }\n\n entries(): MapIterator<[TKey, TValue]> {\n return super.entries().filter(x => x !== undefined && x !== null)\n }\n\n keys(): MapIterator<TKey> {\n return super.keys().filter(x => x !== undefined && x !== null)\n }\n\n values(): MapIterator<TValue> {\n return super.values().filter(x => x !== undefined && x !== null)\n }\n\n static get [Symbol.species]() { return Map; }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * @module ecsjs\n */\nimport {\n type ComponentClass,\n type ComponentDataMap,\n type ComponentMap,\n type IComponent\n} from './definitions.js';\nimport { KeyMap } from './key-map.js';\n\n/**\n * Class for storing entities and their relationships\n * \n * @class EntityMap\n */\nexport class EntityMap {\n\n // entity class maps\n private componentMaps: ComponentMap = new KeyMap()\n\n private nextId: number = 0\n\n constructor() { }\n\n /**\n * Registers a component class\n * \n * @example\n *\n * ```javascript\n * // component class\n * class MyComponent {\n * constructor(x) {\n * this.x = x;\n * }\n * }\n *\n * ecs.register(MyComponent);\n * // or\n * ecs.register(MyComponent1, MyComponent2);\n * ```\n */\n register(...component: ComponentClass<any>[]) {\n for (const definition of component) {\n const componentName = definition.name;\n if (componentName === undefined)\n throw new ReferenceError(\"The component class does not have a name defined. i.e. a constructor name\");\n\n // create the component map\n const componentDataMap: ComponentDataMap<any> = new KeyMap();\n this.componentMaps.set(componentName, componentDataMap);\n }\n\n // chain\n return this;\n }\n\n /**\n * Retrieves an entity map by it's registered type\n */\n getMap<T extends IComponent>(component: ComponentClass<T>): ComponentDataMap<T> | undefined {\n return this.componentMaps.get(component.name);\n }\n\n /**\n * Get the first entity entry for a component class\n * \n * @example\n *\n * ```javascript\n * const [entityId, player] = ecs.firstEntry(PlayerComponent) ?? [];\n * ```\n */\n firstEntry<T extends IComponent>(component: ComponentClass<T>) {\n return this.getMap(component)?.firstEntry();\n }\n\n /**\n * Get the first entity id for a component class\n * \n * @example\n *\n * ```javascript\n * const entityId = ecs.firstKey(PlayerComponent);\n * ```\n */\n firstKey<T extends IComponent>(component: ComponentClass<T>) {\n return this.getMap(component)?.firstKey();\n }\n\n /**\n * Get the first entity component value for a component class\n * \n * @example\n *\n * ```javascript\n * const player = ecs.firstValue(PlayerComponent);\n * ```\n */\n firstValue<T extends IComponent>(component: ComponentClass<T>) {\n return this.getMap(component)?.firstValue();\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.get(entityId, Components.PositionComponent);\n * ```\n */\n get<T extends IComponent>(entityId: number, component: ComponentClass<T>): T {\n let cm = this.componentMaps.get(component.name);\n if (cm === undefined)\n throw new ReferenceError(`\"${component}\" component class does not exist in the componentMaps`);\n\n return cm.get(entityId);\n }\n\n /**\n * Get multiple component values related to an entity\n * \n * @param entityId\n * @param components: ComponentClass<any>[]\n * \n * @example\n *\n * ```javascript\n * // get an entity\n * const [player, position] = ecs.getValues(entityId, PlayerComponent, PositionComponent);\n * ```\n */\n getValues<\n T1 extends IComponent,\n T2 extends IComponent,\n T3 extends IComponent,\n T4 extends IComponent,\n T5 extends IComponent\n >(\n entityId: number,\n a: ComponentClass<T1>,\n b: ComponentClass<T2>,\n c?: ComponentClass<T3>,\n d?: ComponentClass<T4>,\n e?: ComponentClass<T5>\n ): [T1, T2, T3, T4, T5];\n getValues(entityId: number, ...components: ComponentClass<any>[]): IComponent[] {\n return components.map(x => this.get(entityId, x))\n }\n\n /**\n * Retrieves an entity from its registered entity map\n *\n * @example\n *\n * ```javascript\n * // get an entity\n * let positionData = ecs.getByKey(entityId, \"PositionComponent\");\n * ```\n */\n getByKey(entityId: number, componentName: string) {\n // get the component map\n let map = this.componentMaps.get(componentName);\n if (map === undefined)\n throw new ReferenceError(`\"${componentName}\" component does not exist in the componentMaps`);\n\n return map.get(entityId);\n }\n\n /**\n * Checks if the entity exists in it's registed component map\n * The entity type must be registered before calling this method\n * \n * @example\n *\n * ```javascript\n * // get an entity\n * let exists = ecs.has(entityId, PositionComponent);\n * ```\n */\n has<T extends IComponent>(entityId: number, component: ComponentClass<T>): boolean {\n // get the component map\n let map = this.componentMaps.get(component.name);\n if (map === undefined) return false\n\n return map.has(entityId);\n }\n\n /**\n * Adds or updates an entity to it's registered entity map\n *\n * @example\n *\n * ```javascript\n * // add/update an entity\n * let playerPos = ecs.set(entityId, new Components.PositionComponent(0, 0));\n * ```\n */\n set<T extends IComponent>(entityId: number, componentData: T): T {\n // get the component map\n let map = this.componentMaps.get(componentData.constructor.name);\n if (map === undefined)\n throw new ReferenceError(\n `Component map does not exist using the name: ${componentData.constructor.name}`\n );\n\n // set the entity on the entity map\n map.set(entityId, componentData);\n\n // return instance\n return componentData;\n }\n\n /**\n * Set multiple component values for an entity\n * \n * @param entityId\n * @param components: ComponentClass<any>[]\n * \n * @example\n *\n * ```javascript\n * // write the component values to the entity\n * ecs.setValues(entityId, new PlayerComponent(), new PositionComponent());\n * ```\n */\n setValues<\n T1 extends IComponent,\n T2 extends IComponent,\n T3 extends IComponent,\n T4 extends IComponent,\n T5 extends IComponent\n >(\n entityId: number,\n a: T1,\n b: T2,\n c?: T3,\n d?: T4,\n e?: T5\n ): [T1, T2, T3, T4, T5];\n setValues(entityId: number, ...components: IComponent[]): IComponent[] {\n return components.map(x => this.set(entityId, x))\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.remove(entityId, Components.PositionComponent);\n * ```\n */\n remove<T extends IComponent>(entityId: number, component: ComponentClass<T>) {\n return this.removeByKey(entityId, component.name);\n }\n\n /**\n * Removes an entity from its entity map.\n * Removes any related Node entities that are mapped to the entity\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * EntityMap.removeByKey(entityId, \"PositionComponent\");\n * ```\n */\n removeByKey(entityId: number, componentName: string) {\n // get the entity map\n let entityMap = this.componentMaps.get(componentName);\n\n // ensure the map is defined\n if (entityMap === undefined)\n throw new ReferenceError(`A registered entity map does not exist for the given key: ${componentName}`);\n\n // get the entity\n let entity = entityMap.get(entityId);\n if (entity === undefined) return false;\n\n // remove the entity from the entity map\n return entityMap.delete(entityId);\n }\n\n /**\n * Destroys an entity across all component maps\n */\n destroyEntity(entityId: number) {\n this.componentMaps.forEach((dataMap: ComponentDataMap<any>) => {\n if (dataMap.has(entityId)) dataMap.delete(entityId);\n });\n }\n\n // TODO generate a non repeatable id for network play?\n getNextId() {\n this.nextId++;\n return this.nextId;\n }\n\n clear() {\n this.componentMaps.clear();\n return this;\n }\n\n /**\n * Prints all component maps in a tabular format to the console\n */\n printTable() {\n this.componentMaps.forEach(map => console.table(map.toTable(true)));\n return this;\n }\n\n /**\n * Prints an entity component data in tabular format to the console\n */\n printEntity(entityId: number) {\n for (const map of this.componentMaps.values()) {\n if (map.has(entityId) === false) continue;\n\n const data = map.get(entityId);\n const columns = {\n name: data.constructor.name,\n ...data\n };\n console.table({ [entityId]: columns });\n }\n\n return this;\n }\n\n /**\n * Parse's the JSON and returns an EntityMap object\n * \n * @example\n *\n * ```javascript\n * // remove an entity from its entity map\n * const json = JSON.stringfy(ecs);\n * const restoredMap = EntityMap.parse(json);\n * ```\n */\n static parse(json: string) {\n const ecs = new EntityMap();\n\n const restored = JSON.parse(json, function (key: string, value: any) {\n\n if (value.hasOwnProperty('componentMaps')) {\n Reflect.setPrototypeOf(value, EntityMap.prototype);\n return value;\n }\n\n if (value.hasOwnProperty('__KeyMap__')) {\n return new KeyMap(value.iterable);\n }\n\n return this[key];\n });\n\n ecs.componentMaps = restored.componentMaps;\n ecs.nextId = restored.nextId;\n return ecs;\n }\n\n /**\n * A tracing method used for debugging.\n * Intercepts all functions specified and logs each call to the console.\n * \n * @method createWithTracing\n * @static\n * @param {Array} funcFilter A list of function names you want to intercept. If no function names are specified then will log all functions called\n * @return {EntityMap} A new entity map with tracing enabled\n */\n static createWithTracing(funcFilter: any) {\n let traceHandler = {\n get(target: any, propKey: string) {\n const targetValue = target[propKey]\n\n if (typeof targetValue === 'function' && (funcFilter.length === 0 || funcFilter.includes(propKey))) {\n return function (this: any, ...args: any[]) {\n console.groupCollapsed('ecs trace', propKey, args);\n console.trace();\n console.groupEnd();\n return targetValue.apply(this, args);\n }\n }\n\n return targetValue;\n }\n }\n\n return new Proxy(new EntityMap(), traceHandler)\n }\n\n}", "/*\n ecsjs is an entity component system library for JavaScript\n Copyright (C) 2014 Peter Flannery\n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU Affero General Public License as\n published by the Free Software Foundation, either version 3 of the\n License, or (at your option) any later version.\n\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU Affero General Public License for more details.\n\n You should have received a copy of the GNU Affero General Public License\n along with this program. If not, see <http://www.gnu.org/licenses/>.\n */\n\n/**\n * ### An entity component system library for JavaScript\n *\n * @module ecsjs\n * @main ecsjs\n */\nimport { EntityMap } from './entity-map.js'\n\nexport const ecs = new EntityMap()\n\nif (typeof window !== 'undefined') {\n // @ts-ignore: exports to window\n window.ecs = ecs\n} else if (typeof module !== 'undefined' && module !== null) {\n // exports to nodejs\n module.exports = { ecs };\n}"],
|
|
5
|
+
"mappings": "AAsBO,IAAMA,EAAN,cAAmC,GAAkB,CAE1D,YAAYC,EAAiB,CAAC,EAAG,CAC/B,MAAMA,CAAO,CACf,CAEA,YAAqD,CACnD,OAAI,KAAK,OAAS,EAAG,OAEJ,KAAK,QAAQ,EACN,KAAK,EACf,KAChB,CAEA,UAA6B,CAC3B,OAAI,KAAK,OAAS,EAAG,OAEJ,KAAK,KAAK,EACH,KAAK,EACf,KAChB,CAEA,YAAiC,CAC/B,OAAI,KAAK,OAAS,EAAG,OAEJ,KAAK,OAAO,EACL,KAAK,EACf,KAChB,CAEA,QAAS,CACP,MAAO,CAAE,WAAY,EAAG,SAAU,CAAC,GAAG,KAAK,QAAQ,CAAC,CAAE,CACxD,CAEA,QAAQC,EAAc,GAAc,CAClC,IAAMC,EAAQ,CAAC,EACf,OAAW,CAACC,EAAKC,CAAK,IAAK,KAAK,QAAQ,EAAG,CACzC,IAAMC,EAASD,EACTE,EAA2B,CAAC,EAC9BL,GAAe,KAAOK,EAAK,YAAY,EAAIH,GAC/CG,EAAK,aAAa,EAAID,EAAO,YAAY,KACzCH,EAAM,KAAK,CAAE,GAAGI,EAAM,GAAGD,CAAO,CAAC,CACnC,CACA,OAAOH,CACT,CAKA,WAAWD,EAAc,GAAa,CACpC,QAAQ,MAAM,KAAK,QAAQA,CAAW,CAAC,CACzC,CAEA,SAAuC,CACrC,OAAO,MAAM,QAAQ,EAAE,OAAOM,GAAwBA,GAAM,IAAI,CAClE,CAEA,MAA0B,CACxB,OAAO,MAAM,KAAK,EAAE,OAAOA,GAAwBA,GAAM,IAAI,CAC/D,CAEA,QAA8B,CAC5B,OAAO,MAAM,OAAO,EAAE,OAAOA,GAAwBA,GAAM,IAAI,CACjE,CAEA,WAAY,OAAO,OAAO,GAAI,CAAE,OAAO,GAAK,CAE9C,ECvDO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O,MAAMC,EAAc,CACzB,IAAMC,EAAM,IAAIjB,EAEVkB,EAAW,KAAK,MAAMF,EAAM,SAAUG,EAAaC,EAAY,CAEnE,OAAIA,EAAM,eAAe,eAAe,GACtC,QAAQ,eAAeA,EAAOpB,EAAU,SAAS,EAC1CoB,GAGLA,EAAM,eAAe,YAAY,EAC5B,IAAInB,EAAOmB,EAAM,QAAQ,EAG3B,KAAKD,CAAG,CACjB,CAAC,EAED,OAAAF,EAAI,cAAgBC,EAAS,cAC7BD,EAAI,OAASC,EAAS,OACfD,CACT,CAWA,OAAO,kBAAkBI,EAAiB,CACxC,IAAIC,EAAe,CACjB,IAAIC,EAAaC,EAAiB,CAChC,IAAMC,EAAcF,EAAOC,CAAO,EAElC,OAAI,OAAOC,GAAgB,aAAeJ,EAAW,SAAW,GAAKA,EAAW,SAASG,CAAO,GACvF,YAAwBE,EAAa,CAC1C,eAAQ,eAAe,YAAaF,EAASE,CAAI,EACjD,QAAQ,MAAM,EACd,QAAQ,SAAS,EACVD,EAAY,MAAM,KAAMC,CAAI,CACrC,EAGKD,CACT,CACF,EAEA,OAAO,IAAI,MAAM,IAAIzB,EAAasB,CAAY,CAChD,CAEF,ECtYO,IAAMK,EAAM,IAAIC,EAEnB,OAAO,OAAW,IAEpB,OAAO,IAAMD,EACJ,OAAO,OAAW,KAAe,SAAW,OAErD,OAAO,QAAU,CAAE,IAAAA,CAAI",
|
|
6
|
+
"names": ["KeyMap", "entries", "excludeKeys", "table", "key", "value", "entity", "meta", "x", "EntityMap", "_EntityMap", "KeyMap", "component", "definition", "componentName", "componentDataMap", "entityId", "cm", "components", "x", "map", "componentData", "entityMap", "dataMap", "data", "columns", "json", "ecs", "restored", "key", "value", "funcFilter", "traceHandler", "target", "propKey", "targetValue", "args", "ecs", "EntityMap"]
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module ecsjs
|
|
3
3
|
*/
|
|
4
|
-
import { type
|
|
4
|
+
import { type ComponentClass, type ComponentDataMap, type IComponent } from './definitions.js';
|
|
5
5
|
/**
|
|
6
6
|
* Class for storing entities and their relationships
|
|
7
7
|
*
|
|
@@ -75,6 +75,20 @@ export declare class EntityMap {
|
|
|
75
75
|
* ```
|
|
76
76
|
*/
|
|
77
77
|
get<T extends IComponent>(entityId: number, component: ComponentClass<T>): T;
|
|
78
|
+
/**
|
|
79
|
+
* Get multiple component values related to an entity
|
|
80
|
+
*
|
|
81
|
+
* @param entityId
|
|
82
|
+
* @param components: ComponentClass<any>[]
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
*
|
|
86
|
+
* ```javascript
|
|
87
|
+
* // get an entity
|
|
88
|
+
* const [player, position] = ecs.getValues(entityId, PlayerComponent, PositionComponent);
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
getValues<T1 extends IComponent, T2 extends IComponent, T3 extends IComponent, T4 extends IComponent, T5 extends IComponent>(entityId: number, a: ComponentClass<T1>, b: ComponentClass<T2>, c?: ComponentClass<T3>, d?: ComponentClass<T4>, e?: ComponentClass<T5>): [T1, T2, T3, T4, T5];
|
|
78
92
|
/**
|
|
79
93
|
* Retrieves an entity from its registered entity map
|
|
80
94
|
*
|
|
@@ -109,6 +123,20 @@ export declare class EntityMap {
|
|
|
109
123
|
* ```
|
|
110
124
|
*/
|
|
111
125
|
set<T extends IComponent>(entityId: number, componentData: T): T;
|
|
126
|
+
/**
|
|
127
|
+
* Set multiple component values for an entity
|
|
128
|
+
*
|
|
129
|
+
* @param entityId
|
|
130
|
+
* @param components: ComponentClass<any>[]
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
*
|
|
134
|
+
* ```javascript
|
|
135
|
+
* // write the component values to the entity
|
|
136
|
+
* ecs.setValues(entityId, new PlayerComponent(), new PositionComponent());
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
setValues<T1 extends IComponent, T2 extends IComponent, T3 extends IComponent, T4 extends IComponent, T5 extends IComponent>(entityId: number, a: T1, b: T2, c?: T3, d?: T4, e?: T5): [T1, T2, T3, T4, T5];
|
|
112
140
|
/**
|
|
113
141
|
* Removes an entity from its entity map.
|
|
114
142
|
* Removes any related Node entities that are mapped to the entity
|
|
@@ -140,9 +168,13 @@ export declare class EntityMap {
|
|
|
140
168
|
getNextId(): number;
|
|
141
169
|
clear(): this;
|
|
142
170
|
/**
|
|
143
|
-
* Prints all
|
|
171
|
+
* Prints all component maps in a tabular format to the console
|
|
144
172
|
*/
|
|
145
173
|
printTable(): this;
|
|
174
|
+
/**
|
|
175
|
+
* Prints an entity component data in tabular format to the console
|
|
176
|
+
*/
|
|
177
|
+
printEntity(entityId: number): this;
|
|
146
178
|
/**
|
|
147
179
|
* Parse's the JSON and returns an EntityMap object
|
|
148
180
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entity-map.d.ts","sourceRoot":"","sources":["../../../src/entity-map.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,OAAO,EACL,KAAK,
|
|
1
|
+
{"version":3,"file":"entity-map.d.ts","sourceRoot":"","sources":["../../../src/entity-map.ts"],"names":[],"mappings":"AAkBA;;GAEG;AACH,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAErB,KAAK,UAAU,EAChB,MAAM,kBAAkB,CAAC;AAG1B;;;;GAIG;AACH,qBAAa,SAAS;IAGpB,OAAO,CAAC,aAAa,CAA6B;IAElD,OAAO,CAAC,MAAM,CAAY;;IAI1B;;;;;;;;;;;;;;;;;OAiBG;IACH,QAAQ,CAAC,GAAG,SAAS,EAAE,cAAc,CAAC,GAAG,CAAC,EAAE;IAe5C;;OAEG;IACH,MAAM,CAAC,CAAC,SAAS,UAAU,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS;IAI3F;;;;;;;;OAQG;IACH,UAAU,CAAC,CAAC,SAAS,UAAU,EAAE,SAA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U,IAAI,MAAM,GAAG,SAAS;IAQhC,MAAM;;;;IAIN,OAAO,CAAC,WAAW,UAAQ,GAAG,GAAG,EAAE;
|
|
1
|
+
{"version":3,"file":"key-map.d.ts","sourceRoot":"","sources":["../../../src/key-map.ts"],"names":[],"mappings":"AAmBA;;GAEG;AACH,qBAAa,MAAM,CAAC,IAAI,EAAE,MAAM,CAAE,SAAQ,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC;gBAE7C,OAAO,GAAE,GAAG,EAAO;IAI/B,UAAU,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,SAAS;IAQpD,QAAQ,IAAI,IAAI,GAAG,SAAS;IAQ5B,UAAU,IAAI,MAAM,GAAG,SAAS;IAQhC,MAAM;;;;IAIN,OAAO,CAAC,WAAW,UAAQ,GAAG,GAAG,EAAE;IAYnC;;OAEG;IACH,UAAU,CAAC,WAAW,UAAQ,GAAG,IAAI;IAIrC,OAAO,IAAI,WAAW,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAItC,IAAI,IAAI,WAAW,CAAC,IAAI,CAAC;IAIzB,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC;IAI7B,MAAM,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,mBAAkB;CAE9C"}
|
package/package.json
CHANGED
package/src/entity-map.ts
CHANGED
|
@@ -20,12 +20,12 @@
|
|
|
20
20
|
* @module ecsjs
|
|
21
21
|
*/
|
|
22
22
|
import {
|
|
23
|
-
type ComponentDataMap,
|
|
24
23
|
type ComponentClass,
|
|
24
|
+
type ComponentDataMap,
|
|
25
25
|
type ComponentMap,
|
|
26
26
|
type IComponent
|
|
27
27
|
} from './definitions.js';
|
|
28
|
-
import { KeyMap } from './key-map.js'
|
|
28
|
+
import { KeyMap } from './key-map.js';
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* Class for storing entities and their relationships
|
|
@@ -138,6 +138,37 @@ export class EntityMap {
|
|
|
138
138
|
return cm.get(entityId);
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Get multiple component values related to an entity
|
|
143
|
+
*
|
|
144
|
+
* @param entityId
|
|
145
|
+
* @param components: ComponentClass<any>[]
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
*
|
|
149
|
+
* ```javascript
|
|
150
|
+
* // get an entity
|
|
151
|
+
* const [player, position] = ecs.getValues(entityId, PlayerComponent, PositionComponent);
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
getValues<
|
|
155
|
+
T1 extends IComponent,
|
|
156
|
+
T2 extends IComponent,
|
|
157
|
+
T3 extends IComponent,
|
|
158
|
+
T4 extends IComponent,
|
|
159
|
+
T5 extends IComponent
|
|
160
|
+
>(
|
|
161
|
+
entityId: number,
|
|
162
|
+
a: ComponentClass<T1>,
|
|
163
|
+
b: ComponentClass<T2>,
|
|
164
|
+
c?: ComponentClass<T3>,
|
|
165
|
+
d?: ComponentClass<T4>,
|
|
166
|
+
e?: ComponentClass<T5>
|
|
167
|
+
): [T1, T2, T3, T4, T5];
|
|
168
|
+
getValues(entityId: number, ...components: ComponentClass<any>[]): IComponent[] {
|
|
169
|
+
return components.map(x => this.get(entityId, x))
|
|
170
|
+
}
|
|
171
|
+
|
|
141
172
|
/**
|
|
142
173
|
* Retrieves an entity from its registered entity map
|
|
143
174
|
*
|
|
@@ -190,13 +221,46 @@ export class EntityMap {
|
|
|
190
221
|
// get the component map
|
|
191
222
|
let map = this.componentMaps.get(componentData.constructor.name);
|
|
192
223
|
if (map === undefined)
|
|
193
|
-
throw new ReferenceError(
|
|
224
|
+
throw new ReferenceError(
|
|
225
|
+
`Component map does not exist using the name: ${componentData.constructor.name}`
|
|
226
|
+
);
|
|
194
227
|
|
|
195
228
|
// set the entity on the entity map
|
|
196
229
|
map.set(entityId, componentData);
|
|
197
230
|
|
|
198
231
|
// return instance
|
|
199
|
-
return componentData
|
|
232
|
+
return componentData;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Set multiple component values for an entity
|
|
237
|
+
*
|
|
238
|
+
* @param entityId
|
|
239
|
+
* @param components: ComponentClass<any>[]
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
*
|
|
243
|
+
* ```javascript
|
|
244
|
+
* // write the component values to the entity
|
|
245
|
+
* ecs.setValues(entityId, new PlayerComponent(), new PositionComponent());
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
setValues<
|
|
249
|
+
T1 extends IComponent,
|
|
250
|
+
T2 extends IComponent,
|
|
251
|
+
T3 extends IComponent,
|
|
252
|
+
T4 extends IComponent,
|
|
253
|
+
T5 extends IComponent
|
|
254
|
+
>(
|
|
255
|
+
entityId: number,
|
|
256
|
+
a: T1,
|
|
257
|
+
b: T2,
|
|
258
|
+
c?: T3,
|
|
259
|
+
d?: T4,
|
|
260
|
+
e?: T5
|
|
261
|
+
): [T1, T2, T3, T4, T5];
|
|
262
|
+
setValues(entityId: number, ...components: IComponent[]): IComponent[] {
|
|
263
|
+
return components.map(x => this.set(entityId, x))
|
|
200
264
|
}
|
|
201
265
|
|
|
202
266
|
/**
|
|
@@ -262,11 +326,29 @@ export class EntityMap {
|
|
|
262
326
|
}
|
|
263
327
|
|
|
264
328
|
/**
|
|
265
|
-
* Prints all
|
|
329
|
+
* Prints all component maps in a tabular format to the console
|
|
266
330
|
*/
|
|
267
331
|
printTable() {
|
|
268
332
|
this.componentMaps.forEach(map => console.table(map.toTable(true)));
|
|
269
|
-
return this
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Prints an entity component data in tabular format to the console
|
|
338
|
+
*/
|
|
339
|
+
printEntity(entityId: number) {
|
|
340
|
+
for (const map of this.componentMaps.values()) {
|
|
341
|
+
if (map.has(entityId) === false) continue;
|
|
342
|
+
|
|
343
|
+
const data = map.get(entityId);
|
|
344
|
+
const columns = {
|
|
345
|
+
name: data.constructor.name,
|
|
346
|
+
...data
|
|
347
|
+
};
|
|
348
|
+
console.table({ [entityId]: columns });
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return this;
|
|
270
352
|
}
|
|
271
353
|
|
|
272
354
|
/**
|
package/src/key-map.ts
CHANGED
|
@@ -56,13 +56,11 @@ export class KeyMap<TKey, TValue> extends Map<TKey, TValue> {
|
|
|
56
56
|
|
|
57
57
|
toTable(excludeKeys = false): any[] {
|
|
58
58
|
const table = []
|
|
59
|
-
for (const value of this.entries()) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (excludeKeys == false) meta["entity.key"] = value[0]
|
|
59
|
+
for (const [key, value] of this.entries()) {
|
|
60
|
+
const entity = value as { new(): TValue }
|
|
61
|
+
const meta: KeyCollection<any> = {}
|
|
62
|
+
if (excludeKeys == false) meta["entity.key"] = key
|
|
64
63
|
meta["entity.type"] = entity.constructor.name
|
|
65
|
-
|
|
66
64
|
table.push({ ...meta, ...entity })
|
|
67
65
|
}
|
|
68
66
|
return table
|