Skip to content

Commit

Permalink
重构代码: yd_array_pickBy函数改为yd_array_pick并重构部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbimo committed Aug 7, 2024
1 parent efa0752 commit 281251a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
15 changes: 9 additions & 6 deletions lib/array/pickBy.js → lib/array/pick.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/**
* 从数组中选择指定标签的项
* @alias yd_array_pickBy
* @alias yd_array_pick
* @category 数组操作
* @param {object[]} arry - 需要处理的数组,其中每个对象包含 `label` 和 `value` 属性
* @param {string[]} keys - 需要选择的标签列表
* @param {Array} arrObj 数组对象
* @param {string} field 匹配的字段
* @param {Array} values 匹配的值
* @returns {object[]} 返回一个新数组,其中只包含具有指定标签的项
* @author 卞雪瑞 <[email protected]>
* @author 陈随易 <https://chensuiyi.me>
* @summary 这个函数根据给定的标签列表,从数组中选择所有匹配这些标签的项,并返回这些项。
* @example
* yd_array_pickBy(
* yd_array_pick(
* [{ label: 'a', value: 1 }, { label: 'b', value: 2 }, { label: 'c', value: 3 }],
* 'label',
* ['a', 'c']
* );
* 结果:
Expand All @@ -18,6 +21,6 @@
* { label: 'c', value: 3 }
* ]
*/
export default (arry, keys) => {
return arry.filter((item) => keys.includes(item.label));
export default (arrObj, field, values = []) => {
return arrObj.filter((item) => values.includes(item[field]));
};
20 changes: 8 additions & 12 deletions lib/array/pickBy.test.js → lib/array/pick.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import yd_array_pickBy from './pickBy.js';
import { describe, expect, test } from 'vitest';

describe('yd_array_pickBy', () => {
import yd_array_pick from './pick.js';

describe('yd_array_pick', () => {
test('根据标签选择数组中的项', () => {
const array = [
{ label: 'a', value: 1 },
{ label: 'b', value: 2 },
{ label: 'c', value: 3 }
];
const keys = ['a', 'c'];
const result = yd_array_pickBy(array, keys);
const result = yd_array_pick(array, 'label', ['a', 'c']);
expect(result).toEqual([
{ label: 'a', value: 1 },
{ label: 'c', value: 3 }
Expand All @@ -21,8 +21,7 @@ describe('yd_array_pickBy', () => {
{ label: 'a', value: 1 },
{ label: 'b', value: 2 }
];
const keys = ['c', 'd'];
const result = yd_array_pickBy(array, keys);
const result = yd_array_pick(array, 'label', ['c', 'd']);
expect(result).toEqual([]);
});

Expand All @@ -31,8 +30,7 @@ describe('yd_array_pickBy', () => {
{ label: 'a', value: 1 },
{ label: 'b', value: 2 }
];
const keys = ['a', 'b'];
const result = yd_array_pickBy(array, keys);
const result = yd_array_pick(array, 'label', ['a', 'b']);
expect(result).toEqual([
{ label: 'a', value: 1 },
{ label: 'b', value: 2 }
Expand All @@ -41,8 +39,7 @@ describe('yd_array_pickBy', () => {

test('处理空数组', () => {
const array = [];
const keys = ['a', 'b'];
const result = yd_array_pickBy(array, keys);
const result = yd_array_pick(array, 'label', ['a', 'b']);
expect(result).toEqual([]);
});

Expand All @@ -51,8 +48,7 @@ describe('yd_array_pickBy', () => {
{ label: 'a', value: 1 },
{ label: 'b', value: 2 }
];
const keys = [];
const result = yd_array_pickBy(array, keys);
const result = yd_array_pick(array, 'label', []);
expect(result).toEqual([]);
});
});

0 comments on commit 281251a

Please sign in to comment.