Skip to content

Commit aed1120

Browse files
committedJan 9, 2021
docs: improve documentation and examples, add ToC
1 parent d4beacc commit aed1120

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed
 

‎README.md

+73-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
# TS Value Objects
2-
31
![Build](https://github.com/kevbaldwyn/ts-valueobjects/workflows/Build/badge.svg?branch=master)
42
[![Coverage Status](https://coveralls.io/repos/github/kevbaldwyn/ts-valueobjects/badge.svg?branch=master)](https://coveralls.io/github/kevbaldwyn/ts-valueobjects?branch=master)
53
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fkevbaldwyn%2Fts-valueobjects%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/kevbaldwyn/ts-valueobjects/master)
64

5+
- [Core principles](#core-principles)
6+
- [Inspiration](#inspiration)
7+
- [ValueObjectInterface<T>](#valueobjectinterfacet)
8+
- [Type helper classes](#type-helper-classes)
9+
- [StringScalar](#stringscalar)
10+
- [FloatScalar](#floatscalar)
11+
- [BooleanScalar](#booleanscalar)
12+
- [IntegerScalar](#integerscalar)
13+
- [NullScalar](#nullscalar)
14+
- [Enum Type Helper](#enum-type-helper)
15+
- [Composite Value Objects](#composite-value-objects)
16+
- [Domain Value Objects](#domain-value-objects)
17+
- [Nullable Value Objects](#nullable-value-objects)
18+
719
## Core principles
820
This package is built around 3 core principles:
921
1. Value objects MUST be immutable
@@ -54,8 +66,9 @@ const someCoordinate: ValueObjectInterface<{x:number, y:number}> = {
5466
```
5567

5668
## Type helper classes
57-
Creating lots of value objects this way can get verbose so you can use some of the included classes for creating common scalar types (`StringScalar`, `FloatScalar`, `IntegerScalar`, `BooleanScalar`, `NullScalar`), and other more complex types such as `EnumValueObject`.
69+
Creating lots of value objects this way can get verbose so you can use some of the included classes for creating common scalar types (`StringScalar`, `FloatScalar`, `IntegerScalar`, `BooleanScalar`, `NullScalar`).
5870

71+
### StringScalar
5972
```typescript
6073
import { StringScalar } from "ts-valueobjects";
6174

@@ -71,7 +84,59 @@ const anotherEmailAddress = StringScalar.fromNative('another-email@example.com')
7184
console.log(anEmailAddress.isSame(anotherEmailAddress)); // false
7285
```
7386

74-
## Enum Type Helper
87+
### FloatScalar
88+
```typescript
89+
import { FloatScalar } from "ts-valueobjects";
90+
91+
const floatValue = FloatScalar.fromNative(23.5);
92+
const floatValue = new FloatScalar(23.5);
93+
94+
floatValue.isNull();
95+
floatValue.isSame(...);
96+
floatValue.toNative();
97+
```
98+
99+
### BooleanScalar
100+
```typescript
101+
import { BooleanScalar } from "ts-valueobjects";
102+
103+
const boolValue = BooleanScalar.true();
104+
const boolValue = BooleanScalar.false();
105+
const boolValue = BooleanScalar.fromNatiave(true);
106+
const boolValue = new BooleanScalar(true);
107+
108+
boolValue.isNull();
109+
boolValue.isSame(...);
110+
boolValue.toNative();
111+
boolValue.isTrue();
112+
boolValue.isFalse();
113+
```
114+
115+
### IntegerScalar
116+
```typescript
117+
import { IntegerScalar } from "ts-valueobjects";
118+
119+
const integerValue = IntegerScalar.fromNative(BigInt(1));
120+
const integerValue = new IntegerScalar(BigInt(1));
121+
122+
integerValue.isNull();
123+
integerValue.isSame(...);
124+
integerValue.toNative();
125+
```
126+
127+
### NullScalar
128+
```typescript
129+
import { NullScalar } from "ts-valueobjects";
130+
131+
const nullValue = NullScalar.fromNative();
132+
const nullValue = new NullScalar();
133+
134+
integerValue.isNull();
135+
integerValue.isSame(...);
136+
integerValue.toNative();
137+
```
138+
139+
### Enum Type Helper
75140
Using the helper for cretaing Enums will throw errors when trying to access properties that do not exist:
76141
```typescript
77142
import { Enumerate, EnumValueObject } from "ts-valueobjects";
@@ -92,6 +157,8 @@ const value = Enumerated.fromNative(Enumerated.VAL1); // ok
92157
The `CompositeValueObject` allows you to create value objects that are more complex and contain any number of other value objects (including nested `CompositeValueObject`s and Domain Objects).
93158

94159
```typescript
160+
import { CompositeValueObject } from "ts-valueobjects";
161+
95162
class User extends CompositeValueObject<{
96163
name: StringScalar;
97164
email: StringScalar;
@@ -202,6 +269,8 @@ class EmailAddress extends DomainObjectFrom(
202269
The abstract `NullableValueObject` class allows wrapping a `null` and a non-`null` implementation into the same interface as a `ValueObjectInterface`. You just have to define 3 static methods: `fromNative()` which does the null / non-null negotiation, and, `nonNullImplementation()` and `nullImplementation()` which return the relevant implementations for the non-null and the null conditions. These methods should each return a `ValueObjectInterface`. By default `NullableValueObject` includes a `nullImplementation()` that returns a `NullScalar`. However this can be overridden and return any `ValueObjectInterface` implementation you like.
203270

204271
```typescript
272+
import { NullableValueObject, NullOr, StringScalar } from "ts-valueobjects";
273+
205274
class NullableUserName extends NullableValueObject<string> {
206275
public static fromNative(value: NullOr<string>): NullableUserName {
207276
return new this(this.getWhichNullImplementation(value));

0 commit comments

Comments
 (0)
Please sign in to comment.