npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ecos

v0.2.1

Published

Entity Component System

Downloads

2

Readme

#Entity Component System

Simple system to create and utilize objects in the right way.

##Create objects

var factory = require('ecos').factory;
var entityFactory = factory.create({
    props: ['x', 'y'],
    name: 'my_entity'
});

var entity = entityFactory.create();
console.log(entity);
{
    x: undefined,
    y: undefined,
    id: 0,
    type: 'my_entity'
}

##Get objects

var objects = require('ecos').objects;
var entity = objects.get(0);

console.log(entity);
{
    x: undefined,
    y: undefined,
    id: 0,
    type: 'my_entity'
}

##Ideas behind this library

  • Each entity is stored in unified container ecos.objects and accessible by its id
  • Entity should not have direct link to other entities. Parent entity store identificators of child entities instead
  • To get child entity just use ecos.objects.get(childId);
  • Since entities are stored only in one container then no memory leaks will occur when objects will be deleted
  • Different entities can share same methods and extenders and inherit same behaviour without prototype inheritance

##Instalation

As npm package:

npm install ecos --save

As bower package:

bower install ecos --save

##Documentation

###Factory

To create an entity first you need to create a factory that will produce entities:

var factory = require('ecos').factory;
var entityFactory = factory.create(options);

####Options:

  • name - factory identificator, required string
  • props - list of entity properties that will be initialized with undefined, [ string ]
  • methods - list of entity methods, all methods must be registered with factory.registerMethod, [ string ]
  • extend - list of extenders that will be applied to entity, all extenders must be registered with factory.registerExtender, [ string ]
  • presets - list of presets that will be applied to entity, all presets must be registered with factory.registerPreset. [ string ]
  • default - list of entity properties with predefined values, will overwrite props options, { propName: propValue }
  • custom - list of entity properties with predefined values, will overwrite props and default options, is entity with this set will be accessible through entityFactory.customEntity() call, { customEntityName: { propName: propValue } }

####Create basic entity: Will create entity with fields.

var factory = require('ecos').factory;
var entityFactory = factory.create({
    props: [ 'x', 'y' ],
    name: 'example'
});
entityFactory.create();

Result:

{
    type: 'example',
    x: undefined,
    y: undefined,
    id: 0
}

If you pass object with fields to entity constructor then factory will create entity with specified fields defined:

entityFactory.create({x: 100, y: 200});

Result:

{
    type: 'example',
    x: 100,
    y: 200,
    id: 0
}

####Create entity with method: Use factory.registerMethod to register method that can be assigned to entity:

factory.registerMethod('console', function(text){
    console.log(text);
});

var entityFactory = factory.create({
    methods: ['console'],
    name: 'example'
});
entityFactory.create();

Result:

{
    type: 'example',
    console: function
    id: 0
}

####Create entity with GETSET extender: This extender will create field with defined getter and setter.

var extenders = require('ecos').extenders;

factory.registerExtender('value-extender', {
    get: function(){
        return this._value;
    },
    set: function(val){
        this._value = val;
        console.log('Value set to ' + val);
    },
    name: 'value',
    type: extenders.GETSET
});

var entityFactory = factory.create({
    extend: ['value-extender'],
    name: 'example'
});

entityFactory.create();
entity.value = Math.random();

Result:

Value set to 0.758898114

If you wont specify get or set function in extender definition then resulting field wont have getter or setter defined.

####Create entity with FUNCTION extender: This extender will run custom function on object creation.

factory.registerExtender('function-extender', {
    type: extenders.FUNCTION,
    handler: function(thisObject){
        console.log('Creating ' + JSON.stringify(thisObject));
    }
});

var entityFactory = factory.create({
    extend: ['function-extender'],
    name: 'example'
});

entityFactory.create();

Result:

Creating {"type":"example", "id":0}