Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(units): add formatValueToMinUnit function #28

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/utils/lib/__test__/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { formatSizeUnit } from '../format.ts'
import { formatSizeUnit, formatValueToMinUnit } from '../format'

describe('formatSizeUnit', () => {
it('should correctly format bytes', () => {
Expand All @@ -21,3 +21,20 @@ describe('formatSizeUnit', () => {
)
})
})

describe('formatValueToMinUnit', () => {
it('should correctly format value with unit', () => {
expect(formatValueToMinUnit(10, 'KB')).toBe(10240)
expect(formatValueToMinUnit(5, 'MB')).toBe(5242880)
expect(formatValueToMinUnit(1, 's')).toBe(1000)
})

it('should throw an error for invalid unit', () => {
// @ts-ignore
expect(() => formatValueToMinUnit(10, 'TB')).toThrow('Invalid unit: TB')
// @ts-ignore
expect(() => formatValueToMinUnit(5, 'PB')).toThrow('Invalid unit: PB')
// @ts-ignore
expect(() => formatValueToMinUnit(10, 'S')).toThrow('Invalid unit: S')
})
})
33 changes: 33 additions & 0 deletions packages/utils/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,36 @@ export const formatSizeUnit = (val: number) => {
const size = (val / 1024 ** index).toFixed(1)
return `${size} ${unitArr[index]}`
}

/**
* Converts a given value from one unit to its equivalent in the smallest unit of the same type.
*
* For example, it converts a value in kilobytes (KB) to bytes (B), or a value in hours (h) to milliseconds (ms).
* The smallest unit for time is millisecond (ms), and for size is byte (B).
*
* @param {number} value - The numeric value to be converted.
* @param {string} unit - The unit of the value, which is a key of the SizeOrTimeUnit enum.
* @returns {number} The input value converted to the smallest unit.
* @throws {Error} If the provided unit is not a key in the SizeOrTimeUnit enum.
*
* @example
* // returns 1024, converts 1 kilobyte to bytes
* formatValueToMinUnit(1, 'KB');
*/
enum SizeOrTimeUnit {
ms = 1,
s = 1000,
m = 1000 * 60,
h = 1000 * 60 * 60,
d = 1000 * 60 * 60 * 24,
B = 1,
KB = 1024,
MB = 1024 * 1024,
GB = 1024 * 1024 * 1024,
}
export const formatValueToMinUnit = (value: number, unit: keyof typeof SizeOrTimeUnit) => {
if (!(unit in SizeOrTimeUnit)) {
throw new Error(`Invalid unit: ${unit}`)
}
return value * SizeOrTimeUnit[unit]
}
Loading