PHP's uniqid in JavaScript

Here’s what our current JavaScript equivalent to PHP's uniqid looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module.exports = function uniqid (prefix, moreEntropy) {
// discuss at: http://locutus.io/php/uniqid/
// original by: Kevin van Zonneveld (http://kvz.io)
// revised by: Kankrelune (http://www.webfaktory.info/)
// note 1: Uses an internal counter (in locutus global) to avoid collision
// example 1: var $id = uniqid()
// example 1: var $result = $id.length === 13
// returns 1: true
// example 2: var $id = uniqid('foo')
// example 2: var $result = $id.length === (13 + 'foo'.length)
// returns 2: true
// example 3: var $id = uniqid('bar', true)
// example 3: var $result = $id.length === (23 + 'bar'.length)
// returns 3: true

if (typeof prefix === 'undefined') {
prefix = ''
}

var retId
var _formatSeed = function (seed, reqWidth) {
seed = parseInt(seed, 10).toString(16) // to hex str
if (reqWidth < seed.length) {
// so long we split
return seed.slice(seed.length - reqWidth)
}
if (reqWidth > seed.length) {
// so short we pad
return Array(1 + (reqWidth - seed.length)).join('0') + seed
}
return seed
}

var $global = (typeof window !== 'undefined' ? window : global)
$global.$locutus = $global.$locutus || {}
var $locutus = $global.$locutus
$locutus.php = $locutus.php || {}

if (!$locutus.php.uniqidSeed) {
// init seed with big random int
$locutus.php.uniqidSeed = Math.floor(Math.random() * 0x75bcd15)
}
$locutus.php.uniqidSeed++

// start with prefix, add current milliseconds hex string
retId = prefix
retId += _formatSeed(parseInt(new Date().getTime() / 1000, 10), 8)
// add seed hex string
retId += _formatSeed($locutus.php.uniqidSeed, 5)
if (moreEntropy) {
// for more entropy we add a float lower to 10
retId += (Math.random() * 10).toFixed(8).toString()
}

return retId
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/misc/uniqid'). You could also require the misc module in full so that you could access misc.uniqid instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • Uses an internal counter (in locutus global) to avoid collision

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1var $id = uniqid() var $result = $id.length === 13true
2var $id = uniqid('foo') var $result = $id.length === (13 + 'foo'.length)true
3var $id = uniqid('bar', true) var $result = $id.length === (23 + 'bar'.length)true

« More PHP misc functions