@art-site/art-core-ts-compossibleons (中文(简体) )
用于JavaScript的强大而灵活的理解库,为与数组,对象,和其他可移动性的工作提供统一的接口. 它为不同数据结构的绘图、过滤和缩小等共同操作提供了一致的方式.
这个库与其他理解的最大区别是每个理解函数都可以接受任何输入容器作为源:对象,数组或任何可移动的. 理解函数的名称表示返回类型 :
object返回对象array返回数组find返回已找到的值reduce返回减少的值each返回时returning值或未定义
特征
- 用于与数组、对象和其他可移动性工作的统一接口
- 不同数据结构之间的一致行为
- 支持同步操作和同步操作
- 具有类型脚本支持的类型安全操作
- 保持原始数据结构特点的有效实施
安装
npm install @art-suite/art-core-ts-comprehensions
API 概览
核心职能
所有理解函数需要 0, 1 或 2 个参数 :
-
参数 1: 升起的容器
-
参数 2: 有功能或选项对象
-
each(source, withOrOptions?):在源头上标出,执行每个项目的功能。 返回returning如果提供,则不提供undefined。 。 。 . -
array(source, withOrOptions?): 通过对源的每个项目应用同功能来创建新数组. -
object(source, withOrOptions?): 通过对源中的每个项目应用与功能创建新对象. -
reduce(source, withOrOptions?): 使用同功能将源降低为单个值. -
find(source, withOrOptions?): 返回源代码中满足功能的第一个项目.
深入理解
这个 DeepComprehensions 模块为绘图和在深层嵌入的阵列和物体上延展提供了先进的公用设备。 这些函数在需要任意嵌入处理数据结构时特别有用.
说明: 这些函数现在可以直接从软件包中导入:
import { map, deepEach, deepMap, } from "@art-suite/art-core-ts-comprehensions";
出口
map(source, withOrOptions?):在阵列或物体上浅意地映射,返回带有映射值的同类型新容器。 不再出现于嵌入式容器.deepEach(source, withOrOptions?): 在所有值上以深层嵌入的阵列或对象结构来排列,称为每个非容器值的同函数. 支持可选when过滤器.deepMap(source, withOrOptions?):在深层嵌入的阵列或对象中对所有值进行回溯映射,返回形状相同但有映射值的新结构. 支持可选when过滤器.
用法实例
import { deepMap, deepEach, map } from "@art-suite/art-core-ts-comprehensions";
const nested = {
a: 1,
b: 2,
c: [1, 2, 3],
};
// Deeply map all values, incrementing each by 1
const incremented = deepMap(nested, (value) => value + 1);
// Result: { a: 2, b: 3, c: [2, 3, 4] }
// Deeply map, but only for keys not equal to 'c'
const filtered = deepMap(nested, {
with: (value) => value + 1,
when: (value, key) => key !== "c",
});
// Result: { a: 2, b: 3 }
// Deeply iterate and sum all values
let sum = 0;
deepEach(nested, (value) => (sum += value));
// sum === 9
// Shallow map (does not recurse into arrays/objects)
const shallow = map(nested, (value) => value + 10);
// Result: { a: 11, b: 12, c: "1,2,310" } // Note: [1,2,3] + 10 coerces to string
用法实例
基本阵列操作
import { array, each, find } from "@art-suite/art-core-ts-comprehensions";
const numbers = [1, 2, 3, 4, 5];
// Map operation
const doubled = array(numbers, (x) => x * 2);
// Result: [2, 4, 6, 8, 10]
// Filter and transform
const evenDoubled = array(numbers, {
with: (x) => x * 2,
when: (x) => x % 2 === 0,
});
// Result: [4, 8]
// ForEach operation
each(numbers, (x) => console.log(x));
// Logs each number
// Find operation with when option
const firstEven = find(numbers, { when: (x) => x % 2 === 0 });
// Result: 2
// Find and transform
const firstEvenDoubled = find(numbers, {
when: (x) => x % 2 === 0,
with: (x) => x * 2,
});
// Result: 4
对象操作
import { object, each } from "@art-suite/art-core-ts-comprehensions";
const data = {
a: 1,
b: 2,
c: 3,
};
// Transform object values
const doubled = object(data, (value, key) => value * 2);
// Result: { a: 2, b: 4, c: 6 }
// Transform keys with withKey option
const keyTransformed = object(data, { withKey: (value) => value * 11 });
// Result: { 11: 1, 22: 2, 33: 3 }
// Iterate over object
each(data, (value, key) => console.log(`${key}: ${value}`));
// Logs each key-value pair
// Each with returning value
const result = each(data, {
returning: "done",
with: (value, key) => console.log(`${key}: ${value}`),
});
// Logs each key-value pair, result === "done"
减少业务
import { reduce } from "@art-suite/art-core-ts-comprehensions";
const numbers = [1, 2, 3, 4, 5];
// Sum all numbers (uses first element as initial value)
const sum = reduce(numbers, (acc, x) => acc + x);
// Result: 15
// Sum with inject option
const sumWithInitial = reduce(numbers, {
with: (acc, x) => acc + x,
inject: 10,
});
// Result: 25
// Multiply with inject option
const product = reduce(numbers, {
with: (acc, x) => acc * x,
inject: 1,
});
// Result: 120
// Filter and reduce
const evenSum = reduce(numbers, {
with: (acc, x) => acc + x,
when: (x) => x % 2 === 0,
});
// Result: 6 (sum of even numbers only)
// Reduce with inject
reduce([1, 2, 3], { with: (acc, x) => acc + x, inject: 10 });
// Result: 16
// Each with returning
const result = each([1, 2, 3], {
returning: "done",
with: (x) => console.log(x),
});
// Result: "done"
使用不同的数据结构
import { array, object } from "@art-suite/art-core-ts-comprehensions";
// Array-like object
const arrayLike = { 0: "a", 1: "b", 2: "c", length: 3 };
const result1 = array(arrayLike, (x) => x.toUpperCase());
// Result: ['A', 'B', 'C']
// Set
const set = new Set([1, 2, 3]);
const result2 = array(set, (x) => x * 2);
// Result: [2, 4, 6]
// Map
const map = new Map([
["a", 1],
["b", 2],
]);
const result3 = object(map, (value, key) => value * 2);
// Result: { a: 2, b: 4 }
// Stop iteration early with stopWhen
const result4 = array([1, 2, 3, 4], {
with: (x) => x * 2,
stopWhen: (x) => x === 3,
});
// Result: [2, 4] (stops when value equals 3)
// Use into option to append to existing array
const existingArray = [100, 200];
const result5 = array([1, 2, 3], { with: (x) => x * 10, into: existingArray });
// Result: [100, 200, 10, 20, 30]
// Use into option to merge into existing object
const existingObject = { x: 999 };
const result6 = object([1, 2, 3], {
with: (x) => x * 10,
into: existingObject,
});
// Result: { x: 999, 1: 10, 2: 20, 3: 30 }
选项参考
所有理解函数接受一个选项对象作为第二个参数. 以下是所有可用选项的完整参考,以及支持这些选项的功能:
常见选项( 所有函数)
这些选项得到所有理解函数的支持:
-
with: 转换每个值的函数- 签署数字 :
(value, key) => transformedValue(计:array, (中文(简体) ),object, (中文(简体) ),find, (中文(简体) ),each(中文(简体) ) - 签署数字 :
(accumulator, value, key) => newAccumulator(计:reduce(中文(简体) ) - 默认: 如果未提供, 返回原值不变
- 用于数字 :
array, (中文(简体) ),object, (中文(简体) ),reduce, (中文(简体) ),find, (中文(简体) ),each
- 签署数字 :
-
when: 要过滤元素的函数( 预览)- 签署数字 :
(value, key) => boolean - 行为: 只处理返回的元素
true - 用于数字 :
array, (中文(简体) ),object, (中文(简体) ),reduce, (中文(简体) ),find, (中文(简体) ),each
- 签署数字 :
-
stopWhen: 及早停止重复的函数- 签署数字 :
(value, key) => boolean - 行为: 返回时停止迭代
true - 用于数字 :
array, (中文(简体) ),object, (中文(简体) ),each
- 签署数字 :
函数特定选项
array(source, withOrOptions?)
into: 推动结果的矩阵- 类型数字 :
Array - 默认: 创建新数组
- 行为: 结果被推入此数组, 而不是创建新的数组
- 类型数字 :
object(source, withOrOptions?)
into: 指定结果为- 类型数字 :
Object - 默认: 创建新对象
- 行为: 给此对象分配结果而不是创建新对象
- 类型数字 :
withKey: 确定输出密钥的函数- 签署数字 :
(value, key) => newKey - 默认: 使用原始密钥(用于对象)或值(用于数组/可插入)
- 行为:返回的值成为输出对象中的密钥
- 签署数字 :
reduce(source, withOrOptions?)
inject: 减少的初始值- 类型: 任何值
- 默认: 使用第一个元素作为初始值
- 行为: 此值成为初始累积器
each(source, withOrOptions?)
returning: 要返回的值- 类型: 任何值
- 默认数字 :
undefined - 行为: 返回此值(未由
each页:1
find(source, withOrOptions?)
- 除共同选项外,没有特定函数选项
选项组合和行为
with+ 键when: 仅转换已过滤的元素with+ 键stopWhen: 变换直到满足停止条件when+ 键stopWhen: 筛选直到满足停止条件with+ 键when+ 键stopWhen: 变形过滤元素直到满足停止条件
选项使用示例
// Basic with function
array([1, 2, 3], { with: (x) => x * 2 });
// Result: [2, 4, 6]
// Filter with when
array([1, 2, 3, 4], { when: (x) => x % 2 === 0 });
// Result: [2, 4]
// Transform and filter
array([1, 2, 3, 4], { with: (x) => x * 2, when: (x) => x % 2 === 0 });
// Result: [4, 8]
// Stop early
array([1, 2, 3, 4], { with: (x) => x * 2, stopWhen: (x) => x === 3 });
// Result: [2, 4]
// Use into option
const existing = [100];
array([1, 2, 3], { with: (x) => x * 10, into: existing });
// Result: [100, 10, 20, 30]
// Object with custom keys
object([1, 2, 3], { withKey: (x) => `key_${x}`, with: (x) => x * 10 });
// Result: { key_1: 10, key_2: 20, key_3: 30 }
// Reduce with inject
reduce([1, 2, 3], { with: (acc, x) => acc + x, inject: 10 });
// Result: 16
// Each with returning
const result = each([1, 2, 3], {
returning: "done",
with: (x) => console.log(x),
});
// Result: "done"
关键概念
源类型
图书馆使用各种来源类型:
- 阵列
- 对象
- 设定
- 地图
- 类似矩阵的对象
- 带有 a 的任何对象
forEach方法 - 带有 a 的任何对象
map方法 - 带有 a 的任何对象
reduce方法
有函数
每个操作接受一个随功能,接收:
- 当前值
- 当前密钥/索引
- 源对象
- 当前迭代索引
说明编号: reduce,带有函数签名的是 (accumulator, value, key) 改为 (value, key)。 。 。 .
返回值
each: 返回returning如果提供,则不提供undefinedarray: 返回新数组object: 返回新对象reduce: 返回累积值find: 返回第一个匹配项或未定义
支持的容器类型
此库可区分容器 斜拉 和容器 生成数字 :
容器,你可以重排过
您可以使用理解函数( 如 each, (中文(简体) ), reduce, (中文(简体) ), find等,还有 object 财务报告和审定财务报表 array以下列任何一项为准:
- 阵列
- 对象
- 设定
- 地图
- 类似矩阵对象(带有
length属性和索引要素) - 执行可执行协议的任何对象(有:
[Symbol.iterator]方法)
可以生成的容器
目前,仅 数组 财务报告和审定财务报表 普通对象 可以通过理解函数(例如: array, (中文(简体) ), object, (中文(简体) ), deepMap(见a/c.5/49/70,附件).
说明: 未来可能会添加对生成地图和集的支持.
这意味着,当您可以覆盖许多不同类型的容器时,当您使用返回新容器的函数时(如 array, (中文(简体) ), object,或 deepMap),结果将始终是一个新的数组或一个新的平原对象,这取决于所使用的函数和输入类型.
许可证
麻省理工学院