-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
293bceb
commit 1ee301e
Showing
4 changed files
with
46 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
Check if a value is a plain object. | ||
An object is plain if it's created by either `{}`, `new Object()`, or `Object.create(null)`. | ||
@example | ||
``` | ||
import isPlainObject = require('is-plain-obj'); | ||
isPlainObject({foo: 'bar'}); | ||
//=> true | ||
isPlainObject(new Object()); | ||
//=> true | ||
isPlainObject(Object.create(null)); | ||
//=> true | ||
isPlainObject([1, 2, 3]); | ||
//=> false | ||
class Unicorn {} | ||
isPlainObject(new Unicorn()); | ||
//=> false | ||
``` | ||
*/ | ||
declare function isPlainObj(candidate: unknown): candidate is object; | ||
|
||
export = isPlainObj; |
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,10 +1,10 @@ | ||
'use strict'; | ||
|
||
module.exports = input => { | ||
if (Object.prototype.toString.call(input) !== '[object Object]') { | ||
module.exports = candidate => { | ||
if (Object.prototype.toString.call(candidate) !== '[object Object]') { | ||
return false; | ||
} | ||
|
||
const prototype = Object.getPrototypeOf(input); | ||
const prototype = Object.getPrototypeOf(candidate); | ||
return prototype === null || prototype === Object.getPrototypeOf({}); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {expectType} from 'tsd'; | ||
import isPlainObj = require('.'); | ||
|
||
const foo = 'foo'; | ||
|
||
if (isPlainObj(foo)) { | ||
expectType<object>(foo); | ||
} |
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