This package allows you to represent many classes using a unified interface.
yarn add @jamashita/anden
> node -v
v20.12.2
> npm -v
10.5.0
> yarn -v
1.22.21
git cz
A basic error class that extends the built-in Error
class and can contain other Error
instances as a property.
throw new RuntimeError('An error occured.', error);
This error class can hold multiple Error
instances at once, and it extends the RuntimeError
class.
throw new Errors(new Error('Error 1'), new Error('Error 2'), new Error('Error 3'));
This error class can be used to detect cases that are not handled in a switch. TypeScript will generate an alert when there is an unhandled case and this error is included in the default clause.
This error class is mainly used to indicate that a method is not yet implemented and it extends the RuntimeError
class.
throw new UnimplementedError('This method is not yet implemented');
A class that redefines the built-in Object
.
Generates a hash code for the given obj
. If the object's properties have the same values, this method will return the
same hash code.
Converts the givenn
to string. If n
has a toString()
method, the result of calling that method is
returned. Otherwise, the result of String(n)
is returned.
This method is used in Objet.prototype.toString()
to customize the string representation of an object. It is strongly
recommended to implement this method when extending the Objet
class.
Compares the hash code of this instance with the hash code of the given other
, and returns true
if they are the
same.
Returns the hash code of this instance. If the properties have the same values, this method must return the same hash code.
Calls and returns the result of the Objet.prototype.serialize()
method.
A class for entities in Domain-Driven Design (DDD). It is a concrete class extending the Objet
class, and I
is an
identifier class.
Returns the identifier of the entity. It is used in the entity.equals(other: unknown): boolean
method to compare two
entities.
Compares the identifier of this object with the identifier of the given other
. Returns true
if they are the same.
This method should not be overridden, as it relies on the correct implementation of
the Entity.prototype.getIdentifier()
method.
A class for value objects in Domain-Driven Design (DDD). It is a concrete class extending the Objet
class.
A class to check for circular references in an object.
Returns true
if the given value
has circular references.
Used for methods. If the given value does not meet this requirement, TypeError
will be thrown.
class Klazz {
@Validate()
public doSomething(@BigIntValidation({
conditions: [
{
operator: '>=',
value: 3n
},
{
operator: '<=',
value: 5n
}
]
}) n1: bigint): void {
//
}
}
Used for methods. If the given value does not meet this requirement, TypeError
will be thrown.
class Klazz {
@Validate()
public doSomething(@BooleanValidation() n1: boolean): void {
//
}
}
Used for methods. If the given value does not meet this requirement, TypeError
will be thrown.
class Klazz {
@Validate()
public doSomething(@NumberValidation({
conditions: [
{
operator: '>=',
value: 3
},
{
operator: '<=',
value: 5
}
],
int: false,
noNaN: true,
noInfinity: true
}) n1: number): void {
//
}
}
Used for methods. If the given value does not meet this requirement, TypeError
will be thrown.
class Klazz {
@Validate()
public doSomething(@StringValidation({
type: 'length',
min: 1,
max: 100
}) n1: string): void {
//
}
}
Used for methods. If the given value does not meet this requirement, TypeError
will be thrown.
class Klazz {
@Validate()
public doSomething(@SymbolValidation() n1: symbol): void {
//
}
}
A class for creating a deep copy of an object literal. This class does not check whether the given object literal has circular references or not.
Creates a deep copy of the given object literal.
An interface for objects that can be cloned.
Should return a deep copy of itself.
Returns true
if the given n
has a duplicate
method.
A class for checking the equality of two object literals. This class does not check whether the given object literals have circular references or not.
Returns true
if the two given object literals are the same, false
otherwise.
An interface for objects that can be compared for equality.
Should return true
if this and the given object are the same, false
otherwise.
An interface for objects that can be converted to a JSON object.
Returns the JSON representation of this object.
Returns true
if the given n
has a toJSON
method, false
otherwise.
Class for type narrowing.
An interface that extends both Equatable
and Serializable
interfaces.
Returns a hash code of this instance.
An interface for objects that can be converted to a string.
Returns a string representation of this instance.
A class for generating and working with ULIDs (Universally Unique Lexicographically Sortable Identifier),
extends ValueObject
.
Returns a new ULID instance with a randomly generated ULID value.
Returns a new ULID instance from the given string. May throw a ULIDError
if the given id
is not a valid ULID.
Returns the regular expression used for validating ULIDs.
Returns the length of a ULID.
Returns true
if the given string is a valid ULID, false
otherwise.
Used for methods. If the given value does not meet this requirement, TypeError
will be thrown.
class Klazz {
@Validate()
public doSomething(@ULIDValidation() n1: string): void {
//
}
}
A class for generating and working with UUIDs (Universally Unique Identifiers), extends ValueObject
.
Returns a new UUID instance from the given string. May throw a UUIDError
if the given id
is not a valid UUID.
Returns the regular expression used for validating UUIDs.
Returns the length of a UUID.
Returns a new v4 UUID instance.
Returns a new v5 UUID instance.
Returns a new v6 UUID instance.
Returns a new v7 UUID instance.
Returns true
if the given string is a valid UUID, false
otherwise.
Used for methods. If the given value does not meet this requirement, TypeError
will be thrown.
class Klazz {
@Validate()
public doSomething(@UUIDValidation() n1: string): void {
//
}
}
A class for working with date and time, extends ValueObject
.
Returns the earliest Zeit
instance from the given iterable of Zeit
. May throw ZeitError
if the given zeiten
is
empty.
Returns the latest Zeit
instance from the given iterable of Zeit
. May throw ZeitError
if the given zeiten
is
empty.
Returns the current date and time as a Zeit
instance.
Returns a new Zeit
instance from the given dayjs
instance.
Returns a new Zeit
instance from the given Date
instance.
Returns a new Zeit
instance from the given str
using the given format
. May throw ZeitError
if the given str
cannot be parsed using the given format.
Returns true
if the given str
can be parsed using the given format
.
Returns a new Zeit
instance that is value
unit
earlier than this instance.
const zeit1: Zeit = Zeit.ofString('2000-04-02');
const zeit2: Zeit = zeit1.advance(1, 'day')
console.log(zeit2.toString('YYYY-MM-DD'));
// '2000-04-01'
Returns true
if this instance is after the given other
Zeit
instance.
Returns true
if this instance is before the given other
Zeit
instance.
Returns true
if this instance represents a valid date and time.
Returns a new Zeit instance that is value
unit
later than this instance.
const zeit1: Zeit = Zeit.ofString('2000-04-02');
const zeit2: Zeit = zeit1.postpone(1, 'day')
console.log(zeit2.toString('YYYY-MM-DD'));
// '2000-04-03'
Used for methods. If the given value does not meet this requirement, TypeError
will be thrown.
class Klazz {
@Validate()
public doSomething(@ZeitValidation({
format: 'YYYY-MM-DD'
}) n1: string): void {
//
}
}