import { PrimitiveAtom, Getter } from "jotai"; export const parseAtomToJSON = , K extends any>(atomToParse: T, get: Getter): K => { const getAtomValue = get(atomToParse); if (getAtomValue instanceof Array) { return parseAtomArrayToJSON(getAtomValue, get) as K; } if (getAtomValue instanceof Object) { return parseAtomObjectToJSON(atomToParse as PrimitiveAtom, get) as K; } return getAtomValue as K; } const parseAtomObjectToJSON = >(atomObject: T, get: Getter) => { const parsed = {} as Record; const getAtomObject = get(atomObject) as Record>; const itemKeys = Object.keys(getAtomObject); itemKeys.forEach((key) => { const atomValueToParse = getAtomObject[key]; parsed[key] = parseAtomToJSON(atomValueToParse, get); }) return parsed; } const parseAtomArrayToJSON = []>(atomArray: T, get: Getter) => { return atomArray.map((atomElement) => parseAtomToJSON(atomElement, get)); }