Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"dependencies": {
"@apache-arrow/esnext-esm": "^12.0.1",
"@cloudquery/plugin-pb-javascript": "^0.0.6",
"boolean": "^3.2.0",
"yargs": "^17.7.2"
}
}
35 changes: 35 additions & 0 deletions src/scalar/bool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import test from 'ava';
import { DataType } from '@apache-arrow/esnext-esm';
import { Bool } from './bool.js';

[null, undefined].forEach((v) => {
test(`should set values to false when ${v} is passed`, (t) => {
const b = new Bool(v);
t.is(b.Valid, false);
t.true(DataType.isBool(b.DataType));
});
});

[1, true, 'true', 'Y', 'y', 'TRUE', 'on', new Bool(true)].forEach((v, i) => {
test(`should support truthy value '${v}' (${i})`, (t) => {
const b = new Bool(v);
t.is(b.Valid, true);
t.is(b.Value, true);
t.true(DataType.isBool(b.DataType));
t.is(b.toString(), 'true');
});
});

[0, false, 'false', 'N', 'n', 'FALSE', 'off', new Bool(false)].forEach((v, i) => {
test(`should support falsy value '${v}' (${i})`, (t) => {
const b = new Bool(v);
t.is(b.Valid, true);
t.is(b.Value, false);
t.true(DataType.isBool(b.DataType));
t.is(b.toString(), 'false');
});
});

test('should throw when unable to set value', (t) => {
t.throws(() => new Bool({ value: {} }), { message: "Unable to set '[object Object]' as Bool" });
});
54 changes: 54 additions & 0 deletions src/scalar/bool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Bool as ArrowBool } from '@apache-arrow/esnext-esm';
import { boolean, isBooleanable } from 'boolean';
import { isInvalid, NULL_VALUE } from './util.js';

export class Bool {
private _valid = false;
private _value = false;

public constructor(v: unknown) {
this.Valid = v;
return this;
}

public get DataType() {
return new ArrowBool();
}

public get Valid(): boolean {
return this._valid;
}

public get Value(): boolean {
return this._value;
}

public set Valid(value: unknown) {
if (isInvalid(value)) {
this._valid = false;
return;
}

if (value instanceof Bool) {
this._valid = value.Valid;
this._value = value.Value;
return;
}

if (isBooleanable(value)) {
this._value = boolean(value);
this._valid = true;
return;
}

throw new Error(`Unable to set '${value}' as Bool`);
}

public toString() {
if (this._valid) {
return String(this._value);
}

return NULL_VALUE;
}
}
3 changes: 3 additions & 0 deletions src/scalar/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NULL_VALUE = '(null)';

export const isInvalid = (value: unknown) => value === null || value === undefined;