-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { IMPORT_LABEL } from '../constants'; | ||
import { formatLabels, mergeLabels } from './labels-utils'; | ||
|
||
describe('formatLabels', () => { | ||
it('should format labels correctly', () => { | ||
const input = ['Example Label', 'AnotherExampleLabelThatIsWayTooLongAndShouldBeTruncated']; | ||
const expectedOutput = ['example-label', 'anotherexamplelabelthatiswaytoolongandsh']; | ||
|
||
const result = formatLabels(input); | ||
|
||
expect(result).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should handle empty array', () => { | ||
const input: string[] = []; | ||
const expectedOutput: string[] = []; | ||
|
||
const result = formatLabels(input); | ||
|
||
expect(result).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should not alter labels shorter than 40 characters', () => { | ||
const input = ['short', 'medium length label']; | ||
const expectedOutput = ['short', 'medium-length-label']; | ||
|
||
const result = formatLabels(input); | ||
|
||
expect(result).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should convert spaces to hyphens and lowercase all characters', () => { | ||
const input = ['Mixed CASE Label', 'Label With Spaces']; | ||
const expectedOutput = ['mixed-case-label', 'label-with-spaces']; | ||
|
||
const result = formatLabels(input); | ||
|
||
expect(result).toEqual(expectedOutput); | ||
}); | ||
}); | ||
|
||
describe('mergeLabels', () => { | ||
it('should return an array with IMPORT_LABEL when both projectLabels and currentLabels are empty', () => { | ||
const result = mergeLabels([], []); | ||
expect(result).toEqual([IMPORT_LABEL]); | ||
}); | ||
|
||
it('should return formatted projectLabels with IMPORT_LABEL when currentLabels is empty', () => { | ||
const projectLabels = ['Label1', 'Label2']; | ||
const result = mergeLabels(projectLabels, []); | ||
expect(result).toEqual([IMPORT_LABEL, 'label1', 'label2']); | ||
}); | ||
|
||
it('should return formatted currentLabels with IMPORT_LABEL when projectLabels is empty', () => { | ||
const currentLabels = ['label2', 'label3']; | ||
const result = mergeLabels([], currentLabels); | ||
expect(result).toEqual([IMPORT_LABEL, 'label2', 'label3']); | ||
}); | ||
|
||
it('should merge and deduplicate labels from projectLabels and currentLabels, including IMPORT_LABEL', () => { | ||
const projectLabels = ['Label1', 'Label2']; | ||
const currentLabels = ['label2', 'label3']; | ||
const result = mergeLabels(projectLabels, currentLabels); | ||
expect(result).toEqual([IMPORT_LABEL, 'label1', 'label2', 'label3']); | ||
}); | ||
|
||
it('should return up to 20 labels including IMPORT_LABEL', () => { | ||
const projectLabels = Array.from({ length: 15 }, (_, i) => `Label${i + 1}`); | ||
const currentLabels = Array.from({ length: 10 }, (_, i) => `label${i + 16}`); | ||
const result = mergeLabels(projectLabels, currentLabels); | ||
expect(result.length).toBe(20); | ||
expect(result).toContain(IMPORT_LABEL); | ||
}); | ||
|
||
it('should include IMPORT_LABEL if not present in the combined labels', () => { | ||
const projectLabels = ['Label1', 'Label2']; | ||
const currentLabels = ['label3', 'label4']; | ||
const result = mergeLabels(projectLabels, currentLabels); | ||
expect(result).toContain(IMPORT_LABEL); | ||
}); | ||
|
||
it('should not duplicate IMPORT_LABEL if it is already present', () => { | ||
const projectLabels = ['Label1', 'Label2']; | ||
const currentLabels = ['label3', IMPORT_LABEL]; | ||
const result = mergeLabels(projectLabels, currentLabels); | ||
expect(result.filter((label) => label === IMPORT_LABEL).length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { IMPORT_LABEL } from '../constants'; | ||
|
||
// Converts the topics from a GitLab project to a list of formatted labels for a Compass component. | ||
export const formatLabels = (labels: string[]): string[] => { | ||
return labels.map((label) => { | ||
const transformedLabel = label.split(' ').join('-').toLowerCase(); | ||
return transformedLabel.length > 40 ? transformedLabel.slice(0, 40) : transformedLabel; | ||
}); | ||
}; | ||
|
||
// Helper function to merge the project labels with the current labels | ||
export function mergeLabels(projectLabels: string[], currentLabels: string[]): string[] { | ||
const formattedLabels = formatLabels(projectLabels); | ||
const labels = currentLabels ? [...currentLabels, ...formattedLabels] : formattedLabels; | ||
|
||
// Deduplicate and sort the labels for consistency | ||
const uniqueLabels = Array.from(new Set(labels)); | ||
uniqueLabels.sort(); | ||
|
||
// Get up to 20 labels | ||
let trimmedLabels = uniqueLabels.slice(0, 20); | ||
|
||
// If trimmedLabels doesn't include IMPORT_LABEL, add it to the trimmed labels | ||
if (!trimmedLabels.includes(IMPORT_LABEL)) { | ||
trimmedLabels = [IMPORT_LABEL, ...trimmedLabels].slice(0, 20); | ||
} | ||
|
||
return trimmedLabels; | ||
} |