Time
Index
Bound
readonlymaxValue
Maximum value for this type
const anyNumber: number
anyNumber < Number.maxValue // true
readonlyminValue
Minimum value for this type
const anyNumber: number
anyNumber > Number.minValue // true
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 TimeObject to a Time
Time.from({ year: 2020, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 });// new Date('2020-01-01T00:00:00.000Z').getTime()
from
Converts a number or a TimeObject to a Time
Time.from({ year: 2020, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0 });// new Date('2020-01-01T00:00:00.000Z').getTime()
of
Create a new Time value
const time = Time.of(0);
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)
+
Addition operator
type T = ...;const TNumeric: Numeric.Add<T> = ...;const result = TNumeric['+'](left, right);// represents (left + right)
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
delay
Return a new
Taskthat resolves the current time in milliseconds after waitingduration.const wait2s = Time.delay(TimeDuration({ seconds: 2 }));const logTime = Task.andThen(wait2s, (time) => Console.debug(time));Task.run(logTime);// wait 2 seconds then console.debug(Date.now())
diff
Return the difference between 2 time values
const begin = Time.of(0);const end = Time.of(10);Time.diff(end, begin);// TimeDuration.of(10)
format
Return an ISO 8601 string representation
const time = Time.of(0);Time.format(time);// '1970-01-01T00:00:00.000Z'
now
A task that resolves the current time in milliseconds.
const program = () => Task.andThen(Time.now(), (currentTime) => {// use currentTime});
parse
Parse an ISO 8601 string. If invalid, returns
Option.NoneTime.parse('1970-01-01T00:00:00.000Z');// 0
A collection of functions to manipulate time (i.e timestamp)