Generate a uuid collection using JS

Source: Internet
Author: User
/*! Math. UUID. JS (v1.4) http://www.broofa.commailto: robert@broofa.comCopyright (c) 2010 Robert kiefferdual licensed under the MIT and GPL licenses. * // ** generate a random UUID. ** usage: math. UUID (length, Radix) * length-the desired number of characters * Radix-the number of allowable values for each character. ** examples: * // no arguments-returns rfc4122, version 4 ID *> math. UUID () * "92 329d39-6f5c-4520-abfc-aab64544e172 "** // one argument-returns ID of the specified length *> math. UUID (15) // 15 character ID (default base = 62) * "vcydxgltxrvzstv" ** // two arguments-returns ID of the specified length, and Radix. (Radix must be <= 62) *> math. UUID (8, 2) // 8 character ID (base = 2) * "01001010" * >>> math. UUID (8, 10) // 8 character ID (base = 10) * "47473046" * >>> math. u UID (8, 16) // 8 character ID (base = 16) * "098f4d35" */(function () {// Private array of chars to use VaR chars = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy '. split (''); math. UUID = function (Len, Radix) {var chars = chars, UUID = [], I; Radix = Radix | chars. length; If (LEN) {// compact form for (I = 0; I <Len; I ++) UUID [I] = chars [0 | math. random () * Radix];} else {// rfc4 122, version 4 form var R; // rfc4122 requires these characters UUID [8] = UUID [13] = UUID [18] = UUID [23] = '-'; UUID [14] = '4'; // fill in random data. at I = 19 set the high bits of clock sequence as // per rfc4122, sec. 4.1.5 for (I = 0; I <36; I ++) {If (! UUID [I]) {r = 0 | math. Random () * 16; UUID [I] = chars [(I = 19 )? (R & 0x3) | 0x8: R] ;}} return UUID. join ('') ;}; // a more than T, but slightly bulkier, rfc4122v4 solution. we boost performance // by minimizing callto random () math. uuidfast = function () {var chars = chars, UUID = new array (36), RND = 0, R; For (VAR I = 0; I <36; I ++) {if (I = 8 | I = 13 | I = 18 | I = 23) {UUID [I] = '-';} else if (I = 14) {UUID [I] = '4';} else {If (RND <= 0x02) r ND = 0x2000000 + (math. random () * 0x1000000) | 0; r = RND & 0xf; RND = RND> 4; UUID [I] = chars [(I = 19 )? (R & 0x3) | 0x8: R] ;}} return UUID. join ('') ;}; // a more compact, but less than T, rfc4122v4 solution: math. uuidcompact = function () {return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx '. replace (/[XY]/g, function (c) {var r = math. random () * 16 | 0, V = c = 'X '? R: (R & 0x3 | 0x8); Return v. tostring (16 );});};})();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.