WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2024-06-05 1678913eb54d4af40d9f6e44556cda316188dd9d
common/Tools.js
@@ -47,7 +47,7 @@
     * @return {string}
     */
    static createGUID() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            let r = (Math.random() * 16) | 0,
                v = c === 'x' ? r : (r & 0x3) | 0x8;
            return v.toString(16);
@@ -436,16 +436,24 @@
    /**
     * 数值转换为金钱格式
     * @param {Number|String} number
     * @param {String} [forRead=''] 便于阅读财务金额模式
     * @return {string}
     */
    static moneyFormat(number) {
    static moneyFormat(number, forRead = '') {
        if (!number && typeof number !== 'number' && typeof number !== 'string') {
            return '';
        }
        if (typeof number === 'string') {
            number = Number(number) || 0;
        }
        return number.toFixed(2);
        if (forRead === 'forRead') {
            return number.toLocaleString('zh-cn', {
                minimumFractionDigits: 2,
                maximumFractionDigits: 2,
            });
        } else {
            return number.toFixed(2);
        }
    }
    /**
@@ -542,42 +550,40 @@
     * @param {Function} fnc
     * @param {Number} wait
     * @returns {Function}
     * @tutorial Tools.debounce(<fnc>, <wait>)(<DebounceKey>)
     */
    static debounce(fnc, wait = 200) {
        let timeout = null;
        return function() {
            const context = this;
            const args = arguments;
            if (timeout) {
                clearTimeout(timeout);
        return key => {
            if (!key || !Tools.isString(key)) {
                throw 'Debounce function need a key!';
            }
            const callNow = !timeout;
            timeout = setTimeout(() => {
                timeout = null;
            }, wait);
            if (callNow) {
                fnc.apply(context, args);
            const timer = Tools.debounce['dKey-' + key];
            if (timer) {
                clearTimeout(timer);
            }
            Tools.debounce['dKey-' + key] = setTimeout(fnc, wait);
        };
    }
    /**
     * 节流函数(一段时间周期内,仅执行第一次回调)
     * 节流函数(本段时间内,仅执行第一次回调)
     *  在一定时间内只能触发一次函数执行
     *  如果这个时间内再次触发,则忽略,直到下一个时间段再次触发
     * @param {Function} fnc
     * @param {Number} wait
     * @returns {Function}
     * @returns {function(key):void}
     * @tutorial Tools.throttle(<fnc>, <wait>)(<ThrottleKey>)
     */
    static throttle(fnc, wait = 200) {
        let previous = 0;
        return function() {
            const context = this;
            const args = arguments;
            const now = new Date();
        return key => {
            if (!key || !Tools.isString(key)) {
                throw 'Throttle function need a key!';
            }
            const previous = Tools.throttle['tKey-' + key] || 0;
            const now = Date.now();
            if (now - previous > wait) {
                fnc.apply(context, args);
                previous = now;
                Tools.throttle['tKey-' + key] = now;
                fnc();
            }
        };
    }
@@ -599,5 +605,3 @@
        console.info(Math.round(px) + 'px');
    };
}
global.Tools = Tools;