パッケージの詳細

ts-predicate

honestica1.8kMIT1.1.4

TypeScript predicates and assertions library

guard, honestica, lifen, predicate

readme

ts-predicate

ci workflow NPM npm semantic-release: angular Commitizen friendly

Getting started

yarn add ts-predicate
# or
npm install ts-predicate

Documentation

TypeAssertion

IsDefined

static IsDefined(value: unknown): void

Asserts that the value is not nullable, nor NaN.

IsBoolean

static IsBoolean(value: unknown): void

Asserts that the value is a boolean.

IsNumber

static IsNumber(value: unknown): void

Asserts that the value is a number, but not NaN.

IsInteger

static IsInteger(value: unknown): void

Asserts that the value is a safe integer.

IsFiniteNumber

static IsFiniteNumber(value: unknown): void

Asserts that the value is a number, but not NaN nor +/-Infinity.

IsString

static IsString(value: unknown, constraints?: StringConstraints): void

Asserts that the value is a string.

The optional parameter constraints accept an object described by the following interface.

interface StringConstraints
{
    minLength?: number;
    maxLength?: number;
}

If minLength is provided, it'll asserts that the value has at least this length.
If maxLength is provided, it'll asserts that the value has at most this length.

IsFilledString

static IsFilledString(value: unknown): void

Like IsString, but asserts that the string is never empty too.

IsArray

static IsArray(value: unknown, constraints?: ArrayConstraints): void

Asserts that the value is an array.

The optional parameter constraints accept an object described by the following interface.

interface ArrayConstraints<T>
{
    minLength?: number;
    itemGuard?: (item: unknown) => item is T;
}

If minLength is provided, it'll asserts that the value has at least that many items.
If itemGuard is provided, it'll asserts that the predicate hold true for every item.

IsPopulatedArray

static IsPopulatedArray(value: unknown, constraints?: ArrayConstraints): void

Like IsArray, but asserts that the array is never empty too.

IsRecord

static IsRecord(value: unknown): void

Asserts that the value is a record: an object with no prototype, or directly using Object prototype.

IsObject

static IsObject(value: unknown): void

Asserts that the value is an object.

IsFunction

static IsFunction(value: unknown): void

Asserts that the value is a function, generator function, method, or class.

IsCallable

static IsCallable(value: unknown): void

Asserts that the value is not constructible.

HasNullableProperty

static HasNullableProperty(value: object, property: string): void

Asserts that the value is an object with the property defined, though it may be nullish or NaN.

HasProperty

static HasProperty(value: object, property: string): void

Asserts that the value is an object with the property defined.

TypeGuard

IsPrimitive

static IsPrimitive(value: unknown): boolean

Narrow down the value to being nullish, NaN, a boolean, a number, or a string.

IsDefined

static IsDefined(value: unknown): boolean

Narrow down the value to being not nullable, nor NaN.

IsBoolean

static IsBoolean(value: unknown): boolean

Narrow down the value to being a boolean.

IsNumber

static IsNumber(value: unknown): boolean

Narrow down the value to being a number, but not NaN.

IsInteger

static IsInteger(value: unknown): boolean

Narrow down the value to being a safe integer.

IsFiniteNumber

static IsFiniteNumber(value: unknown): boolean

Narrow down the value to being a number, but not NaN nor +/-Infinity.

IsString

static IsString(value: unknown): boolean

Narrow down the value to being a string.

IsFilledString

static IsFilledString(value: unknown): void

Asserts that the value is a non empty string.

IsArray

static IsArray(value: unknown, constraints?: ArrayConstraints): boolean

Narrow down the value to being an array.

The optional parameter constraints accept an object described by the following interface.

interface ArrayConstraints<T>
{
    minLength?: number;
    itemGuard?: (item: unknown) => item is T;
}

If minLength is provided, it'll confirm that the value has at least that many items.
If itemGuard is provided, it'll confirm that the predicate hold true for every item.

IsPopulatedArray

static IsPopulatedArray(value: unknown, constraints?: ArrayConstraints): boolean

Like IsArray, but narrow it to being a populated array.

IsRecord

static IsRecord(value: unknown): boolean

Narrow down the value to being a record: an object with no prototype, or directly using Object prototype.

IsObject

static IsObject(value: unknown): boolean

Narrow down the value to being an object.

IsFunction

static IsFunction(value: unknown): boolean

Narrow down the value to being a function, generator function, method, or class.

IsCallable

static IsCallable(value: unknown): boolean

Narrow down the value to being not constructible.

HasNullableProperty

static HasNullableProperty(value: object, property: string): boolean

Narrow down the value to being an object with the property defined, though it may be nullish or NaN.

HasProperty

static HasProperty(value: object, property: string): boolean

Narrow down the value to being an object with the property defined.

TypeHint

GetBaseType

static GetBaseType(value: any): string

Return a string depending on the type of the given value.

Possible values:

  • undefined
  • null
  • NaN
  • boolean
  • number
  • string
  • array
  • object
  • function
  • generator
  • class

Note: generator refers to a generator function.

GetDetailedType

static GetDetailedType(value: any): string

Return a string depending on the type of the given value.
Provide more details than GetBaseType.

Possible values:

  • undefined
  • null
  • NaN
  • boolean (true or false)
  • number (N)
  • string (N characters)
  • array (N items)
  • anonymous object
  • object anonymous class
  • object ClassName
  • anonymous function
  • function name
  • anonymous generator
  • generator name
  • anonymous class
  • class Name

Note: generator refers to a generator function.

GetName

static GetName(value: any): string|undefined

If given a function, generator function, method, or class, return its name.
If given an object, return its constructor name.
If the given value doesn't have a name, return an empty string.
For any other value, return undefined.

ValueGuard

IsSimilar

static IsSimilar(a: any, b: any): boolean

Return true in the following cases :

  • The same value has been passed as both arguments.
  • Similar primitive values have been passed as arguments.
  • Deeply similar arrays or records have been passed as arguments.

Otherwise, return false.

更新履歴

1.1.4 (2023-10-23)

build

1.1.3 (2023-09-28)

build

  • deps: bump get-func-name from 2.0.0 to 2.0.2 (#23) (fee1ab7)

1.1.2 (2023-07-19)

build

  • deps: bump word-wrap from 1.2.3 to 1.2.4 (#22) (aa976a6)

1.1.1 (2023-07-17)

build

  • deps: bump semver from 5.7.1 to 5.7.2 (#21) (d1ed3d8)

1.1.0 (2023-05-31)

build

  • deps: bump http-cache-semantics from 4.1.0 to 4.1.1 (#16) (6a01a17)

chore

feat

1.0.5 (2022-09-20)

build

  • convert_to_cjs.sh: handle await import (1d5c4a2)

chore

  • .nvmrc: upgrade node to version 18 (6d2c565)
  • deps: upgrade all dependencies (d6d7c22)
  • yarn.lock: update dependencies (5cc49bd)

1.0.4 (2022-07-19)

fix

  • package.json: delete TypeScript peer dependency (ce88fed)

1.0.3 (2022-07-01)

build

  • cjs: adapt script for converting js to cjs files to run in multiple platform (7247b08)
  • convert_to_cjs.sh: add shebang (023d5b2)

chore

  • convert cjs folder extension files into cjs (755fafb)
  • only build to CJS (13ed043)

1.0.2 (2022-06-24)

chore

  • package.json: add typescript as a peerDependency (30d0e09)
  • yarn.lock: update yarn.lock to reflect peerDependency adjustment (0a0fcfa)

docs

  • readme: add links to badges (e47fa63)
  • readme: update getting started by fixing commands (6659d57)

1.0.1 (2022-06-23)

chore

  • clean scripts and dependencies (bc8d831)
  • deps: add missing devDependencies after rebasing main branch (ec0048d)

1.0.0 (2022-06-23)

chore

  • add badges to README.md (d47ac7c)
  • add commitlint and commitizen (bd7a710)
  • ci: remove steps (a0794a6)
  • cspell: updated ignored words (9259503)
  • deps: add missing inquirer devDependency (98af3f6)
  • eslint: check every TS and JS extension files (new and old) (268f0dd)
  • eslint: delete unused and useless eslint plugins (471a368)
  • eslint: use cjs config instead of json (7c2d32e)
  • publish: change branch naming (43e5892)
  • publish: test with token setup (cbc0ef1)
  • quality: add a lot of quality gates and implements CI (858482f)
  • rename package with unscoped package name (a8ec6b3)
  • settings: add token (f92cd44)
  • test: add mocha reports (3bd1ad0)
  • update vscode settings (724b544)
  • yarn: fix yarn.lock (e81251b)

ci

  • delete verify step (4bc87fa)
  • github: add a new guard to check package.json library usage and access (95d5f92)
  • github: change protected branch master to main (c181c91)

docs

  • README: add installation step (48c0c0f)

feat

  • FHIRSDK-1: Modified license. (b2ac47f)

fix

  • fixme: better prototype typing + light formatting (75d5643)
  • github: yml indentation (031c405)
  • github: yml indentation (22e63e3)
  • github: yml indentation (0e9f3a7)
  • github: yml indentation (286d416)

1.0.0-alpha.4 (2022-06-23)

chore

1.0.0-alpha.3 (2022-06-23)

chore

  • rename package with unscoped package name (e8e0f5d)

1.0.0-alpha.2 (2022-06-23)

ci

1.0.0-alpha.1 (2022-06-23)

chore

  • add commitlint and commitizen (35f0607)
  • ci: remove steps (87f5853)
  • cspell: updated ignored words (6a250de)
  • deps: add missing inquirer devDependency (1c9aa28)
  • eslint: check every TS and JS extension files (new and old) (08537da)
  • eslint: delete unused and useless eslint plugins (c799f59)
  • eslint: use cjs config instead of json (fe617c0)
  • merge: resolve merge conflicts (10ab0f6)
  • publish: change branch naming (8adf721)
  • publish: test with token setup (24cf46a)
  • quality: add a lot of quality gates and implements CI (bc935a2)
  • settings: add token (94ba0a5)
  • test: add mocha reports (28c2832)

ci

  • github: add a new guard to check package.json library usage and access (f11bad4)
  • github: change protected branch master to main (621df9e)

feat

  • FHIRSDK-1: Modified license. (b2ac47f)

fix

  • fixme: better prototype typing + light formatting (9c78df0)
  • github: yml indentation (518f7be)
  • github: yml indentation (3df8c7c)
  • github: yml indentation (61f7d42)
  • github: yml indentation (a937421)
  • run prettier on codebase (7cfce67)