Skip to content

Commit cb35674

Browse files
committed
fix: revert change
1 parent e186db6 commit cb35674

File tree

2 files changed

+8
-99
lines changed

2 files changed

+8
-99
lines changed

packages/vega-spec-builder/src/legend/legendFacetUtils.test.ts

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
import { Scale, ScaleField } from 'vega';
12+
import { Scale } from 'vega';
1313

1414
import { COLOR_SCALE, DEFAULT_COLOR, LINE_TYPE_SCALE, SYMBOL_SIZE_SCALE, TABLE } from '@spectrum-charts/constants';
1515

16-
import { getFacets, getFacetsFromKeys, getFieldName } from './legendFacetUtils';
17-
16+
import { getFacets, getFacetsFromKeys } from './legendFacetUtils';
1817

1918
describe('getFacets()', () => {
2019
test('should correctly identify continuous and categorical facets', () => {
@@ -38,8 +37,6 @@ describe('getFacets()', () => {
3837
expect(ordinalFacets).toHaveLength(1);
3938
expect(continuousFacets).toHaveLength(1);
4039
});
41-
42-
4340
});
4441

4542
describe('getFacetsFromKeys()', () => {
@@ -67,69 +64,4 @@ describe('getFacetsFromKeys()', () => {
6764
expect(facets.ordinalFacets).toHaveLength(1);
6865
expect(facets.continuousFacets).toHaveLength(0);
6966
});
70-
71-
72-
});
73-
74-
describe('getFieldName()', () => {
75-
test('should return string field as is', () => {
76-
expect(getFieldName('category')).toBe('category');
77-
expect(getFieldName('series')).toBe('series');
78-
expect(getFieldName('')).toBe('');
79-
});
80-
81-
test('should extract field name from object with field property', () => {
82-
expect(getFieldName({ field: 'category' } as unknown as ScaleField)).toBe('category');
83-
expect(getFieldName({ field: 'series' } as unknown as ScaleField)).toBe('series');
84-
expect(getFieldName({ field: '' } as unknown as ScaleField)).toBe('');
85-
});
86-
87-
test('should extract signal name from object with signal property', () => {
88-
expect(getFieldName({ signal: 'category' } as unknown as ScaleField)).toBe('category');
89-
expect(getFieldName({ signal: 'series' } as unknown as ScaleField)).toBe('series');
90-
expect(getFieldName({ signal: '' } as unknown as ScaleField)).toBe('');
91-
});
92-
93-
test('should handle undefined input', () => {
94-
expect(getFieldName(undefined)).toBe('undefined');
95-
});
96-
97-
test('should handle null input', () => {
98-
expect(getFieldName(null as unknown as ScaleField)).toBe('null');
99-
});
100-
101-
test('should handle other object types with toString fallback', () => {
102-
const obj = { toString: () => 'custom object' };
103-
expect(getFieldName(obj as unknown as ScaleField)).toBe('custom object');
104-
});
105-
106-
test('should handle object with both field and signal properties', () => {
107-
// Should prioritize field over signal
108-
expect(getFieldName({ field: 'category', signal: 'series' } as unknown as ScaleField)).toBe('category');
109-
});
110-
111-
test('should handle object with non-string field property', () => {
112-
expect(getFieldName({ field: 123 } as unknown as ScaleField)).toBe('[object Object]');
113-
});
114-
115-
test('should handle object with non-string signal property', () => {
116-
expect(getFieldName({ signal: 456 } as unknown as ScaleField)).toBe('[object Object]');
117-
});
118-
119-
test('should handle empty object', () => {
120-
expect(getFieldName({} as unknown as ScaleField)).toBe('[object Object]');
121-
});
122-
123-
test('should handle array input', () => {
124-
expect(getFieldName(['category'] as unknown as ScaleField)).toBe('category');
125-
});
126-
127-
test('should handle number input', () => {
128-
expect(getFieldName(123 as unknown as ScaleField)).toBe('123');
129-
});
130-
131-
test('should handle boolean input', () => {
132-
expect(getFieldName(true as unknown as ScaleField)).toBe('true');
133-
expect(getFieldName(false as unknown as ScaleField)).toBe('false');
134-
});
13567
});

packages/vega-spec-builder/src/legend/legendFacetUtils.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* OF ANY KIND, either express or implied. See the License for the specific language
1010
* governing permissions and limitations under the License.
1111
*/
12-
import { Scale, ScaleField, ScaleMultiFieldsRef } from 'vega';
12+
import { Scale, ScaleMultiFieldsRef } from 'vega';
1313

1414
import {
1515
COLOR_SCALE,
@@ -39,29 +39,6 @@ const facetScaleNames: (FacetType | SecondaryFacetType)[] = [
3939
SYMBOL_SIZE_SCALE,
4040
];
4141

42-
/**
43-
* Safely extracts the field name from a field reference
44-
* @param field - The field reference which could be a string or object
45-
* @returns The field name as a string
46-
*/
47-
export const getFieldName = (field: ScaleField | undefined): string => {
48-
if (typeof field === 'string') {
49-
return field;
50-
}
51-
if (field && typeof field === 'object') {
52-
// Handle Vega field reference objects
53-
if ('field' in field && typeof field.field === 'string') {
54-
return field.field;
55-
}
56-
if ('signal' in field && typeof field.signal === 'string') {
57-
return field.signal;
58-
}
59-
// Fallback to toString for other object types
60-
return String(field);
61-
}
62-
return String(field);
63-
};
64-
6542
/**
6643
* Goes through all the scales and finds all the facets that are used
6744
* A facet is a key in the data that is used to differentiate the data
@@ -82,10 +59,10 @@ export const getFacets = (scales: Scale[]): { ordinalFacets: Facet[]; continuous
8259
if (scale.type === 'ordinal' || scale.type === 'point') {
8360
ordinalFacets.push({
8461
facetType: scale.name as FacetType,
85-
field: getFieldName(scale.domain.fields[0]),
62+
field: scale.domain.fields[0].toString(),
8663
});
8764
} else {
88-
continuousFacets.push({ facetType: scale.name as FacetType, field: getFieldName(scale.domain.fields[0]) });
65+
continuousFacets.push({ facetType: scale.name as FacetType, field: scale.domain.fields[0].toString() });
8966
}
9067
}
9168
});
@@ -110,12 +87,12 @@ export const getFacetsFromKeys = (
11087
if (scale.type === 'ordinal' || scale.type === 'point') {
11188
ordinalFacets.push({
11289
facetType: scale.name as FacetType,
113-
field: getFieldName(scale.domain.fields.find((field) => keys.includes(getFieldName(field)))),
90+
field: scale.domain.fields.find((field) => keys.includes(field.toString()))?.toString() as string,
11491
});
11592
} else {
11693
continuousFacets.push({
11794
facetType: scale.name as FacetType,
118-
field: getFieldName(scale.domain.fields.find((field) => keys.includes(getFieldName(field)))),
95+
field: scale.domain.fields.find((field) => keys.includes(field.toString()))?.toString() as string,
11996
});
12097
}
12198
}
@@ -130,7 +107,7 @@ export const getFacetsFromKeys = (
130107
* @returns boolean
131108
*/
132109
const scaleHasKey = (scale: ScaleWithMultiFields, keys: string[]): boolean =>
133-
scale.domain.fields.some((field) => keys.includes(getFieldName(field)));
110+
scale.domain.fields.some((field) => keys.includes(field.toString()));
134111

135112
type ScaleWithMultiFields = Scale & { domain: ScaleMultiFieldsRef };
136113

0 commit comments

Comments
 (0)