UNPKG

@buession/prototype

Version:

A native object extension framework for Javascript.

12 lines (11 loc) 16.7 kB
/*! * Buession prototype v4.1.8 * * @link https://prototype.buession.com/ * @source https://github.com/buession/buession-prototype * @copyright @ 2020-2025 Buession.com Inc. * @license MIT * @Build Time Wed, 16 Apr 2025 15:22:35 GMT */ !function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";window.Prototype={version:"v4.1.8",emptyFunction:function(){},K:function(e){return e}};const e={these:function(){let e;for(let t=0;t<arguments.length;t++){const n=arguments[t];if(Object.isFunction(n))try{e=n();break}catch(e){console.error(e)}}return e}};window.Try=e;class t{constructor(e){this.value=e}static of(e){return new t(e)}static ofNullable(e){return Object.isUndefinedOrNull(e)?t.empty():new t(e)}static empty(){return new t(null)}get(){if(null===this.value||void 0===this.value)throw"No value present";return this.value}orElse(e){return Object.isUndefinedOrNull(this.value)?e:this.value}isPresent(){return!1===Object.isUndefinedOrNull(this.value)}}var n;window.Optional=t,Object.type=function(e){return typeof e},Object.rawType=function(e){return Object.prototype.toString.call(e).slice(8,-1)},Object.isObject=function(e){return null!==e&&"object"==typeof e},Object.isPlainObject=function(e){return"[object Object]"===Object.prototype.toString.call(e)},Object.isMap=function(e){return"[object Map]"===Object.prototype.toString.call(e)},Object.isSet=function(e){return"[object Set]"===Object.prototype.toString.call(e)},Object.isFunction=function(e){return"function"===Object.type(e)},Object.isSymbol=function(e){if("symbol"==typeof e)return!0;try{const t=Symbol.prototype.toString;return"symbol"==typeof e.valueOf()&&/^Symbol\(.*\)$/.test(t.call(e))}catch(e){return!1}},Object.isPromise=function(e){return!1===Object.isUndefinedOrNull(e)&&!0===Object.isFunction(e.then)&&!0===Object.isFunction(e.catch)},Object.isPrimitive=function(e){return!0===Object.isBoolean(e)||!0===Object.isString(e)||!0===Object.isNumber(e)},Object.isArray=function(e){return!0===Array.isArray(e)},Object.isString=function(e){return"string"===Object.type(e)},Object.isNumber=function(e){return"number"===Object.type(e)},Object.isBoolean=function(e){return"boolean"===Object.type(e)},Object.isRegExp=function(e){return"RegExp"===Object.rawType(e)},Object.isFile=function(e){return e instanceof File},Object.isWindow=function(e){return Object.isUndefinedOrNull(e)&&e==e.window},Object.isElement=function(e){return!Object.isUndefinedOrNull(e)&&!(1!=e.nodeType)},Object.isEvent=function(e){return e instanceof Event},Object.isNull=function(e){return null===e},Object.isUndefined=function(e){return void 0===e},Object.isUndefinedOrNull=function(e){return!0===Object.isUndefined(e)||!0===Object.isNull(e)},Object.equals=function(e,t){if(e===t)return!0;if(!(e instanceof Object&&t instanceof Object))return!1;if(e.constructor!==t.constructor)return!1;if(Object.isArray(e)&&Object.isArray(t)&&e.length===t.length){for(let n=0;n<e.length;n++)if(!1===Object.equals(e[n],t[n]))return!1}else{if(!Object.isObject(e)||!Object.isObject(t)||Object.keys(e).length!==Object.keys(t).length)return!1;for(const n in e)if(e.hasOwnProperty.call(n)&&!1===Object.equals(e[n],t[n]))return!1}return!0},Object.clone=function(e){if(Object.isString(e))return String(e);if(Object.isArray(e))return Array.prototype.slice.apply(e);if(Object.isPlainObject(e)){const t=Object.create(null);return Object.keys(e).forEach((n=>{t[n]=Object.clone(e[n])})),t}return e},Object.omit=function(e,...t){const n=Object.clone(e);for(let e=0;e<t.length;e++){delete n[t[e]]}return n},Object.pick=function(e,...t){const n=Object.create(null);return t.forEach((t=>{n[t]=Object.clone(e[t])})),n},Array.prototype.isEmpty=function(){return 0===this.length},Array.prototype.exists=function(e){return-1!==this.indexOf(e)},Array.prototype.first=function(){if(0===this.length)throw"Array index out of range: 0";return this[0]},Array.prototype.last=function(){if(0===this.length)throw"Array index out of range: 0";return this[this.length-1]},Array.prototype.each=Array.prototype.forEach,Array.prototype.size=function(){return this.length},Array.prototype.merge=Array.prototype.concat,Array.prototype.compact=function(){return this.filter((e=>!1===Object.isUndefinedOrNull(e)))},Array.prototype.unique=function(){return Array.from(new Set(this))},Array.prototype.without=function(...e){return this.filter((t=>!1===e.includes(t)))},Array.prototype.clone=function(){return this.slice(0)},Array.prototype.clear=function(){return this.length=0,this},Date.prototype.isLeapYear=function(){const e=this.getFullYear();return e%4==0&&(e%100!=0||e%400==0)},Date.prototype.getSeason=function(){const e=this.getMonth();return e>=3&&e<=5?0:e>=6&&e<=8?1:e>=9&&e<=11?2:e>=12||e<=2?3:0},Date.prototype.getDayOfYear=function(){const e=[31,this.isLeapYear()?29:28,31,30,31,30,31,31,30,31,30,31];let t=0;for(let n=0,r=this.getMonth();n<r;n++)t+=e[n];return t},Date.prototype.getDaysOfYear=function(){return this.isLeapYear()?366:365},Date.prototype.format=function(e){if(!1===Object.isString(e))throw"Invalid argument format";const t={N:["Spring","Summer","Autumn","Winter"],A:["春","夏","秋","冬"]},n={f:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],F:["January","February","March","April","May","June","July","August","September","October","November","December"],C:["一","二","三","四","五","六","七","八","九","十","十一","十二"]},r={W:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],WW:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],WC:["日","一","二","三","四","五","六"]},i=()=>{const e=this.getMonth();return 11===e||e>=0&&e<=1?1:e>=2&&e<=4?2:e>=5&&e<=7?3:e>=8&&e<=10?4:0},o={y:e=>(this.getFullYear()+"").substring(4-e.length),n:()=>i(),N:()=>t.N[i()-1],A:()=>t.A[i()-1],M:e=>{const t=this.getMonth()+1;return(t<10?"0"+t:""+t).substring(2-e.length)},f:()=>{const e=this.getMonth();return n.f[e]},F:()=>{const e=this.getMonth();return n.F[e]},C:()=>{const e=this.getMonth();return n.C[e]},e:()=>this.getDay(),E:()=>this.getDay()+1,l:()=>{const e=this.getDay();return r.W[e]},L:()=>{const e=this.getDay();return r.WC[e]},w:()=>{const e=this.getDay();return r.WC[e]},d:e=>{const t=this.getDate();return(t<10?"0"+t:""+t).substring(2-e.length)},h:e=>{const t=this.getHours();let n=t%12==0?"12":t%12;return n=t<10?"0"+t:""+t,n.substring(2-e.length)},H:e=>{const t=this.getHours();return(t<10?"0"+t:""+t).substring(2-e.length)},m:e=>{const t=this.getMinutes();return(t<10?"0"+t:""+t).substring(2-e.length)},s:e=>{const t=this.getSeconds();return(t<10?"0"+t:""+t).substring(2-e.length)},S:e=>{const t=this.getMilliseconds();return(t<10?"0"+t:""+t).substring(2-e.length)}};return e.replace(/([ynNAMfFCdYTjeElLwWiohHmsSaOPZ])+/g,((e,t)=>{const n=o[t];return Object.isFunction(n)?n(e):e}))},function(e){e.NONE="None",e.LAX="Lax",e.STRICT="Strict"}(n||(n={}));Object.defineProperty(document,"fullScreen",{value:!1===Object.isUndefined(document.fullscreen)?document.fullscreen:!1===Object.isUndefined(document.mozFullScreen)?document.mozFullScreen:!1===Object.isUndefined(document.webkitIsFullScreen)?document.webkitIsFullScreen:!1===Object.isUndefined(document.msFullScreen)?document.msFullScreen:!1===Object.isUndefined(document.fullscreenElement)?null!==document.fullscreenElement:!1===Object.isUndefined(document.mozFullScreenElement)?null!==document.mozFullScreenElement:!1===Object.isUndefined(document.webkitFullscreenElement)?null!==document.webkitFullscreenElement:!1===Object.isUndefined(document.msFullscreenElement)&&null!==document.msFullscreenElement,configurable:!0,writable:!1}),Object.defineProperty(document,"fullScreenEnabled",{value:!1===Object.isUndefined(document.mozFullScreenEnabled)?document.mozFullScreenEnabled:!1===Object.isUndefined(document.webkitFullscreenEnabled)?document.webkitFullscreenEnabled:!1===Object.isUndefined(document.msFullscreenEnabled)?document.msFullscreenEnabled:!1===Object.isUndefined(document.fullscreenEnabled)&&document.fullscreenEnabled,configurable:!0,writable:!1}),Object.defineProperty(document,"fullScreenElement",{value:!1===Object.isUndefined(document.mozFullScreenElement)?document.mozFullScreenElement:!1===Object.isUndefined(document.webkitFullscreenElement)?document.webkitFullscreenElement:!1===Object.isUndefined(document.msFullscreenElement)?document.msFullscreenElement:!1===Object.isUndefined(document.fullscreenElement)?document.fullscreenElement:null,configurable:!0,writable:!1}),Object.defineProperty(document,"httpCookie",{value:new class{constructor(){}set(e,t,n){const r=e=encodeURIComponent(e).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent),i=t?encodeURIComponent(t).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,decodeURIComponent):"";let o="";if(n){if(o+=n.domain?"; domain="+n.domain:"",o+=n.path?"; path="+n.path:"",n.expires){o+="; expires="+(n.expires instanceof Date?n.expires:new Date(Date.now()+864e5*n.expires)).toUTCString()}!1===Object.isUndefinedOrNull(n.sameSite)&&(o+="; sameSite="+n.sameSite),Object.isBoolean(n.secure)&&!0===n.secure&&(o+="; secure"),Object.isBoolean(n.httpOnly)&&!0===n.httpOnly&&(o+="; httpOnly")}return document.cookie=r+"="+i+o}get(e){const t=document.cookie?document.cookie.split("; "):[];for(let n=0;n<t.length;n++){const r=t[n].split("="),i=decodeURIComponent(r[0]);let o=r.slice(1).join("=");if(i===e)return'"'===o[0]&&(o=o.slice(1,-1)),o.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)}return null}delete(e,t){const n=t||{};n.expires=-1,this.set(e,"",n)}},configurable:!0,writable:!1}),Document.prototype.requestFullscreen=function(){const e=document.documentElement;return Object.isFunction(e.mozRequestFullScreen)?e.mozRequestFullScreen():Object.isFunction(e.webkitRequestFullscreen)?e.webkitRequestFullscreen():Object.isFunction(e.msRequestFullscreen)?e.msRequestFullscreen():e.requestFullscreen()},Document.prototype.exitFullscreen=function(){return Object.isFunction(document.mozCancelFullScreen)?document.mozCancelFullScreen():Object.isFunction(document.mozExitFullScreen)?document.mozExitFullScreen():Object.isFunction(document.webkitCancelFullScreen)?document.webkitCancelFullScreen():Object.isFunction(document.webkitExitFullscreen)?document.webkitExitFullscreen():Object.isFunction(document.msExitFullscreen)?document.msExitFullscreen():document.exitFullscreen()},Function.prototype.argumentNames=function(){const e=this.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/);if(null===e)return null;const t=e[1].replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g,"").replace(/\s+/g,"").split(", ");return 1!==t.length||t[0]?t:[]},Function.prototype.delay=function(e){const t=Array.prototype.slice.call(arguments,1);return setTimeout(this.apply(this,t),1e3*e)},Math.rand=function(e,t){e=e||0,t=t||Number.MAX_SAFE_INTEGER;const n=Math.random()*(t-e+1)+e,r=Math.round(n);return r<e?e:r>t?t:r},Number.prototype.toPaddedString=function(e,t){const n=this.toString(t||10);return"0".repeat(e-n.length)+n},Number.isOdd=function(e){return e%2==1},Number.isEven=function(e){return e%2==0},Number.isBetween=function(e,t,n,r=!1){return(t=t||0)>(n=n||0)&&(t^=n,t^=n^=t),1==r?e>=t&&e<=n:e>t&&e<n},String.prototype.exists=function(e){return this.indexOf(e)>=0},String.prototype.equals=function(e){return!1===Object.isUndefinedOrNull(e)&&this===e},String.prototype.equalsIgnoreCase=function(e){return null!=e&&this.toLowerCase()===e.toLowerCase()},String.prototype.isEmpty=function(){return 0===this.length},String.prototype.isNotEmpty=function(){return this.length>0},String.prototype.isBlank=function(){return 0===this.length||0===this.trim().length},String.prototype.isNotBlank=function(){return!1===this.isBlank()},String.prototype.repeat=function(e){if(e<1)return"";{const t=this.toString();let n=t;for(let r=0;r<e;r++)n+=t;return n}},String.prototype.left=function(e){return this.substring(0,e)},String.prototype.right=function(e){return this.substring(this.length-e,this.length)},String.prototype.truncation=function(e,t){return t=t||"...",this.length>e?this.slice(0,e<=t.length?e:e-t.length)+t:String(this)},String.prototype.ltrim=function(){return Object.isFunction(this.trimStart)?this.trimStart():this.replace(/^\s*/g,"")},String.prototype.rtrim=function(){return Object.isFunction(this.trimEnd)?this.trimEnd():this.replace(/\s*$/g,"")},String.prototype.startsWith=function(e){return 0===this.indexOf(e)},String.prototype.endsWith=function(e){const t=this.length-e.length;return t>=0&&this.lastIndexOf(e)===t},String.prototype.lcfirst=function(){return this.charAt(0).toLowerCase()+this.substring(1)},String.prototype.ucfirst=function(){return this.charAt(0).toUpperCase()+this.substring(1)},String.prototype.escapeHTML=function(){return this.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")},String.prototype.unescapeHTML=function(){return this.replace(/&quot;/g,'"').replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&")},String.prototype.stripTag=function(e){return this.replace(new RegExp("<"+e+"(\\s+(\"[^\"]*\"|'[^']*'|[^>])+)?(/)?>|</"+e+">","gi"),"")},String.prototype.stripTags=function(e){if("string"==typeof e)return this.stripTag(e);if(Array.isArray(e)){let t=this.toString();for(let n=0;n<e.length;n++)t=t.stripTag(e[n]);return t}return this.toString()},String.prototype.stripScripts=function(){return this.replace(/<script[^>]*>([\S\s]*?)<\/script>/gim,"")},String.prototype.toArray=function(e){return this.split(e||"")},String.prototype.inspect=function(e){const t={"\b":"\\b","\t":"\\t","\r":"\\r","\n":"\\n","\f":"\\f","\\":"\\\\"},n=this.replace(/[\x00-\x1f\\]/g,(e=>e in t?t[e]:"\\u00"+e.charCodeAt(0).toPaddedString(2,16)));return e?'"'+n.replace(/"/g,'\\"')+'"':"'"+n.replace(/'/g,"\\'")+"'"},String.prototype.hashCode=function(){let e=0;if(0===e&&this.length>0)for(let t=0;t<this.length;t++)e=31*e+this.charCodeAt(t);return e},String.random=function(e,t="LETTER_NUMERIC"){let n="";if("CHINESE"===t){for(let t=0;t<e;t++)n+=String.fromCharCode(Math.rand(19968,40891));return n}const r="0123456789",i="abcdefghijklmnopqrstuvwxyz";let o,c;if("NUMERIC"===t)o=r;else if("LETTER"===t)o=i+i.toUpperCase();else if("LETTER_NUMERIC"===t||Object.isUndefinedOrNull(t))o=r+i+i.toUpperCase();else{if(!Object.isString(t))throw"Invalid argument type value, must be: NUMERIC, LETTER, LETTER_NUMERIC, CHINESE or String Characters";o=t}for(let t=0;t<e;t++)c=Math.rand(0,o.length-1),n+=o[c];return n},String.guid=function(){function e(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return e()+e()+"-"+e()+"-"+e()+"-"+e()+"-"+e()+e()+e()};const r=["Android","iPhone","iPod","Windows Phone","Mobile","Coolpad","mmp","SmartPhone","midp","wap","xoom","Symbian","J2ME","Blackberry","Wince"].some((e=>navigator.userAgent.exists(e))),i=/\(KHTML, like Gecko\) Chrome\//.test(navigator.userAgent),o=navigator.userAgent.exists("Firefox"),c=navigator.userAgent.exists("Mozilla"),s=navigator.userAgent.exists("Edge"),u=navigator.userAgent.exists("MSIE")&&navigator.userAgent.exists("compatible"),l=navigator.userAgent.exists("Opera"),a=navigator.userAgent.exists("Safari"),p=/Netscape([\d]*)\/([^\s]+)/i.test(navigator.userAgent);Object.defineProperty(window,"browser",{value:{userAgent:navigator.userAgent,name:navigator.appName,version:navigator.appVersion,cookieEnabled:navigator.cookieEnabled,language:navigator.language,languages:navigator.languages,isMobile:r,isChrome:i,isFirefox:o,isMozilla:c,isEdge:s,isMSIE:u,isOpera:l,isSafari:a,isNetscape:p},configurable:!0,writable:!1}),Window.prototype.copy=function(e){try{if(!0===Object.isObject(this.clipboardData))this.clipboardData.setData("text",e);else{const t=document.createElement("textarea");t.style.border="none",t.style.margin="0",t.style.padding="0",t.style.position="absolute",t.style.top="-9999px",t.style.left="-9999px",t.value=e,t.setAttribute("readonly",""),document.body.appendChild(t),t.setSelectionRange(0,e.length),t.select(),document.execCommand("copy"),t.remove()}}catch(e){console.error(e)}},Window.prototype.delay=function(e,t,...n){return((e,t,...n)=>{if("function"!=typeof e)throw new TypeError("Expected a function");return setTimeout((function(){e.apply(void 0,n)}),t)})(e,t||0,n)},Location.prototype.getParameters=function(){let e=this.search;const t={};if(-1!==e.indexOf("?")){e=e.substring(1);const n=e.split("&");for(let e=0;e<n.length;e++){const r=n[e].split("="),i=2==r.length?encodeURIComponent(r[1]):"";if(Object.isUndefined(t[r[0]]))t[r[0]]=i;else{if(!1===Object.isArray(t[r[0]])){const e=t[r[0]];delete t[r[0]],t[r[0]]=[e]}t[r[0]].push(i)}}}return t},Location.prototype.getParameter=function(e){return this.getParameters()[e]}})); //# sourceMappingURL=prototype.min.js.map