You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.7 KiB

  1. 'use strict';
  2. var alphabet = require('./alphabet');
  3. var encode = require('./encode');
  4. var decode = require('./decode');
  5. var isValid = require('./is-valid');
  6. // Ignore all milliseconds before a certain time to reduce the size of the date entropy without sacrificing uniqueness.
  7. // This number should be updated every year or so to keep the generated id short.
  8. // To regenerate `new Date() - 0` and bump the version. Always bump the version!
  9. var REDUCE_TIME = 1426452414093;
  10. // don't change unless we change the algos or REDUCE_TIME
  11. // must be an integer and less than 16
  12. var version = 5;
  13. // if you are using cluster or multiple servers use this to make each instance
  14. // has a unique value for worker
  15. // Note: I don't know if this is automatically set when using third
  16. // party cluster solutions such as pm2.
  17. var clusterWorkerId = require('./util/cluster-worker-id') || 0;
  18. // Counter is used when shortid is called multiple times in one second.
  19. var counter;
  20. // Remember the last time shortid was called in case counter is needed.
  21. var previousSeconds;
  22. /**
  23. * Generate unique id
  24. * Returns string id
  25. */
  26. function generate() {
  27. var str = '';
  28. var seconds = Math.floor((Date.now() - REDUCE_TIME) * 0.001);
  29. if (seconds === previousSeconds) {
  30. counter++;
  31. } else {
  32. counter = 0;
  33. previousSeconds = seconds;
  34. }
  35. str = str + encode(alphabet.lookup, version);
  36. str = str + encode(alphabet.lookup, clusterWorkerId);
  37. if (counter > 0) {
  38. str = str + encode(alphabet.lookup, counter);
  39. }
  40. str = str + encode(alphabet.lookup, seconds);
  41. return str;
  42. }
  43. /**
  44. * Set the seed.
  45. * Highly recommended if you don't want people to try to figure out your id schema.
  46. * exposed as shortid.seed(int)
  47. * @param seed Integer value to seed the random alphabet. ALWAYS USE THE SAME SEED or you might get overlaps.
  48. */
  49. function seed(seedValue) {
  50. alphabet.seed(seedValue);
  51. return module.exports;
  52. }
  53. /**
  54. * Set the cluster worker or machine id
  55. * exposed as shortid.worker(int)
  56. * @param workerId worker must be positive integer. Number less than 16 is recommended.
  57. * returns shortid module so it can be chained.
  58. */
  59. function worker(workerId) {
  60. clusterWorkerId = workerId;
  61. return module.exports;
  62. }
  63. /**
  64. *
  65. * sets new characters to use in the alphabet
  66. * returns the shuffled alphabet
  67. */
  68. function characters(newCharacters) {
  69. if (newCharacters !== undefined) {
  70. alphabet.characters(newCharacters);
  71. }
  72. return alphabet.shuffled();
  73. }
  74. // Export all other functions as properties of the generate function
  75. module.exports = generate;
  76. module.exports.generate = generate;
  77. module.exports.seed = seed;
  78. module.exports.worker = worker;
  79. module.exports.characters = characters;
  80. module.exports.decode = decode;
  81. module.exports.isValid = isValid;