Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Created July 16, 2023 11:00
Show Gist options
  • Save steveruizok/28f3f8c2b574d74570df42b00848985f to your computer and use it in GitHub Desktop.
Save steveruizok/28f3f8c2b574d74570df42b00848985f to your computer and use it in GitHub Desktop.
is serializable
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