Skip to content

Instantly share code, notes, and snippets.

@james-jlo-long
Created July 13, 2018 10:02
Show Gist options
  • Save james-jlo-long/241c904d73e0ec8b43050cb846897649 to your computer and use it in GitHub Desktop.
Save james-jlo-long/241c904d73e0ec8b43050cb846897649 to your computer and use it in GitHub Desktop.
A few constants for commonly needed numbers. Not sure how useful this is, but could be helpful to someone
const times = (i, handler, context) => {
let count = 0;
let max = Math.floor(Math.abs(i)) || 0;
while (count < max) {
handler.call(context, count);
count += 1;
}
return count;
};
times(4, (i) => {
// bit = 4, 8, 16 or 32.
// 64 would give a number larger than MAX_SAFE_INTEGER
let bit = Math.pow(2, i + 2);
let top = Math.pow(2, bit - 1);
let constant = {
enumerable: false,
configurable: false,
writable: false,
};
Object.defineProperties(Number, {
[`MIN_VALUE_${bit}_BIT_SIGNED`]: {
...constant,
value: top / -2
},
[`MAX_VALUE_${bit}_BIT_SIGNED`]: {
...constant,
value: (top / 2) - 1
},
[`MAX_VALUE_${bit}_BIT_UNSIGNED`]: {
...constant,
value: top - 1
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment