trim 去除空格

/**
 * trim 去除空格
 *@param  string str 待处理字符串
 *@param  number type 去除空格类型 1-所有空格  2-前后空格  3-前空格 4-后空格 默认为1
 *@return string str 处理后的字符串
 */
function trim(str, type = 1) {
    if (type && type !== 1 && type !== 2 && type !== 3 && type !== 4) return;
    switch (type) {
        case 1:
            return str.replace(/\s/g, "");
        case 2:
            return str.replace(/(^\s)|(\s*$)/g, "");
        case 3:
            return str.replace(/(^\s)/g, "");
        case 4:
            return str.replace(/(\s$)/g, "");
        default:
            return str;
    }
}

turnCase 大小写转换-全大写,全小写,首字母大写

/**
 * 大小写转换
 * @param string str 待转换的字符串
 * @param number type 1-全大写 2-全小写 3-首字母大写 
 * @return  string str 处理后的字符串
 */

function turnCase(str, type) {
    switch (type) {
        case 1:
            return str.toUpperCase()
        case 2:
            return str.toLowerCase();
        case 3:
            return str[0].toUpperCase() + str.substr(1).toLowerCase()
        default:
            return str;
    }
}

parseCase 大小写相互转换

/**
 * 大小写相互转换
 * @param string str 待转换的字符串
 * @return  string result 处理后的字符串
 */


function parseCase(str) {
    let result = '';
    for (let i = 0; i < str.length; i++) {
        let temp = str.charAt(i);
        let code = temp.charCodeAt();
        if ('a' <= temp && temp <= 'z') {
            temp = String.fromCharCode(code - 32);
        } else if ('A' <= temp && temp <= 'Z') {
            temp = String.fromCharCode(code + 32);
        }

        result += temp;
    }
    return result;
}

checkPwdStrength 验证密码强度

/**
 * 密码强度判断 checkPwdStrength
 * @param string str 待转换的字符串
 * 低于6为强度为0,字母+1,数字+1,下划线+1,特殊字符!@#$%+1,最高为4
 * @return  number lv 密码强度等级
 */

function checkPwdStrength(password) {
    //定义一个变量存储密码强度等级,默认为0
    let lv = 0;
    if (password.match(/[a-z]/g)) { lv++; } //密码包含字母
    if (password.match(/[0-9]/g)) { lv++; } //密码包含数字
    if (password.match(/_/g)) { lv++; } //密码包含下划线
    if (password.match(/[!@#$%]/g)) { lv++; } //密码包含!@#$%任意特殊字符
    if (password.length < 6) { lv = 0; }
    if (lv > 4) { lv = 4; }

    return lv;
}

hexColor 随机16进制颜色

/**
 * 随机16进制颜色 hexColor
 * @return  string str 带#号的随机16进制颜色
 */

function hexColor() {

    let str = '#';
    let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
    for (let i = 0; i < 6; i++) {
        let index = Number.parseInt(Math.random() * 16);
        str += arr[index]
    }
    return str;
}

randomCode 指定位数随机码

/**
 * 指定位数随机码 randomCode
 * @param number count 随机码位数
 * @return  string str 指定位数随机码
 */

function randomCode(count) {

    let str = '';
    let number_arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    let string_arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    let arr = [...number_arr, ...string_arr];

    for (let i = 0; i < count; i++) {
        let index = Number.parseInt(Math.random() * arr.length);
        str += arr[index];
    }
    return str;
}

keywordsCount 关键词统计:统计一段文字中指定文字出现次数

/**
 * 关键词统计:统计一段文字中指定文字出现次数 keywordsCount
 *@param string text 进行统计的文本
 *@param string keywords 进行统计的关键词
 *@return number count 关键词出现次数
 * tip:@param document.body.innerText--全文统计
 */

function keywordsCount(text, keywords) {
    return text.split(keywords).length - 1
}

caseFormat  短横,下划线与小驼峰的相互转化 caseFormat

/**
 *  短横,下划线与小驼峰的相互转化 caseFormat
 *  @param string str 待转换字符串
 *  @param string type '-'or '_',默认为下划线
 *  @return string str 转换后字符串
 */

function caseFormat(str, type = '_') {

    if (type && type !== '-' && type !== '_') return;
    let result = '';
    if (str.includes(type)) {
        let arr = str.split(type);
        result = arr[0]
        for (let i = 0; i < arr.length - 1; i++) {
            result += arr[i + 1][0].toUpperCase() + arr[i + 1].substr(1).toLowerCase()
        }
    } else {
        result = str.replace(/[A-Z]/g, function (match) {
            return type + match.toLowerCase();
        });

    }

    return result;
}

dateFormat 时间格式化   形如--"20190803 11:01:07"

/**
 * 时间格式化 dateFormat  形如--"20190803 11:01:07"
 * @param Date date 待格式化的时间
 * @param number type 格式化范围 1-精确到月 2-精确到日 3-精确到分钟 4-精确到秒
 * @return string time 格式化后的时间
 */

function dateFormat(date, type = 4) {
    if (type && type !== 1 && type !== 2 && type !== 3 && type !== 4) return;
    let y = date.getFullYear().toString();
    let m = (date.getMonth() + 1).toString().padStart(2, '0');
    let d = date.getDate().toString().padStart(2, '0');
    let h = date.getHours().toString().padStart(2, '0');
    let M = date.getMinutes().toString().padStart(2, '0');
    let s = date.getSeconds().toString().padStart(2, '0');
    if (type === 1) return y + m;
    if (type === 2) return y + m + d;
    if (type === 3) return y + m + d + " " + h + ":" + M;
    if (type === 4) return y + m + d + " " + h + ":" + M + ":" + s;

}

reverse 逆序输出

/**
 * 逆序输出 reverse 
 * @param string str 源字符串
 * @return string result 逆序输出的字符串
 */

function reverse(str) {
    let result = '';
    for (let i = str.length - 1; i >= 0; i--) {
        result += str[i];
    }
    return result;
};

onlyArray 数组去重

/**
 * 数组去重 onlyArray  
 * @param Array arr 待去重的数组
 * @return Array arr 去重后的数组
 */

function onlyArray(arr) {
    if (!Array.isArray(arr)) return;
    return Array.from(new Set([...arr]))
}

sort 数组排序

/**
 * 数组排序 sort  
 * @param Array arr 待排序的数组
 * @param number rule  排序规则 1-升序 0-降序,默认升序
 * @return Array arr 排序后的数组
 */

function sort(arr, rule = 1) {
    if (!Array.isArray(arr)) return;
    if (rule && rule !== 1 && rule !== 0) return;
    return rule === 1 ? arr.sort((a, b) => a - b) : arr.sort((a, b) => b - a)

}

resort 数组错乱排序

/**
 * 数组错乱排序 resort  
 * @param Array arr 待排序的数组
 * @return Array arr 排序后的数组
 */

function resort(arr) {
    if (!Array.isArray(arr)) return;
    return arr.sort(function () {
        return Math.random() - 0.5
    });
}

totalArr 数组求和

/**
 * 数组求和 totalArr  
 * @param Array arr 待求和的数组
 * @return Array arr 求和后的数组
 */

function totalArr(arr) {
    if (!Array.isArray(arr)) return;
    return arr.reduce(function (pre, cur) {
        return pre + cur
    })
}

mergeArr 数组合并

/**
 * 数组合并 mergeArr  
 * @param Array arr 待合并数组
 * ...params Array arr 合并数组,可放置多个
 * @return Array arr 合并后的数组
 */

function mergeArr(arr, ...everyArr) {
    if (!Array.isArray(arr)) return;
    return arr.concat(...everyArr)

}

lastItem 获取数组最后一项

/**
 * 获取数组最后一项 lastItem 
 * @param Array arr 源数组
 * @return <> item 数组最后一项
 */

function lastItem(arr) {
    if (!Array.isArray(arr)) return;
    return arr[arr.length - 1]

}

debounce 防抖

/**
 * 防抖 debounce  
 * @param function fn 要进行防抖的函数
 * @return function debounceFn 防抖后的函数
 */

const debounce = (fn, delay) => {
    let timer = null; // 创建定时器id
    return (...args) => {
        clearTimeout(timer); //事件频繁触发,清除之前定时器 
        timer = setTimeout(() => { // 创建新定时器,保证限定时间间隔fn只执行一次
            fn(...args);
        }, delay);
    };
}

throttle 节流

/**
 * 节流 throttle  
 * @param function fn 要进行节流的函数
 * @return function throttleFn 节流后的函数
 */

const throttle = (fn, delay) => {
    let lock = true; // 函数外设置状态锁,默认开启
    return (...args) => {
        if (!lock) return; // 在函数开头判断标记是否为true,不为true则return,阻止后续程序进行
        lock = false; // 改变状态锁
        setTimeout(() => {
            fn(...args)
            // 函数执行完,回调中改变状态锁
            lock = true;
        }, delay);
    };
}

deepClone 深拷贝

/**
 * 深拷贝 deepClone  
 * @param Object obj 要进行拷贝的对象
 * @return Object obj 拷贝后的对象
 */


function _forEach(array, iteratee) {
    let index = -1;
    const length = array.length;
    while (++index < length) {
        iteratee(array[index], index);
    }
    return array;
}



function deepClone(target, map = new WeakMap()) {



    if (typeof target === 'object' && target !== null) {
        var isArray = Array.isArray(target);
        var cloneTarget = isArray ? [] : {};
        if (map.get(target)) {
            return map.get(target)
        }
        map.set(target, cloneTarget)

        if (isArray) {
            _forEach(target, (value, key) => {

                cloneTarget[key] = deepClone(target[key], map);
            });
        } else {
            const keys = Object.keys(target);
            _forEach(keys, (value, key) => {
                key = value;
                cloneTarget[key] = deepClone(target[key], map);
            })
        }



        return cloneTarget
    } else {
        return target
    }
}

clearWebSite 让网页变得干净--调试时使用

/**
 * 让网页变得干净   clearWebSite
 * tip:本质是将html-body内容隐藏并清除控制台
 */

function clearWebSite() {
    document.body.setAttribute('hidden', true);
    window.clear();//清除控制台
}

delInvalidprops 去除对象或数组的无效属性

/**
 * 去除对象或数组的无效属性   delInvalidprops
 * @param Object obj 待处理对象或数组
 * @return <> obj 处理后的对象或数组
 */

function delInvalidprops(source) {
    if (typeof source !== 'object') return;
    const invalidProps = [undefined, null, '']

    if (Array.isArray(source)) {
        invalidProps.forEach(invalidProp => {
            source.forEach((item, idx) => {
                if (item === invalidProp) source.splice(idx, 1);
                if (typeof item === 'object') delInvalidprops(item);//递归删除--数组>对象

            })

        })


    } else {

        for (let key in source) {
            if (source[key] === invalidProps[0]) { delete source[key] };
            if (source[key] === invalidProps[1]) { delete source[key] };
            if (source[key] === invalidProps[2]) { delete source[key] };

        }
    }
    return source;

}

downloadByUrl 根据指定url下载文件

/**
 * 根据指定url下载文件   downloadByUrl
 * @param string url 待下载文件url
 * @param string name 指定下载文件的名称 --默认为'下载'
 * tip:html5新特性 --a 标签 download属性  部分浏览器不支持
 */


function downloadByUrl(url, name = '下载') {
    if (typeof url !== "string" || url.length === 0) return;
    const parseUrl = url.split('.');
    const extensionName = parseUrl.pop();
    let a = document.createElement("a");
    a.href = url;
    a.download = name + '.' + extensionName;
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);

}

pastTime 已过时间

/**
 * pastTime 已过时间
 * @param  Date  startTime  起始时间
 * @return string 时间节点描述
 */

function pastTime(startTime) {

    let currentTime = Date.parse(new Date()),
        time = currentTime - startTime,
        day = parseInt(time / (1000 * 60 * 60 * 24)),
        hour = parseInt(time / (1000 * 60 * 60)),
        min = parseInt(time / (1000 * 60)),
        month = parseInt(day / 30),
        year = parseInt(month / 12);
    if (year) return year + "年前"
    if (month) return month + "个月前"
    if (day) return day + "天前"
    if (hour) return hour + "小时前"
    if (min) return min + "分钟前"
    return '刚刚'

}

isLegalEmail 邮箱校验

/**
 * 邮箱校验   isLegalEmail
 * @param  string  email 待校验邮箱
 * @return boolean 校验结果 
 */
function isLegalEmail(email) {
    return /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(email);
}

isLegalName 中文名校验--(2-6)位

/**
 * 中文名校验--(2-6)位    isLegalName
 * @param  string  name 待校验中文名
 * @return boolean 校验结果 
 */
function isLegalName(name) {
    return /^[\u4e00-\u9fa5]{2,6}$/.test(name);
}

isLegalIdCard 身份证校验

/**
 * 身份证校验   isLegalIdCard
 * @param  string  idCard 待校验身份证
 * @return boolean 校验结果 
 */
function isLegalIdCard(idCard) {
    return /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/.test(idCard);
}

isLegalPhone 手机号码校验

/**
 * 手机号码校验   isLegalPhone
 * @param  string  phone 待校验手机号码
 * @return boolean 校验结果 
 */
function isLegalPhone(phone) {
    return /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(phone);
}

jsonFormat 自定义缩进的JSON格式化

/**
 * jsonFormat  自定义缩进的JSON格式化
 * @param  Object/Array  obj 待格式化的对象或数组
 * @param  number  space 缩进数
 * @return string  jsonStr 格式化后的json字符串 
 */
function jsonFormat(obj, space) {

    return JSON.stringify(obj, null, space);
}

setCookie 设置cookie

/**
 * setCookie  设置cookie
 * @param  String  name 要设置的cookie名
 * @param   *   value 要设置的cookie值
 */

function setCookie(name, value) {
    document.cookie = name + "=" + value + ";path=/";
}

getCookie 获取cookie

/**
 * getCookie  获取cookie
 * @param  String  name 要获取的cookie名
 * @return   *  目标cookie的值
 */
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    }

    return null;
}

clearCookie 清除cookie

/**
 * clearCookie  清除cookie
 * @param  String  name 要清除的cookie名
 */


function clearCookie(name) {
    setCookie(name, "")
}

info 美化打印--信息

warn 美化打印--警告

error 美化打印--错误

/**
 * info,warn,error美化打印
 * ...param  String  msg 要输出的信息
 * @return String 美化后的信息
 */



function _print(type) {
    return function (...args) {
        var backgroundColor = _getBackgroundColorByType(type);
        var css = `background-color:${backgroundColor};color:white;padding:0 10px;border-radius:8px;`
        if (args.length === 0) return undefined
        if (args.length === 1) {
            console.log(`%c${args[0]}`, css);
        } else {
            for (var i = 0; i < args.length; i++) {
                console.log(`%c${args[i]}`, css)
            }
        }
    };
};

function _getBackgroundColorByType(type = "info") {
    var map = new Map();
    map.set("info", "#2ecc71");
    map.set("warn", "orange");
    map.set("error", "#FF0000");
    return map.get(type)
}

var info = _print("info")
var warn = _print("warn")
var error = _print("error")

发布到npm

新建一个文件夹,名字就是你要发布到npm上的包的名字,如light-func 

进入文件夹,输入npm init -y (需要node环境)

新建index.js 将上述函数代码复制粘贴进来

npm adduser 注册用户 输入用户名 密码 邮箱

npm login 登录 输入注册信息

npm publish 发布包到npm (镜像源必须为npm源 包名不允许重复)

每次迭代需要更新版本号,发布后可去npm官网查看