Skip to content

Instantly share code, notes, and snippets.

@camshaft
Last active December 18, 2015 19:39
Show Gist options
  • Save camshaft/5834652 to your computer and use it in GitHub Desktop.
Save camshaft/5834652 to your computer and use it in GitHub Desktop.
Enum compression
/**
* Compress scopes with an emum into an efficient integer
*/
function compressScope(scopes, scopesEnum) {
var value = scopesEnum.map(function(scope) {
return (!!~scopes.indexOf(scope)) ? '1' : '0';
}).join('');
return parseInt(value, 2);
};
var requestedScopes = ['user:email', 'app:products']
, scopesEnum = ['user:name', 'user:email', 'user:address', 'app:products', 'app:products:edit'];
compressScope(requestedScopes, scopesEnum) === 10; // 01010b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment