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

0.0.8 • Public • Published

@ddd-ts/value

Eases creation of value objects.

Installation

yarn add @ddd-ts/value

or

npm install @ddd-ts/value

Features

  • [x] string/number primitives
  • [x] serialization/deserialization
  • [x] composition
  • [x] inheritance
  • [ ] validation on deserialization
  • [ ] optionnal ? not sure if support will be needed

Usage

For an exhaustive list of usages, please refer to the test file

import :

import { Value } from "@ddd-ts/value";

string :

class TaskName extends Value(String) {}

number :

class Duration extends Value(Number) {
  static aMinute() {
    return new Duration(60);
  }

  multiply(multiplier: number) {
    return new Duration(this.value * multiplier);
  }
}

list :

class Groceries extends Value([String]) {
  get size() {
    return this.value.length;
  }

  get estimatedShoppingDuration() {
    return Duration.aMinute().multiply(this.size);
  }
}

composite with primitives :

class GeoCoordinates extends Value({ lat: Number, lon: Number }) {
  static EARTH_RADIUS = 6_371_000;

  distanceFrom(other: GeoCoordinates) {
    // ...
  }
}

composite with values :

class Travel extends Value({
  origin: GeoCoordinates,
  destination: GeoCoordinates,
}) {
  get distance() {
    const { origin, destination } = this.value;

    return origin.distanceFrom(destination);
  }
}

serialization / deserialization :

const name = TaskName.deserialize("Documentation");
name.serialize(); // Documentation

validation :

class TaskName extends Value(String) {
  ensureValidity() {
    if (this.value.length > 10) {
      throw new Error("Task name too long !");
    }
  }
}

TaskName.deserialize("This task name is too long...").ensureValidity();

Readme

Keywords

none

Package Sidebar

Install

npm i @ddd-ts/value

Weekly Downloads

0

Version

0.0.8

License

MIT

Unpacked Size

31.1 kB

Total Files

14

Last publish

Collaborators

  • yooooomi
  • aetherall