convertDataUnit
Converts data between different units (e.g., bits, bytes, kilobytes, etc.).
Supports binary prefix and decimal prefix.
Binary prefixes are based on powers of 2 (e.g., 1 KiB = 2^10 bytes) defined by the International Electrotechnical Commission (IEC), while decimal prefixes are part of the International System of Units (SI) based on powers of 10 (e.g., 1 kB = 10^3 bytes).
Added in v0.0.2
Usage
ts
import { convertDataUnit } from 'parsnip-kit'
convertDataUnit(1, 'B', 'bit', 'binary') // 8
convertDataUnit(1, 'B', 'bit', 'decimal') // 8
convertDataUnit(1024, 'B', 'KB', 'binary') // 1
convertDataUnit(1000, 'B', 'KB', 'decimal') // 1
API
Type Parameter
Arguments
Arg | Type | Optional | Default | Description |
---|---|---|---|---|
value | number | false | undefined | The value of the data size to be converted |
from | DataUnit | false | undefined | The original unit of the data size |
to | DataUnit | false | undefined | The target data unit to convert to |
prefix | 'binary' | 'decimal' | false | undefined | The type of prefix to use in conversion (binary or decimal), defaults to 'binary' |
Returns
Type |
---|
number |