-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): add file-system helper (#336)
- Loading branch information
Showing
15 changed files
with
305 additions
and
171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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 formatBytes(bytes: number, decimals = 2) { | ||
if (!+bytes) return '0 B'; | ||
|
||
const k = 1024; | ||
const dm = decimals < 0 ? 0 : decimals; | ||
const sizes = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
|
||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; | ||
} | ||
|
||
export function pluralizeToken(token: string, times: number = 0): string { | ||
return `${times} ${Math.abs(times) === 1 ? token : pluralize(token)}`; | ||
} | ||
|
||
export function formatDuration(duration: number): string { | ||
if (duration < 1000) { | ||
return `${duration} ms`; | ||
} | ||
return `${(duration / 1000).toFixed(2)} s`; | ||
} |
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,80 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { | ||
formatBytes, | ||
formatDuration, | ||
pluralize, | ||
pluralizeToken, | ||
slugify, | ||
} from './formatting'; | ||
|
||
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('formatBytes', () => { | ||
it.each([ | ||
[0, '0 B'], | ||
[1000, '1000 B'], | ||
[10000, '9.77 kB'], | ||
[10000000, '9.54 MB'], | ||
[10000000000, '9.31 GB'], | ||
[10000000000000, '9.09 TB'], | ||
[10000000000000000, '8.88 PB'], | ||
[10000000000000000000, '8.67 EB'], | ||
[10000000000000000000000, '8.47 ZB'], | ||
[10000000000000000000000000, '8.27 YB'], | ||
[10000000000000000000000, '8.47 ZB'], | ||
[10000000000000000000000, '8.47 ZB'], | ||
])('should log file sizes correctly for %s`', (bytes, displayValue) => { | ||
expect(formatBytes(bytes)).toBe(displayValue); | ||
}); | ||
|
||
it('should log file sizes correctly with correct decimal`', () => { | ||
expect(formatBytes(10000, 1)).toBe('9.8 kB'); | ||
}); | ||
}); | ||
|
||
describe('pluralizeToken', () => { | ||
it.each([ | ||
[undefined, '0 files'], | ||
[-2, '-2 files'], | ||
[-1, '-1 file'], | ||
[0, '0 files'], | ||
[1, '1 file'], | ||
[2, '2 files'], | ||
])('should log correct plural for %s`', (times, plural) => { | ||
expect(pluralizeToken('file', times)).toBe(plural); | ||
}); | ||
}); | ||
|
||
describe('formatDuration', () => { | ||
it.each([ | ||
[-1, '-1 ms'], | ||
[0, '0 ms'], | ||
[1, '1 ms'], | ||
[2, '2 ms'], | ||
[1200, '1.20 s'], | ||
])('should log correct plural for %s`', (ms, displayValue) => { | ||
expect(formatDuration(ms)).toBe(displayValue); | ||
}); | ||
}); |
File renamed without changes.
5 changes: 1 addition & 4 deletions
5
...utils/src/lib/promise-result.unit.test.ts → packages/utils/src/lib/guards.unit.test.ts
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
Oops, something went wrong.