Skip to content

Instantly share code, notes, and snippets.

@larchanka

larchanka/uniqid.js

Last active Oct 27, 2019
Embed
What would you like to do?
JavaScript function that copies functionality of PHP's 'uniqid'.
(function () {
this.uniqid = function (pr, en) {
var pr = pr || '', en = en || false, result;
this.seed = function (s, w) {
s = parseInt(s, 10).toString(16);
return w < s.length ? s.slice(s.length - w) : (w > s.length) ? new Array(1 + (w - s.length)).join('0') + s : s;
};
result = pr + this.seed(parseInt(new Date().getTime() / 1000, 10), 8) + this.seed(Math.floor(Math.random() * 0x75bcd15) + 1, 5);
if (en) result += (Math.random() * 10).toFixed(8).toString();
return result;
};
})();
@larchanka

This comment has been minimized.

Copy link
Owner Author

@larchanka larchanka commented Nov 5, 2013

Based on http://phpjs.org/functions/uniqid/

How to use:

  • uniqid() - returns 52643b81917b3
  • uniqid('prefix_') - returns prefix_52643bb682821
  • uniqid('prefix_', true) - returns prefix_52643bdf3b1fb7.38568327
@lsloan

This comment has been minimized.

Copy link

@lsloan lsloan commented Jun 29, 2015

What's the purpose of variable us? It doesn't seem to be used anywhere.

@larchanka

This comment has been minimized.

Copy link
Owner Author

@larchanka larchanka commented Jul 1, 2015

@lsloan

Removed, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment