Skip to content

PlainTime

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

Class: PlainTime

This is a more simplified, but also flexible version of a Date which specifically represents just the time. Related to PlainDate for dates and PlaneDateTime for a combination of both.

Hierarchy

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

Private new PlainTime(props)

Parameters

Name Type
props Required<PlainTimeProps>

Overrides

ValueObject.constructor

Defined in

valueObjects/composed/PlainTime.ts:34

Properties

_value

Protected Readonly _value: void

the actual value of the ValueObject

Inherited from

ValueObject._value

Defined in

valueObjects/ValueObject.ts:4


hours

Readonly hours: number

Defined in

valueObjects/composed/PlainTime.ts:19


milliseconds

Readonly milliseconds: number

Defined in

valueObjects/composed/PlainTime.ts:22


minutes

Readonly minutes: number

Defined in

valueObjects/composed/PlainTime.ts:20


seconds

Readonly seconds: number

Defined in

valueObjects/composed/PlainTime.ts:21

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 time with a given other PlainTime or 'now' to compare it with the current time.

  • returns -1 if this time is earlier than the other one
  • returns 0 if the times are equal
  • returns 1 if this time is sooner than the other one
Examples
'05:24:57'.compare('08:15:30') => -1
'05:24:57'.compare('01:00:00') => 1
'05:24:57'.compare('05:00:01', 'H') => 0 (with density)

Parameters

Name Type Default value Description
other "now" | PlainTime undefined the PlainTime to compare to / 'now' to use the current time
density PlainTimeDensity 'HMSs' the density the comparison has to have

Returns

0 | 1 | -1

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

Defined in

valueObjects/composed/PlainTime.ts:277


createOffset

createOffset(offset, options?): PlainTime

creates a new PlainTime derived from the existing time, with a given offset

Parameters

Name Type
offset Partial<PlainTimeProps>
options? CreationOptions

Returns

PlainTime

Defined in

valueObjects/composed/PlainTime.ts:119


createSet

createSet(newData, options?): PlainTime

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

Parameters

Name Type
newData Partial<PlainTimeProps>
options? CreationOptions

Returns

PlainTime

Defined in

valueObjects/composed/PlainTime.ts:106


distance

distance(toOther, density?): number

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

  • distance = other - this
    • positive result = other time was later
    • negative result = other time was earlier
  • units depending on density:
    • "H" = hours
    • "HM" = minutes
    • "HMS" = seconds
    • "HMSs" = milliseconds (default)

Parameters

Name Type Default value Description
toOther "now" | PlainTime undefined -
density PlainTimeDensity 'HMSs' the density the comparison has to have

Returns

number

the distance whereas the unit correlates to the given density (defaults to ms)

Defined in

valueObjects/composed/PlainTime.ts:313


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 PlainTimeable | PlainTime undefined to compare of equality
density PlainTimeDensity 'HMSs' -

Returns

boolean

Overrides

ValueObject.equals

Defined in

valueObjects/composed/PlainTime.ts:245


getTime

getTime(density): number

returns the time in ms since 1970 similar to Date.getTime()

Parameters

Name Type
density PlainTimeDensity

Returns

number

Defined in

valueObjects/composed/PlainTime.ts:25


toJSON

toJSON(): Object

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

Returns

Object

the value

Name Type
hours number
milliseconds number
minutes number
seconds number

Overrides

ValueObject.toJSON

Defined in

valueObjects/composed/PlainTime.ts:334


toString

toString(): string

Returns

string

Defined in

valueObjects/composed/PlainTime.ts:330


create

Static create(value, options?): PlainTime

PlainTimeable

can be either PlainTimeProps:

{
 hours?: number;
 minutes?: number;
 seconds?: number;
 milliseconds?: number;
}

or HMSs_Array:

[hours?: number, minutes?: number, seconds?: number, milliseconds?: number]

or a string representation:

  • "HH"
  • "HH:MM"
  • "HH:MM:SS"
  • "HH:MM:SS.sss+hh:mm" (with offset)

Parameters

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

Returns

PlainTime

the created ValueObject

Defined in

valueObjects/composed/PlainTime.ts:71


fromList

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

Parameters

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

Returns

PlainTime[]

the array of ValueObjects

Defined in

valueObjects/composed/PlainTime.ts:80


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?): PlainTime

creates a PlainTime from the current time. Similar to new Date()

Parameters

Name Type
options? CreationOptions & PlainTimeNowOptions

Returns

PlainTime

Defined in

valueObjects/composed/PlainTime.ts:88


parseString

Static Private parseString(value, options?): PlainTimeProps | HMSs_Array

Parameters

Name Type
value PlainTimeable
options? CreationOptions

Returns

PlainTimeProps | HMSs_Array

Defined in

valueObjects/composed/PlainTime.ts:198


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<PlainTimeProps>

Throws

various errors if not correct

Parameters

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

Returns

Required<PlainTimeProps>

the value if the validation was successful

Defined in

valueObjects/composed/PlainTime.ts:151


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


validatePlainTimeable

Static validatePlainTimeable(value, options): PlainTimeable

Parameters

Name Type Description
value PlainTimeable to be validated as a string, array or object representation of time
options undefined | CreationOptions constraints the value has to fulfill

Returns

PlainTimeable

the valid time

Defined in

valueObjects/composed/PlainTime.ts:178

DDD basics - v0.0.1

Clone this wiki locally