├── README.md ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # crypto.randomUUID() 2 | 3 | A crypto.randomUUID polyfill from [Stack Overflow](https://stackoverflow.com/a/8809472/2800218). 4 | 5 | **[LICENSE](https://creativecommons.org/licenses/by-sa/4.0/legalcode)** 6 | 7 | Compatible with both IE11 and NodeJS. 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | if (typeof crypto === 'undefined') 2 | var crypto = require('crypto'); 3 | 4 | if (!('randomUUID' in crypto)) 5 | // https://stackoverflow.com/a/2117523/2800218 6 | // LICENSE: https://creativecommons.org/licenses/by-sa/4.0/legalcode 7 | crypto.randomUUID = function randomUUID() { 8 | return ( 9 | [1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, 10 | c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) 11 | ); 12 | }; 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@ungap/random-uuid", 3 | "version": "0.1.0", 4 | "description": "A crypto.randomUUID polyfill", 5 | "main": "index.js", 6 | "type": "commonjs", 7 | "keywords": [ 8 | "crypto", 9 | "randomUUID", 10 | "polyfill" 11 | ], 12 | "author": "Stack Overflow", 13 | "license": "CC BY-SA 4.0", 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/ungap/random-uuid.git" 17 | }, 18 | "bugs": { 19 | "url": "https://github.com/ungap/random-uuid/issues" 20 | }, 21 | "homepage": "https://github.com/ungap/random-uuid#readme" 22 | } 23 | --------------------------------------------------------------------------------