-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sizeConversionArray, sizeConversionString
- Loading branch information
fanyushun
committed
Nov 7, 2024
1 parent
ca51700
commit 9ed2ae8
Showing
5 changed files
with
129 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './sizeConversionBase' | ||
export * from './sizeConversionBase' | ||
export * from './sizeConversionArray' | ||
export * from './sizeConversionString' |
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,51 @@ | ||
import { type SizeUnitType } from './sizeConversionBase' | ||
|
||
export type SizeConversionArrayItemType = { value: number, unit: SizeUnitType, label: string } | ||
|
||
/** | ||
* **sizeConversionArray** | ||
* | ||
* <Badge type="tip" text="version: v0.0.12+" /> | ||
* | ||
* @group size-conversion | ||
* | ||
* @param value - Enter value | ||
* @param fromUnit - Enter value unit B/KB/MB/GB/TB/PB/EB/ZB/YB | ||
* @returns size array, { value: number, unit: SizeUnitType, label: string }[] | ||
* | ||
* @example | ||
* ```ts | ||
* sizeConversionArray(5000, 'GB') | ||
* [ | ||
* { value: 4, unit: 'TB', label: '4TB' }, | ||
* { value: 904, unit: 'GB', label: '904GB' } | ||
* ] | ||
* ``` | ||
*/ | ||
export function sizeConversionArray(value: number, fromUnit: SizeUnitType): SizeConversionArrayItemType[] { | ||
const unitArray: SizeUnitType[] = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | ||
|
||
const list: SizeConversionArrayItemType[] = [] | ||
let nowUnit = fromUnit | ||
let nowUnitIndex = unitArray.indexOf(nowUnit) | ||
while (value > 0) { | ||
if (nowUnitIndex === unitArray.length - 1) { | ||
list.push({ | ||
value, | ||
unit: nowUnit, | ||
label: `${value}${nowUnit}` | ||
}) | ||
return list | ||
} | ||
const nowValue = value % 1024 | ||
list.unshift({ | ||
value: nowValue, | ||
unit: nowUnit, | ||
label: `${nowValue}${nowUnit}` | ||
}) | ||
value = Math.floor(value / 1024) | ||
nowUnitIndex++ | ||
nowUnit = unitArray[nowUnitIndex] | ||
} | ||
return list | ||
} |
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,27 @@ | ||
import { type SizeUnitType } from './sizeConversionBase' | ||
import { sizeConversionArray } from './sizeConversionArray' | ||
|
||
/** | ||
* **sizeConversionString** | ||
* | ||
* <Badge type="tip" text="version: v0.0.12+" /> | ||
* | ||
* @group size-conversion | ||
* | ||
* @param value - Enter value | ||
* @param fromUnit - Enter value unit B/KB/MB/GB/TB/PB/EB/ZB/YB | ||
* @param symbol - Enter symbol, Used for connecting strings | ||
* @returns size string | ||
* | ||
* @example | ||
* ```ts | ||
* sizeConversionString(5000, 'GB') | ||
* // 4TB904GB | ||
* sizeConversionString(5000, 'GB', '-') | ||
* // 4TB-904GB | ||
* ``` | ||
*/ | ||
export function sizeConversionString(value: number, fromUnit: SizeUnitType, symbol?: string): string { | ||
const list = sizeConversionArray(value, fromUnit) | ||
return list.map(item => item.label).join(symbol ?? '') | ||
} |
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,36 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { sizeConversionArray } from '../../lib/main' | ||
|
||
describe('sizeConversionArray', () => { | ||
const res5000GB = [ | ||
{ value: 4, unit: 'TB', label: '4TB' }, | ||
{ value: 904, unit: 'GB', label: '904GB' } | ||
] | ||
test(`sizeConversionArray(5000, 'GB')`, () => { | ||
expect(sizeConversionArray(5000, 'GB')).toEqual(res5000GB) | ||
}) | ||
|
||
const res50000000MB = [ | ||
{ value: 47, unit: 'TB', label: '47TB' }, | ||
{ value: 700, unit: 'GB', label: '700GB' }, | ||
{ value: 128, unit: 'MB', label: '128MB' } | ||
] | ||
test(`sizeConversionArray(50000000, 'MB')`, () => { | ||
expect(sizeConversionArray(50000000, 'MB')).toEqual(res50000000MB) | ||
}) | ||
|
||
const res2000YB = [ | ||
{ value: 2000, unit: 'YB', label: '2000YB' } | ||
] | ||
test(`sizeConversionArray(2000, 'YB')`, () => { | ||
expect(sizeConversionArray(2000, 'YB')).toEqual(res2000YB) | ||
}) | ||
|
||
const res2000000ZB = [ | ||
{ value: 128, unit: 'ZB', label: '128ZB' }, | ||
{ value: 1953, unit: 'YB', label: '1953YB' } | ||
] | ||
test(`sizeConversionArray(2000000, 'ZB')`, () => { | ||
expect(sizeConversionArray(2000000, 'ZB')).toEqual(res2000000ZB) | ||
}) | ||
}) |
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,12 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { sizeConversionString } from '../../lib/main' | ||
|
||
describe('sizeConversionString', () => { | ||
test(`sizeConversionString(5000, 'GB')`, () => { | ||
expect(sizeConversionString(5000, 'GB')).toBe('4TB904GB') | ||
}) | ||
|
||
test(`sizeConversionString(5000, 'GB', '-')`, () => { | ||
expect(sizeConversionString(5000, 'GB', '-')).toBe('4TB-904GB') | ||
}) | ||
}) |