0.0.8 • Published 2 years ago
@ddd-ts/value v0.0.8
@ddd-ts/value
Eases creation of value objects.
Installation
yarn add @ddd-ts/value
or
npm install @ddd-ts/value
Features
- string/number primitives
- serialization/deserialization
- composition
- 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();
0.0.8
2 years ago
0.0.7
2 years ago
0.0.5
2 years ago
0.0.6
2 years ago
0.0.0-feat.ci-cd.92b33a1
2 years ago
0.0.0-feat.ci-cd.168c9db
2 years ago
0.0.0-feat.ci-cd.8496796
2 years ago
0.0.0-feat.ci-cd.1d955dd
2 years ago
0.0.0-ci-cd.82d8d08
2 years ago
0.0.0-ci-cd.cf9d7c1
2 years ago
0.0.4
2 years ago
0.0.5-ci-cd.0
2 years ago
0.0.3-ci-cd.0
2 years ago
0.0.3-ci.0
2 years ago
0.0.3-1
2 years ago
0.0.3-0
2 years ago
0.0.2
2 years ago
0.0.1
2 years ago