Skip to content

Commit

Permalink
feat(data): add Kb and KB units
Browse files Browse the repository at this point in the history
Closes #547
  • Loading branch information
jonahsnider committed Oct 13, 2022
1 parent ce218b4 commit ff242f7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/bundled-conversions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export const conversions = {
kibibit: [2, 1024],
kibibits: [2, 1024],
Kib: [2, 1024],
Kb: [2, 1000],
KB: [2, 8000],
petabit: [2, 1000000000000000],
petabits: [2, 1000000000000000],
Pb: [2, 1000000000000000],
Expand Down
8 changes: 8 additions & 0 deletions packages/bundled-conversions/test/snapshots/index.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ Generated by [AVA](https://avajs.dev).
9,
1,
],
KB: [
2,
8000,
],
Kb: [
2,
1000,
],
KiB: [
2,
8192,
Expand Down
Binary file modified packages/bundled-conversions/test/snapshots/index.test.ts.snap
Binary file not shown.
9 changes: 7 additions & 2 deletions packages/conversions/src/conversions/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export const data: ReadonlyDeep<Family> = {
conversions: [
{names: ['bit', 'bits'], symbols: ['b'], ratio: 1},

...expandMacro(Macros.binary, {names: ['bit', 'bits'], symbols: ['b'], ratio: 1}),
...expandMacro(Macros.iec, {names: ['bit', 'bits'], symbols: ['b'], ratio: 1}),
// IEC prefixes that aren't included under the macro
// Note that IEC says a Kb is 1024 bits and a KB is 8192 bits, but it seems like bad library design to make KB a different unit than kB, especially when MB is SI and not IEC
// Users can use KiB and Kib if they want IEC values
{names: [], symbols: ['Kb'], ratio: 1e3},
{names: [], symbols: ['KB'], ratio: 8e3},
...expandMacro(Macros.si, {names: ['bit', 'bits'], symbols: ['b'], kind: 'big', ratio: 1}),

{
Expand All @@ -21,7 +26,7 @@ export const data: ReadonlyDeep<Family> = {
},

{names: ['byte', 'bytes', 'octect', 'octects'], symbols: ['B'], ratio: 8},
...expandMacro(Macros.binary, {names: ['byte', 'bytes'], symbols: ['B'], ratio: 8}),
...expandMacro(Macros.iec, {names: ['byte', 'bytes'], symbols: ['B'], ratio: 8}),
...expandMacro(Macros.si, {names: ['byte', 'bytes'], symbols: ['B'], kind: 'big', ratio: 8}),

{names: ['hextet', 'hextets'], ratio: 16},
Expand Down
3 changes: 2 additions & 1 deletion packages/conversions/src/macros/definitions/binary.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type {Macro} from '../types.js';

/** @see https://en.wikipedia.org/wiki/Template:Bit_and_byte_prefixes Source */
export const binary: Macro = [
export const iec: Macro = [
{prefix: 'pebi', symbol: 'Pi', value: 1024 ** 5, kind: 'big'},
{prefix: 'tebi', symbol: 'Ti', value: 1024 ** 4, kind: 'big'},
{prefix: 'gibi', symbol: 'Gi', value: 1024 ** 3, kind: 'big'},
{prefix: 'mebi', symbol: 'Mi', value: 1024 ** 2, kind: 'big'},
// Limitations in macros (only a single symbol can be provided) means that we don't include K (note the capitalization) here
{prefix: 'kibi', symbol: 'Ki', value: 1024 ** 1, kind: 'big'},
];

0 comments on commit ff242f7

Please sign in to comment.