0.0.7 • Published 3 years ago

ts-guard-clause v0.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

ts-guard-clause

yarn add ts-guard-clause

Before:

if (body.name !== undefined) {
  throw new Error('Name is undefined')
}

console.log(name)

After:

const name = Guard.against.undefined(body.name)

console.log(name)

Shorter version:

const name = Against.undefined(body.name)

console.log(name)

Add a custom message/error

Guard.against.undefined(body.name, 'Custom Error')
// OR
Guard.against.undefined(body.name, new MyCustomError('Some Error'))

Current clauses

  • null
  • undefined
  • nullish (null and undefined)
  • nullishOrEmpty (null, undfined, not an empty array)
  • expression (custom condition)

Add your own guard clause(s)

First you will need to extend the IGuardClause interface:

// @types/ts-guard-clause/index.d.ts
import 'ts-guard-clause'

declare module 'ts-guard-clause' {
  interface IGuardClause {
    hello<T>(prop: T): T
  }
}

If everything went well then the AddExtension method will give you intellisense for the hello clause.

import { Guard, GuardFactory } from 'ts-guard-clause'

GuardFactory.AddExtension('hello', (prop) => prop === 1)

Guard.against.Hello('hello') // Throws
0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago