mirror of
https://github.com/komari-monitor/komari.git
synced 2026-11-24 17:10:54 +00:00
12 lines
2.7 KiB
JavaScript
12 lines
2.7 KiB
JavaScript
(function(){
|
|
'use strict';
|
|
function inspect(v,opts){const seen=new Set();const depth=opts&&opts.depth!==undefined?opts.depth:2;function show(x,d){if(typeof x==='string')return "'"+x.replace(/'/g,"\'")+"'";if(typeof x==='bigint')return String(x)+'n';if(x===null||typeof x!=='object')return String(x);if(x instanceof Error)return x.stack||String(x);if(seen.has(x))return '[Circular]';if(d<0)return Array.isArray(x)?'[Array]':'[Object]';seen.add(x);let s;if(Array.isArray(x))s='[ '+x.map(v=>show(v,d-1)).join(', ')+' ]';else s='{ '+Object.keys(x).map(k=>(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(k)?k:show(k,d-1))+': '+show(x[k],d-1)).join(', ')+' }';seen.delete(x);return s;}return show(v,depth);}
|
|
function text(v){return typeof v==='object'&&v!==null?inspect(v):String(v);}
|
|
function format(...args){if(!args.length)return '';const first=args[0];if(typeof first!=='string')return args.map(text).join(' ');let i=1;const out=first.replace(/%[%sdifjoOc]/g,code=>{if(code==='%%')return '%';if(i>=args.length)return code;const v=args[i++];switch(code){case '%s':return String(v);case '%d':return typeof v==='bigint'?String(v)+'n':String(Number(v));case '%i':return String(parseInt(v,10));case '%f':return String(parseFloat(v));case '%j':try{return String(JSON.stringify(v));}catch(e){return '[Circular]';}case '%o':case '%O':return inspect(v);case '%c':return '';default:return code;}});return out+(i<args.length?' '+args.slice(i).map(text).join(' '):'');}
|
|
function inherits(ctor,superCtor){ctor.super_=superCtor;Object.setPrototypeOf(ctor.prototype,superCtor.prototype);}
|
|
function promisify(fn){return function(...args){return new Promise((resolve,reject)=>{fn.call(this,...args,(err,v)=>err?reject(err):resolve(v));});};}
|
|
function callbackify(fn){return function(...args){const cb=args.pop();if(typeof cb!=='function')throw new TypeError('Callback must be a function');Promise.resolve(fn.apply(this,args)).then(v=>cb(null,v),e=>cb(e||new Error('Promise rejected with falsy value')));};}
|
|
const types={isArrayBuffer:v=>v instanceof ArrayBuffer,isArrayBufferView:ArrayBuffer.isView,isTypedArray:v=>ArrayBuffer.isView(v)&&!(v instanceof DataView),isUint8Array:v=>v instanceof Uint8Array,isDate:v=>v instanceof Date,isRegExp:v=>v instanceof RegExp,isNativeError:v=>v instanceof Error,isPromise:v=>v instanceof Promise,isMap:v=>v instanceof Map,isSet:v=>v instanceof Set};
|
|
return {format,formatWithOptions:(opts,...args)=>format(...args),inspect,inherits,promisify,callbackify,types,isArray:Array.isArray,isBuffer:v=>typeof Buffer!=='undefined'&&Buffer.isBuffer(v),isString:v=>typeof v==='string',isNumber:v=>typeof v==='number',isFunction:v=>typeof v==='function',isNull:v=>v===null,isUndefined:v=>v===undefined,isObject:v=>v!==null&&typeof v==='object'};
|
|
})()
|