Giter Club home page Giter Club logo

yaff-entitybuilder's Introduction

entitybuilder

Its a small set of methods for building entities by build options and for determine if a given function is registered class definition or it is function which can be invoked.

example

assume we have this:

import { build, isKnownCtor } from 'yaff-entitybuilder';

class MyClass {
    constructor(options) {
        this.cid = 'the class sid';                
        this.options = { ...options };
    }
}

figure 1:

let test1 = build({ class: MyClass, propertyA: 'foo', propertyB: 'bar' });
console.log(test1);

output:

MyClass {
    options: {
        propertyA: 'foo',
        propertyB: 'bar'
    }
}

figure 2:

class MyOtherClass {
    constructor(models, options) {
        this.models = models;
        this.options = options;
    }
}
let test2 = build({ 
    class: MyClass, 
    ctorArguments: [
        [{id: 1}, {id: 2}],
        {
            propertyA: 'foo', propertyB: 'bar'
        }
    ]
});
console.log(test2);

output:

MyOtherClass {
    models: [
        { id: 1 },
        { id: 2 },
    ]
    options: {
        propertyA: 'foo',
        propertyB: 'bar'
    }
}

figure 3:

let result = isKnownCtor(MyClass); // false;
// and
let myclass = build(MyClass); // will throw, because MyClass is not yet registered.

by default know ctors does not include any of your class definition. you should add them manualy

now add this class to known ctors and try again:

addKnownCtor(MyClass);

if (isKnownCtor(MyClass)) { // true;
    console.log(build(MyClass));
} 

output:

MyClass {
    options: {}
}

figure 4:

You can wrap first argument

build(() => MyClass); // OK
build(() => ({ class: MyClass })); // OK

figure 5:

the invoke method

function invoke(arg, invokeArgs = [], invokeContext) {
  if (typeof arg === 'function' && !isKnownCtor(arg)) {
    arg = arg.apply(invokeContext, invokeArgs);
  }
  return arg;
}

class MyNextClass extends MyClass {

    getOption(key) {
        return invoke(this.options[key], [this], this);
    }

}


let test5 = build({ class: MyNextClass, option1: myClass => myClass.cid, option2: String  });
console.log(test5.getOption('option1')); // => "the class sid"
console.log(test5.getOption('option2')); // => String

output:

the class sid
String

API

import { 
    isKnownCtor, removeKnownCtor, addKnownCtor, isKnownCtorInstance
    builderConfig 
    build, normalizeBuildOptions
    invokeValue 
} from 'yaff-builder';

build(arg[, invokeArgs, invokeContext])

arg (required) - buildOptions object or function returned buildOptions.
invokeArgs (optional) - for case when arg is a function, see invokeValue.
invokeContext (optional) - for case when arg is a function, see invokeValue.

Builds entity from buildOptions object. see buildOptions
In case when first argument is a function it will be invoked with given invoke arguments and context. see invokeValue

For examples check reference.md

buildOptions object

{    
    class: function, required
    ctorArguments: [...] optional
    <...>
}

single argument case: new MyClass(options)

variant 1: without ctorArguments property

let buildOptions = {
    class: MyClass,
    property1: 'foo',
    property2: 'bar',
    ...
    propertyN: '...'
}

options will get all properties except class:

{ 
    property1: 'foo',
    property2: 'bar',
    ...
    propertyN: '...'
}

variant 2, strict definition

{
    class:MyClass,
    ctorArguments: [
        {
            property1: 'foo',
            property2: 'bar',
            ...
            propertyN: '...'
        }
    ]
}

builderConfig

shouldThrow: boolean, default: false
Defines the behavior of build() for wrong arguments.

normalizeBuildOptions(arg, invokeArgs, invokeContext)

Tries to normalize given argument to buildOptions. If fails will return undefined.

invokeValue(value, invokeArgs, invokeContext)

Invokes value with provided invokeArgs and invokeContext and return its value in case if its a function and not one of knownCtors otherwise returns value as is.

invokeValue(String); // String
invokeValue(() => 'foo'); // foo

for more details check reference.md.

isKnownCtor(arg)

Checks if given argument is one of known constructors

removeKnownCtor(arg, inherited)

Removes ctor from known constructors.

addKnownCtor(arg)

Adds given argument to known constructors array.

isKnownCtorInstance(arg)

Checks if given argument is instance of any known constructors.


For more details check reference.md

yaff-entitybuilder's People

Contributors

taburetkin avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

gregid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.