Skip to content

Commit

Permalink
Merge pull request #3 from kevbaldwyn/develop
Browse files Browse the repository at this point in the history
Update documentation
  • Loading branch information
kevbaldwyn authored Dec 20, 2020
2 parents f83aba6 + 030276e commit 25eb133
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ This package is heavily inspiried by [funeralzone/valueobjects](https://github.c
The core of the library is the `ValueObjectInterface`, it enforces immutability by making the underlying value readonly and provides an interface for deserialising (`toNative()`) and comparing (`isSame()`) value objects. It requires a generic type that the vlaue being stored will be:

```typescript
import { ValueObjectInterface } from "ts-valueobjects";

const anEmailAddress: ValueObjectInterface<string> = {
value: '[email protected]',
isSame(object: ValueObjectInterface<string>): boolean {
Expand Down Expand Up @@ -55,7 +57,7 @@ const someCoordinate: ValueObjectInterface<{x:number, y:number}> = {
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`.

```typescript
import { StringScalar } from "StringScalar";
import { StringScalar } from "ts-valueobjects";

// creating the above email example is much easier:
const anEmailAddress = new StringScalar('[email protected]');
Expand All @@ -69,12 +71,28 @@ const anotherEmailAddress = StringScalar.fromNative('[email protected]')
console.log(anEmailAddress.isSame(anotherEmailAddress)); // false
```

## Enum Type Helper
Using the helper for cretaing Enums will throw errors when trying to access properties that do not exist:
```typescript
import { Enumerate, EnumValueObject } from "ts-valueobjects";

class Enumerated extends Enumerate(
class extends EnumValueObject {
static VAL1 = "One";
static VAL2 = "Two";
}
) {}
const value = new Enumerated(Enumerated.VAL3); // will throw an error
const value = new Enumerated(Enumerated.VAL1); // ok
// or
const value = Enumerated.fromNative(Enumerated.VAL1); // ok
```

## Domain Value Objects
The above helpers can be combined with the `DomainObjectFrom()` mixin to allow you to easily create typesafe domain value objects that are more expressive of your domain language. For example:

```typescript
import { StringScalar } from "StringScalar";
import { DomainObjectFrom } from "ValueObject";
import { StringScalar, DomainObjectFrom } from "ts-valueobjects";

class EmailAddress extends DomainObjectFrom(
// the class to extend the domain object from
Expand Down

0 comments on commit 25eb133

Please sign in to comment.