Qowaiv is a (Single) Value Object library. It aims to model reusable (Single) Value Objects that can be used a wide variety of modeling scenarios, both inside and outside a Domain-driven context.
Supported scenarios include parsing, formatting, validation, (de)serialization, and domain specific logic.
As developement tool, we addopted TypeScript.
A Value Object that can be represented by a single scalar.
information in computer systems.
let empty = Guid.Empty(); // 00000000-0000-0000-0000-000000000000
let next = Guid.newGuid(); // 123E4567-E89B-12D3-A456-426655440000
let str = next.format("B"); // {123E4567-E89B-12D3-A456-426655440000}
As JavaScript does not support method overloading, this interface makes explicit
by including toString()
and format(f: string)
that the format
method
should be the overloaded version of toString
.
interface IFormattable
{
toString(): string;
format(f: string): string;
}
As JavaScript does not have a way to support equals overloading as a lot of other languages do, but it gives something.
interface IEquatable
{
equals(other: any): boolean;
}
Compared with the follow snippet, you could work arround it, although using a
function eq(l, r)
over l === r
is obvious not trivial, or close to ideal.
function eq(l, r) {
if (arguments.length !== 2) { throw new Error('Invalid number of arguments.'); }
if (l !== null && l !== undefined && typeof (l.equals) === 'function') {
return l.equals(r);
}
if (r !== null && r !== undefined && typeof (r.equals) === 'function') {
return r.equals(l);
}
return l === r;
}
To support JSON.stringify()
this interface was introduced.
interface IJsonStringifyable {
toJSON(): any;
}