点击查看html编辑器说明文档

safeStringifyedit icon

|
|
Fork(复制)
|
|
作者:
lynn-ssk
提交反馈
嵌入
设置
下载
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ...
展开
</head>
<body>
            
            <div class="ccc" id="idccc"></div>
        
</body>
CSS
格式化
            
            
        
JS
格式化
            
            const safeStringify = function(obj, spaces) {
    return JSON.stringify(obj, serializer(), spaces);
};

const toStr = function(val) {
    return val == null ? '' : val.toString();
};

const lowerCase = function(str) {
    return toStr(str).toLocaleLowerCase();
};

const isBuffer = function(val) {
    if (val == null) return false;
    if (val._isBuffer) return true;

    return (
        val.constructor &&
        isFn(val.constructor.isBuffer) &&
        val.constructor.isBuffer(val)
    );
};
const type = function(val, lower = true) {
    let ret;
    if (val === null) ret = 'Null';
    if (val === undefined) ret = 'Undefined';
    if (isNaN(val)) ret = 'NaN';
    if (isBuffer(val)) ret = 'Buffer';

    if (!ret) {
        ret = objToStr(val).match(regObj);
        if (ret) ret = ret[1];
    }

    if (!ret) return '';

    return lower ? lowerCase(ret) : ret;
};

const regObj = /^\[object\s+(.*?)]$/;

const upperFirst = function(str) {
    if (str.length < 1) return str;

    return str[0].toUpperCase() + str.slice(1);
};

const isUndef = function(val) {
    return val === void 0;
};

const isFn = function(val) {
    const objStr = objToStr(val);

    return (
        objStr === '[object Function]' ||
        objStr === '[object GeneratorFunction]' ||
        objStr === '[object AsyncFunction]'
    );
};

const ObjToStr = Object.prototype.toString;

const objToStr = function(val) {
    return ObjToStr.call(val);
};

const isRegExp = function(val) {
    return objToStr(val) === '[object RegExp]';
};
function getObjType(obj) {
  if (obj.constructor && obj.constructor.name) return obj.constructor.name

  return upperFirst({}.toString.call(obj).replace(/(\[object )|]/g, ''))
}
function serializer() {
    const stack = [];
    const keys = [];

    return function(key, val) {
        if (stack.length > 0) {
            const pos = stack.indexOf(this);
            if (pos > -1) {
                stack.splice(pos + 1);
                keys.splice(pos, Infinity, key);
            } else {
                stack.push(this);
                keys.push(key);
            }

            const valPos = stack.indexOf(val);
            if (valPos > -1) {
                if (stack[0] === val) {
                    val = '[Circular ~]';
                } else {
                    val =
                        '[Circular ~.' + keys.slice(0, valPos).join('.') + ']';
                }
            }
        } else {
            stack.push(val);
        }

        if (isRegExp(val) || isFn(val)) {
            val = '[' + upperFirst(type(val)) + ' ' + toStr(val) + ']';
        } else if (isUndef(val)) {
            val = null;
        }

        return val;
    };
}

let ccc = document.querySelector(".ccc")
let testObj = {
  d: function fftest() {
    console.log(12)
  },
  e: [1,2,,3],
  f: ccc,
  a:1,
  b:"safdadsfsafsfsadfsfsadfasfsadfa21sf2as34fas65f4a6sf4asd6f5a4sf5sf4asd6f54f5sa6f4dasf5a6sfas5f4das65f4asf65dasfd5a6sf4as56fadsf6dsa54fadsf",
  c:true,
}
console.log(testObj)
console.log(testObj.d)
console.log(testObj.f)
console.log(safeStringify(testObj))

console.log(getObjType(""))
console.log(getObjType(1))
console.log(getObjType(true))
console.log(getObjType([]))
console.log(getObjType({}))
console.log(getObjType(/abc8/))
console.log(getObjType(new Map))
console.log(getObjType(new Set))

        
预览
控制台
清空