/** * @file 数组相关的 utils */ export { default as chunk } from 'lodash/chunk' export { default as compact } from 'lodash/compact' export { default as concat } from 'lodash/concat' export { default as difference } from 'lodash/difference' export { default as drop } from 'lodash/drop' export { default as dropRight } from 'lodash/dropRight' export { default as fill } from 'lodash/fill' export { default as filter } from 'lodash/filter' export { default as find } from 'lodash/find' export { default as findIndex } from 'lodash/findIndex' export { default as findLast } from 'lodash/findLast' export { default as first } from 'lodash/first' export { default as flatMap } from 'lodash/flatMap' export { default as flatten } from 'lodash/flatten' export { default as flattenDeep } from 'lodash/flattenDeep' export { default as forEach } from 'lodash/forEach' export { default as groupBy } from 'lodash/groupBy' export { default as includes } from 'lodash/includes' export { default as indexOf } from 'lodash/indexOf' export { default as intersection } from 'lodash/intersection' export { default as isArray } from 'lodash/isArray' export { default as keyBy } from 'lodash/keyBy' export { default as last } from 'lodash/last' export { default as map } from 'lodash/map' export { default as nth } from 'lodash/nth' export { default as orderBy } from 'lodash/orderBy' export { default as pullAt } from 'lodash/pullAt' export { default as partition } from 'lodash/partition' export { default as range } from 'lodash/range' export { default as reduce } from 'lodash/reduce' export { default as remove } from 'lodash/remove' export { default as reverse } from 'lodash/reverse' export { default as some } from 'lodash/some' export { default as sortBy } from 'lodash/sortBy' export { default as take } from 'lodash/take' export { default as union } from 'lodash/union' export { default as unionBy } from 'lodash/unionBy' export { default as uniq } from 'lodash/uniq' export { default as uniqBy } from 'lodash/uniqBy' export { default as without } from 'lodash/without' /** * 将指定 key 对应的值为 null 或 undefined 的成员从数组里剔除, 并修正返回值的类型. * 在返回值的类型中, 该 key 对应的值不再是 optional, 而是一定存在. * * @param array - 原数组 * @param key - 要检查的 key * @returns 新数组 */ export const compactByKey = < T extends { [key: string]: any }, K extends keyof T, >( array: T[], key: K, ) => array.filter(e => e[key] != null) as (T & { [key in K]: NonNullable })[] /** * 按指定的字段 prop 将数组 `T[]` 转换为一个形如 `{ T[prop]: T }` 的 map. * * @param array - 输入的数组 * @param prop - 从数组成员中取该字段的值作为 map 的 key * @returns 转换后的 map * * @example * ```javascript * arrayToMap( * [ * { id: 1, name: 'Tom' }, * { id: 2, name: 'Jerry' }, * ], * 'id', * ) // returns { 1: { id: 1, name: 'Tom' }, 2: { id: 2, name: 'Jerry' } } * ``` */ export function arrayToMap(array: T[], prop: K) { return array.reduce<{ [key: string]: T }>((map, item) => { // eslint-disable-next-line map[item[prop] as any] = item return map }, {}) } /** * 将数组中的指定成员替换为一个新的对象. 不改变原数组, 返回新数组. * * @param array - 数组 * @param member - 要被替换的对象 * @param newMember - 要替换为的新对象 * * @returns 替换后的新数组 */ export function replace(array: T[], member: T, newMember: T) { const newArray = [...array] const index = array.findIndex(m => m === member) if (index !== -1) newArray[index] = newMember return newArray }