UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

114 lines (96 loc) 3.26 kB
--- layout: page title: "JavaScript uniqid function" comments: true sharing: true footer: true alias: - /functions/view/uniqid:750 - /functions/view/uniqid - /functions/view/750 - /functions/uniqid:750 - /functions/750 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's uniqid {% codeblock misc/uniqid.js lang:js https://raw.github.com/kvz/phpjs/master/functions/misc/uniqid.js raw on github %} function uniqid (prefix, more_entropy) { // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + revised by: Kankrelune (http://www.webfaktory.info/) // % note 1: Uses an internal counter (in php_js global) to avoid collision // * example 1: uniqid(); // * returns 1: 'a30285b160c14' // * example 2: uniqid('foo'); // * returns 2: 'fooa30285b1cd361' // * example 3: uniqid('bar', true); // * returns 3: 'bara20285b23dfd1.31879087' 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; }; // BEGIN REDUNDANT if (!this.php_js) { this.php_js = {}; } // END REDUNDANT if (!this.php_js.uniqidSeed) { // init seed with big random int this.php_js.uniqidSeed = Math.floor(Math.random() * 0x75bcd15); } this.php_js.uniqidSeed++; retId = prefix; // start with prefix, add current milliseconds hex string retId += formatSeed(parseInt(new Date().getTime() / 1000, 10), 8); retId += formatSeed(this.php_js.uniqidSeed, 5); // add seed hex string if (more_entropy) { // for more entropy we add a float lower to 10 retId += (Math.random() * 10).toFixed(8).toString(); } return retId; } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/misc/uniqid.js) Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are the closest match to this hashtable-like data structure. Please also note that php.js offers community built functions and goes by the [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just: - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/misc/uniqid.js) ### Example 1 This code {% codeblock lang:js example %} uniqid(); {% endcodeblock %} Should return {% codeblock lang:js returns %} 'a30285b160c14' {% endcodeblock %} ### Example 2 This code {% codeblock lang:js example %} uniqid('foo'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 'fooa30285b1cd361' {% endcodeblock %} ### Example 3 This code {% codeblock lang:js example %} uniqid('bar', true); {% endcodeblock %} Should return {% codeblock lang:js returns %} 'bara20285b23dfd1.31879087' {% endcodeblock %} ### Other PHP functions in the misc extension {% render_partial _includes/custom/misc.html %}