UNPKG

camote-utils

Version:

A comprehensive TypeScript utility library featuring advanced string and number formatting, data structures, and algorithms

265 lines (264 loc) 13.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); describe('Type Checking Functions', () => { describe('isEqual', () => { it('should compare primitive values', () => { expect((0, index_1.isEqual)(1, 1)).toBe(true); expect((0, index_1.isEqual)('hello', 'hello')).toBe(true); expect((0, index_1.isEqual)(true, true)).toBe(true); expect((0, index_1.isEqual)(null, null)).toBe(true); expect((0, index_1.isEqual)(undefined, undefined)).toBe(true); expect((0, index_1.isEqual)(1, 2)).toBe(false); expect((0, index_1.isEqual)('hello', 'world')).toBe(false); }); it('should compare arrays', () => { expect((0, index_1.isEqual)([1, 2, 3], [1, 2, 3])).toBe(true); expect((0, index_1.isEqual)([1, 2, 3], [1, 2, 4])).toBe(false); expect((0, index_1.isEqual)([1, [2, 3]], [1, [2, 3]])).toBe(true); expect((0, index_1.isEqual)([1, [2, 3]], [1, [2, 4]])).toBe(false); }); it('should compare objects', () => { expect((0, index_1.isEqual)({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true); expect((0, index_1.isEqual)({ a: 1, b: 2 }, { b: 2, a: 1 })).toBe(true); expect((0, index_1.isEqual)({ a: { b: 2 } }, { a: { b: 2 } })).toBe(true); expect((0, index_1.isEqual)({ a: 1 }, { a: 2 })).toBe(false); }); }); describe('isMatch', () => { it('should handle basic pattern matching', () => { expect((0, index_1.isMatch)({ a: 1, b: 2 }, { a: 1 })).toBe(true); expect((0, index_1.isMatch)({ a: 1, b: 2 }, { a: 2 })).toBe(false); expect((0, index_1.isMatch)({ a: 1 }, { a: 1, b: 2 })).toBe(false); }); it('should handle nested objects', () => { expect((0, index_1.isMatch)({ a: { b: 2 } }, { a: { b: 2 } })).toBe(true); expect((0, index_1.isMatch)({ a: { b: 2, c: 3 } }, { a: { b: 2 } })).toBe(true); expect((0, index_1.isMatch)({ a: { b: 2 } }, { a: { b: 3 } })).toBe(false); }); it('should handle edge cases', () => { expect((0, index_1.isMatch)(null, {})).toBe(false); expect((0, index_1.isMatch)({}, null)).toBe(false); expect((0, index_1.isMatch)(undefined, {})).toBe(false); expect((0, index_1.isMatch)({}, undefined)).toBe(false); expect((0, index_1.isMatch)({}, {})).toBe(true); }); it('should handle arrays', () => { expect((0, index_1.isMatch)([1, 2, 3], [1, 2])).toBe(true); expect((0, index_1.isMatch)({ a: [1, 2] }, { a: [1, 2] })).toBe(true); expect((0, index_1.isMatch)({ a: [1, 2] }, { a: [1, 3] })).toBe(false); }); }); describe('Basic Type Checks', () => { it('should check strings', () => { expect((0, index_1.isString)('')).toBe(true); expect((0, index_1.isString)('hello')).toBe(true); expect((0, index_1.isString)(String('hello'))).toBe(true); expect((0, index_1.isString)(123)).toBe(false); expect((0, index_1.isString)(null)).toBe(false); }); it('should check numbers', () => { expect((0, index_1.isNumber)(123)).toBe(true); expect((0, index_1.isNumber)(0)).toBe(true); expect((0, index_1.isNumber)(Number('123'))).toBe(true); expect((0, index_1.isNumber)(NaN)).toBe(false); expect((0, index_1.isNumber)(Infinity)).toBe(false); expect((0, index_1.isNumber)('123')).toBe(false); }); it('should check booleans', () => { expect((0, index_1.isBoolean)(true)).toBe(true); expect((0, index_1.isBoolean)(false)).toBe(true); expect((0, index_1.isBoolean)(Boolean(1))).toBe(true); expect((0, index_1.isBoolean)(1)).toBe(false); expect((0, index_1.isBoolean)('true')).toBe(false); }); }); describe('Object Type Checks', () => { it('should check arrays', () => { expect((0, index_1.isArray)([])).toBe(true); expect((0, index_1.isArray)([1, 2, 3])).toBe(true); expect((0, index_1.isArray)(new Array())).toBe(true); expect((0, index_1.isArray)({ length: 0 })).toBe(false); expect((0, index_1.isArray)(null)).toBe(false); }); it('should check objects', () => { expect((0, index_1.isObject)({})).toBe(true); expect((0, index_1.isObject)({ a: 1 })).toBe(true); expect((0, index_1.isObject)(Object.create(null))).toBe(true); expect((0, index_1.isObject)([])).toBe(false); expect((0, index_1.isObject)(null)).toBe(false); }); it('should check functions', () => { expect((0, index_1.isFunction)(() => { })).toBe(true); expect((0, index_1.isFunction)(function () { })).toBe(true); expect((0, index_1.isFunction)(async () => { })).toBe(true); expect((0, index_1.isFunction)({})).toBe(false); expect((0, index_1.isFunction)(null)).toBe(false); }); }); describe('Special Type Checks', () => { it('should check dates', () => { expect((0, index_1.isDate)(new Date())).toBe(true); expect((0, index_1.isDate)(new Date('2023-01-01'))).toBe(true); expect((0, index_1.isDate)(new Date('invalid'))).toBe(false); expect((0, index_1.isDate)('2023-01-01')).toBe(false); }); it('should check regular expressions', () => { expect((0, index_1.isRegExp)(/test/)).toBe(true); expect((0, index_1.isRegExp)(new RegExp('test'))).toBe(true); expect((0, index_1.isRegExp)('/test/')).toBe(false); expect((0, index_1.isRegExp)({})).toBe(false); }); it('should check errors', () => { expect((0, index_1.isError)(new Error())).toBe(true); expect((0, index_1.isError)(new TypeError())).toBe(true); expect((0, index_1.isError)({ message: 'error' })).toBe(false); expect((0, index_1.isError)('error')).toBe(false); }); it('should check symbols', () => { expect((0, index_1.isSymbol)(Symbol())).toBe(true); expect((0, index_1.isSymbol)(Symbol('test'))).toBe(true); expect((0, index_1.isSymbol)('symbol')).toBe(false); expect((0, index_1.isSymbol)(123)).toBe(false); expect((0, index_1.isSymbol)({})).toBe(false); }); it('should check arguments object', () => { function test() { expect((0, index_1.isArguments)(arguments)).toBe(true); } test(); expect((0, index_1.isArguments)([])).toBe(false); expect((0, index_1.isArguments)({})).toBe(false); expect((0, index_1.isArguments)(null)).toBe(false); }); }); describe('Collection Type Checks', () => { it('should check Maps', () => { expect((0, index_1.isMap)(new Map())).toBe(true); expect((0, index_1.isMap)(new WeakMap())).toBe(false); expect((0, index_1.isMap)({})).toBe(false); }); it('should check Sets', () => { expect((0, index_1.isSet)(new Set())).toBe(true); expect((0, index_1.isSet)(new WeakSet())).toBe(false); expect((0, index_1.isSet)([])).toBe(false); }); it('should check WeakMaps and WeakSets', () => { expect((0, index_1.isWeakMap)(new WeakMap())).toBe(true); expect((0, index_1.isWeakMap)(new Map())).toBe(false); expect((0, index_1.isWeakSet)(new WeakSet())).toBe(true); expect((0, index_1.isWeakSet)(new Set())).toBe(false); }); }); describe('Buffer and View Checks', () => { it('should check ArrayBuffers', () => { expect((0, index_1.isArrayBuffer)(new ArrayBuffer(8))).toBe(true); expect((0, index_1.isArrayBuffer)(new Uint8Array(8))).toBe(false); expect((0, index_1.isArrayBuffer)([])).toBe(false); }); it('should check DataViews', () => { const buffer = new ArrayBuffer(8); expect((0, index_1.isDataView)(new DataView(buffer))).toBe(true); expect((0, index_1.isDataView)(buffer)).toBe(false); expect((0, index_1.isDataView)([])).toBe(false); }); it('should check TypedArrays', () => { expect((0, index_1.isTypedArray)(new Uint8Array())).toBe(true); expect((0, index_1.isTypedArray)(new Float64Array())).toBe(true); expect((0, index_1.isTypedArray)(new DataView(new ArrayBuffer(8)))).toBe(false); expect((0, index_1.isTypedArray)([])).toBe(false); }); }); describe('Special Value Checks', () => { it('should check NaN', () => { expect((0, index_1.isNaN)(NaN)).toBe(true); expect((0, index_1.isNaN)(Number('not a number'))).toBe(true); expect((0, index_1.isNaN)(123)).toBe(false); expect((0, index_1.isNaN)('123')).toBe(false); }); it('should check null and undefined', () => { expect((0, index_1.isNull)(null)).toBe(true); expect((0, index_1.isNull)(undefined)).toBe(false); expect((0, index_1.isUndefined)(undefined)).toBe(true); expect((0, index_1.isUndefined)(null)).toBe(false); }); it('should check finite numbers', () => { expect((0, index_1.isFinite)(123)).toBe(true); expect((0, index_1.isFinite)(-123.45)).toBe(true); expect((0, index_1.isFinite)(Infinity)).toBe(false); expect((0, index_1.isFinite)(-Infinity)).toBe(false); expect((0, index_1.isFinite)(NaN)).toBe(false); }); }); describe('isEmail', () => { test('valid email', () => { expect((0, index_1.isEmail)("rhynel@dev.com")).toBe(true); }); test('invalid email without domain', () => { expect((0, index_1.isEmail)("not-an-email")).toBe(false); }); test('valid email with domain', () => { expect((0, index_1.isEmail)("user@domain.co")).toBe(true); }); test('invalid email with empty domain', () => { expect((0, index_1.isEmail)("user@.com")).toBe(false); }); test('invalid email with double dots', () => { expect((0, index_1.isEmail)("user@domain..com")).toBe(false); }); }); describe('isStrongPassword', () => { test('strong password', () => { expect((0, index_1.isStrongPassword)("Password123!")).toBe(true); }); test('weak password', () => { expect((0, index_1.isStrongPassword)("weakpass")).toBe(false); }); test('missing uppercase letter', () => { expect((0, index_1.isStrongPassword)("password123!")).toBe(false); }); test('missing lowercase letter', () => { expect((0, index_1.isStrongPassword)("PASSWORD123!")).toBe(false); }); test('missing number', () => { expect((0, index_1.isStrongPassword)("Password!")).toBe(false); }); test('missing special character', () => { expect((0, index_1.isStrongPassword)("Password123")).toBe(false); }); }); // Browser-specific tests if (typeof window !== 'undefined') { describe('DOM Element Checks', () => { it('should check HTML Elements', () => { expect((0, index_1.isElement)(document.createElement('div'))).toBe(true); expect((0, index_1.isElement)(document.body)).toBe(true); expect((0, index_1.isElement)({})).toBe(false); expect((0, index_1.isElement)('<div></div>')).toBe(false); }); }); } }); describe('isAlphanumeric', () => { it('should return true for alphanumeric strings', () => { expect((0, index_1.isAlphanumeric)('abc123')).toBe(true); expect((0, index_1.isAlphanumeric)('ABC123')).toBe(true); expect((0, index_1.isAlphanumeric)('123456')).toBe(true); expect((0, index_1.isAlphanumeric)('abcABC')).toBe(true); }); it('should return false for non-alphanumeric strings', () => { expect((0, index_1.isAlphanumeric)('abc 123')).toBe(false); expect((0, index_1.isAlphanumeric)('abc-123')).toBe(false); expect((0, index_1.isAlphanumeric)('abc_123')).toBe(false); expect((0, index_1.isAlphanumeric)('abc@123')).toBe(false); expect((0, index_1.isAlphanumeric)('')).toBe(false); }); it('should handle edge cases', () => { expect((0, index_1.isAlphanumeric)('0')).toBe(true); expect((0, index_1.isAlphanumeric)('a')).toBe(true); expect((0, index_1.isAlphanumeric)('Z')).toBe(true); expect((0, index_1.isAlphanumeric)(' ')).toBe(false); expect((0, index_1.isAlphanumeric)('\t')).toBe(false); expect((0, index_1.isAlphanumeric)('\n')).toBe(false); }); });