-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tinytype): toJSON supports serialising plain objects with nested …
…Maps and Sets
- Loading branch information
Showing
6 changed files
with
103 additions
and
52 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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './deprecated'; | ||
export * from './equal'; | ||
export * from './isObject'; | ||
export * from './isRecord'; | ||
export * from './significantFields'; | ||
export * from './stringify'; |
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,10 @@ | ||
/** | ||
* @access private | ||
*/ | ||
export function isObject(value: unknown): value is object { | ||
return value !== null | ||
&& value !== undefined | ||
&& typeof value === 'object' | ||
&& Array.isArray(value) === false | ||
&& Object.prototype.toString.call(value) === '[object Object]'; | ||
} |
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,25 @@ | ||
import { isObject } from './isObject'; | ||
|
||
export function isRecord(value: unknown): value is Record<any, any> { | ||
if (! isObject(value)) { | ||
return false; | ||
} | ||
|
||
// If has modified constructor | ||
if (value.constructor === undefined) { | ||
return true; | ||
} | ||
|
||
// If has modified prototype | ||
if (! isObject(value.constructor.prototype)) { | ||
return false; | ||
} | ||
|
||
// If constructor does not have an Object-specific method | ||
if (! Object.prototype.hasOwnProperty.call(value.constructor.prototype, 'isPrototypeOf')) { | ||
return false; | ||
} | ||
|
||
// Most likely a plain Object | ||
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