@ bondsports/ 常规
概览
这个 常规 软件包是一个将基本常数,enum,接口和函数集中起来的实用模块,为多个项目提供一套统一的工具。 通过使这些公用事业标准化,该软件包确保一致性,减少冗余,并简化发展。 它允许开发者专注于核心业务逻辑,同时依赖于一个可再用,可维护,高效的功能的坚实基础.
安装
你可以安装 @bondsports/general 通过 npm 软件包 :
npm install @bondsports/general
常数
概览
这个 consts.ts 文件定义了整个应用程序中用于标准化操作的各种常数,例如处理数组、处理数量、格式化价格和限制字符串长度。 这些常数有助于确保不同模块的一致性和可靠性.
-
DEFAULT_CHUNK_SIZE-
说明:用于并行数组处理的默认块大小.
-
数值数字 :
10 -
用法示例数字 :
import { DEFAULT_CHUNK_SIZE } from '@bondsports/general'; const chunks = splitArrayIntoChunks(dataArray, DEFAULT_CHUNK_SIZE);
-
-
DEFAULT_QUANTITY-
说明: 默认数量值.
-
数值数字 :
1 -
用法示例数字 :
import { DEFAULT_QUANTITY } from '@bondsports/general'; function calculateTotal(quantity = DEFAULT_QUANTITY) { return quantity * pricePerItem; }
-
-
PRICE_DECIMAL_PLACES-
说明:用于价格的小数位数.
-
数值数字 :
2 -
用法示例数字 :
import { PRICE_DECIMAL_PLACES } from '@bondsports/general'; function formatPrice(price: number) { return price.toFixed(PRICE_DECIMAL_PLACES); }
-
-
QUANTITY_DECIMAL_PLACES-
说明: 数量值所用的小数位数.
-
数值数字 :
5 -
用法示例数字 :
import { QUANTITY_DECIMAL_PLACES } from '@bondsports/general'; function formatQuantity(quantity: number) { return quantity.toFixed(QUANTITY_DECIMAL_PLACES); }
-
-
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地位代码和定价领域时使价值标准化和提供清晰度.
-
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.'); } }
-
-
HttpStatus-
说明: Enum代表HTTP状态代码.
-
数值数字 :
- 包括常见的HTTP状态代码,例如:
OK = 200CREATED = 201BAD_REQUEST = 400NOT_FOUND = 404INTERNAL_SERVER_ERROR = 500
- 包括常见的HTTP状态代码,例如:
-
用法示例数字 :
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); } }
-
-
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 文件提供了一套用于操纵和管理数组的实用函数。 这些功能包括平整嵌入式结构的操作,过滤出独特的项目,有条件地将元素插入到数组中.
-
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: [] }]
-
-
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: {} }]
-
-
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]
-
-
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]
-
-
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]
-
-
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' }]
-
-
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
-
-
isArrayEmpty-
说明:检查一个数组是否为空.
-
参数数字 :
array: 要检查的数组 .
-
回返: 如果数组是空的, 否则是假的 .
-
用法示例数字 :
import { isArrayEmpty } from '@bondsports/general'; const emptyCheck = isArrayEmpty([]); console.log(emptyCheck); // Output: true
-
-
insertToMapArray-
说明: 使用指定的密钥,将一个值插入到地图内的数组中。 如果密钥在映射中不存在,则会创建一个与密钥相关的新数组.
-
参数数字 :
map: 插入值的地图.key: 将值与值挂钩的密钥.value: 插入到数组中的值.
-
回返: 无.
-
用法示例数字 :
import { insertToMapArray } from '@bondsports/general'; const map = new Map(); insertToMapArray(map, 'key1', 'value1'); console.log(map.get('key1')); // Output: ['value1']
-
-
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' }
-
-
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数据等操作.
-
titleCase-
说明: 将字符串转换为标题大小写.
-
参数数字 :
text:输入字符串.
-
回返:字符串转换为标题大小写.
-
用法示例数字 :
import { titleCase } from '@bondsports/general'; const title = titleCase('hello world'); console.log(title); // Output: "Hello World"
-
-
generateRandomString-
说明:根据提供的种子,长度和字符集生成随机字符串.
-
参数数字 :
seed:用于生成随机字符串的种子值.length:要生成的随机字符串的长度.charSet: 随机字符串将构成的字符集.
-
回返:随机生成的字符串.
-
用法示例数字 :
import { generateRandomString } from '@bondsports/general'; const randomString = generateRandomString('seed', 10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); console.log(randomString); // A random 10-character string
-
-
fullName-
说明:通过合并出名和姓来产生全名.
-
参数数字 :
firstName: 第一名 (可选).lastName: 姓 (可选).
-
回返:全名为弦.
-
用法示例数字 :
import { fullName } from '@bondsports/general'; const name = fullName('John', 'Doe'); console.log(name); // Output: "John Doe"
-
-
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
-
-
isJSON-
说明:检查给定的数据是否是一个有效的JSON字符串.
-
参数数字 :
data: 需要检查的数据.
-
回返:一个布尔表示数据是否为有效的JSON字符串.
-
用法示例数字 :
import { isJSON } from '@bondsports/general'; const valid = isJSON('{"name": "John"}'); console.log(valid); // Output: true
-
-
isString-
说明:检查一个值是否为字符串.
-
参数数字 :
value: 要检查的值 .
-
回返: 回归
true如果值是一个字符串,false否则. -
用法示例数字 :
import { isString } from '@bondsports/general'; const check = isString('Hello World'); console.log(check); // Output: true
-
随机函数
概览
这个 random.util.ts 文件提供了生成伪随机数的功能。 这些功能包括生成带有可选种子值的随机数字并使用MurmurHash3算法创建出被种子的伪随机数生成器.
-
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
-
-
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 文件提供了一套侧重于处理和验证价格和数量的公用功能。 这些功能包括比较价格,验证金额,四舍五入值和计算总额等操作.
-
samePrice-
说明:检查两种价格是否相同.
-
参数数字 :
firstPrice:第一个比较价.secondPrice:第二个价格比较.
-
回返数字 :
True如果价格是一样的,false否则. -
用法示例数字 :
import { samePrice } from '@bondsports/general'; const isSame = samePrice(10.00, 10.00); console.log(isSame); // Output: true
-
-
biggerPrice-
说明:检查第一个出价是否大于第二个出价.
-
参数数字 :
bigger:更大的价格.smaller:价格更小.
-
回返数字 :
True如果第一个价格大于第二个价格,false否则. -
用法示例数字 :
import { biggerPrice } from '@bondsports/general'; const isBigger = biggerPrice(15.00, 10.00); console.log(isBigger); // Output: true
-
-
biggerAmount-
说明:检查第一个金额是否大于第二个金额.
-
参数数字 :
bigger: 数额越大.smaller: 数额较小.
-
回返数字 :
True如果第一个数额大于第二个数额,false否则. -
用法示例数字 :
import { biggerAmount } from '@bondsports/general'; const isBigger = biggerAmount(5.94, 3.17); console.log(isBigger); // Output: true
-
-
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
-
-
validatePrice-
说明: 验证一个价格是有效的(大于或等于零).
-
参数数字 :
price:要验证的价格.
-
回返数字 :
True如果价格有效,false否则. -
用法示例数字 :
import { validatePrice } from '@bondsports/general'; validatePrice(20.00); // Return: true validatePrice(0); // Return: true validatePrice(-5.00); // Return: false
-
-
validatePrices-
说明:验证一系列价格,检查每个价格是否有效(大于或等于零).
-
参数数字 :
prices: 要验证的一系列价格.
-
回返数字 :
True如果所有价格都有效,false否则. -
用法示例数字 :
import { validatePrices } from '@bondsports/general'; const arePricesValid = validatePrices([10.00, 20.00, 30.00]); console.log(arePricesValid); // Output: true
-
-
validateAmounts-
说明: 验证数组,检查每个价格是否有效(不是无效或未定义).
-
参数数字 :
amounts: 需要验证的数组.
-
回返数字 :
True如果所有金额都是有效的,false否则. -
用法示例数字 :
import { validateAmounts } from '@bondsports/general'; const areAmountsValid = validateAmounts([10.00, -20.00, 30.00]); console.log(areAmountsValid); // Output: true
-
-
toFixed-
说明:将数字绕入指定的精确度并返回结果.
-
参数数字 :
num: 该数字为圆.precision: 十进制位数.
-
回返: 四舍五入数.
-
用法示例数字 :
import { toFixed } from '@bondsports/general'; const roundedNumber = toFixed(10.123456, 3); console.log(roundedNumber); // Output: 10.123
-
-
roundPriceCents-
说明:将价格四舍五入到最接近的一分.
-
参数数字 :
price:价至圆.
-
回返: 四舍五入的价格.
-
用法示例数字 :
import { roundPriceCents } from '@bondsports/general'; const roundedPrice = roundPriceCents(10.123); console.log(roundedPrice); // Output: 10.12
-
-
roundQuantity-
说明: 将给定的数量四舍五入到指定的小数位.
-
参数数字 :
amount:等于圆.
-
回返: 四舍五入的数额.
-
用法示例数字 :
import { roundQuantity } from '@bondsports/general'; const roundedQty = roundQuantity(5.67891); console.log(roundedQty); // Output: 5.67891
-
-
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
-
-
calculatePriceFromTotal-
说明:从总价格中计算出价格,同时考虑到数量和可选税分.
-
参数数字 :
price: 单位价格.taxPercent: 税率(可选).quantity: 数量( 默认值 :DEFAULT_QUANTITY) (中文(简体) ).
-
回返:计算出的价格.
-
用法示例数字 :
import { calculatePriceFromTotal } from '@bondsports/general'; const price = calculatePriceFromTotal(110, 10); console.log(price); // Output: 100.00
-
-
calculatePriceWithTax-
说明: 以税收计算价格.
-
参数数字 :
price:物品的价格.taxPercent: 适用税率(可选).quantity: 项目数量(默认:DEFAULT_QUANTITY) (中文(简体) ).
-
回返:计算出的价格与税.
-
用法示例数字 :
import { calculatePriceWithTax } from '@bondsports/general'; const priceWithTax = calculatePriceWithTax(100, 10, 2); console.log(priceWithTax); // Output: 220.00
-
-
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
-
-
calculateItemUnitPrice-
说明:根据给定的单价,普税状况,以及纳税百分比来计算项目单价.
-
参数数字 :
unitPrice:税前的单价.isTaxInclusive:标出价格是否包含税收的旗帜.taxPercent: 税率.
-
回返:计算出单价.
-
用法示例数字 :
import { calculateItemUnitPrice } from '@bondsports/general'; const unitPrice = calculateItemUnitPrice(100, true, 10); console.log(unitPrice); // Output: 90.91
-
-
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
-
-
calculateQuantityFromTotal-
说明:根据总额、单价和税率计算数量.
-
参数数字 :
totalAmount:总金额.unitPrice:项目单价.isTaxInclusive:指该税是否计入单价.taxPercentage: 税率(可选).
-
回返: 计算的数量.
-
用法示例数字 :
import { calculateQuantityFromTotal } from '@bondsports/general'; const quantity = calculateQuantityFromTotal(110, 100, true, 10); console.log(quantity); // Output: 1.00
-
-
convertCentsToDollars-
说明: 将给定的价格以美分换算成美元.
-
参数数字 :
price:价为分.
-
回返:以美元计价.
-
用法示例数字 :
import { convertCentsToDollars } from '@bondsports/general'; const dollars = convertCentsToDollars(10000); console.log(dollars); // Output: 100.00
-
-
getRoundingSplit-
说明:计算所要求价格和实际价格之间的四舍五入.
-
参数数字 :
requestedPrice: 要求的价格.price:实际价格.
-
回返: 包含分数和分数的对象.
-
用法示例数字 :
import { getRoundingSplit } from '@bondsports/general'; const roundingSplit = getRoundingSplit(100.05, 100); console.log(roundingSplit); // Output: { splitsNumber: 5, splitAmount: 0.01 }
-
-
splitPriceAmount-
说明: 将给定的数量分成多个部分.
-
参数数字 :
amount:拟分出的数额.splitsNumber: 所要拆分的次数( 默认数:1) .
-
回返: 代表拆分数额的数字组.
-
用法示例数字 :
import { splitPriceAmount } from '@bondsports/general'; const splits = splitPriceAmount(100, 3); console.log(splits); // Output: [33.33, 33.33, 33.34]
-
-
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
-
-
calculateTotalPriceByPrices-
说明:通过汇总给定数组中的价格来计算总价.
-
参数数字 :
prices:一系列价格.startPrice: 计算起始价格(默认值:0) (中文(简体) ).
-
回返:总价.
-
用法示例数字 :
import { calculateTotalPriceByPrices } from '@bondsports/general'; const prices = [100, 200, 300]; const totalPrice = calculateTotalPriceByPrices(prices); console.log(totalPrice); // Output: 600.00
-
-
calculateTotalQuantity-
说明:通过汇总给定的数量来计算总量.
-
参数数字 :
quantities: 需要归纳的数量.startQuantity: 计算起始数量(默认值:0) (中文(简体) ).
-
回返: 总量.
-
用法示例数字 :
import { calculateTotalQuantity } from '@bondsports/general'; const quantities = [1, 2, 3]; const totalQty = calculateTotalQuantity(quantities); console.log(totalQty); // Output: 6
-
-
calculateDue-
说明:根据价格和已经支付的金额计算到期金额.
-
参数数字 :
price:项目总价.paidAmount: 已经支付的数额( 默认 :0) (中文(简体) ).
-
回返:到期金额.
-
用法示例数字 :
import { calculateDue } from '@bondsports/general'; const amountDue = calculateDue(100, 50); console.log(amountDue); // Output: 50.00
-
-
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合作,验证值以及进行计算等操作.
-
absoluteValue-
说明:计算一个数字的绝对值.
-
参数数字 :
value:用来计算绝对值的数字.
-
回返:输入数的绝对值.
-
用法示例数字 :
import { absoluteValue } from '@bondsports/general'; const absValue = absoluteValue(-42); console.log(absValue); // Output: 42
-
-
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));
-
-
isInEnum-
说明:检查一个值是否为一个enum的有效成员.
-
参数数字 :
enumType:要检查的电筒.value: 要检查的值 .
-
回返数字 :
True如果该值是该enum的有效成员,false否则. -
用法示例数字 :
import { isInEnum, HttpStatus } from '@bondsports/general'; const isValid = isInEnum(HttpStatus, 200); console.log(isValid); // Output: true
-
-
extractValuesFromEnumArray-
说明: 提取出一组元素中的值.
-
参数数字 :
values: 要提取的数值阵列.enumType: 用于过滤值的enum类型.
-
回返: enum中存在的一系列数值.
-
用法示例数字 :
import { extractValuesFromEnumArray, HttpStatus } from '@bondsports/general'; const validStatuses = extractValuesFromEnumArray([200, 404, 999], HttpStatus); console.log(validStatuses); // Output: [200, 404]
-
-
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'
-
-
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
-
-
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']
-
-
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); }
-