uniqueId.js 570 B

123456789101112131415161718192021222324252627
  1. var baseToString = require('../internal/baseToString');
  2. /** Used to generate unique IDs. */
  3. var idCounter = 0;
  4. /**
  5. * Generates a unique ID. If `prefix` is provided the ID is appended to it.
  6. *
  7. * @static
  8. * @memberOf _
  9. * @category Utility
  10. * @param {string} [prefix] The value to prefix the ID with.
  11. * @returns {string} Returns the unique ID.
  12. * @example
  13. *
  14. * _.uniqueId('contact_');
  15. * // => 'contact_104'
  16. *
  17. * _.uniqueId();
  18. * // => '105'
  19. */
  20. function uniqueId(prefix) {
  21. var id = ++idCounter;
  22. return baseToString(prefix) + id;
  23. }
  24. module.exports = uniqueId;