UNPKG

data-uri-doer

Version:
12 lines (11 loc) 2.15 kB
/*! * data-uri-doer * Encode and decode data uris. * * @version v1.0.0 * @link https://github.com/orianda/data-uri-doer * @author Orianda <orianda@paan.de> * @license MIT */ !function(t){"use strict";var e=function(){"use strict";function t(t){return void 0===t||null===t}function e(t){var n;if(t instanceof Array){n=[];for(var r=0,o=t.length;r<o;r++)n[r]=e(t[r])}else if(t instanceof Object){n={};for(var a in t)t.hasOwnProperty(a)&&(n[a]=e(t[a]))}else n=t;return n}function n(e){var n=[];for(var r in e)if(e.hasOwnProperty(r)){var o=encodeURIComponent(r),a=t(e[r])?"":encodeURIComponent(e[r]);n.push(";"+o+"="+a)}return n.join("")}function r(t){for(var e={},n=t.split(";"),r=0,o=n.length;r<o;r++)if(n[r]){var a=n[r].split("="),i=decodeURIComponent(a.shift());e[i]=decodeURIComponent(a.join("="))}return e}function o(e,r,o,a){return e=t(e)?"":a?btoa(e):encodeURIComponent(e),r=t(r)?"":r.toLowerCase()===c?"":r,o&&o.charset===f&&delete o.charset,"data:"+r+n(o)+(a?";base64":"")+","+e}function a(e,n,o){var a={},i=t(e)?null:e.trim().match(/^data:([^;,]*)((?:;[^;,]*)*?)(;base64)?,(.*)$/);if(!i)throw new Error("Invalid data uri");return a.type=i[1]||n||"",a.attr=r(i[2]),a.attr.charset=a.attr.charset||o,a.data=i[3]?atob(i[4]):decodeURIComponent(i[4]),a}function i(t,e,n){if(!(this instanceof i))return new i(t,e,n);this.data=t||"",this.type=e||c,this.attr=n||{},this.attr.charset=this.attr.charset||f}var c="text/plain",f="US-ASCII";return i.prototype.toString=function(t){return o(this.data,this.type,this.attr,t)},i.parse=function(t,e,n){var r=a(t,e,n);return new i(r.data,r.type,r.attr)},i.clone=function(n){return t(n)?null:new i(n.data,n.type,e(n.attr))},"undefined"==typeof atob&&(global.atob=function(t){return new Buffer(t,"base64").toString()}),"undefined"==typeof btoa&&(global.btoa=function(t){return new Buffer(t).toString("base64")}),i}();if("object"==typeof module&&module instanceof Object&&module.exports instanceof Object)module.exports=e;else{if(!("object"==typeof window&&window instanceof Object))throw new Error("No valid context available.");window[e.name]=e}}(); //# sourceMappingURL=data-uri-doer.min.js.map