base62.js.flow 569 B

123456789101112131415161718192021222324252627
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule base62
  8. * @flow
  9. */
  10. 'use strict';
  11. const BASE62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  12. function base62(number: number): string {
  13. if (!number) {
  14. return '0';
  15. }
  16. let string = '';
  17. while (number > 0) {
  18. string = BASE62[number % 62] + string;
  19. number = Math.floor(number / 62);
  20. }
  21. return string;
  22. }
  23. module.exports = base62;