diff --git a/package.json b/package.json index 7700cb6..5ac9ab9 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,17 @@ { "name": "typing-assets", - "version": "1.2.4", + "version": "1.0.1", "description": "Additional typing assets and helpers for better TypeScript experience", "main": "src/index.ts", "types": "scr/types.d.ts", "bugs": { - "url": "https://github.com/LCcodder/TypeScriptAssets/issues/new" + "url": "https://github.com/LCcodder/typing-assets/issues/new" + }, + "homepage": "https://github.com/LCcodder/typing-assets", + "repository": { + "type": "git", + "url": "https://github.com/LCcodder/typing-assets" }, - "homepage": "https://github.com/LCcodder/TypeScriptAssets", "scripts": { "test": "jest --watch=all" }, diff --git a/src/functions/predicatesAndTyping.ts b/src/functions/predicatesAndTyping.ts index c5f88eb..9d99f50 100644 --- a/src/functions/predicatesAndTyping.ts +++ b/src/functions/predicatesAndTyping.ts @@ -39,15 +39,18 @@ export function generateGuard(prop: keyof T, propPrimitive: string | symbol): } } + +export type Asserter = (checkingVariable: unknown) => asserts checkingVariable is T + /** - * + * @param errorMessage Error message `string` * @param isValid Callback function, that have to return true ro - * @param errorMessage - * @returns + * + * @returns Type asserter which asserts `checkingVariable` is `T` *(boolean)* */ export function generateAsserter ( - isValid: (source: unknown, ...args: unknown[]) => source is T | boolean, - errorMessage: string + errorMessage: string, + isValid: (source: unknown, ...args: unknown[]) => boolean, ): (checkingVariable: unknown) => asserts checkingVariable is T { return (source: unknown, ...args: unknown[]): asserts source is T => { const state = isValid(source, ...args) @@ -73,6 +76,7 @@ export function generatePredicates( /** * @description Generates predicates by provided property and its primitive + * @param errorMessage Error message `string` for *asserter* * @param prop Property to check *(must be string or symbol)* * @param propPrimitive This property `type` alias primitive in string * @returns Object with both asserter and type guard @@ -91,14 +95,14 @@ export function generatePredicates( if (validationOrProp instanceof Function) { return { guard: generateConditionalGuard(validationOrProp), - assert: generateAsserter(validationOrProp as (source: unknown, ...args: unknown[]) => source is T | boolean, errorMessage) + assert: generateAsserter(errorMessage, validationOrProp as (source: unknown, ...args: unknown[]) => source is T | boolean) } } else if (propPrimitive) { const guard = generateGuard(validationOrProp, propPrimitive) return { guard, assert: generateAsserter( - guard, errorMessage + errorMessage, guard ) } } diff --git a/tests/functions/predicatesAndTyping.test.ts b/tests/functions/predicatesAndTyping.test.ts index 5341477..7c4573c 100644 --- a/tests/functions/predicatesAndTyping.test.ts +++ b/tests/functions/predicatesAndTyping.test.ts @@ -1,5 +1,5 @@ import { describe, test, expect } from "@jest/globals" -import {isSameType, generateConditionalGuard, generateAsserter, generateGuard, generatePredicates, Predicates} from "../../src/functions/predicatesAndTyping" +import {isSameType, generateConditionalGuard, generateAsserter, generateGuard, generatePredicates, Predicates, Asserter} from "../../src/functions/predicatesAndTyping" describe("isSameType function", () => { @@ -78,6 +78,37 @@ describe("generateGuard function", () => { }) }) + +describe("generateAsserter function", () => { + const assert: Asserter = generateAsserter( + "Name does not starts with 'A'", + (source: unknown) => !(source as User).name.startsWith("A") + ) + + // type asserter tests + test("Testing asserter to complete", () => { + const testUser = { + id: 123, + name: "Lisa", + age: 19 + } + expect(assert(testUser)).toEqual(undefined) + }) + + test("Testing asserter to throw error", () => { + const testUser = { + id: 123, + name: "Alex", + age: 19 + } + try { + assert(testUser) + } catch (error) { + expect(error).toBeDefined() + } + }) +}) + // testing both type guard and type asserter via 'generatePredicates' generator with first overload describe("generatePredicates function (conditional type guard + type asserter)", () => { const predicates: Predicates = generatePredicates(