Last active
December 18, 2015 19:39
-
-
Save camshaft/5834652 to your computer and use it in GitHub Desktop.
Enum compression
This file contains hidden or 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
| /** | |
| * 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