-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(utils): split out transform helper (#240)
- Loading branch information
Showing
7 changed files
with
155 additions
and
125 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { | ||
countOccurrences, | ||
distinct, | ||
objectToEntries, | ||
objectToKeys, | ||
pluralize, | ||
slugify, | ||
toArray, | ||
} from './transformation'; | ||
|
||
describe('slugify', () => { | ||
it.each([ | ||
['Largest Contentful Paint', 'largest-contentful-paint'], | ||
['cumulative-layout-shift', 'cumulative-layout-shift'], | ||
['max-lines-200', 'max-lines-200'], | ||
['rxjs/finnish', 'rxjs-finnish'], | ||
['@typescript-eslint/no-explicit-any', 'typescript-eslint-no-explicit-any'], | ||
['Code PushUp ', 'code-pushup'], | ||
])('should transform "%s" to valid slug "%s"', (text, slug) => { | ||
expect(slugify(text)).toBe(slug); | ||
}); | ||
}); | ||
|
||
describe('pluralize', () => { | ||
it.each([ | ||
['warning', 'warnings'], | ||
['error', 'errors'], | ||
['category', 'categories'], | ||
['status', 'statuses'], | ||
])('should pluralize "%s" as "%s"', (singular, plural) => { | ||
expect(pluralize(singular)).toBe(plural); | ||
}); | ||
}); | ||
|
||
describe('toArray', () => { | ||
it('should transform non-array value into array with single value', () => { | ||
expect(toArray('src/**/*.ts')).toEqual(['src/**/*.ts']); | ||
}); | ||
|
||
it('should leave array value unchanged', () => { | ||
expect(toArray(['*.ts', '*.js'])).toEqual(['*.ts', '*.js']); | ||
}); | ||
}); | ||
|
||
describe('objectToKeys', () => { | ||
it('should transform object into array of keys', () => { | ||
const keys: 'prop1'[] = objectToKeys({ prop1: 1 }); | ||
expect(keys).toEqual(['prop1']); | ||
}); | ||
|
||
it('should transform empty object into empty array', () => { | ||
const keys: never[] = objectToKeys({}); | ||
expect(keys).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('objectToEntries', () => { | ||
it('should transform object into array of entries', () => { | ||
const keys: ['prop1', number][] = objectToEntries({ prop1: 1 }); | ||
expect(keys).toEqual([['prop1', 1]]); | ||
}); | ||
|
||
it('should transform empty object into empty array', () => { | ||
const keys: [never, never][] = objectToEntries({}); | ||
expect(keys).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('countOccurrences', () => { | ||
it('should return record with counts for each item', () => { | ||
expect( | ||
countOccurrences(['error', 'warning', 'error', 'error', 'warning']), | ||
).toEqual({ error: 3, warning: 2 }); | ||
}); | ||
|
||
it('should return empty record for no matches', () => { | ||
expect(countOccurrences([])).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe('distinct', () => { | ||
it('should remove duplicate strings from array', () => { | ||
expect( | ||
distinct([ | ||
'no-unused-vars', | ||
'no-invalid-regexp', | ||
'no-unused-vars', | ||
'no-invalid-regexp', | ||
'@typescript-eslint/no-unused-vars', | ||
]), | ||
).toEqual([ | ||
'no-unused-vars', | ||
'no-invalid-regexp', | ||
'@typescript-eslint/no-unused-vars', | ||
]); | ||
}); | ||
}); |
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,42 @@ | ||
export function slugify(text: string): string { | ||
return text | ||
.trim() | ||
.toLowerCase() | ||
.replace(/\s+|\//g, '-') | ||
.replace(/[^a-z0-9-]/g, ''); | ||
} | ||
|
||
export function pluralize(text: string): string { | ||
if (text.endsWith('y')) { | ||
return text.slice(0, -1) + 'ies'; | ||
} | ||
if (text.endsWith('s')) { | ||
return `${text}es`; | ||
} | ||
return `${text}s`; | ||
} | ||
|
||
export function toArray<T>(val: T | T[]): T[] { | ||
return Array.isArray(val) ? val : [val]; | ||
} | ||
|
||
export function objectToKeys<T extends object>(obj: T) { | ||
return Object.keys(obj) as (keyof T)[]; | ||
} | ||
|
||
export function objectToEntries<T extends object>(obj: T) { | ||
return Object.entries(obj) as [keyof T, T[keyof T]][]; | ||
} | ||
|
||
export function countOccurrences<T extends PropertyKey>( | ||
values: T[], | ||
): Partial<Record<T, number>> { | ||
return values.reduce<Partial<Record<T, number>>>( | ||
(acc, value) => ({ ...acc, [value]: (acc[value] ?? 0) + 1 }), | ||
{}, | ||
); | ||
} | ||
|
||
export function distinct<T extends string | number | boolean>(array: T[]): T[] { | ||
return Array.from(new Set(array)); | ||
} |