@vicis/decorators
TypeScript icon, indicating that this package has built-in type declarations

1.2.18 • Public • Published

Vicis

Vicis Decorators

JavaScript's Vicis Decorators create serializable objects without a configuration.

Vicis Documentation

npm downloads types build lgtm


Usage

Require CommonJS.

const {
  Cast, Defaults, Exclude, Nullish, Omit, Order, Pick, Rename,
  Required, Replace, Serialize, Transform,
  cast, defaults, defined, exclude, omit, rename,
  replace, serializable, serialize, transform,
} = require("@vicis/decorators");

Import as ECMAScript module.

import {
  Cast, Defaults, Exclude, Nullish, Omit, Order, Pick, Rename,
  Required, Replace, Serialize, Transform,
  cast, defaults, defined, exclude, omit, rename,
  replace, serializable, serialize, transform,
} from "@vicis/decorators";

Classes

Serialize (class)

Configuration

@Serialize({
  pick: ["id", "login"]
})
class MyClass {
  protected id: number | string;
  protected login: string;
}

Cast (class)

Cast →

@Cast({ id: Vicis.INTEGER })
class MyClass {
  protected id: number | string;
}

Defaults (class)

Defaults →

@Defaults({ active: false })
class MyClass {
  protected id: number | string;
  protected login: string;
}

Exclude (class)

Exclude →

@Exclude(["password", "email", /^(?:_)(?:_)?/])
class MyClass {
  protected id: number | string;
  protected login: string;
  protected password: string;
  protected email: string;
}
@Exclude("password", "email", /^(?:_)(?:_)?/)
class MyClass {
  protected id: number | string;
  protected login: string;
  protected password: string;
  protected email: string;
}

Nullish (class)

Nullish →

@Nullish({ active: null })
class MyClass {
  protected id: number | string;
  protected login: string;
}

Omit (class)

Omit →

@Omit(["password", "email"])
class MyClass {
  protected id: number | string;
  protected login: string;
  protected password: string;
  protected email: string;
}
@Omit("password", "email")
class MyClass {
  protected id: number | string;
  protected login: string;
  protected password: string;
  protected email: string;
}

Order (class)

Order →

@Order(["id", "login", "name"])
class MyClass {
  protected id: number | string;
  protected name: string;
  protected login: string;
}
@Order("id", "login", "name")
class MyClass {
  protected id: number | string;
  protected name: string;
  protected login: string;
}

Pick (class)

Pick →

@Pick(["id", "name"])
class MyClass {
  protected id: number | string;
  protected name: string;
  protected login: string;
}
@Pick("id", "name")
class MyClass {
  protected id: number | string;
  protected name: string;
  protected login: string;
}

Rename (class)

Rename →

@Rename({ _uuid: "id" })
class MyClass {
  protected _uuid: string;
}

Required (class)

Required →

@Required(["id", "login"])
class MyClass {
  protected id: number | string;
  protected name: string;
  protected login: string;
}
@Required("id", "login")
class MyClass {
  protected id: number | string;
  protected name: string;
  protected login: string;
}

Replace (class)

Replace →

@Replace({ local: false })
class MyClass {
  protected local: string = "yes";
}

Transform (class)

Transform →

@Transform({ date: (value) => new Date(value) })
class MyClass {
  protected date: string = "2025-06-15";
}

Properties

serialize (property)

Serialize configuration →

Any decorator that does not remove the property mark it as serializable.

@Serialize()
class MyClass {
  @serialize()
  protected id: number | string;
}

Passing string instead of object rename property.

@Serialize()
class MyClass {
  @serialize("ID")
  protected id: number | string;
}

You can use a configuration object.

import { Vicis } from "vicis";
@Serialize()
class MyClass {
  @serialize({
    cast: Vicis.INTEGER,
    required: true,
  })
  protected id: number | string;
}

Or combine multiple decorators.

import { Vicis } from "vicis";
@Serialize()
class MyClass {
  @required
  @cast(Vicis.INTEGER)
  protected id: number | string;
}

cast (property)

Cast →

import { Vicis } from "vicis";
@Serialize()
class MyClass {
  @cast(Vicis.INTEGER)
  protected id: number | string;
}

defaults (property)

Defaults →

@Serialize()
class MyClass {
  @defaults(false)
  protected active: any;
}

defined (property)

Defined →

@Serialize()
class MyClass {
  @defined
  protected email: string;
}

exclude (property)

Exclude →

@Serialize()
class MyClass {
  @exclude
  protected password: string;
}

nullish (property)

Nullish →

@Serialize()
class MyClass {
  @nullish("ok")
  protected active: any;
}

omit (property)

Omit →

@Serialize()
class MyClass {
  @omit
  protected secret: string;
}

pick (property)

Pick →

@Serialize()
class MyClass {
  @pick
  protected id: number | string;
}

rename (property)

Rename →

@Serialize()
class MyClass {
  @rename("firstName")
  protected first_name: string;
}

replace (property)

Replace →

@Serialize()
class MyClass {
  @replace("*****")
  protected hasInformation: string;
}

required (property)

Required →

@Serialize()
class MyClass {
  @required
  protected id: number | string;
}

transform (property)

Transform →

@Serialize()
class MyClass {
  @transform((text) => text.toUpperCase())
  protected abbreviation: string;
}

See also

My Other Projects

r37r0m0d3l open source projects


Package Sidebar

Install

npm i @vicis/decorators

Homepage

vicis.js.org

Weekly Downloads

40

Version

1.2.18

License

MIT

Unpacked Size

85.9 kB

Total Files

10

Last publish

Collaborators

  • r37r0m0d3l