diff --git a/packages/utils/CHANGELOG.md b/packages/utils/CHANGELOG.md index 2a9e19d..80e7793 100644 --- a/packages/utils/CHANGELOG.md +++ b/packages/utils/CHANGELOG.md @@ -1,5 +1,11 @@ # @emqx/shared-ui-utils +## 0.0.9 + +### Patch Changes + +- Add format functions + ## 0.0.8 ### Patch Changes diff --git a/packages/utils/lib/__test__/format.test.ts b/packages/utils/lib/__test__/format.test.ts new file mode 100644 index 0000000..9064d22 --- /dev/null +++ b/packages/utils/lib/__test__/format.test.ts @@ -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', + ) + }) +}) diff --git a/packages/utils/lib/format.ts b/packages/utils/lib/format.ts new file mode 100644 index 0000000..f7e6d68 --- /dev/null +++ b/packages/utils/lib/format.ts @@ -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]}` +} diff --git a/packages/utils/lib/index.ts b/packages/utils/lib/index.ts index d97bbf6..12ca466 100644 --- a/packages/utils/lib/index.ts +++ b/packages/utils/lib/index.ts @@ -1,2 +1,3 @@ export * from './objectUtils' export * from './jsonUtils' +export * from './format' diff --git a/packages/utils/package.json b/packages/utils/package.json index 967775b..9049dbd 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -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": { diff --git a/packages/utils/src/main.ts b/packages/utils/src/main.ts index 3d8db70..472ff26 100644 --- a/packages/utils/src/main.ts +++ b/packages/utils/src/main.ts @@ -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 } } }))