-
Notifications
You must be signed in to change notification settings - Fork 15
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
23117ee
commit 7fffc14
Showing
8 changed files
with
133 additions
and
43 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
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
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,37 @@ | ||
# isPlainObject | ||
|
||
Chec whether the given value was created by the Object constructor, or `Object.create(null)`. | ||
|
||
### Import | ||
|
||
```typescript copy | ||
import { isPlainObject } from '@fullstacksjs/toolbox'; | ||
``` | ||
|
||
### Signature | ||
|
||
```typescript copy | ||
function isPlainObject(x: unknown): x is ObjectType; | ||
``` | ||
|
||
### Examples | ||
|
||
```typescript copy | ||
isPlainObject('') // false | ||
isPlainObject('hello world') // false | ||
isPlainObject(null) // false | ||
isPlainObject(true) // false | ||
isPlainObject(undefined) // false | ||
isPlainObject(NaN) // false | ||
isPlainObject(0) // false | ||
isPlainObject(isPlainObject) // false | ||
isPlainObject(false) // false | ||
isPlainObject([]) // false | ||
isPlainObject([2]) // false | ||
isPlainObject(new Map()) // false | ||
isPlainObject(new Set()) // false | ||
isPlainObject(new RegExp('foo')) // false | ||
isPlainObject({}) // true | ||
isPlainObject({ a: 2 }) // true | ||
isPlainObject({ 2: 'a' }) // true | ||
``` |
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
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
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,30 @@ | ||
import { isPlainObject } from './isPlainObject.ts'; | ||
|
||
describe('isPlainObject', () => { | ||
const cases = [ | ||
{ x: '', expected: false }, | ||
{ x: 'hello world', expected: false }, | ||
{ x: null, expected: false }, | ||
{ x: true, expected: false }, | ||
{ x: undefined, expected: false }, | ||
{ x: NaN, expected: false }, | ||
{ x: 0, expected: false }, | ||
{ x: isPlainObject, expected: false }, | ||
{ x: false, expected: false }, | ||
{ x: [], expected: false }, | ||
{ x: [2], expected: false }, | ||
{ x: new Map(), expected: false }, | ||
{ x: new Set(), expected: false }, | ||
{ x: new RegExp('foo'), expected: false }, | ||
{ x: {}, expected: true }, | ||
{ x: { a: 2 }, expected: true }, | ||
{ x: { 2: 'a' }, expected: true }, | ||
]; | ||
|
||
it.each(cases)( | ||
'should return $expected for $x as input', | ||
({ x, expected }) => { | ||
expect(isPlainObject(x)).toBe(expected); | ||
}, | ||
); | ||
}); |
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,22 @@ | ||
import type { ObjectType } from '../types/types'; | ||
|
||
function isObject(o: unknown): o is ObjectType { | ||
return Object.prototype.toString.call(o) === '[object Object]'; | ||
} | ||
|
||
export function isPlainObject(o: unknown): o is ObjectType { | ||
if (!isObject(o)) return false; | ||
|
||
const ctor = o.constructor; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (ctor == null) return true; | ||
|
||
const prototype = ctor.prototype; | ||
if (!isObject(prototype)) return false; | ||
|
||
// If constructor does not have an Object-specific method | ||
if (!prototype.hasOwnProperty('isPrototypeOf')) return false; | ||
|
||
return true; | ||
} |
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