index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. var alphabet = require('./alphabet');
  3. var build = require('./build');
  4. var isValid = require('./is-valid');
  5. // if you are using cluster or multiple servers use this to make each instance
  6. // has a unique value for worker
  7. // Note: I don't know if this is automatically set when using third
  8. // party cluster solutions such as pm2.
  9. var clusterWorkerId = require('./util/cluster-worker-id') || 0;
  10. /**
  11. * Set the seed.
  12. * Highly recommended if you don't want people to try to figure out your id schema.
  13. * exposed as shortid.seed(int)
  14. * @param seed Integer value to seed the random alphabet. ALWAYS USE THE SAME SEED or you might get overlaps.
  15. */
  16. function seed(seedValue) {
  17. alphabet.seed(seedValue);
  18. return module.exports;
  19. }
  20. /**
  21. * Set the cluster worker or machine id
  22. * exposed as shortid.worker(int)
  23. * @param workerId worker must be positive integer. Number less than 16 is recommended.
  24. * returns shortid module so it can be chained.
  25. */
  26. function worker(workerId) {
  27. clusterWorkerId = workerId;
  28. return module.exports;
  29. }
  30. /**
  31. *
  32. * sets new characters to use in the alphabet
  33. * returns the shuffled alphabet
  34. */
  35. function characters(newCharacters) {
  36. if (newCharacters !== undefined) {
  37. alphabet.characters(newCharacters);
  38. }
  39. return alphabet.shuffled();
  40. }
  41. /**
  42. * Generate unique id
  43. * Returns string id
  44. */
  45. function generate() {
  46. return build(clusterWorkerId);
  47. }
  48. // Export all other functions as properties of the generate function
  49. module.exports = generate;
  50. module.exports.generate = generate;
  51. module.exports.seed = seed;
  52. module.exports.worker = worker;
  53. module.exports.characters = characters;
  54. module.exports.isValid = isValid;