Skip to content

Commit 34939ac

Browse files
authored
Merge pull request #4 from jgphilpott/copilot/update-data-category-units
Convert data category from SI decimal (1000) to IEC binary (1024) units
2 parents dc7b062 + ae231d6 commit 34939ac

File tree

8 files changed

+284
-254
lines changed

8 files changed

+284
-254
lines changed

.tests/validation.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,30 +176,30 @@ describe("External Validation Tests", () => {
176176

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

179-
// Reference: SI decimal prefixes (not binary)
179+
// Reference: IEC binary prefixes (not SI decimal)
180180
test("1 byte = 8 bits (exact)", () => {
181181
const result = polyconvert.data.byte.bit(1)
182182
expect(result).toBe(8)
183183
})
184184

185-
test("1 kilobyte = 1000 bytes (SI decimal)", () => {
185+
test("1 kibibyte = 1024 bytes (IEC binary)", () => {
186186
const result = polyconvert.data.kilobyte.byte(1)
187-
expect(result).toBe(1000)
187+
expect(result).toBe(1024)
188188
})
189189

190-
test("1 megabyte = 1000000 bytes (SI decimal)", () => {
190+
test("1 mebibyte = 1048576 bytes (IEC binary)", () => {
191191
const result = polyconvert.data.megabyte.byte(1)
192-
expect(result).toBe(1000000)
192+
expect(result).toBe(1048576)
193193
})
194194

195-
test("1 gigabyte = 1000000000 bytes (SI decimal)", () => {
195+
test("1 gibibyte = 1073741824 bytes (IEC binary)", () => {
196196
const result = polyconvert.data.gigabyte.byte(1)
197-
expect(result).toBe(1000000000)
197+
expect(result).toBe(1073741824)
198198
})
199199

200-
test("1 terabyte = 1000 gigabytes (SI decimal)", () => {
200+
test("1 tebibyte = 1024 gibibytes (IEC binary)", () => {
201201
const result = polyconvert.data.terabyte.gigabyte(1)
202-
expect(result).toBe(1000)
202+
expect(result).toBe(1024)
203203
})
204204
})
205205

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"gradian",
1313
"hectogray",
1414
"jgphilpott",
15+
"kibibyte",
1516
"kiloampere",
1617
"kilogray",
1718
"megagray",

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,29 +142,29 @@ The third level keys represent all the different units of measurement that you c
142142
polyconvert.data.bit = {
143143

144144
bit: f(x) = x,
145-
byte: f(x) = x/8,
146-
kilobyte: f(x) = x/8e+3,
147-
megabyte: f(x) = x/8e+6,
148-
gigabyte: f(x) = x/8e+9,
149-
terabyte: f(x) = x/8e+12,
150-
petabyte: f(x) = x/8e+15,
151-
exabyte: f(x) = x/8e+18,
152-
zettabyte: f(x) = x/8e+21,
153-
yottabyte: f(x) = x/8e+24
145+
byte: f(x) = x / 8,
146+
kilobyte: f(x) = x / (8 * 1024),
147+
megabyte: f(x) = x / (8 * 1024 ** 2),
148+
gigabyte: f(x) = x / (8 * 1024 ** 3),
149+
terabyte: f(x) = x / (8 * 1024 ** 4),
150+
petabyte: f(x) = x / (8 * 1024 ** 5),
151+
exabyte: f(x) = x / (8 * 1024 ** 6),
152+
zettabyte: f(x) = x / (8 * 1024 ** 7),
153+
yottabyte: f(x) = x / (8 * 1024 ** 8)
154154

155155
}
156156
```
157157

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

160160
```js
161-
polyconvert.data.bit.byte(100) // Returns 12.5 meaning that 100 bits equals 12.5 bytes
161+
polyconvert.data.kilobyte.byte(1) // Returns 1024 meaning that 1 kibibyte equals 1024 bytes
162162
```
163163

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

166166
```js
167-
polyconvert.data.byte.bit(12.5) // Returns 100 meaning that 12.5 bytes equals 100 bits
167+
polyconvert.data.byte.kilobyte(1024) // Returns 1 meaning that 1024 bytes equals 1 kibibyte
168168
```
169169

170170
</details>
@@ -325,7 +325,7 @@ polyconvert.data = {
325325

326326
<sub>
327327
<em>
328-
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.
328+
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.
329329
</em>
330330
</sub>
331331

package-lock.json

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jgphilpott/polyconvert",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "A library of functions for converting between different units of measurement.",
55
"funding": "https://github.com/sponsors/jgphilpott",
66
"main": "polyconvert.js",
@@ -57,6 +57,7 @@
5757
},
5858
"homepage": "https://github.com/jgphilpott/polyconvert#readme",
5959
"devDependencies": {
60+
"baseline-browser-mapping": "^2.9.11",
6061
"coffeescript": "^2.7.0",
6162
"jest": "^30.2.0",
6263
"uglify-js": "^3.19.3"

0 commit comments

Comments
 (0)