Skip to content

Commit 80edcd5

Browse files
authored
feat: Add Bool scalar (#17)
Part of #6
1 parent 8e5e0cd commit 80edcd5

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"dependencies": {
7272
"@apache-arrow/esnext-esm": "^12.0.1",
7373
"@cloudquery/plugin-pb-javascript": "^0.0.6",
74+
"boolean": "^3.2.0",
7475
"yargs": "^17.7.2"
7576
}
7677
}

src/scalar/bool.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import test from 'ava';
2+
import { DataType } from '@apache-arrow/esnext-esm';
3+
import { Bool } from './bool.js';
4+
5+
[null, undefined].forEach((v) => {
6+
test(`should set values to false when ${v} is passed`, (t) => {
7+
const b = new Bool(v);
8+
t.is(b.Valid, false);
9+
t.true(DataType.isBool(b.DataType));
10+
});
11+
});
12+
13+
[1, true, 'true', 'Y', 'y', 'TRUE', 'on', new Bool(true)].forEach((v, i) => {
14+
test(`should support truthy value '${v}' (${i})`, (t) => {
15+
const b = new Bool(v);
16+
t.is(b.Valid, true);
17+
t.is(b.Value, true);
18+
t.true(DataType.isBool(b.DataType));
19+
t.is(b.toString(), 'true');
20+
});
21+
});
22+
23+
[0, false, 'false', 'N', 'n', 'FALSE', 'off', new Bool(false)].forEach((v, i) => {
24+
test(`should support falsy value '${v}' (${i})`, (t) => {
25+
const b = new Bool(v);
26+
t.is(b.Valid, true);
27+
t.is(b.Value, false);
28+
t.true(DataType.isBool(b.DataType));
29+
t.is(b.toString(), 'false');
30+
});
31+
});
32+
33+
test('should throw when unable to set value', (t) => {
34+
t.throws(() => new Bool({ value: {} }), { message: "Unable to set '[object Object]' as Bool" });
35+
});

src/scalar/bool.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Bool as ArrowBool } from '@apache-arrow/esnext-esm';
2+
import { boolean, isBooleanable } from 'boolean';
3+
import { isInvalid, NULL_VALUE } from './util.js';
4+
5+
export class Bool {
6+
private _valid = false;
7+
private _value = false;
8+
9+
public constructor(v: unknown) {
10+
this.Valid = v;
11+
return this;
12+
}
13+
14+
public get DataType() {
15+
return new ArrowBool();
16+
}
17+
18+
public get Valid(): boolean {
19+
return this._valid;
20+
}
21+
22+
public get Value(): boolean {
23+
return this._value;
24+
}
25+
26+
public set Valid(value: unknown) {
27+
if (isInvalid(value)) {
28+
this._valid = false;
29+
return;
30+
}
31+
32+
if (value instanceof Bool) {
33+
this._valid = value.Valid;
34+
this._value = value.Value;
35+
return;
36+
}
37+
38+
if (isBooleanable(value)) {
39+
this._value = boolean(value);
40+
this._valid = true;
41+
return;
42+
}
43+
44+
throw new Error(`Unable to set '${value}' as Bool`);
45+
}
46+
47+
public toString() {
48+
if (this._valid) {
49+
return String(this._value);
50+
}
51+
52+
return NULL_VALUE;
53+
}
54+
}

src/scalar/util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const NULL_VALUE = '(null)';
2+
3+
export const isInvalid = (value: unknown) => value === null || value === undefined;

0 commit comments

Comments
 (0)