1.1.0 • Published 4 years ago
ts-extended-types v1.1.0
ts-extended-types
Set of complementary basic types related to the ones provided by TypeScript to make your application even more type-safe
Install
Install with npm:
$ npm install --save ts-extended-typesBasic usage
import {int, stringInt, double, stringDouble, LocalDate, LocalTime, LocalDateTime, uuid } from 'ts-extended-types';
var intNumber: int;
var veryBigInt: stringInt
var doubleNumber: double;
var veryBigDoubleNumber: stringDouble
var localDate: LocalDate;
var localTime: LocalTime;
var localDateTime: LocalDateTime;
var uuidString: uuid;Type definitions
int: type that represents a integer number. It can be assigned to a number, but it cannot be assigned from a number.stringInt: type that represents a integer number. It can be assigned to a number, but it cannot be assigned from a number: it can be so long that can be represented using a string instead of a number.double: type that represents a floating point number with double precision. It can be assigned to a number, but it cannot be assigned from a number.stringDouble: type that represents a floating point number with double precision; it can be so long that can be represented using a string instead of a number.LocalDate: type that represents a local date without time (without timezone). Provided methods: -getFullYear(): int: Gets the year -getMonth(): int: Gets the month (value between 0 to 11) -getDate(): int: Gets the day-of-the-month -getDay(): int: Gets the day of the week (0 represents Sunday)LocalTime: type that represents a local time without date (without timezone). Provided methods: -getHours(): int: Gets the hours -getMinutes(): int: Gets the minutes -getSeconds(): int: Gets the seconds -getMilliseconds(): int: Gets the millisecondsLocalDateTime: type that represents a local date with time (without timezone). Provided methods: all methods provided byLocalDateandLocalTimeplus: -getTime(): int: Gets the time value in millisecondsuuid: type that represents an uuid string. It can be assigned to a string, but it cannot be assigned from a string.
Note:
- In JavaScript
intordoubledon't exists, all of these values are represented using anumber. - In JavaScript
stringIntorstringDoubledon't exists, all of these values are represented using anumberor astring. - In JavaScript
LocalDate,LocalTimeorLocalDateTimedon't exists, all of these values are represented using theDateobject. - In JavaScript
uuiddon't exists, this value is represented using astring.
Provided functions
Provided int functions
isInt(value: any) => value is int: Returnstrueif the value is aintnumber, otherwise returnsfalse.asInt(value: number) => int: Cast the number provided by argument asint, throws an error if the provided number is not an integer.roundToInt(value: number) => int: Round the provided number to an integer and then cast it toint.truncateToInt(value: number) => int: Truncate the provided number to an integer and then cast it toint.floorToInt(value: number) => int: Floor the provided number to an integer and then cast it toint.ceilToInt(value: number) => int: Ceil the provided number to an integer and then cast it toint.
Provided stringInt functions
isStringInt(value: any) => value is stringInt: Returnstrueif the value is astringIntnumber, otherwise returnsfalse.asStringInt(value: number|string) => stringInt: Cast the number provided by argument asstringInt, throws an error if the provided number is not an integer.roundToStringInt(value: number) => stringInt: Round the provided number to an integer and then cast it tostringInt.truncateToStringInt(value: number) => stringInt: Truncate the provided number to an integer and then cast it tostringInt.floorToStringInt(value: number) => stringInt: Floor the provided number to an integer and then cast it tostringInt.ceilToStringInt(value: number) => stringInt: Ceil the provided number to an integer and then cast it tostringInt.
Provided double functions
isDouble(value: any) => value is double: Returnstrueif the value is adoubleprecision number, otherwise returnsfalse.asDouble(value: number) => double: Cast the number provided by argument asdouble.
Provided stringDouble functions
isStringDouble(value: any) => value is stringDouble: Returnstrueif the value is astringDoubleprecision number, otherwise returnsfalse.asStringDouble(value: number|string) => double: Cast the number provided by argument asstringDouble, throws an error if the provided number is not a double.
Provided LocalDate functions
createLocalDate() => LocalDate: create a newLocalDatewith the current date.createLocalDate(date: Date) => LocalDate: create a newLocalDatewith the same date provided by argument.createLocalDate(year: number, month: number, date: number) => LocalDate: create a newLocalDatewith the year, month and date (day of the month) provided by arguments.isLocalDate(value: any) => value is LocalDate: Returnstrueif the value is aLocalDate, otherwise returnsfalse
Provided LocalTime functions
createLocalTime() => LocalTime: create a newLocalTimewith the current time.createLocalTime(date: Date) => LocalTime: create a newLocalTimewith the same time provided by argument.createLocalTime(hours: number, minutes?: number, seconds?: number, milliseconds?: number) => LocalTime: create a newLocalTimewith the hours, minutes, seconds and milliseconds provided by arguments.isLocalTime(value: any) => value is LocalTime: Returnstrueif the value is aLocalTime, otherwise returnsfalse
Provided LocalDateTime functions
createLocalDateTime() => LocalDateTime: create a newLocalDatewith the current date and time.createLocalDateTime(date: Date) => LocalDateTime: create a newLocalDateTimewith the same date and time provided by argument.createLocalDateTime(year: number, month: number, date: number, hours?: number, minutes?: number, seconds?: number, milliseconds?: number) => LocalDateTime: create a newLocalDateTimewith the year, month, date (day of the month), hours, minutes, seconds and milliseconds provided by arguments.isLocalDate(value: any) => value is LocalDate: Returnstrueif the value is aLocalDate, otherwise returnsfalse
Provided uuid functions
isUuid(value: any) => value is uuid: Returnstrueif the value is anuuidstring, otherwise returnsfalse.asUuid(value: string) => uuid: Cast the string provided by argument asuuid, throws an error if the provided string is not an uuid.
License
MIT