Created
March 17, 2019 20:32
-
-
Save habbes/e2269d0f4f0d75a443dddfe66ab76657 to your computer and use it in GitHub Desktop.
WonderfulLeafyReality created by habbes1 - https://repl.it/@habbes1/WonderfulLeafyReality
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 hasAtMostOneAllowedKey = (arg, allowedKeys) => { | |
const [firstKey, ...otherKeys] = allowedKeys; | |
if (!firstKey) { | |
return true; | |
} | |
if (firstKey in arg) { | |
return !otherKeys.some(otherKey => otherKey in arg); | |
} | |
else { | |
return hasAtMostOneAllowedKey(arg, otherKeys); | |
} | |
} | |
const exclusiveKeys = ['tags', 'addTags', 'removeTags']; | |
function testWith(arg, expected) { | |
const res = hasAtMostOneAllowedKey(arg, exclusiveKeys); | |
if (res == expected) console.log("SUCCESS"); | |
else console.error(`ERROR: expected ${expected} but got ${res}`); | |
} | |
testWith({ tags: [] }, true); | |
testWith({ }, true); | |
testWith({ a: 1, tags: [] }, true); | |
testWith({ a: 2, tags: [], addTags: [] }, false); | |
testWith({ a: 1, addTags: []}, true) | |
testWith({ a: 1, removeTags: [] }, true); | |
testWith({ a: 1, addTags: [], removeTags: []}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment