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(utils): add utils format function #7

Merged
merged 2 commits into from
Nov 30, 2023
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
6 changes: 6 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @emqx/shared-ui-utils

## 0.0.9

### Patch Changes

- Add format functions

## 0.0.8

### Patch Changes
Expand Down
21 changes: 21 additions & 0 deletions packages/utils/lib/__test__/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, it, expect } from 'vitest'
import { formatSizeUnit } from '../format.ts'

describe('formatSizeUnit', () => {
it('should correctly format bytes', () => {
expect(formatSizeUnit(1024)).toBe('1.0 KB')
expect(formatSizeUnit(1048576)).toBe('1.0 MB')
expect(formatSizeUnit(0)).toBe('0 Bytes')
expect(formatSizeUnit(12969)).toBe('12.7 KB')
})

it('should throw an error for invalid input', () => {
expect(() => formatSizeUnit(-1)).toThrow('Invalid input: input should be a non-negative number')
expect(() => formatSizeUnit(null)).toThrow(
'Invalid input: input should be a non-negative number',
)
expect(() => formatSizeUnit(undefined)).toThrow(
'Invalid input: input should be a non-negative number',
)
})
})
18 changes: 18 additions & 0 deletions packages/utils/lib/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Formats a size unit value into a human-readable string representation.
* @param val - The size unit value to format.
* @returns A string representing the formatted size unit value.
* @throws {Error} If the input is not a non-negative number.
*/
export const formatSizeUnit = (val: number) => {
if (typeof val !== 'number' || isNaN(val) || val < 0) {
throw new Error('Invalid input: input should be a non-negative number')
}
if (val === 0) {
return '0 Bytes'
}
const unitArr = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const index = Math.floor(Math.log(val) / Math.log(1024))
const size = (val / 1024 ** index).toFixed(1)
return `${size} ${unitArr[index]}`
}
1 change: 1 addition & 0 deletions packages/utils/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './objectUtils'
export * from './jsonUtils'
export * from './format'
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@emqx/shared-ui-utils",
"version": "0.0.8",
"version": "0.0.9",
"homepage": "https://emqx.io",
"license": "Apache-2.0",
"repository": {
Expand Down
4 changes: 3 additions & 1 deletion packages/utils/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { flattenObject, unflattenObject } from '../lib'
import { formatSizeUnit, flattenObject, unflattenObject } from '../lib'

console.log(formatSizeUnit(12969))

console.log('Output of flattenObject function: ', flattenObject({ a: { b: { c: 1 } } }))

Expand Down