UNPKG

peter-generator

Version:

A simple thumbnail generator for JS.

2 lines (1 loc) 2.61 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e["Peter-generator"]={})}(this,function(e){"use strict";var t=function(){return(t=Object.assign||function(e){for(var t,r=1,i=arguments.length;i>r;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};const r=require("./constructor");require("./input")(r),require("./resize")(r),require("./composite")(r),require("./operation")(r),require("./colour")(r),require("./channel")(r),require("./output")(r),require("./utility")(r),module.exports=r;var i=Object.freeze({}),o={maxAttempts:5,maxWidth:70,sizeLimit:"100kB"},n=function(){function e(e){void 0===e&&(e={});try{this.checkPropsType(e)}catch(e){console.error(e)}var r=t({},o,e);this.maxAttempts=r.maxAttempts,this.maxWidth=r.maxWidth,this.sizeLimit=this.harmonizeByteSize(r.sizeLimit)}return e.prototype.checkPropsType=function(e){if(e.maxWidth&&"number"!=typeof e.maxWidth)throw Error("The maxWidth option should be of type number");if(e.sizeLimit&&"string"!=typeof e.sizeLimit&&"number"!=typeof e.sizeLimit)throw Error("The sizeLimit option should be of type string or number");if(e.maxAttempts&&"number"!=typeof e.maxAttempts)throw Error("The maxAttempts option should be of type number")},e.prototype.harmonizeByteSize=function(e){if("number"==typeof e)return Math.floor(e);var t=e.trim().match(/(-?\d*(?:\.\d+)?)\s*(\D*)/);if(!t[1])throw Error("The numerical value of limit is missing");if(t[2]&&!t[2].match(/^(kB|MB|GB)$/i))throw Error("The unit value of limit is not valid (expected: kB, MB, GB)");var r=parseInt(t[1],10);return Math.floor("gb"===t[2].toLowerCase()?1073741824*r:"mb"===t[2].toLowerCase()?1048576*r:"kb"===t[2].toLowerCase()?1024*r:r)},e.prototype.resize=function(e,r){var o=this;e.metadata().then(function(n){var a=n.height,s=n.width;if(o.sizeLimit<n.size){if(!r.attemptN||o.maxAttempts>=r.attemptN){var u=r.width||o.maxWidth;return e.resize(Math.floor(u),Math.floor(u/(r.ratio||s/a))).toBuffer().then(function(e){o.resize(i(e),t({},r,{attemptN:r.attemptN?r.attemptN+1:1,width:u/1.5}))})}r.onFail("Number of attempts exceeded, failed to generate thumbnail")}else e.toBuffer().then(r.onSuccess).catch(r.onFail)}).catch(r.onFail)},e.prototype.generate=function(e){var t=this;if(!Buffer.isBuffer(e))throw Error("The generate method expects an input of type Buffer");var r=i(e);return new Promise(function(e,i){t.resize(r,{onSuccess:e,onFail:i})})},e}();e.ThumbnailGenerator=n,Object.defineProperty(e,"__esModule",{value:!0})});