-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
35 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,48 @@ | ||
export const isObj = (v: unknown): v is Record<string, unknown> => { | ||
return !!v && typeof v === "object"; | ||
}; | ||
|
||
export const hasProp = <K extends PropertyKey>( | ||
data: object, | ||
prop: K | ||
): data is Record<K, unknown> => { | ||
return prop in data; | ||
}; | ||
|
||
/** | ||
* Helper to conditionally spread an item into an array. | ||
* Conditionally spread an item into an array. | ||
*/ | ||
export const arraySpread = <TItem>( | ||
/** Conditional value. When truthy, `itemToSpread` is returned. */ | ||
isTruthy: any, | ||
conditional: any, | ||
/** Item that gets returned when conditional is truthy */ | ||
itemToSpread: TItem | TItem[], | ||
/** Option to spread multiple values when `itemToSpread` is an array */ | ||
spreadMultiple = false | ||
) => { | ||
if (isTruthy) | ||
if (isTruthy(conditional)) | ||
return spreadMultiple && Array.isArray(itemToSpread) | ||
? (itemToSpread as TItem[]) | ||
: [itemToSpread as TItem]; | ||
return [] as TItem[]; | ||
}; | ||
|
||
export const hasProp = <K extends PropertyKey>( | ||
data: object, | ||
prop: K | ||
): data is Record<K, unknown> => { | ||
return prop in data; | ||
}; | ||
|
||
export const isObj = (v: unknown): v is Record<string, unknown> => { | ||
return !!v && typeof v === "object"; | ||
}; | ||
|
||
/** | ||
* @returns `true` when all supplied values are NOT any of the bellow: | ||
* - Is falsy | ||
* - Is empty array | ||
* - Is empty object | ||
*/ | ||
export const isTruthy = (...v: any[]) => { | ||
for (let i = 0; i < v.length; i++) { | ||
if ( | ||
!v[i] || | ||
(Array.isArray(v[i]) && v[i]?.length === 0) || | ||
(isObj(v[i]) && Object.keys(v[i] ?? {})?.length === 0) | ||
) | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
export const objKeys = Object.keys as <T extends object>(obj: T) => Array<keyof T>; |