Created
July 16, 2023 11:00
-
-
Save steveruizok/28f3f8c2b574d74570df42b00848985f to your computer and use it in GitHub Desktop.
is serializable
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
const serializableTypes = new Set(['string', 'number', 'boolean', 'undefined']) | |
/** | |
* Get whether a value is serializable. | |
* | |
* @example | |
* | |
* ```ts | |
* const A = isSerializable(1) // true | |
* const B = isSerializable('a') // true | |
* const C = isSerializable(true) // true | |
* const D = isSerializable(undefined) // false | |
* ``` | |
* | |
* @param value - The value to check. | |
* @public | |
*/ | |
export function isSerializable(value: any): boolean { | |
if (serializableTypes.has(typeof value) || value === null) return true | |
if (Array.isArray(value)) return value.every(isSerializable) | |
if (isPlainObject(value)) return Object.values(value).every(isSerializable) | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment