Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions packages/ella/src/general/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import { dispatchCustomEvent } from '../dom';
import { CUSTOM_EVENTS } from '../utils/constants';
import {
AllowedValueType,
Empty,
GenericArguments,
GenericFunction,
MultiLevelObject,
PickEmptyType,
SingleLevelObject,
TabsData
} from '../utils/types';
Expand All @@ -27,28 +30,27 @@ export { default as isEqual } from 'lodash.isequal';
* }
* ```
*/
export function isEmpty(data: any) {
export function isEmpty<T extends AllowedValueType>(data: T | Empty): data is PickEmptyType<T> {
try {
if (data === null || data === undefined || typeof data === 'undefined') {
return true;
}

const dataType = typeof data;

switch (dataType) {
}

switch (typeof data) {
case 'string':
if (data.trim() === '' || data === 'null' || data === null) {
if ((data as string).trim() === '' || (data as string) === 'null' || data === null) {
return true;
}

return false;

case 'object':
const keys = Object.keys(data);
const len = keys.length;
if (Array.isArray(data) && data.length === 0) {
return true;
}

if (len <= 0) {
if (Object.keys(data).length === 0) {
return true;
}

Expand All @@ -58,11 +60,6 @@ export function isEmpty(data: any) {
return false;

default:
// for array
if (Array.isArray(data) && data.length <= 0) {
return true;
}

return false;
}

Expand Down
10 changes: 10 additions & 0 deletions packages/ella/src/utils/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ export type TabsData = {
searchId: string;
[key:string]: unknown;
};


// isEmpty function

export type AllowedValueType = string |number| Array<any> | object | null | undefined | symbol;

export type Empty = [] | {} | '';

export type PickEmptyType<T> =
T extends AllowedValueType ? (T extends Empty ? never : T) : T;