It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
JSR Score
47%
Published
3 months ago (0.1.0)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import { expect, test } from 'npm:vitest@^1.6.0'; import { assert, assertHasProperty, hasProperty, isArrayElement, isArrayOf, isFunction, isNonNull, isNumber, isObject, isObjectOf, isOptional, isString, } from './index.ts'; test('isNonNull', () => { expect(isNonNull('')).toBe(true); expect(isNonNull(0)).toBe(true); expect(isNonNull(false)).toBe(true); expect(isNonNull(undefined)).toBe(false); expect(isNonNull(null)).toBe(false); }); test('isNumber', () => { expect(isNumber(0)).toBe(true); expect(isNumber(1)).toBe(true); expect(isNumber(NaN)).toBe(true); expect(isNumber(Infinity)).toBe(true); expect(isNumber('')).toBe(false); expect(isNumber(false)).toBe(false); expect(isNumber(undefined)).toBe(false); expect(isNumber(null)).toBe(false); }); test('isString', () => { expect(isString('')).toBe(true); expect(isString('a')).toBe(true); expect(isString('0')).toBe(true); expect(isString('false')).toBe(true); expect(isString(0)).toBe(false); expect(isString(false)).toBe(false); expect(isString(undefined)).toBe(false); expect(isString(null)).toBe(false); }); test('isFunction', () => { expect(isFunction(() => {})).toBe(true); expect(isFunction(function () {})).toBe(true); expect(isFunction(function named() {})).toBe(true); expect(isFunction(class {})).toBe(true); // NOSONAR expect(isFunction(class Named {})).toBe(true); // NOSONAR expect(isFunction({})).toBe(false); expect(isFunction('')).toBe(false); expect(isFunction(0)).toBe(false); expect(isFunction(false)).toBe(false); expect(isFunction(undefined)).toBe(false); expect(isFunction(null)).toBe(false); }); test('isObject', () => { expect(isObject({})).toBe(true); expect(isObject({ a: 0 })).toBe(true); expect(isObject([])).toBe(true); expect(isObject(null)).toBe(false); expect(isObject('')).toBe(false); expect(isObject(0)).toBe(false); expect(isObject(false)).toBe(false); expect(isObject(undefined)).toBe(false); }); test('isObjectOf', () => { expect(isObjectOf({}, isNumber)).toBe(true); expect(isObjectOf({ a: 0 }, isNumber)).toBe(true); expect(isObjectOf({ a: '0' }, isNumber)).toBe(false); expect(isObjectOf({ a: 0 }, isString)).toBe(false); expect(isObjectOf([], isNumber)).toBe(true); }); test('isArrayOf', () => { expect(isArrayOf([], isNumber)).toBe(true); expect(isArrayOf([0], isNumber)).toBe(true); expect(isArrayOf([0, 1], isNumber)).toBe(true); expect(isArrayOf([0, '1'], isNumber)).toBe(false); expect(isArrayOf([0], isString)).toBe(false); expect(isArrayOf({}, isNumber)).toBe(false); expect(isArrayOf(null, isNumber)).toBe(false); expect(isArrayOf('', isNumber)).toBe(false); expect(isArrayOf(0, isNumber)).toBe(false); expect(isArrayOf(false, isNumber)).toBe(false); expect(isArrayOf(undefined, isNumber)).toBe(false); }); test('isArrayElement', () => { expect(isArrayElement(0, [0])).toBe(true); expect(isArrayElement(1, [0, 1])).toBe(true); expect(isArrayElement(0, [1, 0])).toBe(true); expect(isArrayElement('0', [0])).toBe(false); expect(isArrayElement(0, [1])).toBe(false); expect(isArrayElement({}, [0])).toBe(false); expect(isArrayElement(null, [0])).toBe(false); expect(isArrayElement('', [0])).toBe(false); expect(isArrayElement(false, [0])).toBe(false); expect(isArrayElement(undefined, [0])).toBe(false); }); test('hasProperty', () => { expect(hasProperty({ a: 0 }, 'a', isNumber)).toBe(true); expect(hasProperty({ a: 0 }, 'b', isNumber)).toBe(false); expect(hasProperty({}, 'a', isNumber)).toBe(false); }); test('assert', () => { expect(() => assert(0, isNumber)).not.toThrow(); expect(() => assert(0, isString)).toThrow(); }); test('assertHasProperty', () => { expect(() => assertHasProperty({ a: 0 }, 'a', isNumber)).not.toThrow(); expect(() => assertHasProperty({ a: 0 }, 'b', isNumber)).toThrow(); expect(() => assertHasProperty({}, 'a', isNumber)).toThrow(); }); test('isOptional', () => { expect(isOptional(undefined, isNumber)).toBe(true); expect(isOptional(null, isNumber)).toBe(false); expect(isOptional(0, isNumber)).toBe(true); expect(isOptional('0', isNumber)).toBe(false); expect(isOptional({}, isNumber)).toBe(false); expect(isOptional([], isNumber)).toBe(false); expect(isOptional('', isNumber)).toBe(false); expect(isOptional(false, isNumber)).toBe(false); });