Class Item<P>Abstract

Description

The Item extensions is designed to form the structure of the object, solve the problem of application architecture, dependency injection and inversion of control.

Features:

  • cast to type properties
  • DI
  • IoC

Example

import * as collection from '@biorate/collection';
const { embed, singletone } = collection;
const BindedSymbol = Symbol('Binded');

@singletone()
class Binded {
public hello = 'world!';
}

class Nested extends collection.Item {
@embed(Nested.Map) public map: Map<any, any> = null;
@embed(Nested.Set) public itemSet: Set<any> = null;
@embed(BindedSymbol) public binded: Binded = null;
}

class Item extends collection.Item {
@embed(Item.Int) public int: number = null;
@embed(Item.Float) public float: number = null;
@embed(Item.String) public string: string = null;
@embed(Item.Bool) public bool: boolean = null;
@embed(Item.Date) public date: Date = null;
@embed(Item.Array) public array: number[] = null;
@embed(Item.Object) public object: Record<string, any> = null;
@embed(Item.Json) public json: Record<string, any> = null;
@embed(Nested) public nested: Nested = null;
@embed(BindedSymbol) public binded: Binded = null;
}

Item.bind(BindedSymbol, Binded);

const data = {
int: 1,
float: 1.1,
string: 'test',
bool: true,
date: new Date(),
array: [1, 2, 3],
object: { a: 1, b: 2 },
json: '{"test": 1}',
nested: {
map: [
[1, 'a'],
[2, 'b'],
],
itemSet: [1, 2, 3],
},
};

const item = new Item();

item.initialize(data);

console.log(item);
// Item {
// int: 1,
// float: 1.1,
// string: 'test',
// bool: true,
// date: 2021-05-19T06:42:26.049Z,
// array: [ 1, 2, 3 ],
// object: { a: 1, b: 2 },
// json: { test: 1 },
// nested: Nested {
// map: Map { 1 => 'a', 2 => 'b' },
// itemSet: Set { 1, 2, 3 },
// binded: Binded { hello: 'world!' }
// }
// }

console.log(item.binded === item.nested.binded); // true

Type Parameters

  • P = {
        parent?: any;
    }

Hierarchy

Constructors

  • Type Parameters

    • P = {
          parent?: any;
      }

    Parameters

    • data: Record<string, any> = null

      data object

    • parent: P = null

      parent item

    Returns Item<P>

    Example

    import * as collection from '@biorate/collection';

    class Item extends collection.Item {
    @embed(Item.Int) public id: number = null;
    @embed(Item.String) public title: string = null;
    }

    const item = new Item().initialize({ id: 1, title: 'one' });

Properties

Array: symbol = Types.Array

Description

cast to Array type symbol

Bool: symbol = Types.Bool

Description

cast to boolean type symbol

Date: symbol = Types.Date

Description

cast to Date type symbol

Float: symbol = Types.Float

Description

cast to float type symbol

Int: symbol = Types.Int

Description

cast to int type symbol

Json: symbol = Types.Json

Description

cast to JSON type symbol

Luxon: symbol = Types.Luxon

Description

cast to Luxon type symbol

Map: symbol = Types.Map

Description

cast to Map type symbol

Object: symbol = Types.Object

Description

cast to Object type symbol

Set: symbol = Types.Set

Description

cast to Set type symbol

String: symbol = Types.String

Description

cast to string type symbol

bindings: Map<string | symbol | Function, Function> = ...

Description

Binding map, for IoC pattern realization

Accessors

Methods

  • Parameters

    • data: Record<string, any>

      data object

    • field: string

      object field

    • Class: (new (...args) => any)

      Instance class

        • new (...args): any
        • Parameters

          • Rest ...args: any[]
            Rest

          Returns any

    Returns void

    Description

    Instantiate and initialize class

  • Parameters

    • data: Record<string, any> = ...

      data object

    Returns Item<P>

    Description

    Initialize object properties

    Example

    import * as collection from '@biorate/collection';

    class Item extends collection.Item {
    @embed(Item.Int) public id: number = null;
    @embed(Item.String) public title: string = null;
    }

    const item = new Item();
    console.log(item); // Item { id: null, title: null }
    item.initialize({ id: 1, title: 'one' });
    console.log(item); // Item { id: 1, title: 'one' }
    item.initialize({ id: 2, title: 'two' });
    console.log(item); // Item { id: 2, title: 'two' }
  • Parameters

    • data: Record<string, any>

      data object

    Returns Item<P>

    Description

    Change data values

    Example

    import * as collection from '@biorate/collection';

    class Item extends collection.Item {
    @embed(Nested.Int) public int: number = null;
    @embed(Nested.String) public string: string = null;
    }

    const item = new Item().initialize({ int: 1, string: 'test' }});
    item.set({ int: 2, string: 'hello' }});
    console.log(item); // Item { int: 2, string: 'hello' }
  • Parameters

    • key: string | symbol | Function

      dependency identifier

    • val: Function

      dependency Class

    Returns Map<string | symbol | Function, Function>

    Description

    Bind string, symbol, or Class to Class

Generated using TypeDoc