Skip to content

Commit

Permalink
refactor: 抽取 prefix 逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Nov 24, 2023
1 parent 05808f1 commit dea9605
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/s2-core/src/data-set/custom-tree-pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { i18n } from '../common/i18n';
import type { Meta, S2DataConfig } from '../common/interface';
import {
getDataPath,
getDataPathPrefix,
transformDimensionsValues,
} from '../utils/dataset/pivot-data-set';
import { DataHandler } from '../utils/dataset/proxy-handler';
Expand All @@ -20,13 +21,15 @@ export class CustomTreePivotDataSet extends PivotDataSet {
query,
columns as string[],
);

const path = getDataPath({
rowDimensionValues,
colDimensionValues,
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
rowFields: rows,
colFields: columns as string[],
prefix: getDataPathPrefix(rows, columns as string[]),
});
const data = get(this.indexesData, path);
if (data) {
Expand Down
1 change: 1 addition & 0 deletions packages/s2-core/src/data-set/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type DataPathParams = {
// 完整维度信息:'四川省[&]成都市'
dimensionPath: string;
}) => void;
prefix?: string;
};

export interface CellDataParams {
Expand Down
4 changes: 4 additions & 0 deletions packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
deleteMetaById,
flattenIndexesData,
getDataPath,
getDataPathPrefix,
getFlattenDimensionValues,
getSatisfiedPivotMetaValues,
isMultiValue,
Expand Down Expand Up @@ -410,6 +411,7 @@ export class PivotDataSet extends BaseDataSet {
colPivotMeta: this.colPivotMeta,
rowFields: rows,
colFields: columns as string[],
prefix: getDataPathPrefix(rows, columns as string[]),
});
const data = get(this.indexesData, path);
if (data) {
Expand Down Expand Up @@ -538,6 +540,7 @@ export class PivotDataSet extends BaseDataSet {
queryType,
});

const prefix = getDataPathPrefix(totalRows, columns as string[]);
const all: Data[] = [];

for (const rowQuery of rowQueries) {
Expand All @@ -549,6 +552,7 @@ export class PivotDataSet extends BaseDataSet {
colPivotMeta: this.colPivotMeta,
rowFields: totalRows,
colFields: columns as string[],
prefix,
});

let hadMultiField = false;
Expand Down
20 changes: 12 additions & 8 deletions packages/s2-core/src/utils/dataset/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ export function getDimensionsWithParentPath(
?.filter((item) => item);
}

export function getDataPathPrefix(rowFields: string[], colFields: string[]) {
return rowFields
.concat(colFields)
.filter((i) => i !== EXTRA_FIELD)
.join(ID_SEPARATOR);
}

/**
* Transform a single data to path
* {
Expand Down Expand Up @@ -180,15 +187,9 @@ export function getDataPath(params: DataPathParams) {
colFields,
rowPivotMeta,
colPivotMeta,
prefix = '',
} = params;

const getDimensionPrefix = () => {
return rowFields
.concat(colFields)
.filter((i) => i !== EXTRA_FIELD)
.join(ID_SEPARATOR);
};

// 根据行、列维度值生成对应的 path 路径,始终将总计小计置于第 0 位,明细数据从第 1 位开始,有两个情况:
// 如果是汇总格子: path = [0, 0, 0, 0] path 中会存在 0 的值
// 如果是明细格子: path = [1, 1, 1] 数字均不为 0
Expand Down Expand Up @@ -253,7 +254,7 @@ export function getDataPath(params: DataPathParams) {
const rowPath = getPath(rowFields, rowDimensionValues, rowPivotMeta, false);
const colPath = getPath(colFields, colDimensionValues, colPivotMeta, true);

return [getDimensionPrefix(), ...rowPath, ...colPath];
return [prefix, ...rowPath, ...colPath];
}
interface Param {
rows: string[];
Expand Down Expand Up @@ -305,6 +306,8 @@ export function transformIndexesData(params: Param) {
).push(dimensionPath);
};

const prefix = getDataPathPrefix(rows, columns as string[]);

data.forEach((item) => {
// 空数据没有意义,直接跳过
if (!item || isEmpty(item)) {
Expand Down Expand Up @@ -333,6 +336,7 @@ export function transformIndexesData(params: Param) {
colFields: columns,
isFirstCreate: true,
onFirstCreate,
prefix,
});
paths.push(path);
set(indexesData, path, item);
Expand Down

0 comments on commit dea9605

Please sign in to comment.