MCPcopy
hub / github.com/tensorflow/tfjs / plainObjectCheck

Function plainObjectCheck

tfjs-layers/src/user_defined_metadata.ts:64–104  ·  view source on GitHub ↗
(x: any)

Source from the content-addressed store, hash-verified

62 */
63// tslint:disable-next-line:no-any
64export function plainObjectCheck(x: any): boolean {
65 if (x === null) {
66 // Note: typeof `null` is 'object', and `null` is valid in JSON.
67 return true;
68 } else if (typeof x === 'object') {
69 if (Object.getPrototypeOf(x) === Object.prototype) {
70 // `x` is a JavaScript object and its prototype is Object.
71 const keys = Object.keys(x);
72 for (const key of keys) {
73 if (typeof key !== 'string') {
74 // JSON keys must be strings.
75 return false;
76 }
77 if (!plainObjectCheck(x[key])) { // Recursive call.
78 return false;
79 }
80 }
81 return true;
82 } else {
83 // `x` is a JavaScript object but its prototype is not Object.
84 if (Array.isArray(x)) {
85 // `x` is a JavaScript array.
86 for (const item of x) {
87 if (!plainObjectCheck(item)) { // Recursive call.
88 return false;
89 }
90 }
91 return true;
92 } else {
93 // `x` is a JavaScript object and its prototype is not Object,
94 // and it's not an Array. I.e., it's a complex object such as
95 // `Error` and `Date`.
96 return false;
97 }
98 }
99 } else {
100 // `x` is not a JavaScript object or `null`.
101 const xType = typeof x;
102 return xType === 'string' || xType === 'number' || xType === 'boolean';
103 }
104}

Callers 2

checkUserDefinedMetadataFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…