uniqueId.js 562 B

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