美文网首页
基础工具函数

基础工具函数

作者: 秋天de童话 | 来源:发表于2022-01-03 11:19 被阅读0次
    function type(obj) {
      return Object.prototype.toString.call(obj).replace(/\[object\s|\]/g, "");
    }
    
    function isArray(list) {
      return type(list) === "Array";
    }
    
    function isObject(obj) {
      return type(obj) === "Object";
    }
    
    function isString(str) {
      return type(str) === "String";
    }
    
    function isNotEmptyObj(obj) {
      return isObject(obj) && JSON.stringify(obj) != "{}";
    }
    
    function objForEach(obj, fn) {
      isNotEmptyObj(obj) && Object.keys(obj).forEach(fn);
    }
    
    function aryForEach(ary, fn) {
      ary.length && ary.forEach(fn);
    }
    
    function setAttr(node, key, value) {
      switch (key) {
        case "style":
          node.style.cssText = value;
          break;
        case "value":
          var tagName = node.tagName || "";
          tagName = tagName.toLowerCase();
          if (tagName === "input" || tagName === "textarea") {
            node.value = value;
          } else {
            // if it is not a input or textarea, use `setAttribute` to set
            node.setAttribute(key, value);
          }
          break;
        default:
          node.setAttribute(key, value);
          break;
      }
    }
    
    function toArray(data) {
      if (!data) {
        return [];
      }
      const ary = [];
      aryForEach(data, item => {
        ary.push(item);
      });
    
      return ary;
    }
    
    export {
      isArray,
      isObject,
      isString,
      isNotEmptyObj,
      objForEach,
      aryForEach,
      setAttr,
      toArray
    };
    

    详情见:https://segmentfault.com/a/1190000019994425

    相关文章

      网友评论

          本文标题:基础工具函数

          本文链接:https://www.haomeiwen.com/subject/zwdxcrtx.html