Install via yarn add locutus and import:
import { uniqid } from 'locutus/php/misc/uniqid'.
Or with CommonJS: const { uniqid } = require('locutus/php/misc/uniqid')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
var $id = uniqid()
var $result = $id.length === 13
true
2
var $id = uniqid('foo')
var $result = $id.length === (13 + 'foo'.length)
true
3
var $id = uniqid('bar', true)
var $result = $id.length === (23 + 'bar'.length)
true
Notes
Uses an internal counter (in locutus global) to avoid collision
Dependencies
This function uses the following Locutus functions:
exportfunctionuniqid(prefix?: string, moreEntropy?: boolean): string { // discuss at: https://locutus.io/php/uniqid/ // original by: Kevin van Zonneveld (https://kvz.io) // revised by: Kankrelune (https://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
const normalizedPrefix = prefix ?? ''
let retId = '' const formatSeed = (seed: number, reqWidth: number): string => { const hexSeed = Number.parseInt(String(seed), 10).toString(16) // to hex str if (reqWidth < hexSeed.length) { // so long we split return hexSeed.slice(hexSeed.length - reqWidth) } if (reqWidth > hexSeed.length) { // so short we pad returnnewArray(1 + (reqWidth - hexSeed.length)).join('0') + hexSeed } return hexSeed }
let uniqidSeed = getPhpRuntimeNumber('uniqidSeed', Math.floor(Math.random() * 0x75bcd15)) // init seed with big random int uniqidSeed++ setPhpRuntimeEntry('uniqidSeed', uniqidSeed)
// start with prefix, add current milliseconds hex string retId = normalizedPrefix retId += formatSeed(Number.parseInt(String(newDate().getTime() / 1000), 10), 8) // add seed hex string retId += formatSeed(uniqidSeed, 5) if (moreEntropy) { // for more entropy we add a float lower to 10 retId += (Math.random() * 10).toFixed(8).toString() }
exportfunctionuniqid(prefix, moreEntropy) { // discuss at: https://locutus.io/php/uniqid/ // original by: Kevin van Zonneveld (https://kvz.io) // revised by: Kankrelune (https://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
const normalizedPrefix = prefix ?? ''
let retId = '' constformatSeed = (seed, reqWidth) => { const hexSeed = Number.parseInt(String(seed), 10).toString(16) // to hex str if (reqWidth < hexSeed.length) { // so long we split return hexSeed.slice(hexSeed.length - reqWidth) } if (reqWidth > hexSeed.length) { // so short we pad returnnewArray(1 + (reqWidth - hexSeed.length)).join('0') + hexSeed } return hexSeed }
let uniqidSeed = getPhpRuntimeNumber('uniqidSeed', Math.floor(Math.random() * 0x75bcd15)) // init seed with big random int uniqidSeed++ setPhpRuntimeEntry('uniqidSeed', uniqidSeed)
// start with prefix, add current milliseconds hex string retId = normalizedPrefix retId += formatSeed(Number.parseInt(String(newDate().getTime() / 1000), 10), 8) // add seed hex string retId += formatSeed(uniqidSeed, 5) if (moreEntropy) { // for more entropy we add a float lower to 10 retId += (Math.random() * 10).toFixed(8).toString() }
typePhpRuntimeNumberKey = { [K in keyof PhpRuntimeKnownEntryMap]: PhpRuntimeKnownEntryMap[K] extendsnumber ? K : never }[keyof PhpRuntimeKnownEntryMap]
functiongetPhpRuntimeNumber(key: PhpRuntimeNumberKey, fallback: number): number
functiongetPhpRuntimeNumber(key: string, fallback: number): number
functiongetPhpRuntimeNumber(key: string, fallback: number): number { const value = getPhpRuntimeEntry(key) returntypeof value === 'number' ? value : fallback }
// php/misc/uniqid (target function module) functionuniqid(prefix?: string, moreEntropy?: boolean): string { // discuss at: https://locutus.io/php/uniqid/ // original by: Kevin van Zonneveld (https://kvz.io) // revised by: Kankrelune (https://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
const normalizedPrefix = prefix ?? ''
let retId = '' const formatSeed = (seed: number, reqWidth: number): string => { const hexSeed = Number.parseInt(String(seed), 10).toString(16) // to hex str if (reqWidth < hexSeed.length) { // so long we split return hexSeed.slice(hexSeed.length - reqWidth) } if (reqWidth > hexSeed.length) { // so short we pad returnnewArray(1 + (reqWidth - hexSeed.length)).join('0') + hexSeed } return hexSeed }
let uniqidSeed = getPhpRuntimeNumber('uniqidSeed', Math.floor(Math.random() * 0x75bcd15)) // init seed with big random int uniqidSeed++ setPhpRuntimeEntry('uniqidSeed', uniqidSeed)
// start with prefix, add current milliseconds hex string retId = normalizedPrefix retId += formatSeed(Number.parseInt(String(newDate().getTime() / 1000), 10), 8) // add seed hex string retId += formatSeed(uniqidSeed, 5) if (moreEntropy) { // for more entropy we add a float lower to 10 retId += (Math.random() * 10).toFixed(8).toString() }
functiongetPhpRuntimeNumber(key, fallback) { const value = getPhpRuntimeEntry(key) returntypeof value === 'number' ? value : fallback }
// php/misc/uniqid (target function module) functionuniqid(prefix, moreEntropy) { // discuss at: https://locutus.io/php/uniqid/ // original by: Kevin van Zonneveld (https://kvz.io) // revised by: Kankrelune (https://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
const normalizedPrefix = prefix ?? ''
let retId = '' constformatSeed = (seed, reqWidth) => { const hexSeed = Number.parseInt(String(seed), 10).toString(16) // to hex str if (reqWidth < hexSeed.length) { // so long we split return hexSeed.slice(hexSeed.length - reqWidth) } if (reqWidth > hexSeed.length) { // so short we pad returnnewArray(1 + (reqWidth - hexSeed.length)).join('0') + hexSeed } return hexSeed }
let uniqidSeed = getPhpRuntimeNumber('uniqidSeed', Math.floor(Math.random() * 0x75bcd15)) // init seed with big random int uniqidSeed++ setPhpRuntimeEntry('uniqidSeed', uniqidSeed)
// start with prefix, add current milliseconds hex string retId = normalizedPrefix retId += formatSeed(Number.parseInt(String(newDate().getTime() / 1000), 10), 8) // add seed hex string retId += formatSeed(uniqidSeed, 5) if (moreEntropy) { // for more entropy we add a float lower to 10 retId += (Math.random() * 10).toFixed(8).toString() }
return retId }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.