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

yaff-entitybuilder

v1.0.5

Published

javascript small tool for constructing an entity

Downloads

8

Readme

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