Skip to content
Merged
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
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