Skip to content

Commit

Permalink
feat: add isPlainObject type guard (#16)
Browse files Browse the repository at this point in the history
* feat: add `isPlainObject` type guard

* docs: add isPlainObject
  • Loading branch information
aleclarson authored Jun 24, 2024
1 parent 0e3a83a commit ddae618
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
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

0 comments on commit ddae618

Please sign in to comment.