xjp-js-array-string
v1.0.0
Published
A collection of JavaScript array and string methods with examples
Maintainers
Readme
xjp-js-array-string
xjp-js-array-string 是一个全面的 JavaScript 工具库,专注于提供丰富的数组、字符串、对象数组操作以及精确数值计算功能。本库采用模块化设计,将常用方法分类整理,使开发者能够更加便捷地处理各种数据操作场景。
特性
- 分类清晰:所有方法按功能分类组织,便于查找和使用
- 参数化设计:支持传入动态数据源,提高代码复用性
- 精确计算:集成 Decimal.js,解决 JavaScript 浮点数计算精度问题
- 实用性强:包含大量实际开发中常用的工具方法
- 易于扩展:模块化结构便于后续添加新功能
模块概览
1. 数组方法 (arrayMethods)
分类:
create:数组创建相关方法modify:数组增删改查操作search:数组查找相关方法iterate:数组迭代方法order:数组排序和反转combine:数组连接和分割other:其他常用数组操作
主要方法:
create.emptyArray():创建空数组create.arrayOf(...items):使用指定元素创建数组create.arrayFrom(arrayLike):从类数组对象或可迭代对象创建数组modify.push(arr, ...items):向数组末尾添加元素modify.insertAt(arr, index, item):在指定位置插入元素search.indexOf(arr, value):查找元素索引iterate.map(arr, callback):映射数组元素iterate.filter(arr, callback):过滤数组元素order.sort(arr, comparator):排序数组combine.concat(arr1, arr2):连接数组other.unique(arr):移除数组重复元素other.shuffle(arr):随机打乱数组other.chunk(arr, size):将数组分割成指定大小的块other.isEqual(arr1, arr2):深度比较两个数组是否相等other.difference(arr1, arr2):计算两个数组的差集other.intersection(arr1, arr2):计算两个数组的交集other.deepFlatten(arr):深度扁平化数组
2. 字符串方法 (stringMethods)
分类:
create:字符串创建相关方法basic:字符串基本操作search:字符串查找方法extract:字符串截取方法transform:字符串转换方法replace:字符串替换方法split:字符串分割和连接other:其他常用字符串操作
主要方法:
create.template(strings, ...values):创建模板字符串basic.length(str):获取字符串长度basic.charAt(str, index):获取指定位置的字符search.indexOf(str, searchValue):查找子字符串索引search.includes(str, searchValue):检查是否包含子字符串extract.slice(str, start, end):截取字符串transform.toLowerCase(str):转换为小写transform.toUpperCase(str):转换为大写transform.trim(str):去除首尾空白replace.replace(str, searchValue, replaceValue):替换字符串split.split(str, separator):分割字符串other.capitalize(str):首字母大写other.camelCase(str):转换为驼峰命名other.kebabCase(str):转换为短横线命名other.snakeCase(str):转换为下划线命名other.escapeHTML(str):转义 HTML 特殊字符other.unescapeHTML(str):还原转义的 HTML 特殊字符other.stripTags(str):去除 HTML 标签other.isEmail(str):验证邮箱格式other.isUrl(str):验证 URL 格式
3. 对象数组方法 (objectArrayMethods)
分类:
sort:对象数组排序方法search:对象数组查找方法extract:对象数组属性提取stats:对象数组统计方法transform:对象数组转换方法
主要方法:
sort.byProperty(arr, property):按属性排序sort.byNestedProperty(arr, path):按嵌套属性排序sort.byMultipleProperties(arr, sortOptions):按多个属性排序search.findById(arr, id):根据 ID 查找对象search.findByProperty(arr, property, value):根据属性值查找对象search.filterByProperty(arr, property, value):根据属性值过滤对象extract.pluck(arr, property):提取指定属性的值extract.groupByProperty(arr, property):按属性分组stats.sumByProperty(arr, property):按属性求和stats.averageByProperty(arr, property):按属性求平均值transform.toMap(arr, keyProperty, valueProperty):转换为 Maptransform.toObject(arr, keyProperty, valueProperty):转换为对象transform.mergeArrays(arr1, arr2, keyProperty):合并对象数组transform.paginate(arr, page, pageSize):对对象数组分页transform.arrayToTree(arr, options):将平面数组转换为树形结构transform.createSnapshot(arr):创建对象数组的深拷贝快照
4. 精确计算 (decimalCalculation)
分类:
basic:基本精确计算array:数组精确计算objectArray:对象数组精确计算financial:财务相关计算
主要方法:
basic.add(a, b):精确加法basic.subtract(a, b):精确减法basic.multiply(a, b):精确乘法basic.divide(a, b, precision):精确除法array.sum(arr):数组精确求和array.average(arr, precision):数组精确平均值objectArray.sumByProperty(arr, property):按属性精确求和objectArray.averageByProperty(arr, property, precision):按属性精确平均值financial.currencyFormat(amount):货币格式化financial.percentage(part, total):计算百分比financial.discount(price, discountRate):计算折扣financial.compoundInterest(principal, rate, years):计算复利financial.presentValue(futureValue, rate, years):计算现值financial.taxCalculation(amount, taxRate):计算税额
安装
npm install xjp-js-array-string使用示例
数组方法
const { arrayMethods } = require('xjp-js-array-string');
// 创建数组
const numbers = arrayMethods.create.arrayOf(1, 2, 3, 4, 5);
// 添加元素
const extendedNumbers = arrayMethods.modify.push(numbers, 6, 7);
console.log(extendedNumbers); // [1, 2, 3, 4, 5, 6, 7]
// 查找元素
const index = arrayMethods.search.indexOf(numbers, 3);
console.log(index); // 2
// 映射元素
const doubled = arrayMethods.iterate.map(numbers, n => n * 2);
console.log(doubled); // [2, 4, 6, 8, 10]
// 数组去重
const uniqueNumbers = arrayMethods.other.unique([1, 2, 2, 3, 3, 3]);
console.log(uniqueNumbers); // [1, 2, 3]
// 数组比较
const isEqual = arrayMethods.other.isEqual([1, 2, 3], [1, 2, 3]);
console.log(isEqual); // true
// 使用数组方法
const fruits = ['apple', 'banana', 'orange'];
fruits.forEach(fruit => console.log(fruit));
// 使用字符串方法
const str = 'Hello, World!';
const upperCaseStr = str.toUpperCase();
// 使用对象数组方法
const users = [{ id: 1, name: 'Alice', age: 25 }, { id: 2, name: 'Bob', age: 30 }];
const sortedByAge = [...users].sort((a, b) => a.age - b.age);
// 使用精确计算
const { Decimal } = decimalCalculation;
const preciseSum = [0.1, 0.2, 0.3].reduce((acc, curr) => {
return new Decimal(acc).plus(new Decimal(curr));
}, new Decimal(0)).toNumber(); // 正确得到0.6注意事项
- 本包主要用于学习和参考JavaScript的数组和字符串方法
- 实际项目中可以直接使用JavaScript内置的这些方法,无需安装本包
- 更多详细用法请参考JavaScript官方文档
