Created
December 12, 2018 07:58
-
-
Save zempo/c237c0e41e99015140620ef2e64b3c11 to your computer and use it in GitHub Desktop.
validate object keys drill created by zempo1 - https://repl.it/@zempo1/validate-object-keys-drill
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
// running the function with `objectA` and `expectedKeys` | |
// should return `true` | |
const objectA = { | |
id: 2, | |
name: 'Jane Doe', | |
age: 34, | |
city: 'Chicago', | |
}; | |
// running the function with `objectB` and `expectedKeys` | |
// should return `false` | |
const objectB = { | |
id: 3, | |
age: 33, | |
city: 'Peoria', | |
}; | |
const expectedKeys = ['id', 'name', 'age', 'city']; | |
function validateKeys(object, expectedKeys) { | |
if (Object.keys(object).length !== expectedKeys.length) { | |
return false; | |
} | |
for (let i = 0; i < expectedKeys.length; i++) { | |
if (!Object.keys(object).find(key => key === expectedKeys[i])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
function testIt() { | |
const objectA = { | |
id: 2, | |
name: 'Jane Doe', | |
age: 34, | |
city: 'Chicago', | |
}; | |
const objectB = { | |
id: 3, | |
age: 33, | |
city: 'Peoria', | |
}; | |
const objectC = { | |
id: 9, | |
name: 'Billy Bear', | |
age: 62, | |
city: 'Milwaukee', | |
status: 'paused', | |
}; | |
const objectD = { | |
foo: 2, | |
bar: 'Jane Doe', | |
bizz: 34, | |
bang: 'Chicago', | |
}; | |
const expectedKeys = ['id', 'name', 'age', 'city']; | |
if (typeof validateKeys(objectA, expectedKeys) !== 'boolean') { | |
console.error('FAILURE: validateKeys should return a boolean value'); | |
return; | |
} | |
if (!validateKeys(objectA, expectedKeys)) { | |
console.error( | |
`FAILURE: running validateKeys with the following object and keys | |
should return true but returned false: | |
Object: ${JSON.stringify(objectA)} | |
Expected keys: ${expectedKeys}` | |
); | |
return; | |
} | |
if (validateKeys(objectB, expectedKeys)) { | |
console.error( | |
`FAILURE: running validateKeys with the following object and keys | |
should return false but returned true: | |
Object: ${JSON.stringify(objectB)} | |
Expected keys: ${expectedKeys}` | |
); | |
return; | |
} | |
if (validateKeys(objectC, expectedKeys)) { | |
console.error( | |
`FAILURE: running validateKeys with the following object and keys | |
should return false but returned true: | |
Object: ${JSON.stringify(objectC)} | |
Expected keys: ${expectedKeys}` | |
); | |
return; | |
} | |
if (validateKeys(objectD, expectedKeys)) { | |
console.error( | |
`FAILURE: running validateKeys with the following object and keys | |
should return false but returned true: | |
Object: ${JSON.stringify(objectD)} | |
Expected keys: ${expectedKeys}` | |
); | |
return; | |
} | |
console.log('SUCCESS: validateKeys is working'); | |
} | |
testIt(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function validateKeys(object, expectedKeys) {
// if there's not the same number of object keys
// and expected keys, then we know there are missing or
// extra keys, so return false
if (Object.keys(object).length !== expectedKeys.length) {
return false;
}
// we iterate over each expected key and verify that
// it's found in
object
.for (let i = 0; i < expectedKeys.length; i++) {
if (!Object.keys(object).find(key => key === expectedKeys[i])) {
return false;
}
}
// if we get to this point in our code, the keys are valid
// so we return
true
return true;
}
starred for future study!!!