Skip to content

Commit

Permalink
fix: Properly encode uuid (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed Aug 14, 2023
1 parent e8bb0a0 commit 72efa22
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/scalar/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FixedSizeBinary } from '@apache-arrow/esnext-esm';
import { validate } from 'uuid';
import { parse, stringify } from 'uuid';

import { Nullable } from '../schema/types.js';

Expand Down Expand Up @@ -33,14 +33,15 @@ export class UUID implements Scalar<Nullable<Uint8Array>> {
}

if (typeof value === 'string') {
this._value = new TextEncoder().encode(value);
this._valid = validate(value);
// parse throws on invalid uuids
this._value = parse(value);
this._valid = true;
return;
}

if (value instanceof Uint8Array) {
this._value = value;
this._valid = validate(new TextDecoder().decode(value));
this._valid = true;
return;
}

Expand All @@ -51,8 +52,9 @@ export class UUID implements Scalar<Nullable<Uint8Array>> {
}

if (typeof value!.toString === 'function' && value!.toString !== Object.prototype.toString) {
this._value = Buffer.from(value!.toString());
this._valid = validate(value!.toString());
// parse throws on invalid uuids
this._value = parse(value!.toString());
this._valid = true;
return;
}

Expand All @@ -61,7 +63,7 @@ export class UUID implements Scalar<Nullable<Uint8Array>> {

public toString() {
if (this._valid) {
return new TextDecoder().decode(this._value);
return stringify(this._value!);
}

return NULL_VALUE;
Expand Down

0 comments on commit 72efa22

Please sign in to comment.