MCPcopy Create free account
hub / github.com/apache/fory / data2TypeInfo

Function data2TypeInfo

javascript/benchmark/index.js:32–91  ·  view source on GitHub ↗
(
  data,
  typeName,
)

Source from the content-addressed store, hash-verified

30
31
32export const data2TypeInfo = (
33 data,
34 typeName,
35) => {
36 if (data === null || data === undefined) {
37 return null;
38 }
39 if (Array.isArray(data)) {
40 const item = data2TypeInfo(data[0], typeName);
41 if (!item) {
42 throw new Error("empty array can't convert");
43 }
44 return Type.list(item);
45 }
46 if (data instanceof Date) {
47 return Type.timestamp();
48 }
49 if (typeof data === "string") {
50 return Type.string();
51 }
52 if (data instanceof Set) {
53 return Type.set(data2TypeInfo([...data.values()][0], typeName));
54 }
55 if (data instanceof Map) {
56 return Type.map(
57 data2TypeInfo([...data.keys()][0], typeName),
58 data2TypeInfo([...data.values()][0], typeName),
59 );
60 }
61 if (typeof data === "boolean") {
62 return Type.bool();
63 }
64 if (typeof data === "number") {
65 if (data > Number.MAX_SAFE_INTEGER || data < Number.MIN_SAFE_INTEGER) {
66 return Type.int64();
67 }
68 return Type.int32();
69 }
70
71 if (typeof data === "object") {
72 if (isUint8Array(data)) {
73 return Type.binary();
74 }
75
76 return Type.struct(
77 {
78 typeName
79 },
80 Object.fromEntries(
81 Object.entries(data)
82 .map(([key, value]) => {
83 return [key, data2TypeInfo(value, `${typeName}.${key}`)];
84 })
85 .filter(([, v]) => Boolean(v)),
86 ),
87 );
88 }
89

Callers

nothing calls this directly

Calls 14

isUint8ArrayFunction · 0.85
timestampMethod · 0.80
int64Method · 0.80
int32Method · 0.80
binaryMethod · 0.80
structMethod · 0.80
isArrayMethod · 0.45
listMethod · 0.45
stringMethod · 0.45
setMethod · 0.45
valuesMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected