Created
March 5, 2020 22:13
-
-
Save huytd/81880fe17525de9ebd724078d2569f9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const root = { | |
val: 1, | |
left: { | |
val: 2, | |
left: null, | |
right: null | |
}, | |
right: { | |
val: 3, | |
left: null, | |
right: null | |
} | |
}; | |
const schemalize = obj => { | |
const schema = []; | |
for (let key in obj) { | |
schema.push({ | |
name: key, | |
type: typeof obj[key] | |
}); | |
} | |
return schema; | |
}; | |
const compareSchema = (a, b) => { | |
if (a.length != b.length) return false; | |
for (let i = 0; i < a.length; i++) { | |
if (a[i].name !== b[i].name || a[i].type !== b[i].type) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
const isSameSchema = schemas => !schemas.some(s => !compareSchema(schemas[0], s)); | |
const rootSchema = schemalize(root); | |
const leftSchema = schemalize(root.left); | |
const rightSchema = schemalize(root.right); | |
const result = isSameSchema([rootSchema, leftSchema, rightSchema]); | |
debug(result); | |
debug(rootSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment