@bondsports/general

中文文档
0.2.66发布于 2026/10/12ISCTypeScriptCommonJS近 30 天下载 7456

常规

中文使用文档
文档目录9 节
  1. 概览
  2. 安装
  3. 常数
  4. 数字
  5. 矩阵函数
  6. 字符串函数
  7. 随机函数
  8. 价格函数
  9. 一般职能

@ bondsports/ 常规

概览

这个 常规 软件包是一个将基本常数,enum,接口和函数集中起来的实用模块,为多个项目提供一套统一的工具。 通过使这些公用事业标准化,该软件包确保一致性,减少冗余,并简化发展。 它允许开发者专注于核心业务逻辑,同时依赖于一个可再用,可维护,高效的功能的坚实基础.

安装

你可以安装 @bondsports/general 通过 npm 软件包 :

npm install @bondsports/general

常数


概览

这个 consts.ts 文件定义了整个应用程序中用于标准化操作的各种常数,例如处理数组、处理数量、格式化价格和限制字符串长度。 这些常数有助于确保不同模块的一致性和可靠性.

  1. DEFAULT_CHUNK_SIZE

    • 说明:用于并行数组处理的默认块大小.

    • 数值数字 : 10

    • 用法示例数字 :

      import { DEFAULT_CHUNK_SIZE } from '@bondsports/general';
      
      const chunks = splitArrayIntoChunks(dataArray, DEFAULT_CHUNK_SIZE);
      
  2. DEFAULT_QUANTITY

    • 说明: 默认数量值.

    • 数值数字 : 1

    • 用法示例数字 :

      import { DEFAULT_QUANTITY } from '@bondsports/general';
      
      function calculateTotal(quantity = DEFAULT_QUANTITY) {
        return quantity * pricePerItem;
      }
      
  3. PRICE_DECIMAL_PLACES

    • 说明:用于价格的小数位数.

    • 数值数字 : 2

    • 用法示例数字 :

      import { PRICE_DECIMAL_PLACES } from '@bondsports/general';
      
      function formatPrice(price: number) {
        return price.toFixed(PRICE_DECIMAL_PLACES);
      }
      
  4. QUANTITY_DECIMAL_PLACES

    • 说明: 数量值所用的小数位数.

    • 数值数字 : 5

    • 用法示例数字 :

      import { QUANTITY_DECIMAL_PLACES } from '@bondsports/general';
      
      function formatQuantity(quantity: number) {
        return quantity.toFixed(QUANTITY_DECIMAL_PLACES);
      }
      
  5. STRING_DEFAULT_LIMIT

    • 说明:字符串的默认限制.

    • 数值数字 : 255

    • 用法示例数字 :

      import { STRING_DEFAULT_LIMIT } from '@bondsports/general';
      
      function validateStringLength(str: string) {
        return str.length <= STRING_DEFAULT_LIMIT;
      }
      

数字


概览

这个 enums.ts 文件定义了在整个应用程序中使用的几个计数符(enum)。 这些图表有助于在涉及共同地位、HTTP地位代码和定价领域时使价值标准化和提供清晰度.

  1. PromiseStatusEnum

    • 说明: Enum代表承诺状态.

    • 数值数字 :

      • FULFILLED = 'fulfilled'
      • REJECTED = 'rejected'
    • 用法示例数字 :

      import { PromiseStatusEnum } from '@bondsports/general';
      
      function handlePromiseStatus(status: PromiseStatusEnum) {
        if (status === PromiseStatusEnum.FULFILLED) {
          console.log('The promise was fulfilled.');
        } else if (status === PromiseStatusEnum.REJECTED) {
          console.error('The promise was rejected.');
        }
      }
      
  2. HttpStatus

    • 说明: Enum代表HTTP状态代码.

    • 数值数字 :

      • 包括常见的HTTP状态代码,例如:
        • OK = 200
        • CREATED = 201
        • BAD_REQUEST = 400
        • NOT_FOUND = 404
        • INTERNAL_SERVER_ERROR = 500
    • 用法示例数字 :

      import { HttpStatus } from '@bondsports/general';
      
      function handleHttpResponse(status: HttpStatus) {
        switch (status) {
          case HttpStatus.OK:
            console.log('Request was successful.');
            break;
          case HttpStatus.NOT_FOUND:
            console.error('Resource not found.');
            break;
          case HttpStatus.INTERNAL_SERVER_ERROR:
            console.error('Internal server error.');
            break;
          default:
            console.error('Unhandled status code:', status);
        }
      }
      
  3. PriceFieldEnum

    • 说明: Enum 代表用于定价的字段.

    • 数值数字 :

      • PRICE = 'price'
      • TOTAL_PRICE = 'totalPrice'
      • AMOUNT = 'amount'
      • PAID_AMOUNT = 'paidAmount'
      • FEE_AMOUNT = 'feeAmount'
      • REFUND_AMOUNT = 'refundAmount'
      • DISCOUNT_AMOUNT = 'discountAmount'
      • DISPLAY_FULL_PRICE = 'displayFullPrice'
      • DISPLAY_TOTAL_PRICE_WITH_TAX = 'displayTotalPriceWithTax'
    • 用法示例数字 :

      import { PriceFieldEnum } from '@bondsports/general';
      
      function calculateFieldTotal(items: any[], field: PriceFieldEnum) {
        return items.reduce((sum, item) => sum + item[field], 0);
      }
      
      const items = [
        { price: 100, totalPrice: 110 },
        { price: 200, totalPrice: 220 }
      ];
      
      const total = calculateFieldTotal(items, PriceFieldEnum.TOTAL_PRICE);
      console.log('Total Price:', total);  // Output: Total Price: 330
      

矩阵函数


概览

这个 array.util.ts 文件提供了一套用于操纵和管理数组的实用函数。 这些功能包括平整嵌入式结构的操作,过滤出独特的项目,有条件地将元素插入到数组中.

  1. recursiveFlattenArray

    • 说明: 递归平整基于指定属性的一系列对象.

    • 参数数字 :

      • array:要平整的阵列.
      • properties: 以平整数组的属性.
    • 回返:平面阵列.

    • 用法示例数字 :

      import { recursiveFlattenArray } from '@bondsports/general';
      
      const nestedArray = [{ id: 1, children: [{ id: 2, children: [] }] }];
      const flatArray = recursiveFlattenArray(nestedArray, ['children']);
      console.log(flatArray);
      // Output: [{ id: 1, children: [{ id: 2, children: [] }] }, { id: 2, children: [] }]
      
  2. recursiveFlattenObject

    • 说明: 递归地取出指定的属性来平分对象.

    • 参数数字 :

      • entity:要平分的对象.
      • properties: 从对象中提取的属性.
    • 回返:一阵平地物体.

    • 用法示例数字 :

      import { recursiveFlattenObject } from '@bondsports/general';
      
      const nestedObject = { id: 1, children: { id: 2, children: {} } };
      const flatObject = recursiveFlattenObject(nestedObject, ['children']);
      console.log(flatObject);
      // Output: [{ id: 1, children: { id: 2, children: {} } }, { id: 2, children: {} }]
      
  3. insertIf

    • 说明: 如果条件属实,将元素插入数组.

    • 参数数字 :

      • condition:检查的条件.
      • ...elements: 要插入数组的元素.
    • 回返: 由此产生的数组.

    • 用法示例数字 :

      import { insertIf } from '@bondsports/general';
      
      const items = [
          ...insertIf(true, 1, 2, 3),
          ...insertIf(false, 1, 2, 3)
      ];
      console.log(items); // Output: [1, 2, 3]
      
  4. uniqueIds

    • 说明: 从提供的ID数组返回一系列独特的ID.

    • 参数数字 :

      • idsArrays: 需要合并并过滤的ID序列.
    • 回返:一组独特的ID.

    • 用法示例数字 :

      import { uniqueIds } from '@bondsports/general';
      
      const ids = uniqueIds([1, 2, 3], [3, 4, 5]);
      console.log(ids); // Output: [1, 2, 3, 4, 5]
      
  5. uniqueItems

    • 说明: 从多个数组返回一组独特的项目.

    • 参数数字 :

      • arrays: 合并并从中取出独有项目的数组.
    • 回返: 一系列独特的项目.

    • 用法示例数字 :

      import { uniqueItems } from '@bondsports/general';
      
      const items = uniqueItems([1, 2, 3], [2, 3, 4], [4, 5, 6]);
      console.log(items); // Output: [1, 2, 3, 4, 5, 6]
      
  6. uniqueObjectsList

    • 说明: 由多个对象阵列的给定密钥返回一组独特的对象.

    • 参数数字 :

      • groupKeys:我们要从中提取对象的独特密钥.
      • arrays: 合并并从中取出独有项目的数组.
    • 回返: 一组独特的对象.

    • 用法示例数字 :

      import { uniqueObjectsList } from '@bondsports/general';
      
      const objects = [
        { id: 1, name: 'John' },
        { id: 2, name: 'Jane' },
        { id: 1, name: 'John' }
      ];
      const uniqueObjects = uniqueObjectsList(['id'], objects);
      console.log(uniqueObjects); // Output: [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }]
      
  7. arraysEquals

    • 说明:检查多个数组是否等于第一个数组.

    • 参数数字 :

      • firstArray: 第一个比较阵列.
      • ...arrays: 要比较的额外数组.
    • 回返: 如果所有数组是等同的, 返回真实的, 否则是虚假的 .

    • 用法示例数字 :

      import { arraysEquals } from '@bondsports/general';
      
      const isEqual = arraysEquals([1, 2, 3], [1, 2, 3], [1, 2, 3]);
      console.log(isEqual); // Output: true
      
  8. isArrayEmpty

    • 说明:检查一个数组是否为空.

    • 参数数字 :

      • array: 要检查的数组 .
    • 回返: 如果数组是空的, 否则是假的 .

    • 用法示例数字 :

      import { isArrayEmpty } from '@bondsports/general';
      
      const emptyCheck = isArrayEmpty([]);
      console.log(emptyCheck); // Output: true
      
  9. insertToMapArray

    • 说明: 使用指定的密钥,将一个值插入到地图内的数组中。 如果密钥在映射中不存在,则会创建一个与密钥相关的新数组.

    • 参数数字 :

      • map: 插入值的地图.
      • key: 将值与值挂钩的密钥.
      • value: 插入到数组中的值.
    • 回返: 无.

    • 用法示例数字 :

      import { insertToMapArray } from '@bondsports/general';
      
      const map = new Map();
      insertToMapArray(map, 'key1', 'value1');
      console.log(map.get('key1')); // Output: ['value1']
      
  10. arrayToMap

    • 说明: 转换带有一个 id 字段输入地图 id 作为关键,实体作为价值.

    • 参数数字 :

      • array: 要转换的实体数组.
    • 回返: 生成的地图 id 作为关键,实体作为价值.

    • 用法示例数字 :

      import { arrayToMap } from '@bondsports/general';
      
      const entities = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }];
      const map = arrayToMap(entities);
      console.log(map.get(1)); // Output: { id: 1, name: 'John' }
      
  11. isIntersecting

    • 说明:检查两个阵列是否有共同的元素.

    • 参数数字 :

      • array: 第一个比较阵列.
      • other: 第二组比较.
    • 回返: 如果数组有共同的元素, 则不正确 .

    • 用法示例数字 :

      import { isIntersecting } from '@bondsports/general';
      
      const hasCommon = isIntersecting([1, 2, 3], [3, 4, 5]);
      console.log(hasCommon); // Output: true
      

字符串函数


概览

这个 string.util.ts 文件提供了一套用于操纵和验证字符串的功能。 这些功能包括将字符串转换为标题大小写,生成随机字符串,格式化名称,裁剪白空格,以及验证JSON数据等操作.

  1. titleCase

    • 说明: 将字符串转换为标题大小写.

    • 参数数字 :

      • text:输入字符串.
    • 回返:字符串转换为标题大小写.

    • 用法示例数字 :

      import { titleCase } from '@bondsports/general';
      
      const title = titleCase('hello world');
      console.log(title); // Output: "Hello World"
      
  2. generateRandomString

    • 说明:根据提供的种子,长度和字符集生成随机字符串.

    • 参数数字 :

      • seed:用于生成随机字符串的种子值.
      • length:要生成的随机字符串的长度.
      • charSet: 随机字符串将构成的字符集.
    • 回返:随机生成的字符串.

    • 用法示例数字 :

      import { generateRandomString } from '@bondsports/general';
      
      const randomString = generateRandomString('seed', 10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
      console.log(randomString); // A random 10-character string
      
  3. fullName

    • 说明:通过合并出名和姓来产生全名.

    • 参数数字 :

      • firstName: 第一名 (可选).
      • lastName: 姓 (可选).
    • 回返:全名为弦.

    • 用法示例数字 :

      import { fullName } from '@bondsports/general';
      
      const name = fullName('John', 'Doe');
      console.log(name); // Output: "John Doe"
      
  4. trimString

    • 说明:从字符串中导出并跟随出白空间.

    • 参数数字 :

      • text:弦去修饰.
    • 回返:被修剪的字符串,或 null 如果输入字符串为空或 null。 。 。 .

    • 用法示例数字 :

      import { trimString } from '@bondsports/general';
      
      const trimmed1 = trimString('  Hello World  ');
      console.log(trimmed1); // Output: "Hello World"
      
      const trimmed2 = trimString('  ');
      console.log(trimmed2); // Output: null
      
  5. isJSON

    • 说明:检查给定的数据是否是一个有效的JSON字符串.

    • 参数数字 :

      • data: 需要检查的数据.
    • 回返:一个布尔表示数据是否为有效的JSON字符串.

    • 用法示例数字 :

      import { isJSON } from '@bondsports/general';
      
      const valid = isJSON('{"name": "John"}');
      console.log(valid); // Output: true
      
  6. isString

    • 说明:检查一个值是否为字符串.

    • 参数数字 :

      • value: 要检查的值 .
    • 回返: 回归 true 如果值是一个字符串, false 否则.

    • 用法示例数字 :

      import { isString } from '@bondsports/general';
      
      const check = isString('Hello World');
      console.log(check); // Output: true
      

随机函数


概览

这个 random.util.ts 文件提供了生成伪随机数的功能。 这些功能包括生成带有可选种子值的随机数字并使用MurmurHash3算法创建出被种子的伪随机数生成器.

  1. murmurHash3Generator

    • 说明:使用MurmurHash3算法生成一个被种子的伪随机数生成器.

    • 参数数字 :

      • seed:假随机数生成器的种子.
    • 回返:生成伪随机数的函数.

    • 用法示例数字 :

      import { murmurHash3Generator } from '@bondsports/general';
      
      const randomGenerator = murmurHash3Generator('my-seed');
      const randomNumber = randomGenerator();
      console.log(randomNumber); // Output: A pseudo-random number based on the seed
      
  2. randomNumber

    • 说明:在指定区域之间生成随机数字.

    • 参数数字 :

      • max:范围的最大值(含).
      • min: 区域最小值( 默认: 0) .
      • seed: 一个可选种子值来生成一个决定性的随机数.
    • 回返:一个函数,当调用时,生成指定范围内的随机数.

    • 用法示例数字 :

      import { randomNumber } from '@bondsports/general';
      
      const getRandomNumber = randomNumber(100, 0, 'my-seed');
      console.log(getRandomNumber()); // Output: A deterministic random number between 0 and 100
      

价格函数


概览

这个 price.util.ts 文件提供了一套侧重于处理和验证价格和数量的公用功能。 这些功能包括比较价格,验证金额,四舍五入值和计算总额等操作.

  1. samePrice

    • 说明:检查两种价格是否相同.

    • 参数数字 :

      • firstPrice:第一个比较价.
      • secondPrice:第二个价格比较.
    • 回返数字 : True 如果价格是一样的, false 否则.

    • 用法示例数字 :

      import { samePrice } from '@bondsports/general';
      
      const isSame = samePrice(10.00, 10.00);
      console.log(isSame); // Output: true
      
  2. biggerPrice

    • 说明:检查第一个出价是否大于第二个出价.

    • 参数数字 :

      • bigger:更大的价格.
      • smaller:价格更小.
    • 回返数字 : True 如果第一个价格大于第二个价格, false 否则.

    • 用法示例数字 :

      import { biggerPrice } from '@bondsports/general';
      
      const isBigger = biggerPrice(15.00, 10.00);
      console.log(isBigger); // Output: true
      
      
  3. biggerAmount

    • 说明:检查第一个金额是否大于第二个金额.

    • 参数数字 :

      • bigger: 数额越大.
      • smaller: 数额较小.
    • 回返数字 : True 如果第一个数额大于第二个数额, false 否则.

    • 用法示例数字 :

      import { biggerAmount } from '@bondsports/general';
      
      const isBigger = biggerAmount(5.94, 3.17);
      console.log(isBigger); // Output: true
      
  4. validateAmount

    • 说明: 验证某一数额是否有效(不是无效或未定义).

    • 参数数字 :

      • amount: 验证的金额.
    • 回返数字 : True 如果金额有效, false 否则.

    • 用法示例数字 :

      import { validateAmount } from '@bondsports/general';
      
      validateAmount(99.99); // Return: true
      validateAmount(-10.00); // Return: true
      validateAmount(0); // Return: true
      validateAmount(null); // Return: false
      
  5. validatePrice

    • 说明: 验证一个价格是有效的(大于或等于零).

    • 参数数字 :

      • price:要验证的价格.
    • 回返数字 : True 如果价格有效, false 否则.

    • 用法示例数字 :

      import { validatePrice } from '@bondsports/general';
      
      validatePrice(20.00); // Return: true
      validatePrice(0); // Return: true
      validatePrice(-5.00); // Return: false
      
  6. validatePrices

    • 说明:验证一系列价格,检查每个价格是否有效(大于或等于零).

    • 参数数字 :

      • prices: 要验证的一系列价格.
    • 回返数字 : True 如果所有价格都有效, false 否则.

    • 用法示例数字 :

      import { validatePrices } from '@bondsports/general';
      
      const arePricesValid = validatePrices([10.00, 20.00, 30.00]);
      console.log(arePricesValid); // Output: true
      
  7. validateAmounts

    • 说明: 验证数组,检查每个价格是否有效(不是无效或未定义).

    • 参数数字 :

      • amounts: 需要验证的数组.
    • 回返数字 : True 如果所有金额都是有效的, false 否则.

    • 用法示例数字 :

      import { validateAmounts } from '@bondsports/general';
      
      const areAmountsValid = validateAmounts([10.00, -20.00, 30.00]);
      console.log(areAmountsValid); // Output: true
      
  8. toFixed

    • 说明:将数字绕入指定的精确度并返回结果.

    • 参数数字 :

      • num: 该数字为圆.
      • precision: 十进制位数.
    • 回返: 四舍五入数.

    • 用法示例数字 :

      import { toFixed } from '@bondsports/general';
      
      const roundedNumber = toFixed(10.123456, 3);
      console.log(roundedNumber); // Output: 10.123
      
  9. roundPriceCents

    • 说明:将价格四舍五入到最接近的一分.

    • 参数数字 :

      • price:价至圆.
    • 回返: 四舍五入的价格.

    • 用法示例数字 :

      import { roundPriceCents } from '@bondsports/general';
      
      const roundedPrice = roundPriceCents(10.123);
      console.log(roundedPrice); // Output: 10.12
      
  10. roundQuantity

    • 说明: 将给定的数量四舍五入到指定的小数位.

    • 参数数字 :

      • amount:等于圆.
    • 回返: 四舍五入的数额.

    • 用法示例数字 :

      import { roundQuantity } from '@bondsports/general';
      
      const roundedQty = roundQuantity(5.67891);
      console.log(roundedQty); // Output: 5.67891
      
  11. sameQuantity

    • 说明:函数检查两个数量是否相等.

    • 参数数字 :

      • firstQuantity: 比较的第一个数量.
      • secondQuantity:第二批比较.
    • 回返: 回归 true 如果数量大致相等(即其差额小于) 0.0001),否则返回 false。 。 。 .

    • 用法示例数字 :

      import { sameQuantity } from '@bondsports/general';
      
      const quantity1 = 5.00001;
      const quantity2 = 5.000012;
      
      const areQuantitiesEqual = sameQuantity(quantity1, quantity2);
      console.log(areQuantitiesEqual); // Output: true
      
  12. calculatePriceFromTotal

    • 说明:从总价格中计算出价格,同时考虑到数量和可选税分.

    • 参数数字 :

      • price: 单位价格.
      • taxPercent: 税率(可选).
      • quantity: 数量( 默认值 : DEFAULT_QUANTITY) (中文(简体) ).
    • 回返:计算出的价格.

    • 用法示例数字 :

      import { calculatePriceFromTotal } from '@bondsports/general';
      
      const price = calculatePriceFromTotal(110, 10);
      console.log(price); // Output: 100.00
      
  13. calculatePriceWithTax

    • 说明: 以税收计算价格.

    • 参数数字 :

      • price:物品的价格.
      • taxPercent: 适用税率(可选).
      • quantity: 项目数量(默认: DEFAULT_QUANTITY) (中文(简体) ).
    • 回返:计算出的价格与税.

    • 用法示例数字 :

      import { calculatePriceWithTax } from '@bondsports/general';
      
      const priceWithTax = calculatePriceWithTax(100, 10, 2);
      console.log(priceWithTax); // Output: 220.00
      
  14. calculateTotalPriceWithTax

    • 说明:计算包括税收在内的项目的总价格.

    • 参数数字 :

      • price:物品的价格.
      • isTaxInclusive:价格是否已经包括税收.
      • taxPercent: 如果 isTaxInclusive 实值 false。 。 。 .
      • quantity: 项目数量(默认: DEFAULT_QUANTITY) (中文(简体) ).
    • 回返:总价格,包括税收.

    • 用法示例数字 :

      import { calculateTotalPriceWithTax } from '@bondsports/general';
      
      const totalPrice = calculateTotalPriceWithTax(100, false, 10);
      console.log(totalPrice); // Output: 110.00
      
  15. calculateItemUnitPrice

    • 说明:根据给定的单价,普税状况,以及纳税百分比来计算项目单价.

    • 参数数字 :

      • unitPrice:税前的单价.
      • isTaxInclusive:标出价格是否包含税收的旗帜.
      • taxPercent: 税率.
    • 回返:计算出单价.

    • 用法示例数字 :

      import { calculateItemUnitPrice } from '@bondsports/general';
      
      const unitPrice = calculateItemUnitPrice(100, true, 10);
      console.log(unitPrice); // Output: 90.91
      
  16. calculateUnitPriceFromTotal

    • 说明:根据给定的总价格,普税状况,税率和数量计算出单价.

    • 参数数字 :

      • totalPrice:总价.
      • isTaxInclusive:标出价格是否包含税收的旗帜.
      • taxPercent: 税率.
      • quantity: 项目数量(默认: DEFAULT_QUANTITY) (中文(简体) ).
    • 回返:计算出单价.

    • 用法示例数字 :

      import { calculateUnitPriceFromTotal } from '@bondsports/general';
      
      const unitPrice = calculateUnitPriceFromTotal(330, true, 10, 3);
      console.log(unitPrice); // Output: 110.00
      
  17. calculateQuantityFromTotal

    • 说明:根据总额、单价和税率计算数量.

    • 参数数字 :

      • totalAmount:总金额.
      • unitPrice:项目单价.
      • isTaxInclusive:指该税是否计入单价.
      • taxPercentage: 税率(可选).
    • 回返: 计算的数量.

    • 用法示例数字 :

      import { calculateQuantityFromTotal } from '@bondsports/general';
      
      const quantity = calculateQuantityFromTotal(110, 100, true, 10);
      console.log(quantity); // Output: 1.00
      
  18. convertCentsToDollars

    • 说明: 将给定的价格以美分换算成美元.

    • 参数数字 :

      • price:价为分.
    • 回返:以美元计价.

    • 用法示例数字 :

      import { convertCentsToDollars } from '@bondsports/general';
      
      const dollars = convertCentsToDollars(10000);
      console.log(dollars); // Output: 100.00
      
  19. getRoundingSplit

    • 说明:计算所要求价格和实际价格之间的四舍五入.

    • 参数数字 :

      • requestedPrice: 要求的价格.
      • price:实际价格.
    • 回返: 包含分数和分数的对象.

    • 用法示例数字 :

      import { getRoundingSplit } from '@bondsports/general';
      
      const roundingSplit = getRoundingSplit(100.05, 100);
      console.log(roundingSplit); // Output: { splitsNumber: 5, splitAmount: 0.01 }
      
  20. splitPriceAmount

    • 说明: 将给定的数量分成多个部分.

    • 参数数字 :

      • amount:拟分出的数额.
      • splitsNumber: 所要拆分的次数( 默认数:1) .
    • 回返: 代表拆分数额的数字组.

    • 用法示例数字 :

      import { splitPriceAmount } from '@bondsports/general';
      
      const splits = splitPriceAmount(100, 3);
      console.log(splits); // Output: [33.33, 33.33, 33.34]
      
  21. calculateTotalPriceByPriceField

    • 说明: 以一系列项目中指定的价格字段计算总价.

    • 参数数字 :

      • items: 项目数组.
      • startPrice: 起始价格( 默认值 : 0) (中文(简体) ).
      • priceField: 用于计算的价格字段(默认值: PriceFieldEnum.PRICE) (中文(简体) ).
    • 回返:总价.

    • 用法示例数字 :

      import { calculateTotalPriceByPriceField } from '@bondsports/general';
      
      const items = [{ price: 100 }, { price: 200 }, { price: 300 }];
      const totalPrice = calculateTotalPriceByPriceField(items);
      console.log(totalPrice); // Output: 600.00
      
  22. calculateTotalPriceByPrices

    • 说明:通过汇总给定数组中的价格来计算总价.

    • 参数数字 :

      • prices:一系列价格.
      • startPrice: 计算起始价格(默认值: 0) (中文(简体) ).
    • 回返:总价.

    • 用法示例数字 :

      import { calculateTotalPriceByPrices } from '@bondsports/general';
      
      const prices = [100, 200, 300];
      const totalPrice = calculateTotalPriceByPrices(prices);
      console.log(totalPrice); // Output: 600.00
      
  23. calculateTotalQuantity

    • 说明:通过汇总给定的数量来计算总量.

    • 参数数字 :

      • quantities: 需要归纳的数量.
      • startQuantity: 计算起始数量(默认值: 0) (中文(简体) ).
    • 回返: 总量.

    • 用法示例数字 :

      import { calculateTotalQuantity } from '@bondsports/general';
      
      const quantities = [1, 2, 3];
      const totalQty = calculateTotalQuantity(quantities);
      console.log(totalQty); // Output: 6
      
  24. calculateDue

    • 说明:根据价格和已经支付的金额计算到期金额.

    • 参数数字 :

      • price:项目总价.
      • paidAmount: 已经支付的数额( 默认 : 0) (中文(简体) ).
    • 回返:到期金额.

    • 用法示例数字 :

      import { calculateDue } from '@bondsports/general';
      
      const amountDue = calculateDue(100, 50);
      console.log(amountDue); // Output: 50.00
      
  25. calculateItemDue

    • 说明: 根据其价格和已经支付的数额计算某一项目的应付款.

    • 参数数字 :

      • item:项目,它必须有一个 price 财务报告和审定财务报表 paidAmount 属性.
    • 回返:该物品的到期金额.

    • 用法示例数字 :

      import { calculateItemDue } from '@bondsports/general';
      
      const item = { price: 100, paidAmount: 50 };
      const amountDue = calculateItemDue(item);
      console.log(amountDue); // Output: 50.00
      

一般职能


概览

这个 general.util.ts 文件提供了一套多功能的通用功能,用于一个应用程序的各个部分。 这些功能包括处理承诺,与enums合作,验证值以及进行计算等操作.

  1. absoluteValue

    • 说明:计算一个数字的绝对值.

    • 参数数字 :

      • value:用来计算绝对值的数字.
    • 回返:输入数的绝对值.

    • 用法示例数字 :

      import { absoluteValue } from '@bondsports/general';
      
      const absValue = absoluteValue(-42);
      console.log(absValue); // Output: 42
      
  2. promiseAllSettled

    • 说明: 执行多个承诺并返回一系列确定的结果.

    • 参数数字 :

      • promises:承诺执行.
    • 回返:一个能解决一系列已实现的价值观的承诺.

    • 用法示例数字 :

      import { promiseAllSettled } from '@bondsports/general';
      
      const promises = [
        Promise.resolve(1),
        Promise.resolve(2),
        Promise.reject(new Error('Failed'))
      ];
      
      promiseAllSettled(...promises)
        .then(results => console.log(results))
        .catch(error => console.error(error));
      
  3. isInEnum

    • 说明:检查一个值是否为一个enum的有效成员.

    • 参数数字 :

      • enumType:要检查的电筒.
      • value: 要检查的值 .
    • 回返数字 : True 如果该值是该enum的有效成员, false 否则.

    • 用法示例数字 :

      import { isInEnum, HttpStatus } from '@bondsports/general';
      
      const isValid = isInEnum(HttpStatus, 200);
      console.log(isValid); // Output: true
      
  4. extractValuesFromEnumArray

    • 说明: 提取出一组元素中的值.

    • 参数数字 :

      • values: 要提取的数值阵列.
      • enumType: 用于过滤值的enum类型.
    • 回返: enum中存在的一系列数值.

    • 用法示例数字 :

      import {
          extractValuesFromEnumArray,
          HttpStatus
      } from '@bondsports/general';
      
      const validStatuses = extractValuesFromEnumArray([200, 404, 999], HttpStatus);
      console.log(validStatuses); // Output: [200, 404]
      
  5. useConditionalPromise

    • 说明: 执行基于承诺的基于布尔条件的操作.

    • 参数数字 :

      • operation:要执行的以承诺为基础的行动.
      • condition: 决定是执行操作还是使用倒计时的布尔条件.
      • fallback: 如果条件不实, 可选择的倒置函数将执行 .
    • 回返:一个能解决操作结果或倒置功能的保证.

    • 用法示例数字 :

      import { useConditionalPromise } from '@bondsports/general';
      
      const operation = () => Promise.resolve('Success');
      const fallback = () => 'Fallback';
      
      useConditionalPromise(operation, true, fallback)
        .then(result => console.log(result)); // Output: 'Success'
      
  6. validateFieldsToUpdate

    • 说明: 验证要在实体对象中更新的字段.

    • 参数数字 :

      • classType: 实体对象的构造函数.
      • entity: 原始实体对象.
      • fieldsToUpdate: 实体对象中要更新的字段.
    • 回返数字 : True 如果有字段需要更新, false 否则.

    • 用法示例数字 :

      import { validateFieldsToUpdate } from '@bondsports/general';
      
      class User {
        id: number;
        name: string;
        email: string;
      }
      
      const user = new User();
      user.id = 1;
      user.name = 'John';
      user.email = '[email protected]';
      
      const fieldsToUpdate = { name: 'John Doe' };
      
      const isValid = validateFieldsToUpdate(User, user, fieldsToUpdate);
      console.log(isValid); // Output: true
      
  7. getClassAttributes

    • 说明:获取类实例的属性.

    • 参数数字 :

      • classType: 类的构造函数.
    • 回返: 一系列属性名称.

    • 用法示例数字 :

      import { getClassAttributes } from './general.util';
      
      class Product {
        id: number;
        name: string;
        price: number;
      }
      
      const attributes = getClassAttributes(Product);
      console.log(attributes); // Output: ['id', 'name', 'price']
      
  8. parallelize

    • 说明:对数组中的每个项目并行执行指定的同步函数。 返回一个能解决一系列结果的承诺.

    • 参数数字 :

      • array:要处理的项目数组.
      • func: 每个项目执行的同步函数.
      • chunkSize: 每个块中要处理的项目数量(默认: DEFAULT_CHUNK_SIZE) (中文(简体) ).
    • 回返:一个能决定一系列结果的承诺.

    • 用法示例数字 :

      import { parallelize } from './general.util';
      
      async function processItems(items: any[]) {
        const results = await parallelize(items, async (item) => {
          // some async processing
          return item;
        });
        console.log(results);
      }
      

版本与发布记录

@bondsports/general 共发布 112 个版本,首次发布于 2024/02/19;过去 12 个月发布了 41 个版本。

版本发布日期距今
0.2.66latest2026/10/1226 天前
0.2.65-squad-c-1squad-c2026/10/041 个月前
0.2.652026/09/241 个月前
0.2.642026/09/221 个月前
0.2.632026/09/091 个月前
0.2.622026/08/162 个月前

其他发布通道:a-1 → 0.2.58-a-1,a-2 → 0.2.58-a-2,squad-c → 0.2.65-squad-c-1。

关于 @bondsports/general 的常见问题

@bondsports/general 是什么?

常规

如何安装 @bondsports/general?

在项目目录运行 npm install @bondsports/general,或使用 pnpm add / yarn add / bun add。

@bondsports/general 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bondsports/general 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bondsports/general 可以商用吗?

@bondsports/general 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bondsports/general 的下载量有多少?

根据 npm 官方下载统计,@bondsports/general 在 2026/10/09 – 2026/11/07 这 30 天内被下载 7,456 次,日均约 249 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bondsports/general 还在维护吗?

最新版本 0.2.66 发布于 2026/10/12(26 天前);至今共发布 112 个版本,其中过去 12 个月发布了 41 个;首次发布于 2024/02/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。