Skip to content

Commit

Permalink
feat: add isSymbol & add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
cc-hearts committed Sep 5, 2024
1 parent 449f4fb commit 02509a6
Show file tree
Hide file tree
Showing 4 changed files with 3,886 additions and 4,574 deletions.
9 changes: 9 additions & 0 deletions __test__/lib/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,13 @@ describe('formatDateByArray', () => {
const result = formatDateByArray([2023, 5, 10, 10, 10, 10])
expect(isValidDate(new Date(result))).toEqual(true)
})

it('should handle invalid date generated from array', () => {
const invalidDateArray = [2024, NaN, 1];
const result = formatDateByArray(invalidDateArray);
expect(result).toEqual('Invalid Date');
const consoleWarnSpy = jest.spyOn(console, 'warn');
expect(consoleWarnSpy).toHaveBeenCalledWith(`Invalid date generated from array: 2024,NaN,1`);
consoleWarnSpy.mockRestore();
});
})
56 changes: 48 additions & 8 deletions __test__/lib/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
isNumber,
isEffectiveNumber,
isPromise,
isEffectiveArray
} from '../../lib/validate'
isValidArray,
isValidDate
} from '../../lib'

describe('isArrayEquals', () => {
it('should return true if two arrays are equal', () => {
Expand Down Expand Up @@ -221,17 +222,56 @@ describe('isPromise', () => {
})
})

describe('isEffectiveArray', () => {
describe('isValidArray', () => {
test('should return true for a non-empty array', () => {
expect(isEffectiveArray([1, 2, 3])).toBe(true)
expect(isValidArray([1, 2, 3])).toBe(true)
})

test('should return false for an empty array', () => {
expect(isEffectiveArray([])).toBe(false)
expect(isValidArray([])).toBe(false)
})

test('should return false for a non-array', () => {
// @ts-expect-error: ignore
expect(isEffectiveArray(123)).toBe(false)
test('should return false for a non-array value', () => {
// @ts-ignore

Check failure on line 235 in __test__/lib/validate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
expect(isValidArray(123)).toBe(false)
})

test('should return false for an array with length 0', () => {
expect(isValidArray([])).toBe(false)
})

test('should return true for an array with length greater than 0', () => {
expect(isValidArray([1])).toBe(true)
})

test('should return true for a complex array', () => {
expect(isValidArray([{ a: 1 }, { b: 2 }])).toBe(true)
})

test('should return false for undefined', () => {
// @ts-ignore

Check failure on line 252 in __test__/lib/validate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
expect(isValidArray(undefined)).toBe(false)
})

test('should return false for null', () => {
// @ts-ignore

Check failure on line 257 in __test__/lib/validate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
expect(isValidArray(null)).toBe(false)
})

test('should return false for an object', () => {
// @ts-ignore

Check failure on line 262 in __test__/lib/validate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
expect(isValidArray({})).toBe(false)
})
})

describe('isValidDate function', () => {
it('should return true for a valid date', () => {
const validDate = new Date();
expect(isValidDate(validDate)).toBe(true);
});

it('should return false for an invalid date', () => {
const invalidDate = new Date('invalid');
expect(isValidDate(invalidDate)).toBe(false);
});
});
21 changes: 18 additions & 3 deletions lib/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ export const _toString = Object.prototype.toString
export function isObject(val: unknown): val is object {
return _toString.call(val) === '[object Object]'
}

/**
* Checks if the given value is an symbol.
*
* @param {unknown} val - The value to be checked.
* @return {boolean} Returns true if the value is an object, otherwise false.
*/

export function isSymbol(val: unknown): val is Symbol {
return typeof val === 'symbol'
}
/**
* Checks if the given value is a function.
*
Expand Down Expand Up @@ -146,10 +157,10 @@ export function isArrayEquals(
* Checks if the given object has its own property.
*
* @param {object} obj - The object to check.
* @param {string} prop - The property to check.
* @param {PropertyKey} prop - The property to check.
* @return {boolean} Returns true if the object has its own property, otherwise false.
*/
export function hasOwn(obj: object, prop: string): boolean {
export function hasOwn(obj: object, prop: PropertyKey): boolean {
return Object.prototype.hasOwnProperty.call(obj, prop)
}

Expand All @@ -159,10 +170,14 @@ export function hasOwn(obj: object, prop: string): boolean {
* @param {unknown[]} arr - The array to be checked.
* @returns {boolean} Returns true if the array is valid, false otherwise.
*/
export function isEffectiveArray(arr: unknown[]): boolean {
export function isValidArray(arr: unknown[]): boolean {
return Array.isArray(arr) && arr.length > 0
}

export function isPropertyKey(val: unknown): val is PropertyKey {
return isStr(val) || isNumber(val) || isSymbol(val)
}

export function isValidDate(date: Date) {
return date.toString() !== 'Invalid Date'
}
Loading

0 comments on commit 02509a6

Please sign in to comment.