Skip to content

Commit 4c07a3d

Browse files
authored
feat: Add String type (#29)
1 parent 3429de0 commit 4c07a3d

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@ dist
131131

132132
# Editor settings
133133
.idea
134+
.vscode

src/scalar/text.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { DataType } from '@apache-arrow/esnext-esm';
2+
import test from 'ava';
3+
4+
import { Text } from './text.js';
5+
6+
// eslint-disable-next-line unicorn/no-null
7+
[null, undefined].forEach((v) => {
8+
test(`should set values to empty string when ${v} is passed`, (t) => {
9+
const s = new Text(v);
10+
t.is(s.value, '');
11+
t.is(s.valid, false);
12+
t.true(DataType.isUtf8(s.dataType));
13+
});
14+
});
15+
16+
[
17+
{ value: '', expected: '' },
18+
{ value: 'test string', expected: 'test string' },
19+
{ value: String('new string object'), expected: 'new string object' },
20+
{ value: new Text('new text object'), expected: 'new text object' },
21+
{ value: new TextEncoder().encode('test'), expected: 'test' },
22+
].forEach(({ value, expected }, index) => {
23+
test(`valid strings: '${value}' (${index})`, (t) => {
24+
const s = new Text(value);
25+
t.is(s.valid, true);
26+
t.is(s.value, expected);
27+
t.true(DataType.isUtf8(s.dataType));
28+
});
29+
});
30+
31+
test('should throw when unable to set value', (t) => {
32+
t.throws(() => new Text({ value: {} }), { message: "Unable to set '[object Object]' as Text" });
33+
});

src/scalar/text.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { Utf8 as ArrowString } from '@apache-arrow/esnext-esm';
2+
3+
import { Scalar } from './scalar.js';
4+
import { isInvalid, NULL_VALUE } from './util.js';
5+
6+
export class Text implements Scalar<string> {
7+
private _valid = false;
8+
private _value = '';
9+
10+
public constructor(v: unknown) {
11+
this.value = v;
12+
return this;
13+
}
14+
15+
public get dataType() {
16+
return new ArrowString();
17+
}
18+
19+
public get valid(): boolean {
20+
return this._valid;
21+
}
22+
23+
public get value(): string {
24+
return this._value;
25+
}
26+
27+
public set value(value: unknown) {
28+
if (isInvalid(value)) {
29+
this._valid = false;
30+
return;
31+
}
32+
33+
if (typeof value === 'string') {
34+
this._value = value;
35+
this._valid = true;
36+
return;
37+
}
38+
39+
if (value instanceof Uint8Array) {
40+
this._value = new TextDecoder().decode(value);
41+
this._valid = true;
42+
return;
43+
}
44+
45+
if (typeof value!.toString === 'function' && value!.toString !== Object.prototype.toString) {
46+
this._value = value!.toString();
47+
this._valid = true;
48+
return;
49+
}
50+
51+
throw new Error(`Unable to set '${value}' as Text`);
52+
}
53+
54+
public toString() {
55+
if (this._valid) {
56+
return this._value.toString();
57+
}
58+
59+
return NULL_VALUE;
60+
}
61+
}

0 commit comments

Comments
 (0)