annotate src/implab/Uuid.js @ 5:3d124d0b9078

improved declare/override, added override.before, override.after, override.hide, inherited.arguments.
author cin
date Fri, 16 Jun 2017 02:14:25 +0300
parents 00779cb63b12
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 // uuid.js
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2 //
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 // Copyright (c) 2010-2012 Robert Kieffer
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 // MIT License - http://opensource.org/licenses/mit-license.php
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
5 define([], function () {
00779cb63b12 formatting
cin
parents: 1
diff changeset
6 'use strict';
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
8 var _window = 'undefined' !== typeof window ? window : null;
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
10 // Unique ID creation requires a high quality random # generator. We
00779cb63b12 formatting
cin
parents: 1
diff changeset
11 // feature
00779cb63b12 formatting
cin
parents: 1
diff changeset
12 // detect to determine the best RNG source, normalizing to a function
00779cb63b12 formatting
cin
parents: 1
diff changeset
13 // that
00779cb63b12 formatting
cin
parents: 1
diff changeset
14 // returns 128-bits of randomness, since that's what's usually required
00779cb63b12 formatting
cin
parents: 1
diff changeset
15 var _rng, _mathRNG, _nodeRNG, _whatwgRNG, _previousRoot;
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
17 function setupBrowser() {
00779cb63b12 formatting
cin
parents: 1
diff changeset
18 // Allow for MSIE11 msCrypto
00779cb63b12 formatting
cin
parents: 1
diff changeset
19 var _crypto = _window.crypto || _window.msCrypto;
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
21 if (!_rng && _crypto && _crypto.getRandomValues) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
22 // WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
00779cb63b12 formatting
cin
parents: 1
diff changeset
23 //
00779cb63b12 formatting
cin
parents: 1
diff changeset
24 // Moderately fast, high quality
00779cb63b12 formatting
cin
parents: 1
diff changeset
25 try {
00779cb63b12 formatting
cin
parents: 1
diff changeset
26 var _rnds8 = new Uint8Array(16);
00779cb63b12 formatting
cin
parents: 1
diff changeset
27 _whatwgRNG = _rng = function whatwgRNG() {
00779cb63b12 formatting
cin
parents: 1
diff changeset
28 _crypto.getRandomValues(_rnds8);
00779cb63b12 formatting
cin
parents: 1
diff changeset
29 return _rnds8;
00779cb63b12 formatting
cin
parents: 1
diff changeset
30 };
00779cb63b12 formatting
cin
parents: 1
diff changeset
31 _rng();
00779cb63b12 formatting
cin
parents: 1
diff changeset
32 } catch (e) { /**/ }
00779cb63b12 formatting
cin
parents: 1
diff changeset
33 }
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
35 if (!_rng) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
36 // Math.random()-based (RNG)
00779cb63b12 formatting
cin
parents: 1
diff changeset
37 //
00779cb63b12 formatting
cin
parents: 1
diff changeset
38 // If all else fails, use Math.random(). It's fast, but is of
00779cb63b12 formatting
cin
parents: 1
diff changeset
39 // unspecified
00779cb63b12 formatting
cin
parents: 1
diff changeset
40 // quality.
00779cb63b12 formatting
cin
parents: 1
diff changeset
41 var _rnds = new Array(16);
00779cb63b12 formatting
cin
parents: 1
diff changeset
42 _mathRNG = _rng = function () {
00779cb63b12 formatting
cin
parents: 1
diff changeset
43 for (var i = 0, r; i < 16; i++) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
44 if ((i & 0x03) === 0) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
45 r = Math.random() * 0x100000000;
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 }
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
47 _rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
48 }
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
50 return _rnds;
00779cb63b12 formatting
cin
parents: 1
diff changeset
51 };
00779cb63b12 formatting
cin
parents: 1
diff changeset
52 if ('undefined' !== typeof console && console.warn) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
53 console
00779cb63b12 formatting
cin
parents: 1
diff changeset
54 .warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()");
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 }
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
57 }
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
59 function setupNode() {
00779cb63b12 formatting
cin
parents: 1
diff changeset
60 // Node.js crypto-based RNG -
00779cb63b12 formatting
cin
parents: 1
diff changeset
61 // http://nodejs.org/docs/v0.6.2/api/crypto.html
00779cb63b12 formatting
cin
parents: 1
diff changeset
62 //
00779cb63b12 formatting
cin
parents: 1
diff changeset
63 // Moderately fast, high quality
00779cb63b12 formatting
cin
parents: 1
diff changeset
64 if ('function' === typeof require) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
65 try {
00779cb63b12 formatting
cin
parents: 1
diff changeset
66 var _rb = require('crypto').randomBytes;
00779cb63b12 formatting
cin
parents: 1
diff changeset
67 _nodeRNG = _rng = _rb && function () {
00779cb63b12 formatting
cin
parents: 1
diff changeset
68 return _rb(16);
00779cb63b12 formatting
cin
parents: 1
diff changeset
69 };
00779cb63b12 formatting
cin
parents: 1
diff changeset
70 _rng();
00779cb63b12 formatting
cin
parents: 1
diff changeset
71 } catch (e) { /**/ }
00779cb63b12 formatting
cin
parents: 1
diff changeset
72 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
73 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
74
00779cb63b12 formatting
cin
parents: 1
diff changeset
75 if (_window) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
76 setupBrowser();
00779cb63b12 formatting
cin
parents: 1
diff changeset
77 } else {
00779cb63b12 formatting
cin
parents: 1
diff changeset
78 setupNode();
00779cb63b12 formatting
cin
parents: 1
diff changeset
79 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
80
00779cb63b12 formatting
cin
parents: 1
diff changeset
81 // Buffer class to use
00779cb63b12 formatting
cin
parents: 1
diff changeset
82 var BufferClass = ('function' === typeof Buffer) ? Buffer : Array;
00779cb63b12 formatting
cin
parents: 1
diff changeset
83
00779cb63b12 formatting
cin
parents: 1
diff changeset
84 // Maps for number <-> hex string conversion
00779cb63b12 formatting
cin
parents: 1
diff changeset
85 var _byteToHex = [];
00779cb63b12 formatting
cin
parents: 1
diff changeset
86 var _hexToByte = {};
00779cb63b12 formatting
cin
parents: 1
diff changeset
87 for (var i = 0; i < 256; i++) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
88 _byteToHex[i] = (i + 0x100).toString(16).substr(1);
00779cb63b12 formatting
cin
parents: 1
diff changeset
89 _hexToByte[_byteToHex[i]] = i;
00779cb63b12 formatting
cin
parents: 1
diff changeset
90 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
91
00779cb63b12 formatting
cin
parents: 1
diff changeset
92 // **`parse()` - Parse a UUID into it's component bytes**
00779cb63b12 formatting
cin
parents: 1
diff changeset
93 function parse(s, buf, offset) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
94 var i = (buf && offset) || 0,
00779cb63b12 formatting
cin
parents: 1
diff changeset
95 ii = 0;
00779cb63b12 formatting
cin
parents: 1
diff changeset
96
00779cb63b12 formatting
cin
parents: 1
diff changeset
97 buf = buf || [];
00779cb63b12 formatting
cin
parents: 1
diff changeset
98 s.toLowerCase().replace(/[0-9a-f]{2}/g, function (oct) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
99 if (ii < 16) { // Don't overflow!
00779cb63b12 formatting
cin
parents: 1
diff changeset
100 buf[i + ii++] = _hexToByte[oct];
00779cb63b12 formatting
cin
parents: 1
diff changeset
101 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
102 });
00779cb63b12 formatting
cin
parents: 1
diff changeset
103
00779cb63b12 formatting
cin
parents: 1
diff changeset
104 // Zero out remaining bytes if string was short
00779cb63b12 formatting
cin
parents: 1
diff changeset
105 while (ii < 16) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
106 buf[i + ii++] = 0;
00779cb63b12 formatting
cin
parents: 1
diff changeset
107 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
108
00779cb63b12 formatting
cin
parents: 1
diff changeset
109 return buf;
00779cb63b12 formatting
cin
parents: 1
diff changeset
110 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
111
00779cb63b12 formatting
cin
parents: 1
diff changeset
112 // **`unparse()` - Convert UUID byte array (ala parse()) into a string**
00779cb63b12 formatting
cin
parents: 1
diff changeset
113 function unparse(buf, offset) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
114 var i = offset || 0,
00779cb63b12 formatting
cin
parents: 1
diff changeset
115 bth = _byteToHex;
00779cb63b12 formatting
cin
parents: 1
diff changeset
116 return bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] +
00779cb63b12 formatting
cin
parents: 1
diff changeset
117 bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] + '-' +
00779cb63b12 formatting
cin
parents: 1
diff changeset
118 bth[buf[i++]] + bth[buf[i++]] + '-' + bth[buf[i++]] +
00779cb63b12 formatting
cin
parents: 1
diff changeset
119 bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] +
00779cb63b12 formatting
cin
parents: 1
diff changeset
120 bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]];
00779cb63b12 formatting
cin
parents: 1
diff changeset
121 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
122
00779cb63b12 formatting
cin
parents: 1
diff changeset
123 // **`v1()` - Generate time-based UUID**
00779cb63b12 formatting
cin
parents: 1
diff changeset
124 //
00779cb63b12 formatting
cin
parents: 1
diff changeset
125 // Inspired by https://github.com/LiosK/UUID.js
00779cb63b12 formatting
cin
parents: 1
diff changeset
126 // and http://docs.python.org/library/uuid.html
00779cb63b12 formatting
cin
parents: 1
diff changeset
127
00779cb63b12 formatting
cin
parents: 1
diff changeset
128 // random #'s we need to init node and clockseq
00779cb63b12 formatting
cin
parents: 1
diff changeset
129 var _seedBytes = _rng();
00779cb63b12 formatting
cin
parents: 1
diff changeset
130
00779cb63b12 formatting
cin
parents: 1
diff changeset
131 // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit =
00779cb63b12 formatting
cin
parents: 1
diff changeset
132 // 1)
00779cb63b12 formatting
cin
parents: 1
diff changeset
133 var _nodeId = [
00779cb63b12 formatting
cin
parents: 1
diff changeset
134 _seedBytes[0] | 0x01,
00779cb63b12 formatting
cin
parents: 1
diff changeset
135 _seedBytes[1],
00779cb63b12 formatting
cin
parents: 1
diff changeset
136 _seedBytes[2],
00779cb63b12 formatting
cin
parents: 1
diff changeset
137 _seedBytes[3],
00779cb63b12 formatting
cin
parents: 1
diff changeset
138 _seedBytes[4],
00779cb63b12 formatting
cin
parents: 1
diff changeset
139 _seedBytes[5]
00779cb63b12 formatting
cin
parents: 1
diff changeset
140 ];
00779cb63b12 formatting
cin
parents: 1
diff changeset
141
00779cb63b12 formatting
cin
parents: 1
diff changeset
142 // Per 4.2.2, randomize (14 bit) clockseq
00779cb63b12 formatting
cin
parents: 1
diff changeset
143 var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
144
00779cb63b12 formatting
cin
parents: 1
diff changeset
145 // Previous uuid creation time
00779cb63b12 formatting
cin
parents: 1
diff changeset
146 var _lastMSecs = 0,
00779cb63b12 formatting
cin
parents: 1
diff changeset
147 _lastNSecs = 0;
00779cb63b12 formatting
cin
parents: 1
diff changeset
148
00779cb63b12 formatting
cin
parents: 1
diff changeset
149 // See https://github.com/broofa/node-uuid for API details
00779cb63b12 formatting
cin
parents: 1
diff changeset
150 function v1(options, buf, offset) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
151 var i = buf && offset || 0;
00779cb63b12 formatting
cin
parents: 1
diff changeset
152 var b = buf || [];
00779cb63b12 formatting
cin
parents: 1
diff changeset
153
00779cb63b12 formatting
cin
parents: 1
diff changeset
154 options = options || {};
00779cb63b12 formatting
cin
parents: 1
diff changeset
155
00779cb63b12 formatting
cin
parents: 1
diff changeset
156 var clockseq = (options.clockseq != null) ? options.clockseq : _clockseq;
00779cb63b12 formatting
cin
parents: 1
diff changeset
157
00779cb63b12 formatting
cin
parents: 1
diff changeset
158 // UUID timestamps are 100 nano-second units since the Gregorian
00779cb63b12 formatting
cin
parents: 1
diff changeset
159 // epoch,
00779cb63b12 formatting
cin
parents: 1
diff changeset
160 // (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
00779cb63b12 formatting
cin
parents: 1
diff changeset
161 // time is handled internally as 'msecs' (integer milliseconds) and
00779cb63b12 formatting
cin
parents: 1
diff changeset
162 // 'nsecs'
00779cb63b12 formatting
cin
parents: 1
diff changeset
163 // (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01
00779cb63b12 formatting
cin
parents: 1
diff changeset
164 // 00:00.
00779cb63b12 formatting
cin
parents: 1
diff changeset
165 var msecs = (options.msecs != null) ? options.msecs : new Date()
00779cb63b12 formatting
cin
parents: 1
diff changeset
166 .getTime();
00779cb63b12 formatting
cin
parents: 1
diff changeset
167
00779cb63b12 formatting
cin
parents: 1
diff changeset
168 // Per 4.2.1.2, use count of uuid's generated during the current
00779cb63b12 formatting
cin
parents: 1
diff changeset
169 // clock
00779cb63b12 formatting
cin
parents: 1
diff changeset
170 // cycle to simulate higher resolution clock
00779cb63b12 formatting
cin
parents: 1
diff changeset
171 var nsecs = (options.nsecs != null) ? options.nsecs : _lastNSecs + 1;
00779cb63b12 formatting
cin
parents: 1
diff changeset
172
00779cb63b12 formatting
cin
parents: 1
diff changeset
173 // Time since last uuid creation (in msecs)
00779cb63b12 formatting
cin
parents: 1
diff changeset
174 var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs) / 10000;
00779cb63b12 formatting
cin
parents: 1
diff changeset
175
00779cb63b12 formatting
cin
parents: 1
diff changeset
176 // Per 4.2.1.2, Bump clockseq on clock regression
00779cb63b12 formatting
cin
parents: 1
diff changeset
177 if (dt < 0 && options.clockseq == null) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
178 clockseq = clockseq + 1 & 0x3fff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
179 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
180
00779cb63b12 formatting
cin
parents: 1
diff changeset
181 // Reset nsecs if clock regresses (new clockseq) or we've moved onto
00779cb63b12 formatting
cin
parents: 1
diff changeset
182 // a new
00779cb63b12 formatting
cin
parents: 1
diff changeset
183 // time interval
00779cb63b12 formatting
cin
parents: 1
diff changeset
184 if ((dt < 0 || msecs > _lastMSecs) && options.nsecs == null) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
185 nsecs = 0;
00779cb63b12 formatting
cin
parents: 1
diff changeset
186 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
187
00779cb63b12 formatting
cin
parents: 1
diff changeset
188 // Per 4.2.1.2 Throw error if too many uuids are requested
00779cb63b12 formatting
cin
parents: 1
diff changeset
189 if (nsecs >= 10000) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
190 throw new Error(
00779cb63b12 formatting
cin
parents: 1
diff changeset
191 'uuid.v1(): Can\'t create more than 10M uuids/sec');
00779cb63b12 formatting
cin
parents: 1
diff changeset
192 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
193
00779cb63b12 formatting
cin
parents: 1
diff changeset
194 _lastMSecs = msecs;
00779cb63b12 formatting
cin
parents: 1
diff changeset
195 _lastNSecs = nsecs;
00779cb63b12 formatting
cin
parents: 1
diff changeset
196 _clockseq = clockseq;
00779cb63b12 formatting
cin
parents: 1
diff changeset
197
00779cb63b12 formatting
cin
parents: 1
diff changeset
198 // Per 4.1.4 - Convert from unix epoch to Gregorian epoch
00779cb63b12 formatting
cin
parents: 1
diff changeset
199 msecs += 12219292800000;
00779cb63b12 formatting
cin
parents: 1
diff changeset
200
00779cb63b12 formatting
cin
parents: 1
diff changeset
201 // `time_low`
00779cb63b12 formatting
cin
parents: 1
diff changeset
202 var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
00779cb63b12 formatting
cin
parents: 1
diff changeset
203 b[i++] = tl >>> 24 & 0xff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
204 b[i++] = tl >>> 16 & 0xff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
205 b[i++] = tl >>> 8 & 0xff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
206 b[i++] = tl & 0xff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
207
00779cb63b12 formatting
cin
parents: 1
diff changeset
208 // `time_mid`
00779cb63b12 formatting
cin
parents: 1
diff changeset
209 var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
210 b[i++] = tmh >>> 8 & 0xff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
211 b[i++] = tmh & 0xff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
212
00779cb63b12 formatting
cin
parents: 1
diff changeset
213 // `time_high_and_version`
00779cb63b12 formatting
cin
parents: 1
diff changeset
214 b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
00779cb63b12 formatting
cin
parents: 1
diff changeset
215 b[i++] = tmh >>> 16 & 0xff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
216
00779cb63b12 formatting
cin
parents: 1
diff changeset
217 // `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
00779cb63b12 formatting
cin
parents: 1
diff changeset
218 b[i++] = clockseq >>> 8 | 0x80;
00779cb63b12 formatting
cin
parents: 1
diff changeset
219
00779cb63b12 formatting
cin
parents: 1
diff changeset
220 // `clock_seq_low`
00779cb63b12 formatting
cin
parents: 1
diff changeset
221 b[i++] = clockseq & 0xff;
00779cb63b12 formatting
cin
parents: 1
diff changeset
222
00779cb63b12 formatting
cin
parents: 1
diff changeset
223 // `node`
00779cb63b12 formatting
cin
parents: 1
diff changeset
224 var node = options.node || _nodeId;
00779cb63b12 formatting
cin
parents: 1
diff changeset
225 for (var n = 0; n < 6; n++) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
226 b[i + n] = node[n];
00779cb63b12 formatting
cin
parents: 1
diff changeset
227 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
228
00779cb63b12 formatting
cin
parents: 1
diff changeset
229 return buf ? buf : unparse(b);
00779cb63b12 formatting
cin
parents: 1
diff changeset
230 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
231
00779cb63b12 formatting
cin
parents: 1
diff changeset
232 // **`v4()` - Generate random UUID**
00779cb63b12 formatting
cin
parents: 1
diff changeset
233
00779cb63b12 formatting
cin
parents: 1
diff changeset
234 // See https://github.com/broofa/node-uuid for API details
00779cb63b12 formatting
cin
parents: 1
diff changeset
235 function v4(options, buf, offset) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
236 // Deprecated - 'format' argument, as supported in v1.2
00779cb63b12 formatting
cin
parents: 1
diff changeset
237 var i = buf && offset || 0;
00779cb63b12 formatting
cin
parents: 1
diff changeset
238
00779cb63b12 formatting
cin
parents: 1
diff changeset
239 if (typeof (options) === 'string') {
00779cb63b12 formatting
cin
parents: 1
diff changeset
240 buf = (options === 'binary') ? new BufferClass(16) : null;
00779cb63b12 formatting
cin
parents: 1
diff changeset
241 options = null;
00779cb63b12 formatting
cin
parents: 1
diff changeset
242 }
00779cb63b12 formatting
cin
parents: 1
diff changeset
243 options = options || {};
00779cb63b12 formatting
cin
parents: 1
diff changeset
244
00779cb63b12 formatting
cin
parents: 1
diff changeset
245 var rnds = options.random || (options.rng || _rng)();
00779cb63b12 formatting
cin
parents: 1
diff changeset
246
00779cb63b12 formatting
cin
parents: 1
diff changeset
247 // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
00779cb63b12 formatting
cin
parents: 1
diff changeset
248 rnds[6] = (rnds[6] & 0x0f) | 0x40;
00779cb63b12 formatting
cin
parents: 1
diff changeset
249 rnds[8] = (rnds[8] & 0x3f) | 0x80;
00779cb63b12 formatting
cin
parents: 1
diff changeset
250
00779cb63b12 formatting
cin
parents: 1
diff changeset
251 // Copy bytes to buffer, if provided
00779cb63b12 formatting
cin
parents: 1
diff changeset
252 if (buf) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
253 for (var ii = 0; ii < 16; ii++) {
00779cb63b12 formatting
cin
parents: 1
diff changeset
254 buf[i + ii] = rnds[ii];
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
255 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
256 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
257
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
258 return buf || unparse(rnds);
00779cb63b12 formatting
cin
parents: 1
diff changeset
259 }
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
260
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
261 // Export public API
00779cb63b12 formatting
cin
parents: 1
diff changeset
262 var uuid = function () {
00779cb63b12 formatting
cin
parents: 1
diff changeset
263 return new String(v4());
00779cb63b12 formatting
cin
parents: 1
diff changeset
264 };
00779cb63b12 formatting
cin
parents: 1
diff changeset
265 uuid.v1 = v1;
00779cb63b12 formatting
cin
parents: 1
diff changeset
266 uuid.v4 = v4;
00779cb63b12 formatting
cin
parents: 1
diff changeset
267 uuid.create = v4;
00779cb63b12 formatting
cin
parents: 1
diff changeset
268 uuid.empty = "00000000-0000-0000-0000-000000000000";
00779cb63b12 formatting
cin
parents: 1
diff changeset
269 uuid.parse = parse;
00779cb63b12 formatting
cin
parents: 1
diff changeset
270 uuid.unparse = unparse;
00779cb63b12 formatting
cin
parents: 1
diff changeset
271 uuid.BufferClass = BufferClass;
00779cb63b12 formatting
cin
parents: 1
diff changeset
272 uuid._rng = _rng;
00779cb63b12 formatting
cin
parents: 1
diff changeset
273 uuid._mathRNG = _mathRNG;
00779cb63b12 formatting
cin
parents: 1
diff changeset
274 uuid._nodeRNG = _nodeRNG;
00779cb63b12 formatting
cin
parents: 1
diff changeset
275 uuid._whatwgRNG = _whatwgRNG;
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
276
3
00779cb63b12 formatting
cin
parents: 1
diff changeset
277 return uuid;
00779cb63b12 formatting
cin
parents: 1
diff changeset
278 });