Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: aggregateObj #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"gh-pages": "^6.1.1",
"husky": "^8.0.3",
"lint-staged": "^15.2.0",
"lodash-es": "^4.17.21",
"moment": "^2.29.4",
"prettier": "^3.1.1",
"react": "^18.2.0",
Expand Down
40 changes: 40 additions & 0 deletions packages/tools/docs/demo/AggregateObjDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import ReactJson from 'react-json-view';
import { aggregateObj } from '@orca-fe/tools';

interface ExamResult {
teacherName: string;
teacherSubject: string;
highScore: number;
lowScore: number;
avgScore: number;
passRate: number;
studentsCount: number;
}

const result: ExamResult = {
teacherName: '张伟',
teacherSubject: '数学',
highScore: 98,
lowScore: 72,
avgScore: 85.5,
passRate: 95,
studentsCount: 40,
};

export default function AggregateObjDemo() {
return (
<div>
<div>原对象:</div>
<ReactJson src={result} />
<div>聚合后的对象:</div>
<ReactJson
src={aggregateObj(result, {
teacherInfo: ['teacherName', 'teacherSubject'],
'baseInfo.score': ['highScore', 'lowScore', 'avgScore'],
'baseInfo.pass': ['passRate', 'studentsCount'],
})}
/>
</div>
);
}
22 changes: 22 additions & 0 deletions packages/tools/docs/obj.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,25 @@ export default () => (
</div>
);
```

## aggregateObj 聚合对象

```ts
/**
* 对象聚合函数,通过将对象的某些属性值聚合到一个属性
* @param obj 要聚合的对象
* @param mapping 聚合规则映射,指定哪些属性应该被聚合到哪个属性中
* @param options 配置选项·
* @returns 返回聚合后的对象
*/
function aggregateObj<T extends Object>(obj: T, mapping: AggregateKeysMapping = {}, options: AggregateOptions = {}): Object;
```

<code src="./demo/AggregateObjDemo"></code>

### 选项

| 参数 | 说明 | 类型 | 默认值 |
| ------------------ | --------------------------------------------------- | ------------------ | ------ |
| maxDepth | 最大递归深度,auto 表示完全递归,支持自定义递归深度 | `auto` \| `number` | 1 |
| omitAggregatedKeys | 是否剔除已经被聚合的键 | `boolean` | true |
55 changes: 55 additions & 0 deletions packages/tools/src/obj.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import lodash from 'lodash-es';

/* eslint-disable @typescript-eslint/ban-types,@typescript-eslint/no-explicit-any */
type ObjMapCallback = (key: string, value: any) => any;

Expand Down Expand Up @@ -83,3 +85,56 @@ export function convertNullToUndefined<T>(value: T): T {

return convert(value);
}

export type AggregateKeysMapping = Record<string, string[]>;

export type AggregateOptions = {

/** 最大递归深度,默认为 1,auto 表示完全递归,支持自定义递归深度 */
maxDepth?: 'auto' | number;

/** 是否剔除已经被聚合的键,默认是 */
omitAggregatedKeys?: boolean;
};

/**
* 对象聚合函数,通过将对象的某些属性值聚合到一个属性
* @param obj 要聚合的对象
* @param depth 当前聚合的深度,默认为1
* @param mapping 聚合规则映射,指定哪些属性应该被聚合到哪个属性中,不允许待映射键和聚合映射键相同
* @param options 配置选项
* @returns 返回聚合后的对象
*/
function aggregate<T extends Object>(obj: T, depth: number = 1, mapping: AggregateKeysMapping = {}, options: AggregateOptions = {}): Object {
const { maxDepth = 1, omitAggregatedKeys = true } = options;
let result = lodash.cloneDeep(obj) as any;
Object.entries(mapping).forEach(([key, aggregatedKeys]) => {
// 存在同名键直接跳过
if (Object.prototype.hasOwnProperty.call(result, key)) {
return;
}
// 存在键值为对象的情况,未达到递归最大深度设置,继续递归
if (lodash.isPlainObject(result[key]) && (maxDepth === 'auto' || depth < maxDepth)) {
lodash.set(result, key, aggregate(result[key], depth + 1, mapping, options));
return;
}
lodash.set(result, key, lodash.pick(result, aggregatedKeys));
// 剔除已经被聚合的键
if (omitAggregatedKeys) {
result = lodash.omit(result, aggregatedKeys);
}
});

return result;
}

/**
* 对象聚合函数,通过将对象的某些属性值聚合到一个属性
* @param obj 要聚合的对象
* @param mapping 聚合规则映射,指定哪些属性应该被聚合到哪个属性中
* @param options 配置选项·
* @returns 返回聚合后的对象
*/
export function aggregateObj<T extends Object>(obj: T, mapping: AggregateKeysMapping = {}, options: AggregateOptions = {}): Object {
return aggregate(obj, 1, mapping, options);
}
25 changes: 14 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading