1.0.2 • Published 6 months ago

func-js-lib v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

func-js-lib

一个实用的 JavaScript 工具库,提供了日常开发中常用的工具函数集合。

更新说明

版本号更新
v1.0.2新增睡眠函数(JsLib.common.sleep); 新增随机获取手机号、uuid方法 (JsLib.string.get_mobileJsLib.string.get_uuid); 新增比较方法(JsLib.common.equal)
v1.0.1新增合并对象方法参数(指定软、硬合并);新增对象属性过滤方法(JsLib.object.filter_prop)
v1.0.0修复bug;优化代码结构;完善声明文件;(稳定的版本)
v0.0.11新增了数组删除元素方法(JsLib.array.del_el)
v0.0.10-stable修复trans_params末尾多余&符号;新增注册方法覆盖警告;新增数组分页方法(JsLib.array.paginate);
v0.0.9新增按需引入; 修复dayjs方法无法传递参数;
v0.0.8新增对象合并方法(JsLib.object.merge);新增数组去重方法(JsLib.array.unique);新增数组扁平化转树结构方法(JsLib.array.to_tree);完善声明文件;

安装

npm install func-js-lib

使用方法

import JsLib from 'func-js-lib';
// 或者 
const JsLib = require('func-js-lib')
// 支持每个模块的按需引入
import {
  IsKit,
  CommonKit,
  CheckKit,
  StringKit,
  ArrayKit,
  UrlKit,
  NumberKit,
  PoolingKit,
  ObjectKit,
  DayJs
} from "func-js-lib";
// 示例
IsKit.empty("");
CheckKit.password("xxxxxx");
// ...

功能模块

1. 类型判断 (is)

用于判断数据类型的工具函数集合。

  • 是否为空
  • 是否不为空
  • 是否为数组
  • 是否为对象
  • 是否为字符串
// 判断是否为空
JsLib.is.empty("");      // true
JsLib.is.empty([]);      // true
JsLib.is.empty({});      // true

// 判断是否不为空
JsLib.is.not_empty("test");  // true

// 判断是否为数组
JsLib.is.arr([1, 2, 3]);    // true

// 判断是否为对象
JsLib.is.obj({});           // true

// 判断是否为字符串
JsLib.is.str(""); // true

2. 数据验证 (check)

提供各种数据格式的验证功能。

  • 密码指定格式校验
  • url正确性校验
  • 邮箱校验
  • 手机号/固话校验
  • 大陆身份证校验
// 密码验证(8-16位,包含大小写字母、数字和特殊符号)
/* 默认配置
    {
        minLength: 8, // 最小长度
        maxLength: 16,// 最大长度
        requireUppercase: true, // 是否包含大写
        requireLowercase: true, // 是否包含小写
        requireNumber: true, // 是否包含数字
        requireSpecial: true, // 是否包含特殊符号
    }
 */
JsLib.check.password("Abc123!@#");  // true

// URL 验证
JsLib.check.url("https://example.com");  // true

// 邮箱验证
JsLib.check.email("test@example.com");   // true

// 手机号验证
JsLib.check.phone("13812345678");        // true
JsLib.check.phone("010-12345678", "landline");  // true

// 身份证验证
JsLib.check.id_card("110101199001011234");  // true

3. 日期工具 (day)

基于 dayjs 的日期处理工具。

import JsLib from "func-js-lib";
// 或者
import { DayJs } from "func-js-lib";

console.log(JsLib.day(1619670000000).format("YYYY-MM-DD HH:mm:ss"))
console.log(JsLib.day(new Date()).subtract(7, 'days').format("YYYY-MM-DD"))

console.log(DayJs(1619670000000).format("YYYY-MM-DD HH:mm:ss"))
console.log(DayJs(new Date()).subtract(7, 'days').format("YYYY-MM-DD"))

4. URL 参数处理 (url)

处理 URL 参数的工具函数。

  • 地址栏参数转对象
  • 对象转地址栏参数
// 解析 URL 参数
const url = 'https://example.com?name=test&obj[name]=test&obj[age]=23&hobbies[]=reading&hobbies[]=sports';
JsLib.url.get_params(url);
// 输出:
// {
//   name: 'test',
//   obj: { name: 'test', age: '23' },
//   hobbies: ['reading', 'sports']
// }

// 获取单个参数
const name = JsLib.url.get_param("name", "http://localhost:8080?name=zhangs&age=23");
console.log(name);// zhangs

// 对象转 URL 参数
const params = {
  name: "张三",
  age: 25,
  hobbies: {
    hobby1: "读书",
    hobby2: "运动"
  }
};
JsLib.url.trans_params(params);
// 输出: "name=张三&age=25&hobbies[hobby1]=读书&hobbies[hobby2]=运动"

5. 字符串处理 (string)

  • 去除空格
  • 字符串脱敏
  • 随机获取手机号
  • 随机获取uuid
// 去除空格
JsLib.string.trim("  hello  ");             // "hello"
JsLib.string.trim("  hello  ", "left");     // "hello  "
JsLib.string.trim("  hello  ", "right");    // "  hello"
JsLib.string.trim("  hello  ", "all");      // "hello"

// 字符串脱敏
JsLib.string.desensitization("13812345678", 3, 4);  // "138****5678"

JsLib.string.get_mobile();
JsLib.string.get_uuid();

6. 数组处理 (array)

  • 补充数组到指定长度
  • 数组去重
  • 扁平化数组转树结构
  • 数组分页
  • 数组删除指定元素
// 补充数组到指定长度
JsLib.array.fill([1, 2], 4, 0);  // [1, 2, 0, 0]
// 支持对象补充
const fill = JsLib.array.fill([{name: 'zs'}], 3, {name: '-'});
console.log(fill); // [ { name: 'zs' }, { name: '-' }, { name: '-' } ]

// 数组去重
const arr = JsLib.array.unique([1, 1, 1, 3, 4, 5, 6, 5]);
console.log(arr); // [ 1, 3, 4, 5, 6 ]

// 扁平化数组转树结构
const toTree = JsLib.array.to_tree(
  [
    {id: 1, parentId: null, name: 'A'},
    {id: 2, parentId: 1, name: 'B'},
    {id: 3, parentId: 1, name: 'C'},
    {id: 4, parentId: 2, name: 'D'}
  ]
);

// 数组分页
let data = []
for (let i = 1 ; i <= 30 ; i++) {
  data.push(i);
}
let page1 = JsLib.array.paginate(data, 1, 10);

let page2 = JsLib.array.paginate(data, 2, 10);
let page3 = JsLib.array.paginate(data, 3, 10);
let page4 = JsLib.array.paginate(data, 4, 10);
console.log(page1, page2, page3, page4);
/*
打印:
[
  1, 2, 3, 4,  5,
  6, 7, 8, 9, 10
]
[
  11, 12, 13, 14, 15,
  16, 17, 18, 19, 20
]
[
  21, 22, 23, 24, 25,
  26, 27, 28, 29, 30
]
[]
 */

// 数组删除元素
const array1 = [
  {id: 1, name: 'John'},
  {id: 2, name: 'Alice'},
  {id: 3, name: 'Bob'},
  1, 2, 3, "hello", "world"
];

// 测试用例1:删除基本数据类型
console.log(JsLib.array.del_el(array1, 2)); // [{ id: 1, name: 'John' }, { id: 3, name: 'Bob' },1,3 "hello", "world"]

// 测试用例2:删除引用数据类型对象
const target1 = {id: 2, name: 'Alice'};
console.log(JsLib.array.del_el(array1, target1)); // [{ id: 1, name: 'John' }, { id: 3, name: 'Bob' }, 1, 2, 3, "hello", "world"]

// 测试用例3:删除引用数据类型,根据属性 key 'id' 进行删除
const target2 = {id: 2};
console.log(JsLib.array.del_el(array1, target2, 'id')); // [{ id: 1, name: 'John' }, { id: 3, name: 'Bob' }, 1, 2, 3, "hello", "world"]

// 测试用例4:删除字符串 "hello"
console.log(JsLib.array.del_el(array1, "hello")); // [{ id: 1, name: 'John' }, { id: 2, name: 'Alice' }, { id: 3, name: 'Bob' }, 1, 2, 3, "world"]

7. 数字处理 (number)

  • 获取指定范围随机数
// 获取 0 到 10 之间的随机整数
const randomNum1 = JsLib.number.random(10);
console.log(randomNum1); // 例如:8

// 获取 -10 到 10 之间的随机整数
const randomNum2 = JsLib.number.random(-10, 10);
console.log(randomNum2); // 例如:-5

// 获取 -20 到 -5 之间的随机整数
const randomNum3 = JsLib.number.random(-20, -5);
console.log(randomNum3); // 例如:-18

// 获取 0 到 50 之间的随机整数(只有一个参数,min 默认为 0)
const randomNum4 = JsLib.number.random(50);
console.log(randomNum4); // 例如:35

// 获取 -50 到 0 之间的随机整数(min 为负数,max 为正数)
const randomNum5 = JsLib.number.random(-50, 0);
console.log(randomNum5); // 例如:-15

8. 通用工具 (common)

  • 获取数据类型
  • 防抖函数
  • 节流函数
  • 深拷贝
  • 睡眠函数
  • 深度比较函数
// 获取数据类型;返回类型 null | undefined | array | date | regexp | function | string | number | object
JsLib.common.get_type([1, 2, 3]);  // "array"

// 防抖函数
const debouncedFn = JsLib.common.debounce(() => {
  console.log('执行操作');
}, 1000);

// 节流函数
const throttledFn = JsLib.common.throttle(() => {
  console.log('执行操作');
}, 1000);

// 深拷贝
const deepClone = JsLib.common.deep_clone([
  {id: 1, name: 'John'},
  {id: 2, name: 'Alice'},
  {id: 3, name: 'Bob'},
  1, 2, 3, "hello", "world"
]);
console.log(deepClone === array1); // false

// 睡眠
await JsLib.common.sleep(300);

// 比较
console.log(JsLib.common.equal(1, 1)); // true
console.log(JsLib.common.equal(1, "2")); //false
console.log(JsLib.common.equal({a: 1}, {a: 1})); //true
console.log(JsLib.common.equal([1, 2, 3], [1, 2, 3])); //true

9. 自定义扩展

可以通过 register 方法注册自定义功能。

// js-lib.js
import JsLib from "func-js-lib"
// 注册自定义方法
JsLib.register('custom', 'sayHello', (name) => `Hello, ${name}!`);
// 添加自定义各种方法...
export default JsLib;

// ------------------

// 再项目中别的文件引入使用
import JsLib from "./js-lib.js"

// 使用自定义方法
JsLib.custom.sayHello("World");  // "Hello, World!"

10. 轮询工具类(polling)

await JsLib.polling.start(() => {
  console.log("233")
}, 1000, true); // 1,函数 2,间隔 3,是否立即执行

setTimeout(() => {
  JsLib.polling.stop(); // 停止轮询
}, 5000)

11. 对象处理 (object)

  • 对象合并
  • 对象属性过滤
// 合并对象
const object = JsLib.object.merge({a: 1, b: 3}, {a: 2, b: 4});
console.log(object); //{ a: 2, b: 4 }
// 指定合并类型
// 合并类型:hard 硬合并(包含2者所有属性)、soft 软合并(只包含target对象的属性) 默认硬合并
const object = JsLib.object.merge({a: 1, b: 3}, {a: 2, c: 4}, "soft"); // {a:2,b:3}

// 过滤对象
const object = JsLib.object.filter_prop({a: 1, b: 3, c: 4}, ['a', 'c']); // {b:3}

注意事项

  1. 该库支持importrequire导入方式
  2. 日期处理功能依赖 dayjs,会自动安装此依赖
  3. URL 参数处理支持浏览器环境以及node环境
1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

7 months ago

0.0.11

7 months ago

0.0.10-stable

7 months ago

0.0.9

7 months ago

0.0.10

7 months ago

0.0.8

7 months ago

0.0.7

7 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.1

7 months ago

0.0.2

7 months ago