@cloudquery/plugin-sdk-javascript
Version:
This is the high-level package to use for developing CloudQuery plugins in JavaScript
64 lines • 1.84 kB
JavaScript
import { Float32 as ArrowFloat32 } from '@apache-arrow/esnext-esm';
import { FormatError } from '../errors/errors.js';
import { isInvalid, NULL_VALUE } from './util.js';
export class Float32 {
_valid = false;
_value = null;
constructor(v) {
this.value = v;
return this;
}
get dataType() {
return new ArrowFloat32();
}
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 Float32) {
this._valid = value.valid;
this._value = value.value;
return;
}
if (typeof value === 'number') {
if (!this.validFloat32(value)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Float32`);
}
this._value = value;
this._valid = true;
return;
}
const floatValue = Number.parseFloat(String(value));
if (!Number.isNaN(floatValue)) {
if (!this.validFloat32(floatValue)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Float32`);
}
this._value = floatValue;
this._valid = true;
return;
}
throw new FormatError(`Unable to set Float32 from value`, { props: { value } });
}
toString() {
if (this._valid) {
return String(this._value);
}
return NULL_VALUE;
}
validFloat32(n) {
const float32 = new Float32Array(1);
float32[0] = n;
return float32[0] === n;
}
}
//# sourceMappingURL=float32.js.map