Skip to content

Commit

Permalink
Merge pull request #1 from shtse8/kirosIsK/isObj
Browse files Browse the repository at this point in the history
Modify typed.ts
  • Loading branch information
shtse8 authored Mar 15, 2024
2 parents 117bcca + 20c86a4 commit 2eadfb4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,5 @@ dist

# Finder (MacOS) folder config
.DS_Store
/astro/package-lock.json
/package-lock.json
7 changes: 5 additions & 2 deletions src/typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function isFn(value: unknown): value is Function {
* @returns true if the value is an object, false otherwise
*/
export function isObj(value: unknown): value is object {
return !!value && typeof value === 'object';
return !!value && typeof value === 'object' && !isArr(value);
}

/**
Expand Down Expand Up @@ -195,5 +195,8 @@ export function isTruthy(value: unknown): boolean {
* @returns
*/
export function isEmpty(value: any): boolean {
return value === null || value === undefined || (typeof value === 'string' && value.trim() === '') || (Array.isArray(value) && value.length === 0) || (typeof value === 'object' && Object.keys(value).length === 0);
return isNullish(value)
|| (isStr(value) && value.trim() === '')
|| (isArr(value) && value.length === 0)
|| (isObj(value) && Object.keys(value).length === 0);
}
3 changes: 3 additions & 0 deletions tests/typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('typed', () => {
expect(x.isKeyOf('b', { a: 1 })).toBe(false)
})
test('isObj', () => {
expect(x.isObj([])).toBe(false)
expect(x.isObj({})).toBe(true)
expect(x.isObj(123)).toBe(false)
})
Expand Down Expand Up @@ -79,6 +80,8 @@ describe('typed', () => {
expect(x.isEmpty('')).toBe(true)
expect(x.isEmpty([])).toBe(true)
expect(x.isEmpty({})).toBe(true)
expect(x.isEmpty(new Set())).toBe(true)
expect(x.isEmpty(new Map())).toBe(true)
expect(x.isEmpty(0)).toBe(false)
expect(x.isEmpty('abc')).toBe(false)
expect(x.isEmpty([1, 2, 3])).toBe(false)
Expand Down

0 comments on commit 2eadfb4

Please sign in to comment.