1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (factory((global.vlq = global.vlq || {})));
  5. }(this, (function (exports) { 'use strict';
  6. var charToInteger = {};
  7. var integerToChar = {};
  8. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split( '' ).forEach( function ( char, i ) {
  9. charToInteger[ char ] = i;
  10. integerToChar[ i ] = char;
  11. });
  12. function decode ( string ) {
  13. var result = [];
  14. var shift = 0;
  15. var value = 0;
  16. for ( var i = 0; i < string.length; i += 1 ) {
  17. var integer = charToInteger[ string[i] ];
  18. if ( integer === undefined ) {
  19. throw new Error( 'Invalid character (' + string[i] + ')' );
  20. }
  21. var hasContinuationBit = integer & 32;
  22. integer &= 31;
  23. value += integer << shift;
  24. if ( hasContinuationBit ) {
  25. shift += 5;
  26. } else {
  27. var shouldNegate = value & 1;
  28. value >>= 1;
  29. result.push( shouldNegate ? -value : value );
  30. // reset
  31. value = shift = 0;
  32. }
  33. }
  34. return result;
  35. }
  36. function encode ( value ) {
  37. var result;
  38. if ( typeof value === 'number' ) {
  39. result = encodeInteger( value );
  40. } else {
  41. result = '';
  42. for ( var i = 0; i < value.length; i += 1 ) {
  43. result += encodeInteger( value[i] );
  44. }
  45. }
  46. return result;
  47. }
  48. function encodeInteger ( num ) {
  49. var result = '';
  50. if ( num < 0 ) {
  51. num = ( -num << 1 ) | 1;
  52. } else {
  53. num <<= 1;
  54. }
  55. do {
  56. var clamped = num & 31;
  57. num >>= 5;
  58. if ( num > 0 ) {
  59. clamped |= 32;
  60. }
  61. result += integerToChar[ clamped ];
  62. } while ( num > 0 );
  63. return result;
  64. }
  65. exports.decode = decode;
  66. exports.encode = encode;
  67. Object.defineProperty(exports, '__esModule', { value: true });
  68. })));