Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isPlainObject type guard #16

Merged
merged 2 commits into from
Jun 24, 2024
Merged
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
20 changes: 20 additions & 0 deletions docs/typed/is-plain-object.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: isPlainObject
description: 'Determine if a value is a plain object'
group: Typed
---

## Basic usage

Pass in a value and get a boolean telling you if the value is a plain object.

```ts
import { isPlainObject } from 'radash'

isPlainObject({}) // => true
isPlainObject(Object.create(null)) // => true

isPlainObject([]) // => false
isPlainObject(null) // => false
isPlainObject(new Date()) // => false
```
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export {
isIntString,
isNumber,
isObject,
isPlainObject,
isPrimitive,
isPromise,
isString,
Expand Down
27 changes: 27 additions & 0 deletions src/tests/typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ describe('typed module', () => {
})
})

describe('isPlainObject function', () => {
test('returns true for object literal', () => {
const result = _.isPlainObject({})
expect(result).toBeTruthy()
})
test('returns true for Object.create(null)', () => {
const result = _.isPlainObject(Object.create(null))
expect(result).toBeTruthy()
})
test('returns false for non-plain object', () => {
const result = _.isPlainObject(new Date())
expect(result).toBeFalsy()
})
test('returns false for array', () => {
const result = _.isPlainObject([1, 2, 3])
expect(result).toBeFalsy()
})
test('returns false for null', () => {
const result = _.isPlainObject(null)
expect(result).toBeFalsy()
})
test('returns false for undefined', () => {
const result = _.isPlainObject(undefined)
expect(result).toBeFalsy()
})
})

describe('isPrimitive function', () => {
test('returns true for all the primitives', () => {
const arr = [
Expand Down
12 changes: 11 additions & 1 deletion src/typed.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const toString = /* @__PURE__ */ Object.prototype.toString

export const isSymbol = (value: any): value is symbol => {
return !!value && value.constructor === Symbol
}
Expand All @@ -8,6 +10,14 @@ export const isObject = (value: any): value is object => {
return !!value && value.constructor === Object
}

export const isPlainObject = (value: any): value is object => {
if (toString.call(value) !== '[object Object]') {
return false
}
const proto = Object.getPrototypeOf(value)
return proto === null || proto === Object.getPrototypeOf({})
}

/**
* Checks if the given value is primitive.
*
Expand Down Expand Up @@ -55,7 +65,7 @@ export const isNumber = (value: any): value is number => {
}

export const isDate = (value: any): value is Date => {
return Object.prototype.toString.call(value) === '[object Date]'
return toString.call(value) === '[object Date]'
}

/**
Expand Down