Skip to content

Instantly share code, notes, and snippets.

@habbes
Created March 17, 2019 20:32
Show Gist options
  • Save habbes/e2269d0f4f0d75a443dddfe66ab76657 to your computer and use it in GitHub Desktop.
Save habbes/e2269d0f4f0d75a443dddfe66ab76657 to your computer and use it in GitHub Desktop.
WonderfulLeafyReality created by habbes1 - https://repl.it/@habbes1/WonderfulLeafyReality
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