Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

type-binder

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

type-binder

A JavaScript type-binder.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-92.59%
Maintainers
1
Weekly downloads
 
Created
Source

JavaScript object type binding

Type binding

Given an ES6/TypeScript class

class Person {

    name: string;

    sayHello() {
        return `Hello, my name is ${this.name}!`;
    }

}

from a plain JavaScript object you can easily create an instance of Person

let object = { name: "Foo" }; // or JSON.parse('{ "name": "Foo" }');

let foo = new TypeBinder().bind(object, Person);

foo.sayHello(); // returns "Hello,  my name is Foo!"

To bind also object properties, you can use a decorator

import { bind } from "type-binder";

class Person {

    name: string;

    @bind(Person) parent;

}

let foo = new TypeBinder().bind({ name: "Foo", parent: { name: "Bar" } }, Person);

foo.parent.sayHello(); // returns "Hello, my name is Bar!";

This works also with generic typing, using the @generics decorator

import { generics } from "type-binder";

class Person {

    name: string;

    @generics(Person) children: Set<Person>;

}

let foo = new TypeBinder().bind({ name: "Foo", children: [ { name: "Bar" }, { name: "Baz" } ] });

for (let child of foo.children) {
    child.sayHello();
}

And you can add binding callbacks for additional types

class Person {

    name: string;

    constructor(name: string) {
        this.name = name;
    }

}

let binder = new TypeBinder();

binder.setBindingCallback(Person, object => new Person(object.name));

Object identity

The @identifier decorator takes a function which returns an identifier of that object, which the binder will use to return same instances for all objects with the same identifier (could be scoped, see code).

import { identifier } from "type-binder";

@identifier<Person>(person => person.name)
class Person {

    name: string

}

let binder = new TypeBinder();

let foo1 = binder.bind({ "name": "Foo" });

let foo2 = binder.bind({ "name": "Foo" });

foo1 === foo2; // true

Property tracker

Decorating a property with @track will instruct the binder to save the initial property value in the object's metadata.

import { track } from "type-binder";

class Person {

    @track() name: string

}

let person = new TypeBinder().bind({ name: "Foo" });

person.name = "Bar";

let changed = new TypeBinder().propertyHasChanged(person, "name");

changed === true; // true

Keywords

FAQs

Package last updated on 02 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc