MCPcopy
hub / github.com/toeverything/AFFiNE / toPlain

Function toPlain

tools/doc-diff/src/plain.ts:13–66  ·  view source on GitHub ↗
(value: unknown)

Source from the content-addressed store, hash-verified

11export const MISSING: unique symbol = Symbol('missing');
12
13export function toPlain(value: unknown): PlainValue {
14 if (value === null) {
15 return null;
16 }
17 switch (typeof value) {
18 case 'boolean':
19 case 'number':
20 case 'string':
21 return value;
22 case 'undefined':
23 return null;
24 }
25
26 if (value instanceof Text) {
27 return value.toString();
28 }
29
30 if (value instanceof Doc) {
31 return { __type: 'YDoc', guid: value.guid };
32 }
33
34 if (value instanceof YArray) {
35 return value.toArray().map(toPlain);
36 }
37
38 if (value instanceof YMap) {
39 const keys = Array.from(value.keys()).sort();
40 const obj: Record<string, PlainValue> = {};
41 for (const key of keys) {
42 obj[key] = toPlain(value.get(key));
43 }
44 return obj;
45 }
46
47 if (value instanceof Uint8Array) {
48 return Buffer.from(value).toString('base64');
49 }
50
51 if (Array.isArray(value)) {
52 return value.map(toPlain);
53 }
54
55 if (value && typeof value === 'object') {
56 const record = value as Record<string, unknown>;
57 const keys = Object.keys(record).sort();
58 const obj: Record<string, PlainValue> = {};
59 for (const key of keys) {
60 obj[key] = toPlain(record[key]);
61 }
62 return obj;
63 }
64
65 return String(value);
66}
67
68function stableComparable(value: unknown): unknown {
69 if (value === MISSING) {

Callers 2

extractRootDocPagesMetaFunction · 0.90
extractYjsTableFunction · 0.90

Calls 6

mapMethod · 0.80
toStringMethod · 0.65
keysMethod · 0.65
getMethod · 0.65
toArrayMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected