import { anyPass, assocPath, isEmpty, isNil, reject } from 'ramda'; import superjson from 'superjson'; export function toDots( obj: Record, path = '' ): Record { return Object.entries(obj).reduce((acc, [key, value]) => { if (typeof value === 'object' && value !== null) { return { ...acc, ...toDots(value as Record, `${path}${key}.`), }; } return { ...acc, [`${path}${key}`]: value, }; }, {}); } export function toObject( obj: Record ): Record { let result: Record = {}; Object.entries(obj).forEach(([key, value]) => { result = assocPath(key.split('.'), value, result); }); return result; } export const strip = reject(anyPass([isEmpty, isNil])); export function getSafeJson(str: string): T | null { try { return JSON.parse(str); } catch (e) { return null; } } export function getSuperJson(str: string): T | null { const json = getSafeJson(str); if ( typeof json === 'object' && json !== null && 'json' in json && 'meta' in json ) { return superjson.parse(str); } return json; }