UNPKG

@udraft/core

Version:

uDraft is a language and stack agnostic code-generation tool that simplifies full-stack development by converting a single YAML file into code for rapid development.

50 lines (41 loc) 1.11 kB
import { UAttribute } from "./attribute"; export class UField { private _name: string; private _type: string; private _attributes: UAttribute<any>[] = []; constructor(name: string, type: string) { this._name = name; this._type = type; } $name() { return this._name; } $type() { return this._type; } $attribute<Type>(attribute: UAttribute<Type> | string) { const a = this._attributes.find((attr) => typeof attribute == "string" ? attr.$name() == attribute : attr.$name() == attribute.$name() ); return a ? a : null; } $attributes() { return [...this._attributes]; } $clone(name: string) { return new UField(name, this.$type()).attributes(this.$attributes()); } attributes(attributes: UAttribute<any>[]) { this.remove(attributes); this._attributes = this._attributes.concat(attributes); return this; } remove(attributes: UAttribute<any>[]) { this._attributes = this._attributes.filter( (attribute) => !attributes.some((a) => a.$name() == attribute.$name()) ); return this; } }