Aller au contenu principal

TimeDuration

A collection of functions to manipulate time duration (i.e amount of milliseconds)

Index

Codec

__decode__

  • __decode__(this: void, input: unknown, context: Context<TimeDuration>): Result<TimeDuration, CodecError>
  • Returns the decoded input, Result.Ok or Result.Error()

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const input: unknown = ...;
    const decoded = Codec.decode(someCodec, input);

__encode__

  • __encode__(this: void, input: TimeDuration): unknown
  • Returns the encoded input

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const someObject: SomeObject = { foo: "bar" }
    const encoded = Codec.decode(someCodec, someObject);

__schema__

  • __schema__(this: void): JSONValue
  • Returns the JSONSchema corresponding to the decoded type

    @example
    const someCodec: Codec<unknown> = ...;
    const jsonSchema = Codec.schema(someCodec);

Comparator

!=

  • !=(this: void, left: TimeDuration, right: TimeDuration): boolean
  • "Not equal to" operator

    @example
    const TEqual: Equal<T>;
    TEqual['!='](value, otherValue); // true
    TEqual['!='](value, value); // false

  • (this: void, left: TimeDuration, right: TimeDuration): boolean
  • "Less than" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<'](smallerT, smallerT); // false
    TCompare['<'](smallerT, greaterT); // true
    TCompare['<'](greaterT, smallerT); // false

<=

  • <=(this: void, left: TimeDuration, right: TimeDuration): boolean
  • "Less than or equal to" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['<='](smallerT, smallerT); // true
    TCompare['<='](smallerT, greaterT); // true
    TCompare['<='](greaterT, smallerT); // false

==

  • ==(this: void, left: TimeDuration, right: TimeDuration): boolean
  • "Equal to" operator

    @example
    type T = // ...
    const TEqual: Equal<T>;
    TEqual['=='](value, value); // true
    TEqual['=='](value, otherValue); // false

  • (this: void, left: TimeDuration, right: TimeDuration): boolean
  • "Greater than" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>'](smallerT, smallerT); // false
    TCompare['>'](smallerT, greaterT); // false
    TCompare['>'](greaterT, smallerT); // true

>=

  • >=(this: void, left: TimeDuration, right: TimeDuration): boolean
  • "Greater than or equal to" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare['>='](smallerT, smallerT); // true
    TCompare['>='](smallerT, greaterT); // false
    TCompare['>='](greaterT, smallerT); // true

clamp

  • clamp(this: void, value: TimeDuration, minValue: TimeDuration, maxValue: TimeDuration): TimeDuration
  • Clamp value between minValue and maxValue

    @example
    type T;
    const TCompare: Comparable<T>;
    TCompare.clamp(value, min, max); // min if value < min, max if value > max, otherwise value itself

compare

  • compare(this: void, left: TimeDuration, right: TimeDuration): Ordering
  • Return an Ordering that represents comparison result

    @see
    @example
    type T;
    const TCompare: Comparable<T>;
    const sorted = [3, 1, 1].sort(TCompare.compare);

equals

  • equals(this: void, left: TimeDuration, right: TimeDuration): boolean
  • Alias to '=='

    @example
    type T = // ...
    const TEqual: Equal<T>;
    TEqual.equals(value, value); // true
    TEqual.equals(value, otherValue); // false

max

  • max(this: void, left: TimeDuration, right: TimeDuration): TimeDuration
  • "maximum" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.max(smallerT, greaterT); // greaterT

min

  • min(this: void, left: TimeDuration, right: TimeDuration): TimeDuration
  • "minimum" operator

    @example
    type T;
    const TCompare: Comparable<T>;
    const smallerT: T;
    const greaterT: T;
    TCompare.min(smallerT, greaterT); // smallerT

Constructor

days

  • days(amount: number): TimeDuration
  • Return a duration of amount days

    @example
    const duration = TimeDuration.days(1);// 1000 * 60 * 60 * 24

hours

  • hours(amount: number): TimeDuration
  • Return a duration of amount hours

    @example
    const duration = TimeDuration.hours(1);// 1000 * 60 * 60

milliseconds

  • milliseconds(amount: number): TimeDuration
  • Return a duration of amount milliseconds

    @example
    const duration = TimeDuration.milliseconds(1);// 1

minutes

  • minutes(amount: number): TimeDuration
  • Return a duration of amount minutes

    @example
    const duration = TimeDuration.minutes(1);// 1000 * 60

of

  • of(milliseconds: number): TimeDuration
  • Return a duration from a number

    @example
    const duration = TimeDuration.of(0);// typeof duration === 'number'

seconds

  • seconds(amount: number): TimeDuration
  • Return a duration of amount seconds

    @example
    const duration = TimeDuration.seconds(1);// 1000

weeks

  • weeks(amount: number): TimeDuration
  • Return a duration of amount days

    @example
    const duration = TimeDuration.weeks(1);// 1000 * 60 * 60 * 24 * 7

Formatting

asString

  • asString(this: void, self: TimeDuration): string
  • Converts the given value to a String.

    @example
    Number.asString(123); // '123'

Numeric

*

  • *(left: TimeDuration, right: number): TimeDuration
  • Multiplication operator

    @example
    type T = ...;
    const TNumeric: Numeric.Multiply<T> = ...;
    const result = Numeric['*'](left, right);// represents (left * right)

+

  • +(left: TimeDuration, right: TimeDuration): TimeDuration
  • Addition operator

    @example
    type T = ...;
    const TNumeric: Numeric.Add<T> = ...;
    const result = TNumeric['+'](left, right);// represents (left + right)

-

  • -(left: TimeDuration, right: TimeDuration): TimeDuration
  • Subtraction operator

    @example
    type T = ...;
    const TNumeric: Numeric.Subtract<T> = ...;
    const result = Numeric['-'](left, right);// represents (left - right)

abs

  • abs(this: void, value: TimeDuration): TimeDuration
  • Absolute value. It should satisfy Numeric['*'](Numeric.abs(x), Numeric.sign(x)) == x

    @example
    type T = ...;
    const TSigned: Numeric.Signed<T> = ...;
    const result = TSigned.abs(value);// absolute value of (value)

isNegative

  • isNegative(this: void, self: TimeDuration): boolean
  • Returns true if the number is negative and false if the number is zero or positive.

isPositive

  • isPositive(this: void, self: TimeDuration): boolean
  • Returns true if the number is positive and false if the number is zero or negative.

negate

  • negate(self: TimeDuration): TimeDuration
  • Negates the given value.

    @example
    Number.negate(5); // -5
    Number.negate(Number.negate(5)); // 5

sign

  • sign(this: void, value: TimeDuration): TimeDuration
  • Sign of a number. It should satisfy TSigned['*'](TSigned.abs(x), TSigned.sign(x)) == x

Type

__inspect__

__inspect__: Option<(anyValue: TimeDuration, depth: number, options: InspectOptions, inspect: InspectFunction) => string>

When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)

@example
import { inspect } from 'node:util';

interface Foo {
foo: boolean;
}
const Foo = Struct.define<Foo>({
typeName: 'Foo',
__inspect__: (self) => `Foo { ${String(self.foo)} }`,
});
const myStruct = Struct.create(Foo, { foo: true });// 'Foo { true }'
inspect(myStruct);// 'Foo { true }'
@param

asInstance

  • asInstance(anyValue: unknown): Option<TimeDuration>
  • Try to convert anyValue to enum value or else returns Option.None

    @example
    const StringType: Type<string>;
    StringType.asInstance('foo'); // Option.Some('foo')
    StringType.asInstance(12); // Option.None

hasInstance

  • hasInstance(anyValue: unknown): anyValue is TimeDuration
  • Return true if the given value is an instance of the class.

    @example
    const StringType: Type<string>;
    StringType.hasInstance('foo'); // true
    StringType.hasInstance(42); // false

Other

typeName

Re-exports typeName

__call__

  • Converts a number or a TimeDurationObject to a TimeDuration

    @example
    const duration = TimeDuration.from(1000);// 1000
    const duration = TimeDuration.from({ milliseconds: 3, seconds: 2, hours: 1 });// 3 + 1000 * 2 + 1000 * 60 * 60 * 1

from

  • Converts a number or a TimeDurationObject to a TimeDuration

    @example
    const duration = TimeDuration.from(1000);// 1000
    const duration = TimeDuration.from({ milliseconds: 3, seconds: 2, hours: 1 });// 3 + 1000 * 2 + 1000 * 60 * 60 * 1

toDays

  • toDays(self: TimeDuration, truncate?: boolean): number
  • Converts a time duration to days.

    @example
    TimeDuration.toDays(TimeDuration.from({ days: 1 })); // == 1
    TimeDuration.toDays(TimeDuration.from({ days: 1.5 })); // == 1.5
    TimeDuration.toDays(TimeDuration.from({ days: 1.5 }), true); // == 1

toHours

  • toHours(self: TimeDuration, truncate?: boolean): number
  • Converts a time duration to hours.

    @example
    TimeDuration.toHours(TimeDuration(3600000));// == 1
    TimeDuration.toHours(TimeDuration(3600000 * 1.5));// == 1.5
    TimeDuration.toHours(TimeDuration(3600000 * 1.5, true));// == 1

toMinutes

  • toMinutes(self: TimeDuration, truncate?: boolean): number
  • Converts a time duration to minutes.

    @example
    TimeDuration.toMinutes(TimeDuration(60000)); // == 1.0
    TimeDuration.toMinutes(TimeDuration(150000), true); // == 2.5

toSeconds

  • toSeconds(self: TimeDuration, truncate?: boolean): number
  • Converts a time duration to seconds.

    @example
    TimeDuration.toSeconds(TimeDuration(1500));// == 1.5
    TimeDuration.toSeconds(TimeDuration(1500), true);// == 1

toWeeks

  • toWeeks(self: TimeDuration, truncate?: boolean): number
  • Converts a time duration to weeks.

    @example
    TimeDuration.toWeeks(TimeDuration.from({ weeks: 1 })); // == 1
    TimeDuration.toWeeks(TimeDuration.from({ weeks: 1.5 })); // == 1.5
    TimeDuration.toWeeks(TimeDuration.from({ weeks: 1.5 }), true); // == 1

unwrap

  • unwrap(value: TimeDuration): number
  • Convert a tagged value to the underlying type

wrap

  • wrap(value: number): TimeDuration
  • Convert an underlying type to a tagged type