diff --git a/packages/ts-predicate/package.json b/packages/ts-predicate/package.json index 8800aa86..d7bb9183 100644 --- a/packages/ts-predicate/package.json +++ b/packages/ts-predicate/package.json @@ -1,6 +1,6 @@ { "name": "@vitruvius-labs/ts-predicate", - "version": "7.0.0", + "version": "7.1.0", "description": "TypeScript predicates library", "author": { "name": "VitruviusLabs" diff --git a/packages/ts-predicate/src/extended/integer/predicate/_index.mts b/packages/ts-predicate/src/extended/integer/predicate/_index.mts index 8de366a2..9ee9807b 100644 --- a/packages/ts-predicate/src/extended/integer/predicate/_index.mts +++ b/packages/ts-predicate/src/extended/integer/predicate/_index.mts @@ -1,2 +1,3 @@ export * from "./type-assertion/_index.mjs"; +export * from "./type-cast/_index.mjs"; export * from "./type-guard/_index.mjs"; diff --git a/packages/ts-predicate/src/extended/integer/predicate/type-cast/_index.mts b/packages/ts-predicate/src/extended/integer/predicate/type-cast/_index.mts new file mode 100644 index 00000000..b0fe6e19 --- /dev/null +++ b/packages/ts-predicate/src/extended/integer/predicate/type-cast/_index.mts @@ -0,0 +1,6 @@ +export * from "./int8.mjs"; +export * from "./int16.mjs"; +export * from "./int32.mjs"; +export * from "./uint8.mjs"; +export * from "./uint16.mjs"; +export * from "./uint32.mjs"; diff --git a/packages/ts-predicate/src/extended/integer/predicate/type-cast/int16.mts b/packages/ts-predicate/src/extended/integer/predicate/type-cast/int16.mts new file mode 100644 index 00000000..532610f9 --- /dev/null +++ b/packages/ts-predicate/src/extended/integer/predicate/type-cast/int16.mts @@ -0,0 +1,11 @@ +import type { Int16 } from "../../definition/type/integers.mjs"; +import { assertInt16 } from "../type-assertion/assert-int16.mjs"; + +function int16(value: unknown): Int16 +{ + assertInt16(value); + + return value; +} + +export { int16 }; diff --git a/packages/ts-predicate/src/extended/integer/predicate/type-cast/int32.mts b/packages/ts-predicate/src/extended/integer/predicate/type-cast/int32.mts new file mode 100644 index 00000000..b506f174 --- /dev/null +++ b/packages/ts-predicate/src/extended/integer/predicate/type-cast/int32.mts @@ -0,0 +1,11 @@ +import type { Int32 } from "../../definition/type/integers.mjs"; +import { assertInt32 } from "../type-assertion/assert-int32.mjs"; + +function int32(value: unknown): Int32 +{ + assertInt32(value); + + return value; +} + +export { int32 }; diff --git a/packages/ts-predicate/src/extended/integer/predicate/type-cast/int8.mts b/packages/ts-predicate/src/extended/integer/predicate/type-cast/int8.mts new file mode 100644 index 00000000..0e353dcf --- /dev/null +++ b/packages/ts-predicate/src/extended/integer/predicate/type-cast/int8.mts @@ -0,0 +1,11 @@ +import type { Int8 } from "../../definition/type/integers.mjs"; +import { assertInt8 } from "../type-assertion/assert-int8.mjs"; + +function int8(value: unknown): Int8 +{ + assertInt8(value); + + return value; +} + +export { int8 }; diff --git a/packages/ts-predicate/src/extended/integer/predicate/type-cast/uint16.mts b/packages/ts-predicate/src/extended/integer/predicate/type-cast/uint16.mts new file mode 100644 index 00000000..a63b94b0 --- /dev/null +++ b/packages/ts-predicate/src/extended/integer/predicate/type-cast/uint16.mts @@ -0,0 +1,11 @@ +import type { UInt16 } from "../../definition/type/integers.mjs"; +import { assertUInt16 } from "../type-assertion/assert-uint16.mjs"; + +function uInt16(value: unknown): UInt16 +{ + assertUInt16(value); + + return value; +} + +export { uInt16 }; diff --git a/packages/ts-predicate/src/extended/integer/predicate/type-cast/uint32.mts b/packages/ts-predicate/src/extended/integer/predicate/type-cast/uint32.mts new file mode 100644 index 00000000..6a3b99dc --- /dev/null +++ b/packages/ts-predicate/src/extended/integer/predicate/type-cast/uint32.mts @@ -0,0 +1,11 @@ +import type { UInt32 } from "../../definition/type/integers.mjs"; +import { assertUInt32 } from "../type-assertion/assert-uint32.mjs"; + +function uInt32(value: unknown): UInt32 +{ + assertUInt32(value); + + return value; +} + +export { uInt32 }; diff --git a/packages/ts-predicate/src/extended/integer/predicate/type-cast/uint8.mts b/packages/ts-predicate/src/extended/integer/predicate/type-cast/uint8.mts new file mode 100644 index 00000000..e692f90e --- /dev/null +++ b/packages/ts-predicate/src/extended/integer/predicate/type-cast/uint8.mts @@ -0,0 +1,11 @@ +import type { UInt8 } from "../../definition/type/integers.mjs"; +import { assertUInt8 } from "../type-assertion/assert-uint8.mjs"; + +function uInt8(value: unknown): UInt8 +{ + assertUInt8(value); + + return value; +} + +export { uInt8 }; diff --git a/packages/ts-predicate/test/extended/integer/predicate/type-cast/int16.spec.mts b/packages/ts-predicate/test/extended/integer/predicate/type-cast/int16.spec.mts new file mode 100644 index 00000000..ee2ab2d7 --- /dev/null +++ b/packages/ts-predicate/test/extended/integer/predicate/type-cast/int16.spec.mts @@ -0,0 +1,83 @@ +import { doesNotThrow, strictEqual, throws } from "node:assert"; +import { describe, it } from "node:test"; +import { GroupType, consumeValue, getInvertedValues, getValues } from "@vitruvius-labs/testing-ground"; +import { type Int16, NoValue, int16 } from "../../../../../src/_index.mjs"; +import { IntegerBoundaryEnum } from "../../../../../src/extended/integer/definition/enum/integer-boundary.enum.mjs"; + +describe("int16", (): void => { + it("should return the provided value when given an integer with the boundaries", (): void => { + const VALUES: Array = [ + IntegerBoundaryEnum.INT16_MIN, + IntegerBoundaryEnum.INT16_MIN + 1, + -1, + 0, + 1, + IntegerBoundaryEnum.INT16_MAX - 1, + IntegerBoundaryEnum.INT16_MAX, + ]; + + for (const ITEM of VALUES) + { + let result: unknown = NoValue; + + const WRAPPER = (): void => { + result = int16(ITEM); + }; + + doesNotThrow(WRAPPER); + + strictEqual(result, ITEM); + } + }); + + it("should throw when given an integer outside the boundaries", (): void => { + const VALUES: Array = [ + IntegerBoundaryEnum.INT16_MIN - 1, + IntegerBoundaryEnum.INT16_MAX + 1, + ]; + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + int16(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given a non-integer number", (): void => { + const VALUES: Array = getValues(GroupType.REAL, GroupType.INFINITY); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + int16(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given anything else", (): void => { + const VALUES: Array = getInvertedValues(GroupType.NUMBER); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + int16(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should narrow the type to an Int16", (): void => { + const WRAPPER = (): void => + { + consumeValue(int16(NoValue)); + }; + + throws(WRAPPER); + }); +}); diff --git a/packages/ts-predicate/test/extended/integer/predicate/type-cast/int32.spec.mts b/packages/ts-predicate/test/extended/integer/predicate/type-cast/int32.spec.mts new file mode 100644 index 00000000..fb92d56c --- /dev/null +++ b/packages/ts-predicate/test/extended/integer/predicate/type-cast/int32.spec.mts @@ -0,0 +1,83 @@ +import { doesNotThrow, strictEqual, throws } from "node:assert"; +import { describe, it } from "node:test"; +import { GroupType, consumeValue, getInvertedValues, getValues } from "@vitruvius-labs/testing-ground"; +import { type Int32, NoValue, int32 } from "../../../../../src/_index.mjs"; +import { IntegerBoundaryEnum } from "../../../../../src/extended/integer/definition/enum/integer-boundary.enum.mjs"; + +describe("int32", (): void => { + it("should return the provided value when given an integer with the boundaries", (): void => { + const VALUES: Array = [ + IntegerBoundaryEnum.INT32_MIN, + IntegerBoundaryEnum.INT32_MIN + 1, + -1, + 0, + 1, + IntegerBoundaryEnum.INT32_MAX - 1, + IntegerBoundaryEnum.INT32_MAX, + ]; + + for (const ITEM of VALUES) + { + let result: unknown = NoValue; + + const WRAPPER = (): void => { + result = int32(ITEM); + }; + + doesNotThrow(WRAPPER); + + strictEqual(result, ITEM); + } + }); + + it("should throw when given an integer outside the boundaries", (): void => { + const VALUES: Array = [ + IntegerBoundaryEnum.INT32_MIN - 1, + IntegerBoundaryEnum.INT32_MAX + 1, + ]; + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + int32(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given a non-integer number", (): void => { + const VALUES: Array = getValues(GroupType.REAL, GroupType.INFINITY); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + int32(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given anything else", (): void => { + const VALUES: Array = getInvertedValues(GroupType.NUMBER); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + int32(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should narrow the type to an Int32", (): void => { + const WRAPPER = (): void => + { + consumeValue(int32(NoValue)); + }; + + throws(WRAPPER); + }); +}); diff --git a/packages/ts-predicate/test/extended/integer/predicate/type-cast/int8.spec.mts b/packages/ts-predicate/test/extended/integer/predicate/type-cast/int8.spec.mts new file mode 100644 index 00000000..f8c2441a --- /dev/null +++ b/packages/ts-predicate/test/extended/integer/predicate/type-cast/int8.spec.mts @@ -0,0 +1,83 @@ +import { doesNotThrow, strictEqual, throws } from "node:assert"; +import { describe, it } from "node:test"; +import { GroupType, consumeValue, getInvertedValues, getValues } from "@vitruvius-labs/testing-ground"; +import { type Int8, NoValue, int8 } from "../../../../../src/_index.mjs"; +import { IntegerBoundaryEnum } from "../../../../../src/extended/integer/definition/enum/integer-boundary.enum.mjs"; + +describe("int8", (): void => { + it("should return the provided value when given an integer with the boundaries", (): void => { + const VALUES: Array = [ + IntegerBoundaryEnum.INT8_MIN, + IntegerBoundaryEnum.INT8_MIN + 1, + -1, + 0, + 1, + IntegerBoundaryEnum.INT8_MAX - 1, + IntegerBoundaryEnum.INT8_MAX, + ]; + + for (const ITEM of VALUES) + { + let result: unknown = NoValue; + + const WRAPPER = (): void => { + result = int8(ITEM); + }; + + doesNotThrow(WRAPPER); + + strictEqual(result, ITEM); + } + }); + + it("should throw when given an integer outside the boundaries", (): void => { + const VALUES: Array = [ + IntegerBoundaryEnum.INT8_MIN - 1, + IntegerBoundaryEnum.INT8_MAX + 1, + ]; + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + int8(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given a non-integer number", (): void => { + const VALUES: Array = getValues(GroupType.REAL, GroupType.INFINITY); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + int8(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given anything else", (): void => { + const VALUES: Array = getInvertedValues(GroupType.NUMBER); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + int8(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should narrow the type to an Int8", (): void => { + const WRAPPER = (): void => + { + consumeValue(int8(NoValue)); + }; + + throws(WRAPPER); + }); +}); diff --git a/packages/ts-predicate/test/extended/integer/predicate/type-cast/uint16.spec.mts b/packages/ts-predicate/test/extended/integer/predicate/type-cast/uint16.spec.mts new file mode 100644 index 00000000..3a713dd0 --- /dev/null +++ b/packages/ts-predicate/test/extended/integer/predicate/type-cast/uint16.spec.mts @@ -0,0 +1,77 @@ +import { doesNotThrow, strictEqual, throws } from "node:assert"; +import { describe, it } from "node:test"; +import { GroupType, consumeValue, getInvertedValues, getValues } from "@vitruvius-labs/testing-ground"; +import { NoValue, type UInt16, uInt16 } from "../../../../../src/_index.mjs"; +import { IntegerBoundaryEnum } from "../../../../../src/extended/integer/definition/enum/integer-boundary.enum.mjs"; + +describe("uInt16", (): void => { + it("should return the provided value when given an integer with the boundaries", (): void => { + const VALUES: Array = [ + 0, + 1, + IntegerBoundaryEnum.UINT16_MAX - 1, + IntegerBoundaryEnum.UINT16_MAX, + ]; + + for (const ITEM of VALUES) + { + let result: unknown = NoValue; + + const WRAPPER = (): void => { + result = uInt16(ITEM); + }; + + doesNotThrow(WRAPPER); + + strictEqual(result, ITEM); + } + }); + + it("should throw when given an integer outside the boundaries", (): void => { + const VALUES: Array = [-1, IntegerBoundaryEnum.UINT16_MAX + 1]; + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + uInt16(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given a non-integer number", (): void => { + const VALUES: Array = getValues(GroupType.REAL, GroupType.INFINITY); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + uInt16(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given anything else", (): void => { + const VALUES: Array = getInvertedValues(GroupType.NUMBER); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + uInt16(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should narrow the type to an UInt16", (): void => { + const WRAPPER = (): void => + { + consumeValue(uInt16(NoValue)); + }; + + throws(WRAPPER); + }); +}); diff --git a/packages/ts-predicate/test/extended/integer/predicate/type-cast/uint32.spec.mts b/packages/ts-predicate/test/extended/integer/predicate/type-cast/uint32.spec.mts new file mode 100644 index 00000000..c9280efd --- /dev/null +++ b/packages/ts-predicate/test/extended/integer/predicate/type-cast/uint32.spec.mts @@ -0,0 +1,77 @@ +import { doesNotThrow, strictEqual, throws } from "node:assert"; +import { describe, it } from "node:test"; +import { GroupType, consumeValue, getInvertedValues, getValues } from "@vitruvius-labs/testing-ground"; +import { NoValue, type UInt32, uInt32 } from "../../../../../src/_index.mjs"; +import { IntegerBoundaryEnum } from "../../../../../src/extended/integer/definition/enum/integer-boundary.enum.mjs"; + +describe("uInt32", (): void => { + it("should return the provided value when given an integer with the boundaries", (): void => { + const VALUES: Array = [ + 0, + 1, + IntegerBoundaryEnum.UINT32_MAX - 1, + IntegerBoundaryEnum.UINT32_MAX, + ]; + + for (const ITEM of VALUES) + { + let result: unknown = NoValue; + + const WRAPPER = (): void => { + result = uInt32(ITEM); + }; + + doesNotThrow(WRAPPER); + + strictEqual(result, ITEM); + } + }); + + it("should throw when given an integer outside the boundaries", (): void => { + const VALUES: Array = [-1, IntegerBoundaryEnum.UINT32_MAX + 1]; + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + uInt32(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given a non-integer number", (): void => { + const VALUES: Array = getValues(GroupType.REAL, GroupType.INFINITY); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + uInt32(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given anything else", (): void => { + const VALUES: Array = getInvertedValues(GroupType.NUMBER); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + uInt32(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should narrow the type to an UInt32", (): void => { + const WRAPPER = (): void => + { + consumeValue(uInt32(NoValue)); + }; + + throws(WRAPPER); + }); +}); diff --git a/packages/ts-predicate/test/extended/integer/predicate/type-cast/uint8.spec.mts b/packages/ts-predicate/test/extended/integer/predicate/type-cast/uint8.spec.mts new file mode 100644 index 00000000..36826fb7 --- /dev/null +++ b/packages/ts-predicate/test/extended/integer/predicate/type-cast/uint8.spec.mts @@ -0,0 +1,77 @@ +import { doesNotThrow, strictEqual, throws } from "node:assert"; +import { describe, it } from "node:test"; +import { GroupType, consumeValue, getInvertedValues, getValues } from "@vitruvius-labs/testing-ground"; +import { NoValue, type UInt8, uInt8 } from "../../../../../src/_index.mjs"; +import { IntegerBoundaryEnum } from "../../../../../src/extended/integer/definition/enum/integer-boundary.enum.mjs"; + +describe("uInt8", (): void => { + it("should return the provided value when given an integer with the boundaries", (): void => { + const VALUES: Array = [ + 0, + 1, + IntegerBoundaryEnum.UINT8_MAX - 1, + IntegerBoundaryEnum.UINT8_MAX, + ]; + + for (const ITEM of VALUES) + { + let result: unknown = NoValue; + + const WRAPPER = (): void => { + result = uInt8(ITEM); + }; + + doesNotThrow(WRAPPER); + + strictEqual(result, ITEM); + } + }); + + it("should throw when given an integer outside the boundaries", (): void => { + const VALUES: Array = [-1, IntegerBoundaryEnum.UINT8_MAX + 1]; + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + uInt8(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given a non-integer number", (): void => { + const VALUES: Array = getValues(GroupType.REAL, GroupType.INFINITY); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + uInt8(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should throw when given anything else", (): void => { + const VALUES: Array = getInvertedValues(GroupType.NUMBER); + + for (const ITEM of VALUES) + { + const WRAPPER = (): void => { + uInt8(ITEM); + }; + + throws(WRAPPER); + } + }); + + it("should narrow the type to an UInt8", (): void => { + const WRAPPER = (): void => + { + consumeValue(uInt8(NoValue)); + }; + + throws(WRAPPER); + }); +});