npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@risotto/value-objects

v1.1.1

Published

Risotto library for value objects

Downloads

23

Readme

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';