返回

JS开发工具函数仓库助攻,快速提升开发效率!

前端

在日常的JavaScript开发中,开发者经常会面临各种需求,有时需要快速找到合适的解决方案。这时,一个趁手的工具库可以极大地提高开发效率。本文将介绍一个精心整理的JavaScript开发工具函数仓库,涵盖字符串操作、数组处理、日期时间处理、正则表达式和加密解密等功能。通过这些工具函数,开发者可以迅速解决问题,提升工作效率。

字符串操作

trim()

去除字符串两端的空格。

let str = "   Hello, World!   ";
console.log(str.trim()); // "Hello, World!"

startsWith()

判断字符串是否以指定字符串开头。

let str = "Hello, World!";
console.log(str.startsWith("Hello")); // true

endsWith()

判断字符串是否以指定字符串结尾。

let str = "Hello, World!";
console.log(str.endsWith("World!")); // true

includes()

判断字符串是否包含指定字符串。

let str = "Hello, World!";
console.log(str.includes("World")); // true

repeat()

重复字符串指定次数。

let str = "Hello";
console.log(str.repeat(3)); // "HelloHelloHello"

数组处理

push()

将一个或多个元素添加到数组的末尾。

let arr = [1, 2, 3];
arr.push(4);
console.log(arr); // [1, 2, 3, 4]

pop()

从数组的末尾删除一个元素并返回该元素。

let arr = [1, 2, 3];
console.log(arr.pop()); // 3
console.log(arr); // [1, 2]

shift()

从数组的开头删除一个元素并返回该元素。

let arr = [1, 2, 3];
console.log(arr.shift()); // 1
console.log(arr); // [2, 3]

unshift()

将一个或多个元素添加到数组的开头。

let arr = [1, 2, 3];
arr.unshift(0);
console.log(arr); // [0, 1, 2, 3]

indexOf()

返回数组中指定元素的索引。

let arr = [1, 2, 3];
console.log(arr.indexOf(2)); // 1

日期时间处理

now()

返回当前时间戳。

console.log(Date.now()); // 当前时间的时间戳

getDate()

返回当前日期。

let date = new Date();
console.log(date.getDate()); // 当前日期(1-31)

getMonth()

返回当前月份。

let date = new Date();
console.log(date.getMonth()); // 当前月份(0-11)

getFullYear()

返回当前年份。

let date = new Date();
console.log(date.getFullYear()); // 当前年份(四位数)

getHours()

返回当前小时。

let date = new Date();
console.log(date.getHours()); // 当前小时(0-23)

正则表达式

test()

判断字符串是否与正则表达式匹配。

let str = "Hello, World!";
let regex = /World/;
console.log(regex.test(str)); // true

exec()

返回与正则表达式匹配的子字符串。

let str = "Hello, World!";
let regex = /World/;
console.log(regex.exec(str)); // ["World"]

replace()

将字符串中的指定部分替换为另一个字符串。

let str = "Hello, World!";
let newStr = str.replace("World", "JavaScript");
console.log(newStr); // "Hello, JavaScript!"

split()

将字符串拆分为数组。

let str = "Hello, World!";
let arr = str.split(", ");
console.log(arr); // ["Hello", "World!"]

加密解密

md5()

计算字符串的MD5值。

function md5(string) {
    // 实现MD5算法的代码...
}
console.log(md5("Hello")); // "5d41402abc4b2a76b9719d911017c592"

sha1()

计算字符串的SHA1值。

function sha1(string) {
    // 实现SHA1算法的代码...
}
console.log(sha1("Hello")); // "aaf4c61ddcc5e8a2dabede0f3b482cd9a"

base64Encode()

将字符串编码为Base64字符串。

function base64Encode(string) {
    return btoa(string);
}
console.log(base64Encode("Hello")); // "SGVsbG8="

base64Decode()

将Base64字符串解码为字符串。

function base64Decode(encodedString) {
    return atob(encodedString);
}
console.log(base64Decode("SGVsbG8=")); // "Hello"

如何使用这个仓库

  1. 克隆或下载仓库:将仓库克隆到本地或者直接下载压缩包。
    git clone https://github.com/your-github-username/js-utils.git
    
  2. 引入所需工具函数:在你的项目中引入所需的工具函数文件。例如,如果需要使用字符串操作函数,可以引入 stringUtils.js
    import { trim, startsWith, endsWith, includes, repeat } from './path/to/stringUtils';
    
  3. 使用工具函数:在代码中使用这些工具函数来解决问题,提高开发效率。例如:
    let str = "   Hello, World!   ";
    console.log(trim(str)); // "Hello, World!"
    

贡献

欢迎大家对这个仓库进行贡献。如果你有更好的工具函数,或者发现了一些问题,请随时提交Pull Request。以下是一些贡献指南:

  1. 确保你的代码风格与仓库保持一致。
  2. 添加必要的单元测试。
  3. 在提交Pull Request时,详细描述你的改动内容。

结语

希望这份JS开发工具函数仓库能够帮助你提高开发效率,让你的开发过程更加轻松愉快。如果你觉得这个仓库对你有所帮助,请不要吝啬你的星星和分享,让更多人知道这个有用的工具库。