Skip to content

feat: Add String type #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,4 @@ dist

# Editor settings
.idea
.vscode/launch.json
27 changes: 27 additions & 0 deletions src/scalar/text.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DataType } from '@apache-arrow/esnext-esm';
import test from 'ava';

import { Text } from './text.js';

// eslint-disable-next-line unicorn/no-null
[null, undefined].forEach((v) => {
test(`should set values to empty string when ${v} is passed`, (t) => {
const s = new Text(v);
t.is(s.value, '');
t.is(s.valid, false);
t.true(DataType.isUtf8(s.dataType));
});
});

['', 'test string', String('new string object'), new Text('new text object')].forEach((v, index) => {
test(`valid strings: '${v}' (${index})`, (t) => {
const s = new Text(v);
t.is(s.valid, true);
t.is(s.value, v.toString());
t.true(DataType.isUtf8(s.dataType));
});
});

test('should throw when unable to set value', (t) => {
t.throws(() => new Text({ value: {} }), { message: "Unable to set '[object Object]' as Text" });
});
58 changes: 58 additions & 0 deletions src/scalar/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Utf8 as ArrowString } from '@apache-arrow/esnext-esm';

import { Scalar } from './scalar.js';
import { isInvalid, NULL_VALUE } from './util.js';

export class Text implements Scalar<string> {
private _valid = false;
private _value = '';

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

public get dataType() {
return new ArrowString();
}

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

public get value(): string {
return this._value;
}

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

switch (typeof value) {
case 'object': {
if (value !== undefined && value !== null && Object.hasOwn(value, 'toString')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (value !== undefined && value !== null && Object.hasOwn(value, 'toString')) {
if (Object.hasOwn(value, 'toString')) {

We already check for invalid value above with isInvalid()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this check the typescript compiler/builder gave errors. If there is a way around that I would be happy to change it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do value! to tell TypeScript is can never be null or undefined

this._value = value.toString();
this._valid = true;
return;
}
break;
}
case 'string': {
this._value = value.toString();
this._valid = true;
return;
}
}
throw new Error(`Unable to set '${value}' as Text`);
}

public toString = (): string => {
if (this._valid) {
return this._value.toString();
}

return NULL_VALUE;
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erezrokah - Do you know why I had to change this method from:

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

    return NULL_VALUE;
  };

In order for the Object.hasOwn to see that this class has a toString method?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is in https://stackoverflow.com/questions/22445261/why-does-hasownproperty-not-recognise-functions-on-an-objects-prototype

I think if we do typeof value!.toString === 'function' && value!.toString !== Object.prototype.toString is should work for both cases

}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"allowJs": true,
"declaration": true,
"outDir": "dist",
"sourceMap": true
"sourceMap": true,
"lib": ["esnext", "dom"]
}
}