@risotto/value-objects
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Table of Contents

Installing

npm install @risotto/value-objects

Once the package is installed, you can import the library using import or require approach:

import { StringValueObject } from '@risotto/value-objects';

StringValueObject

Initialize String value object

import { StringValueObject } from '@risotto/value-objects';

export class ProductName extends StringValueObject {}

const name = new ProductName('foo');
console.log(name.value); // foo

equals()

const name1 = new ProductName('foo');
const name2 = new ProductName('bar');
const name3 = new ProductName('foo');

console.log(name1.equals(name2)); // false
console.log(name1.equals(name3)); // true

NumberValueObject

Initialize Number value object

import { NumberValueObject } from '@risotto/value-objects';

export class ProductStockQuantity extends NumberValueObject {}

const quantity = new ProductStockQuantity(24);
console.log(quantity.value); // 24

isBiggerThan()

const number1 = new ProductStockQuantity(4);
const number2 = new ProductStockQuantity(2);

console.log(number1.isBiggerThan(number2)); // true
console.log(number2.isBiggerThan(number1)); // false

isLessThan()

const number1 = new ProductStockQuantity(2);
const number2 = new ProductStockQuantity(4);

console.log(number1.isLessThan(number2)); // true
console.log(number2.isLessThan(number1)); // false

UUID

Initialize Uuid value object

import { Uuid } from '@risotto/value-objects';

const uuid = new Uuid('d2362e93-8f8e-45ed-8154-28a43f70d551');
console.log(uuid.value); // d2362e93-8f8e-45ed-8154-28a43f70d551

random()

import { Uuid } from '@risotto/value-objects';

const random = Uuid.random();
console.log(random.value); // random uuid

Enum

Initialize Enum value object

import { EnumValueObject } from '@risotto/value-objects';
import { InvalidArgumentError } from '@risotto/value-objects/exceptions';

enum Status {
  IN_STOCK = 'IN_STOCK',
  OUT_OF_STOCK = 'OUT_OF_STOCK'
}

export class ProductStatus extends EnumValueObject<Status> {
  constructor(value: Status) {
    super(value, Object.values(Status))
  }

  // custom implementation from abstract class
  protected throwErrorForInvalidValue(value: Status): void {
    throw new InvalidArgumentError(`The status ${value} is invalid`);
  }
}

usage:

const status = new ProductStatus(Status.IN_STOCK);
console.log(status.value); // IN_STOCK

Custom Value Objects

You can create your custom implementations by extending from any of the next classes: StringValueObject, NumberValueObject, EnumValueObject, Uuid, and ValueObject

Primitives

With Primitives type you can get the primitive values of any class that extends from ValueObject abstract class

i.e:

import { Primitives } from "@risotto/value-objects/primitives";

export class Product {
  constructor(readonly id: ProductId, readonly name: ProductName) {}

  toPrimitives(): Primitives<Product> {
    return {
      id: this.id.value,
      name: this.name.value
    }
  }
}

Exception

All exceptions thrown by Value Objects are instance of InvalidArgumentError and imports as follow:

import { InvalidArgumentError } from '@risotto/value-objects/exceptions';

Package Sidebar

Install

npm i @risotto/value-objects

Weekly Downloads

53

Version

1.1.1

License

MIT

Unpacked Size

46 kB

Total Files

40

Last publish

Collaborators

  • pepeflores97