Skip to content

Commit

Permalink
fix: fix numberToUUID for values with leading zeroes (#261)
Browse files Browse the repository at this point in the history
* fix: get correct UUIDs when `value` has leading zeroes

* add spec test
  • Loading branch information
JFKakaJFK authored Jan 2, 2022
1 parent d03096d commit b335dcc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/conversion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ describe('numberToUUID', () => {
const result = numberToUUID(value);
expect(result).toEqual('0000180d-0000-1000-8000-00805f9b34fb');
});

it('should also work with leading zeroes', () => {
const value = 0x0042;
const result = numberToUUID(value);
expect(result).toEqual('00000042-0000-1000-8000-00805f9b34fb');
});
});

describe('hexStringToDataView', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function dataViewToText(value: DataView): string {
* @return string, e.g. '0000180d-0000-1000-8000-00805f9b34fb'
*/
export function numberToUUID(value: number): string {
return `0000${value.toString(16)}-0000-1000-8000-00805f9b34fb`;
return `0000${value.toString(16).padStart(4, '0')}-0000-1000-8000-00805f9b34fb`;
}

export function hexStringToDataView(value: string): DataView {
Expand Down

0 comments on commit b335dcc

Please sign in to comment.