MCPcopy Index your code
hub / github.com/TanStack/db / toStableSerializable

Function toStableSerializable

packages/db-sqlite-persistence-core/src/persisted.ts:624–697  ·  view source on GitHub ↗
(value: unknown)

Source from the content-addressed store, hash-verified

622}
623
624function toStableSerializable(value: unknown): unknown {
625 if (value == null) {
626 return value
627 }
628
629 switch (typeof value) {
630 case `string`:
631 case `number`:
632 case `boolean`:
633 return value
634 case `bigint`:
635 return value.toString()
636 case `function`:
637 case `symbol`:
638 case `undefined`:
639 return undefined
640 }
641
642 if (value instanceof Date) {
643 return value.toISOString()
644 }
645
646 if (Array.isArray(value)) {
647 return value
648 .map((entry) => toStableSerializable(entry))
649 .filter((entry) => entry !== undefined)
650 }
651
652 if (value instanceof Set) {
653 return Array.from(value)
654 .map((entry) => toStableSerializable(entry))
655 .filter((entry) => entry !== undefined)
656 .sort((left, right) => {
657 const leftSerialized = JSON.stringify(left)
658 const rightSerialized = JSON.stringify(right)
659 return leftSerialized < rightSerialized
660 ? -1
661 : leftSerialized > rightSerialized
662 ? 1
663 : 0
664 })
665 }
666
667 if (value instanceof Map) {
668 return Array.from(value.entries())
669 .map(([key, mapValue]) => ({
670 key: toStableSerializable(key),
671 value: toStableSerializable(mapValue),
672 }))
673 .filter((entry) => entry.key !== undefined && entry.value !== undefined)
674 .sort((left, right) => {
675 const leftSerialized = JSON.stringify(left.key)
676 const rightSerialized = JSON.stringify(right.key)
677 return leftSerialized < rightSerialized
678 ? -1
679 : leftSerialized > rightSerialized
680 ? 1
681 : 0

Callers 3

stableSerializeFunction · 0.70

Calls 6

filterMethod · 0.80
fromMethod · 0.80
toStringMethod · 0.45
mapMethod · 0.45
entriesMethod · 0.45
keysMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…