Skip to content

Commit

Permalink
Added test for type asserter
Browse files Browse the repository at this point in the history
  • Loading branch information
LCcodder committed Jun 9, 2024
1 parent 85aa428 commit 0b8dd14
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
18 changes: 11 additions & 7 deletions src/functions/predicatesAndTyping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ export function generateGuard<T>(prop: keyof T, propPrimitive: string | symbol):
}
}


export type Asserter<T> = (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 <T>(
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)
Expand All @@ -73,6 +76,7 @@ export function generatePredicates<T>(

/**
* @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
Expand All @@ -91,14 +95,14 @@ export function generatePredicates<T>(
if (validationOrProp instanceof Function) {
return {
guard: generateConditionalGuard<T>(validationOrProp),
assert: generateAsserter<T>(validationOrProp as (source: unknown, ...args: unknown[]) => source is T | boolean, errorMessage)
assert: generateAsserter<T>(errorMessage, validationOrProp as (source: unknown, ...args: unknown[]) => source is T | boolean)
}
} else if (propPrimitive) {
const guard = generateGuard<T>(validationOrProp, propPrimitive)
return {
guard,
assert: generateAsserter<T>(
guard, errorMessage
errorMessage, guard
)
}
}
Expand Down
33 changes: 32 additions & 1 deletion tests/functions/predicatesAndTyping.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -78,6 +78,37 @@ describe("generateGuard function", () => {
})
})


describe("generateAsserter function", () => {
const assert: Asserter<User> = 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<User> = generatePredicates<User>(
Expand Down

0 comments on commit 0b8dd14

Please sign in to comment.