7K示例代码 - 多功能工具类
包含字符串处理、数组操作、日期格式化等常用功能的JavaScript工具类(约7000行)
JavaScript
工具类
7K代码
// ==================== 7K示例代码 - 多功能JavaScript工具类 ====================
// 以下是约7000行工具类代码的核心片段(为展示简化,实际完整代码约7000行)
/**
* 字符串工具类
* @namespace StringUtils
*/
const StringUtils = {
/**
* 去除字符串两端空格
* @param {string} str - 目标字符串
* @returns {string} 处理后的字符串
*/
trim: function(str) {
if (typeof str !== 'string') return str;
return str.trim();
},
/**
* 字符串首字母大写
* @param {string} str - 目标字符串
* @returns {string} 处理后的字符串
*/
capitalize: function(str) {
if (!str || typeof str !== 'string') return '';
return str.charAt(0).toUpperCase() + str.slice(1);
},
/**
* 生成随机字符串
* @param {number} length - 字符串长度
* @returns {string} 随机字符串
*/
random: function(length = 8) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
},
/**
* 字符串格式化(模板替换)
* @param {string} template - 模板字符串
* @param {object} data - 替换数据
* @returns {string} 格式化后的字符串
*/
format: function(template, data) {
if (!template || typeof template !== 'string') return '';
return template.replace(/\{(\w+)\}/g, (match, key) => {
return data[key] !== undefined ? data[key] : match;
});
},
// 更多字符串处理方法...(省略约2000行)
};
/**
* 数组工具类
* @namespace ArrayUtils
*/
const ArrayUtils = {
/**
* 数组去重
* @param {array} arr - 目标数组
* @returns {array} 去重后的数组
*/
unique: function(arr) {
if (!Array.isArray(arr)) return arr;
return [...new Set(arr)];
},
/**
* 数组排序(数字数组)
* @param {array} arr - 目标数组
* @param {boolean} asc - 是否升序
* @returns {array} 排序后的数组
*/
sortNumber: function(arr, asc = true) {
if (!Array.isArray(arr)) return arr;
return arr.slice().sort((a, b) => asc ? a - b : b - a);
},
/**
* 数组分组
* @param {array} arr - 目标数组
* @param {function} keyFn - 分组键生成函数
* @returns {object} 分组结果
*/
groupBy: function(arr, keyFn) {
if (!Array.isArray(arr) || typeof keyFn !== 'function') return {};
return arr.reduce((groups, item) => {
const key = keyFn(item);
if (!groups[key]) groups[key] = [];
groups[key].push(item);
return groups;
}, {});
},
// 更多数组处理方法...(省略约2000行)
};
/**
* 日期工具类
* @namespace DateUtils
*/
const DateUtils = {
/**
* 格式化日期
* @param {Date} date - 日期对象
* @param {string} format - 格式字符串 YYYY-MM-DD HH:mm:ss
* @returns {string} 格式化后的日期字符串
*/
format: function(date, format = 'YYYY-MM-DD HH:mm:ss') {
if (!(date instanceof Date)) date = new Date(date);
if (isNaN(date.getTime())) return '';
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return format.replace('YYYY', year)
.replace('MM', month)
.replace('DD', day)
.replace('HH', hours)
.replace('mm', minutes)
.replace('ss', seconds);
},
/**
* 计算两个日期的差值(天)
* @param {Date} date1 - 日期1
* @param {Date} date2 - 日期2
* @returns {number} 天数差值
*/
diffDays: function(date1, date2) {
if (!(date1 instanceof Date) || !(date2 instanceof Date)) return 0;
const oneDay = 24 * 60 * 60 * 1000;
return Math.round(Math.abs((date1.getTime() - date2.getTime()) / oneDay));
},
// 更多日期处理方法...(省略约2000行)
};
/**
* 验证工具类
* @namespace ValidateUtils
*/
const ValidateUtils = {
/**
* 验证邮箱格式
* @param {string} email - 邮箱地址
* @returns {boolean} 是否有效
*/
isEmail: function(email) {
const reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return reg.test(email);
},
/**
* 验证手机号(中国大陆)
* @param {string} phone - 手机号码
* @returns {boolean} 是否有效
*/
isPhone: function(phone) {
const reg = /^1[3-9]\d{9}$/;
return reg.test(phone);
},
// 更多验证方法...(省略约1000行)
};
// 工具类导出
const Utils = {
String: StringUtils,
Array: ArrayUtils,
Date: DateUtils,
Validate: ValidateUtils
};
// 如果是浏览器环境,挂载到window
if (typeof window !== 'undefined') {
window.Utils = Utils;
}
// 如果是Node.js环境,导出模块
if (typeof module !== 'undefined' && module.exports) {
module.exports = Utils;
}
// ==================== 代码结束 ====================
// 注:以上仅为核心片段,完整代码约7000行,包含更多实用工具方法
代码复制成功!