Sha256: b1e22bf1fc60266f15f7d790f15030bfbde618a02069ab46572af3f60cfd38f5
Contents?: true
Size: 1.33 KB
Versions: 8
Compression:
Stored size: 1.33 KB
Contents
var charToInteger = {}; var integerToChar = {}; 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split( '' ).forEach( function ( char, i ) { charToInteger[ char ] = i; integerToChar[ i ] = char; }); export function decode ( string ) { var result = []; var shift = 0; var value = 0; for ( var i = 0; i < string.length; i += 1 ) { var integer = charToInteger[ string[i] ]; if ( integer === undefined ) { throw new Error( 'Invalid character (' + string[i] + ')' ); } var hasContinuationBit = integer & 32; integer &= 31; value += integer << shift; if ( hasContinuationBit ) { shift += 5; } else { var shouldNegate = value & 1; value >>= 1; result.push( shouldNegate ? -value : value ); // reset value = shift = 0; } } return result; } export function encode ( value ) { var result; if ( typeof value === 'number' ) { result = encodeInteger( value ); } else { result = ''; for ( var i = 0; i < value.length; i += 1 ) { result += encodeInteger( value[i] ); } } return result; } function encodeInteger ( num ) { var result = ''; if ( num < 0 ) { num = ( -num << 1 ) | 1; } else { num <<= 1; } do { var clamped = num & 31; num >>= 5; if ( num > 0 ) { clamped |= 32; } result += integerToChar[ clamped ]; } while ( num > 0 ); return result; }
Version data entries
8 entries across 8 versions & 3 rubygems