npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

xjp-js-array-string

v1.0.0

Published

A collection of JavaScript array and string methods with examples

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):转换为 Map
  • transform.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官方文档