Skip to content

Instantly share code, notes, and snippets.

@momothereal momothereal/idtest.js Secret
Last active Jun 13, 2019

Embed
What would you like to do?
const keys = ["foo", "bar", "max"]
const properties = {
"foo": [
"true",
"false"
],
"bar": [
"true",
"false",
"maybe"
],
"max": [
"true",
"false"
]
}
function propEnumLength(prop) {
return properties[prop].length;
}
function propBitSize(prop) {
let len = propEnumLength(prop);
return Math.ceil(Math.log2(len));
}
function indexOf(prop, val) {
return properties[prop].indexOf(val);
}
// Test case
let state = {
"foo": "true",
"bar": "maybe",
"max": "false"
}
// Algo
let shift = 0; // k
for (key of keys) {
let val = state[key];
let index = indexOf(key, val); // i
let len = propEnumLength(key); // l
shift = len * shift + index; // k = l * (previous k) + i
}
// Result
console.log(shift);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.