@veloss/assertion
v0.0.5
Published
Simple assertion library with no dependencies
Maintainers
Readme
@veloss/assertion
데이터에 대한 타입을 검증하고 검증된 데이터를 안전한 타입으로 변환하는 라이브러리입니다.
Usage
Install:
# npm
npm install @veloss/assertion
# yarn
yarn add @veloss/assertion
# pnpm
pnpm add @veloss/assertionImport:
// ESM / Typescript
import { isNumber, ... } from "@veloss/assertion";
// CommonJS
const { isNumber, ... } = require("@veloss/assertion");isNumber
Type: (value: any) => value is number
validate if the value is a number.
function isNumber(value: any): value is number;Example
import { isNumber } from "@veloss/assertion";
console.log(isNumber(1)); // true
console.log(isNumber("1")); // falseisNotNumber
Type: (value: any) => value is null | undefined | string | boolean | object | any[]
validate if the value is not a number.
function isNotNumber(
value: any
): value is null | undefined | string | boolean | object | any[];Example
import { isNotNumber } from "@veloss/assertion";
console.log(isNotNumber(1)); // false
console.log(isNotNumber("1")); // trueisNumeric
Type: (value: any) => boolean
validate if the value is a numeric value.
function isNumeric(value: any): boolean;Example
import { isNumeric } from "@veloss/assertion";
console.log(isNumeric(1)); // true
console.log(isNumeric("1")); // true
console.log(isNumeric("1.1")); // true
console.log(isNumeric("1.1.1")); // falseisInteger
Type: (value: any) => value is number
validate if the value is an integer.
function isInteger(value: any): value is number;Example
import { isInteger } from "@veloss/assertion";
console.log(isInteger(1)); // true
console.log(isInteger(1.1)); // falseisFloat
Type: (value: any) => value is number
validate if the value is a float.
function isFloat(value: any): value is number;Example
import { isFloat } from "@veloss/assertion";
console.log(isFloat(1.1)); // true
console.log(isFloat(1)); // falseisArray
Type: <T>(value: any) => value is T[]
validate if the value is an array.
function isArray<T>(value: any): value is T[];Example
import { isArray } from "@veloss/assertion";
console.log(isArray([1, 2, 3])); // true
console.log(isArray("1,2,3".split(","))); // trueisEmptyArray
Type: (value: any) => value is []
validate if the value is an empty array.
function isEmptyArray(value: any): value is [];Example
import { isEmptyArray } from "@veloss/assertion";
console.log(isEmptyArray([])); // true
console.log(isEmptyArray([1, 2, 3])); // falseisFunction
Type: <T extends AnyFunction = AnyFunction>(value: any) => value is T
validate if the value is a function.
function isFunction<T extends AnyFunction = AnyFunction>(
value: any
): value is T;Example
import { isFunction } from "@veloss/assertion";
console.log(isFunction(() => {})); // true
console.log(isFunction(function () {})); // trueisDefined
Type: <T>(value: T) => value is NonNullable<T>
validate if the value is defined.
function isDefined<T>(value: T): value is NonNullable<T>;Example
import { isDefined } from "@veloss/assertion";
console.log(isDefined(1)); // true
console.log(isDefined(null)); // falseisUndefined
Type: (value: any) => value is undefined
validate if the value is undefined.
function isUndefined(value: any): value is undefined;Example
import { isUndefined } from "@veloss/assertion";
console.log(isUndefined(undefined)); // true
console.log(isUndefined(null)); // falseisNull
Type: (value: any) => value is null
validate if the value is null.
function isNull(value: any): value is null;Example
import { isNull } from "@veloss/assertion";
console.log(isNull(null)); // true
console.log(isNull(undefined)); // falseisObject
Type: <T extends object = object>(value: any) => value is T
validate if the value is an object.
function isObject<T extends object = object>(value: any): value is T;Example
import { isObject } from "@veloss/assertion";
console.log(isObject({})); // true
console.log(isObject([])); // falseisEmptyObject
Type: (value: any) => value is {}
validate if the value is an empty object.
function isEmptyObject(value: any): value is {};Example
import { isEmptyObject } from "@veloss/assertion";
console.log(isEmptyObject({})); // true
console.log(isEmptyObject({ a: 1 })); // falseisNotEmptyObject
Type: (value: any) => value is object
validate if the value is not an empty object.
function isNotEmptyObject(value: any): value is object;Example
import { isNotEmptyObject } from "@veloss/assertion";
console.log(isNotEmptyObject({})); // false
console.log(isNotEmptyObject({ a: 1 })); // trueisString
Type: (value: any) => value is string
validate if the value is a string.
function isString(value: any): value is string;Example
import { isString } from "@veloss/assertion";
console.log(isString("")); // true
console.log(isString(1)); // falseisBoolean
Type: (value: any) => value is boolean
validate if the value is a boolean.
function isBoolean(value: any): value is boolean;Example
import { isBoolean } from "@veloss/assertion";
console.log(isBoolean(true)); // true
console.log(isBoolean(1)); // falseisSymbol
Type: (value: any) => value is symbol
validate if the value is a symbol.
function isSymbol(value: any): value is symbol;Example
import { isSymbol } from "@veloss/assertion";
console.log(isSymbol(Symbol())); // true
console.log(isSymbol(1)); // falseisDate
Type: (value: any) => value is Date
validate if the value is a date.
function isDate(value: any): value is Date;Example
import { isDate } from "@veloss/assertion";
console.log(isDate(new Date())); // true
console.log(isDate(1)); // falseisEmpty
Type: (value: any) => boolean
validate if the value is empty.
function isEmpty(value: any): boolean;Example
import { isEmpty } from "@veloss/assertion";
console.log(isEmpty("")); // true
console.log(isEmpty([])); // true
console.log(isEmpty({})); // true
console.log(isEmpty(null)); // true
console.log(isEmpty(undefined)); // true
console.log(isEmpty(0)); // false
console.log(isEmpty("1")); // falseisNullOrUndefined
Type: (value: any) => value is null | undefined
validate if the value is null or undefined.
function isNullOrUndefined(value: any): value is null | undefined;Example
import { isNullOrUndefined } from "@veloss/assertion";
console.log(isNullOrUndefined(null)); // true
console.log(isNullOrUndefined(undefined)); // true
console.log(isNullOrUndefined(0)); // falseisBrowser
Type: () => boolean
validate if the code is running in a browser environment.
function isBrowser(): boolean;Example
import { isBrowser } from "@veloss/assertion";
console.log(isBrowser()); // trueisNode
Type: () => boolean
validate if the code is running in a Node.js environment.
function isNode(): boolean;Example
import { isNode } from "@veloss/assertion";
console.log(isNode()); // falseisFalsy
Type: (value: any) => value is false | null | undefined | 0 | ""
validate if the value is falsy.
function isFalsy(value: any): value is false | null | undefined | 0 | "";Example
import { isFalsy } from "@veloss/assertion";
console.log(isFalsy(null)); // true
console.log(isFalsy(undefined)); // true
console.log(isFalsy(0)); // true
console.log(isFalsy("")); // true
console.log(isFalsy(1)); // falseisTruthy
Type: (value: any) => value is true
validate if the value is truthy.
function isTruthy(value: any): value is true;Example
import { isTruthy } from "@veloss/assertion";
console.log(isTruthy(true)); // true
console.log(isTruthy(1)); // falseisPromiseLike
Type: <T = any>(value: any) => value is PromiseLike<T>
validate if the value is a promise-like object.
function isPromiseLike<T = any>(value: any): value is PromiseLike<T>;Example
import { isPromiseLike } from "@veloss/assertion";
console.log(isPromiseLike(Promise.resolve())); // true
console.log(isPromiseLike({ then: () => {} })); // trueisPrimitive
Type: (value: any) => value is string | number | boolean | symbol | null | undefined
validate if the value is a primitive value.
function isPrimitive(
value: any
): value is string | number | boolean | symbol | null | undefined;Example
import { isPrimitive } from "@veloss/assertion";
console.log(isPrimitive("")); // true
console.log(isPrimitive(1)); // true
console.log(isPrimitive(true)); // true
console.log(isPrimitive(Symbol())); // true
console.log(isPrimitive(null)); // true
console.log(isPrimitive(undefined)); // true
console.log(isPrimitive({})); // false💻 Development
- Clone this repository
- Enable Corepack using
corepack enable(usenpm i -g corepackfor Node.js < 16.10) - Install dependencies using
pnpm install - Run interactive tests using
pnpm test - Build the project using
pnpm build - format and lint the project using
pnpm formatandpnpm lintandpnpm typecheck
