Skip to content

PlainDate

Christian Prinz edited this page Oct 17, 2022 · 1 revision

Class: PlainDate

This is a more simplified, but also flexible version of a Date which specifically represents just the date (without the time). Related to PlainTime for the time and PlaneDateTime for a combination of both.

Hierarchy

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

Private new PlainDate(props)

Parameters

Name Type
props Required<PlainDateProps>

Overrides

ValueObject.constructor

Defined in

valueObjects/composed/PlainDate.ts:24

Properties

_value

Protected Readonly _value: void

the actual value of the ValueObject

Inherited from

ValueObject._value

Defined in

valueObjects/ValueObject.ts:4


date

Readonly date: number

Defined in

valueObjects/composed/PlainDate.ts:21


month

Readonly month: number

Defined in

valueObjects/composed/PlainDate.ts:20


weekday

Readonly weekday: number

Defined in

valueObjects/composed/PlainDate.ts:22


year

Readonly year: number

Defined in

valueObjects/composed/PlainDate.ts:19

Accessors

value

get value(): T

the actual value of this ValueObject

Returns

T

Inherited from

ValueObject.value

Defined in

valueObjects/ValueObject.ts:13

Methods

compare

compare(other, density?): 0 | 1 | -1

compares this date with a given other PlainDate or 'now' to compare it with the current date.

  • returns -1 if this date is older than the other one
  • returns 0 if the dates are equal
  • returns 1 if this date is more recent than the other one
Examples
'2020-05-25'.compare('2020-09-18') => -1
'2020-05-25'.compare('2020-01-01') => 1
'2020-05-25'.compare('2020-05-06', 'YM') => 0 (with density)

Parameters

Name Type Default value Description
other PlainDate | "now" undefined the PlainDate to compare to / 'now' to use the current date
density PlainDateDensity 'YMD' the density the comparison has to have

Returns

0 | 1 | -1

-1 | 0 | 1 indicating which date is more recent

Defined in

valueObjects/composed/PlainDate.ts:248


createOffset

createOffset(offset, options?): PlainDate

creates a new PlainDate derived from the existing date, with a given offset

Parameters

Name Type
offset Partial<PlainDateProps>
options? CreationOptions

Returns

PlainDate

Defined in

valueObjects/composed/PlainDate.ts:110


createSet

createSet(newData, options?): PlainDate

creates a new PlainDate derived from the existing date, where the given newData partial replaces the old data.

Parameters

Name Type
newData Partial<PlainDateProps>
options? CreationOptions

Returns

PlainDate

Defined in

valueObjects/composed/PlainDate.ts:98


distance

distance(toOther, density?): number

compares this date with a given other PlainDate or 'now' to compare it with the current date and returns the distance between both.

  • distance = other - this
    • positive result = other date was later
    • negative result = other date was earlier
  • always returns the distance in days (independent from the density)

Parameters

Name Type Default value Description
toOther PlainDate | "now" undefined -
density PlainDateDensity 'YMD' the density the comparison has to have

Returns

number

the distance in days

Defined in

valueObjects/composed/PlainDate.ts:274


equals

equals(obj, density?): boolean

compares if the given value is either an equal ValueObject, or an equal value which would create an equal ValueObject

Parameters

Name Type Default value Description
obj PlainDateable | PlainDate undefined to compare of equality
density PlainDateDensity 'YMD' -

Returns

boolean

Overrides

ValueObject.equals

Defined in

valueObjects/composed/PlainDate.ts:217


toDate

toDate(density?): Date

Parameters

Name Type Default value
density PlainDateDensity 'YMD'

Returns

Date

Defined in

valueObjects/composed/PlainDate.ts:296


toJSON

toJSON(): Object

this function is invoked by JSON.stringify() and converts the inner "_propertyKey" to "propertyKey"

Returns

Object

the value

Name Type
date number
month number
weekday number
year number

Overrides

ValueObject.toJSON

Defined in

valueObjects/composed/PlainDate.ts:287


toString

toString(): string

Returns

string

Defined in

valueObjects/composed/PlainDate.ts:283


create

Static create(value, options?): PlainDate

PlainDateable

can be either PlainDateProps:

{
 year: number;
 month?: number;
 date?: number;
}

or YMD_Array:

[year: number, month?: number, date?: number]

or a string representation:

  • "DD.MM.YYYY"
  • "DD.M.YYYY"
  • "D.MM.YYYY"
  • "D.M.YYYY"
  • "YYYY-MM-DD"
  • "YYYY-MM-D"
  • "YYYY-M-DD"
  • "YYYY-M-D"

Parameters

Name Type Description
value PlainDateable to create the ValueObject of
options? CreationOptions constraints the value has to fulfill

Returns

PlainDate

the created ValueObject

Defined in

valueObjects/composed/PlainDate.ts:64


fromList

Static fromList(values, options?): PlainDate[]

Parameters

Name Type Description
values undefined | PlainDateable[] an array of primitives to map to an array of ValueObjects
options? CreationOptions & ListCreationOptions constraints the values / list has to fulfill

Returns

PlainDate[]

the array of ValueObjects

Defined in

valueObjects/composed/PlainDate.ts:73


listEquals

Static listEquals<ValueType>(a, b): boolean

compares 2 Lists of ValueObjects / values on equality

Type parameters

Name
ValueType

Parameters

Name Type Description
a ValueObject<ValueType>[] the list of ValueObjects to compare with
b ValueObject<ValueType>[] | ValueType[] a list of ValueObjects / values for comparison

Returns

boolean

true if the lists are equal

Inherited from

ValueObject.listEquals

Defined in

valueObjects/ValueObject.ts:29


now

Static now(options?): PlainDate

creates a PlainDate from the current date. Similar to new Date()

Parameters

Name Type
options? CreationOptions & PlainDateNowOptions

Returns

PlainDate

Defined in

valueObjects/composed/PlainDate.ts:81


parseString

Static Private parseString(value, options?): PlainDateProps | YMD_Array

Parameters

Name Type
value PlainDateable
options? CreationOptions

Returns

PlainDateProps | YMD_Array

Defined in

valueObjects/composed/PlainDate.ts:192


prefix

Static Protected prefix(options, addition?): string

constructs a prefix for possible error messages based on the ValueObjects name and an additional string:

  • example for NonEmptyString with name='Person':
"Person > NonEmptyString"

Parameters

Name Type Default value Description
options undefined | CreationOptions undefined the CreationOptions where the optional name: string is taken from
addition string '' An additional name which will be added as "name.addition"

Returns

string

the name of the ValueObject

Inherited from

ValueObject.prefix

Defined in

valueObjects/ValueObject.ts:77


validate

Static validate(value, options?): Required<PlainDateProps>

Throws

various errors if not correct

Parameters

Name Type Description
value PlainDateable to be validated as a valid date with the corresponding constraints (options)
options? CreationOptions constraints the value has to fulfill

Returns

Required<PlainDateProps>

the value if the validation was successful

Defined in

valueObjects/composed/PlainDate.ts:137


validateInterval

Static Protected validateInterval(value, options): void

Parameters

Name Type
value string | number
options IntervalCreationOptions

Returns

void

Inherited from

ValueObject.validateInterval

Defined in

valueObjects/ValueObject.ts:86


validateList

Static Protected validateList<Primitive>(list, options?): list is Primitive[]

Type parameters

Name
Primitive

Parameters

Name Type
list undefined | Primitive[]
options? CreationOptions & ListCreationOptions

Returns

list is Primitive[]

Inherited from

ValueObject.validateList

Defined in

valueObjects/ValueObject.ts:101


validatePlainDateable

Static validatePlainDateable(value, options?): PlainDateable

Parameters

Name Type Description
value PlainDateable to be validated as a string, array or object representation of a date
options? CreationOptions constraints the value has to fulfill

Returns

PlainDateable

the valid date

Defined in

valueObjects/composed/PlainDate.ts:179

DDD basics - v0.0.1

Clone this wiki locally