@cloudquery/plugin-sdk-javascript
Version:
This is the high-level package to use for developing CloudQuery plugins in JavaScript
64 lines • 1.88 kB
JavaScript
import { Uint16 as ArrowUint16 } from '@apache-arrow/esnext-esm';
import { bigIntToNumber } from '@apache-arrow/esnext-esm/util/bigint.js';
import { FormatError } from '../errors/errors.js';
import { isInvalid, NULL_VALUE } from './util.js';
export class Uint16 {
_valid = false;
_value = null;
constructor(v) {
this.value = v;
return this;
}
get dataType() {
return new ArrowUint16();
}
get valid() {
return this._valid;
}
get value() {
if (!this._valid) {
return null;
}
return this._value;
}
set value(value) {
if (isInvalid(value)) {
this._valid = false;
return;
}
if (value instanceof Uint16) {
this._valid = value.valid;
this._value = value.value;
return;
}
if (typeof value === 'bigint') {
if (!this.validUint16(value)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Uint16`);
}
this._value = value;
this._valid = true;
return;
}
if (typeof value === 'number') {
const v = BigInt(value);
if (!this.validUint16(v)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Uint16`);
}
this._value = v;
this._valid = true;
return;
}
throw new FormatError(`Unable to set Uint16 from value`, { props: { value } });
}
toString() {
if (this._valid) {
return String(this._value);
}
return NULL_VALUE;
}
validUint16(n) {
const number_ = bigIntToNumber(n);
return Number.isSafeInteger(number_) && number_ >= 0 && number_ <= 65_535;
}
}
//# sourceMappingURL=uint16.js.map