Skip to content

Commit

Permalink
feat: add isValidArray function
Browse files Browse the repository at this point in the history
  • Loading branch information
cc-hearts committed Aug 3, 2024
1 parent 02cfee7 commit 36ae02a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
45 changes: 44 additions & 1 deletion __test__/lib/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
isFalsy,
isNumber,
isEffectiveNumber,
isPromise
isPromise,
isValidArray
} from '../../lib/validate'

describe('isArrayEquals', () => {
Expand Down Expand Up @@ -219,3 +220,45 @@ describe('isPromise', () => {
expect(isPromise(null)).toBe(false)
})
})

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

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

test('should return false for a non-array value', () => {
// @ts-ignore

Check failure on line 234 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 251 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 256 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 261 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);
});
})
6 changes: 3 additions & 3 deletions lib/shard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This function does nothing.
*
* @return {void} No return value.
* @return No return value.
*/
export const noop = () => {
/** */
Expand All @@ -10,8 +10,8 @@ export const noop = () => {
/**
* Sleeps for a given delay.
*
* @param {number} delay - The delay, in milliseconds.
* @return {Promise<void>} A promise that resolves after the delay.
* @param delay - The delay, in milliseconds.
* @return A promise that resolves after the delay.
*/
export const sleep = (delay: number) =>
new Promise((resolve) => setTimeout(resolve, delay))
15 changes: 13 additions & 2 deletions lib/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export function isNull(val: unknown): val is null {
/**
* Determines whether a value is a primitive.
*
* @param {unknown} val - The value to check.
* @return {boolean} Returns `true` if the value is a primitive, `false` otherwise.
* @param val - The value to check.
* @return Returns `true` if the value is a primitive, `false` otherwise.
*/
export function isPrimitive(val: unknown) {
return typeof val !== 'object' || val === null
Expand Down Expand Up @@ -152,3 +152,14 @@ export function isArrayEquals(
export function hasOwn(obj: object, prop: string): boolean {
return Object.prototype.hasOwnProperty.call(obj, prop)
}

/**
* Checks if an array is valid, where validity means it is an array and has at least one element.
*
* @param arr The array to check for validity.
* @return Returns true if the array is valid, and false otherwise.
*/
export function isValidArray(arr: unknown[]) {
return Array.isArray(arr) && arr.length > 0;
}

0 comments on commit 36ae02a

Please sign in to comment.