TimeDuration
Index
Codec
__decode__
Returns the decoded
input,Result.OkorResult.Error()interface SomeObject {foo: string}const someCodec: Codec<SomeObject> = ...;const input: unknown = ...;const decoded = Codec.decode(someCodec, input);
__encode__
Returns the encoded
inputinterface SomeObject {foo: string}const someCodec: Codec<SomeObject> = ...;const someObject: SomeObject = { foo: "bar" }const encoded = Codec.decode(someCodec, someObject);
__schema__
Returns the JSONSchema corresponding to the decoded type
const someCodec: Codec<unknown> = ...;const jsonSchema = Codec.schema(someCodec);
Comparator
!=
"Not equal to" operator
const TEqual: Equal<T>;TEqual['!='](value, otherValue); // trueTEqual['!='](value, value); // false
<
"Less than" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare['<'](smallerT, smallerT); // falseTCompare['<'](smallerT, greaterT); // trueTCompare['<'](greaterT, smallerT); // false
<=
"Less than or equal to" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare['<='](smallerT, smallerT); // trueTCompare['<='](smallerT, greaterT); // trueTCompare['<='](greaterT, smallerT); // false
==
"Equal to" operator
type T = // ...const TEqual: Equal<T>;TEqual['=='](value, value); // trueTEqual['=='](value, otherValue); // false
>
"Greater than" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare['>'](smallerT, smallerT); // falseTCompare['>'](smallerT, greaterT); // falseTCompare['>'](greaterT, smallerT); // true
>=
"Greater than or equal to" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare['>='](smallerT, smallerT); // trueTCompare['>='](smallerT, greaterT); // falseTCompare['>='](greaterT, smallerT); // true
clamp
Clamp value between minValue and maxValue
type T;const TCompare: Comparable<T>;TCompare.clamp(value, min, max); // min if value < min, max if value > max, otherwise value itself
compare
equals
Alias to '=='
type T = // ...const TEqual: Equal<T>;TEqual.equals(value, value); // trueTEqual.equals(value, otherValue); // false
max
"maximum" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare.max(smallerT, greaterT); // greaterT
min
"minimum" operator
type T;const TCompare: Comparable<T>;const smallerT: T;const greaterT: T;TCompare.min(smallerT, greaterT); // smallerT
Constructor
__call__
Converts a number or a TimeDurationObject to a TimeDuration
const duration = TimeDuration.from(1000);// 1000const 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
const duration = TimeDuration.from(1000);// 1000const duration = TimeDuration.from({ milliseconds: 3, seconds: 2, hours: 1 });// 3 + 1000 * 2 + 1000 * 60 * 60 * 1
of
Return a duration from a number
const duration = TimeDuration.of(0);// typeof duration === 'number'
Formatting
asString
Converts the given value to a String.
Number.asString(123); // '123'
Numeric
-
Subtraction operator
type T = ...;const TNumeric: Numeric.Subtract<T> = ...;const result = Numeric['-'](left, right);// represents (left - right)
*
Multiplication operator
type T = ...;const TNumeric: Numeric.Multiply<T> = ...;const result = Numeric['*'](left, right);// represents (left * right)
**
Power operator
type T = ...;const TNumeric: Numeric.Power<T> = ...;const result = Numeric['**'](left, right);// represents (left ** right)
+
Addition operator
type T = ...;const TNumeric: Numeric.Add<T> = ...;const result = TNumeric['+'](left, right);// represents (left + right)
abs
Absolute value. It should satisfy
Numeric['*'](Numeric.abs(x), Numeric.sign(x)) == xtype T = ...;const TSigned: Numeric.Signed<T> = ...;const result = TSigned.abs(value);// absolute value of (value)
isNegative
Returns true if the number is negative and false if the number is zero or positive.
Number.isPositive(-1);// trueNumber.isPositive(0);// falseNumber.isPositive(1);// false
isPositive
Returns true if the number is positive and false if the number is zero or negative.
Number.isPositive(-1);// falseNumber.isPositive(0);// falseNumber.isPositive(1);// true
isZero
Returns true if self is equal to the additive identity.
Number.isZero(0); // trueNumber.isZero(1); // false
negate
Negates the given value.
Number.negate(5); // -5Number.negate(Number.negate(5)); // 5
sign
Sign of a number. It should satisfy
TSigned['*'](TSigned.abs(x), TSigned.sign(x)) == xNumber.sign(-2.5);// -1Number.sign(0);// 0Number.sign(2.5);// 1
zero
Returns the additive identity element of
T, 0.Number.zero(); // 0
Type
__inspect__
When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)
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 }'
asInstance
Try to convert anyValue to enum value or else returns
Option.Noneconst StringType: Type<string>;StringType.asInstance('foo'); // Option.Some('foo')StringType.asInstance(12); // Option.None
hasInstance
Return
trueif the given value is an instance of the class.const StringType: Type<string>;StringType.hasInstance('foo'); // trueStringType.hasInstance(42); // false
unwrap
Convert a tagged value to the underlying type
wrap
Convert an underlying type to a tagged type
Other
typeName
toDays
Converts a time duration to days.
TimeDuration.toDays(TimeDuration.from({ days: 1 })); // == 1TimeDuration.toDays(TimeDuration.from({ days: 1.5 })); // == 1.5TimeDuration.toDays(TimeDuration.from({ days: 1.5 }), true); // == 1
toHours
Converts a time duration to hours.
TimeDuration.toHours(TimeDuration(3600000));// == 1TimeDuration.toHours(TimeDuration(3600000 * 1.5));// == 1.5TimeDuration.toHours(TimeDuration(3600000 * 1.5, true));// == 1
toMinutes
Converts a time duration to minutes.
TimeDuration.toMinutes(TimeDuration(60000)); // == 1.0TimeDuration.toMinutes(TimeDuration(150000), true); // == 2.5
toSeconds
Converts a time duration to seconds.
TimeDuration.toSeconds(TimeDuration(1500));// == 1.5TimeDuration.toSeconds(TimeDuration(1500), true);// == 1
toWeeks
Converts a time duration to weeks.
TimeDuration.toWeeks(TimeDuration.from({ weeks: 1 })); // == 1TimeDuration.toWeeks(TimeDuration.from({ weeks: 1.5 })); // == 1.5TimeDuration.toWeeks(TimeDuration.from({ weeks: 1.5 }), true); // == 1
A collection of functions to manipulate time duration (i.e amount of milliseconds)