UNPKG

uuid-tool

Version:

Lightweight UUID for JavaScript. Parse and generate UUIDs. Convert between string and byte array.

1 lines 2.97 kB
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var n in r)("object"==typeof exports?exports:t)[n]=r[n]}}(this,(function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";r.r(e),r.d(e,"UuidTool",(function(){return i})),r.d(e,"Uuid",(function(){return o}));var n={uuid:/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i},o=function(){function t(t){this.id="","string"==typeof t?this.fromString(t):Array.isArray(t)?this.fromBytes(t):this.generate()}return t.fromJson=function(e){if("string"==typeof e&&(e=JSON.parse(e)),!e||!e.id)throw new TypeError("The input cannot be converted to Uuid.");return new t(e.id)},t.prototype.generate=function(){var t=new Array(16).fill(0).map((function(){return 256*Math.random()&255}));return t[6]=79&(64|t[6]),this.fromBytes(t),this},t.prototype.fromBytes=function(t){return this.id=t.map((function(t){return("00"+t.toString(16)).slice(-2)})).join("").replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/,"$1-$2-$3-$4-$5"),this.id=this.toString(),this},t.prototype.toBytes=function(){return(this.id.replace(/-/g,"").match(/.{2}/g)||[]).map((function(t){return parseInt(t,16)}))},t.prototype.fromString=function(t){return this.id=t.trim(),this.id=this.toString(),this},t.prototype.toString=function(){switch(t.stringExportFormat){default:case"lowercase":return this.id.toLowerCase();case"uppercase":return this.id.toUpperCase()}},t.prototype.isValid=function(){return"string"==typeof this.id&&36===this.id.length&&n.uuid.test(this.id)},t.prototype.equals=function(e){return"string"==typeof e&&(e=new t(e)),this.toString()===e.toString()},t.stringExportFormat="lowercase",t}(),i=function(){function t(){}return t.toBytes=function(t){return(new o).fromString(t).toBytes()},t.toString=function(t){return(new o).fromBytes(t).toString()},t.newUuid=function(){return(new o).toString()},t.isUuid=function(t){return(new o).fromString(t).isValid()},t.compare=function(t,e){return new o(t).equals(new o(e))},t}()}])}));