Skip to content
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
18 changes: 9 additions & 9 deletions .tests/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,30 +176,30 @@ describe("External Validation Tests", () => {

describe("Data Conversions", () => {

// Reference: SI decimal prefixes (not binary)
// Reference: IEC binary prefixes (not SI decimal)
test("1 byte = 8 bits (exact)", () => {
const result = polyconvert.data.byte.bit(1)
expect(result).toBe(8)
})

test("1 kilobyte = 1000 bytes (SI decimal)", () => {
test("1 kibibyte = 1024 bytes (IEC binary)", () => {
const result = polyconvert.data.kilobyte.byte(1)
expect(result).toBe(1000)
expect(result).toBe(1024)
})

test("1 megabyte = 1000000 bytes (SI decimal)", () => {
test("1 mebibyte = 1048576 bytes (IEC binary)", () => {
const result = polyconvert.data.megabyte.byte(1)
expect(result).toBe(1000000)
expect(result).toBe(1048576)
})

test("1 gigabyte = 1000000000 bytes (SI decimal)", () => {
test("1 gibibyte = 1073741824 bytes (IEC binary)", () => {
const result = polyconvert.data.gigabyte.byte(1)
expect(result).toBe(1000000000)
expect(result).toBe(1073741824)
})

test("1 terabyte = 1000 gigabytes (SI decimal)", () => {
test("1 tebibyte = 1024 gibibytes (IEC binary)", () => {
const result = polyconvert.data.terabyte.gigabyte(1)
expect(result).toBe(1000)
expect(result).toBe(1024)
})
})

Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"gradian",
"hectogray",
"jgphilpott",
"kibibyte",
"kiloampere",
"kilogray",
"megagray",
Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,29 @@ The third level keys represent all the different units of measurement that you c
polyconvert.data.bit = {

bit: f(x) = x,
byte: f(x) = x/8,
kilobyte: f(x) = x/8e+3,
megabyte: f(x) = x/8e+6,
gigabyte: f(x) = x/8e+9,
terabyte: f(x) = x/8e+12,
petabyte: f(x) = x/8e+15,
exabyte: f(x) = x/8e+18,
zettabyte: f(x) = x/8e+21,
yottabyte: f(x) = x/8e+24
byte: f(x) = x / 8,
kilobyte: f(x) = x / (8 * 1024),
megabyte: f(x) = x / (8 * 1024 ** 2),
gigabyte: f(x) = x / (8 * 1024 ** 3),
terabyte: f(x) = x / (8 * 1024 ** 4),
petabyte: f(x) = x / (8 * 1024 ** 5),
exabyte: f(x) = x / (8 * 1024 ** 6),
zettabyte: f(x) = x / (8 * 1024 ** 7),
yottabyte: f(x) = x / (8 * 1024 ** 8)

}
```

So, to use these functions, reference them through the `polyconvert` object and pass in the value you want to convert, like this:

```js
polyconvert.data.bit.byte(100) // Returns 12.5 meaning that 100 bits equals 12.5 bytes
polyconvert.data.kilobyte.byte(1) // Returns 1024 meaning that 1 kibibyte equals 1024 bytes
```

To reverse the conversion simply switch the order of the unit keys, like this:

```js
polyconvert.data.byte.bit(12.5) // Returns 100 meaning that 12.5 bytes equals 100 bits
polyconvert.data.byte.kilobyte(1024) // Returns 1 meaning that 1024 bytes equals 1 kibibyte
```

</details>
Expand Down Expand Up @@ -325,7 +325,7 @@ polyconvert.data = {

<sub>
<em>
Note: These data units use decimal (SI) prefixes (k=10^3, M=10^6, etc.). Binary IEC units (kibibyte KiB = 1024 bytes, mebibyte MiB, etc.) are not included. If you need binary units open a feature request.
Note: These data units use binary (IEC) prefixes (KiB=1024 bytes, MiB=1024 KiB, etc.). The unit names use traditional naming (kilobyte, megabyte, etc.) but follow IEC 60027-2 binary standards where each step is 1024 (2^10) rather than 1000.
</em>
</sub>

Expand Down
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jgphilpott/polyconvert",
"version": "1.0.4",
"version": "1.0.5",
"description": "A library of functions for converting between different units of measurement.",
"funding": "https://github.com/sponsors/jgphilpott",
"main": "polyconvert.js",
Expand Down Expand Up @@ -57,6 +57,7 @@
},
"homepage": "https://github.com/jgphilpott/polyconvert#readme",
"devDependencies": {
"baseline-browser-mapping": "^2.9.11",
"coffeescript": "^2.7.0",
"jest": "^30.2.0",
"uglify-js": "^3.19.3"
Expand Down
Loading