Skip to content

Commit

Permalink
Add TypeScript definition (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 20, 2019
1 parent 293bceb commit 1ee301e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
29 changes: 29 additions & 0 deletions index.d.ts
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;
6 changes: 3 additions & 3 deletions index.js
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({});
};
8 changes: 8 additions & 0 deletions index.test-d.ts
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);
}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"obj",
Expand All @@ -31,7 +32,8 @@
"simple"
],
"devDependencies": {
"ava": "^1.0.1",
"xo": "^0.23.0"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}

0 comments on commit 1ee301e

Please sign in to comment.