-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 9 commits
7d16640
fb2cac2
205dff6
ca7d276
c46377b
9291ac9
343c39f
b968074
8708186
d48bf8a
ab7a6e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,3 +131,4 @@ dist | |
|
||
# Editor settings | ||
.idea | ||
.vscode/launch.json |
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" }); | ||
}); |
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) { | ||||||
erezrokah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
case 'object': { | ||||||
if (value !== undefined && value !== null && Object.hasOwn(value, 'toString')) { | ||||||
erezrokah marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We already check for invalid There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do |
||||||
this._value = value.toString(); | ||||||
this._valid = true; | ||||||
return; | ||||||
} | ||||||
break; | ||||||
} | ||||||
case 'string': { | ||||||
this._value = value.toString(); | ||||||
erezrokah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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; | ||||||
}; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @erezrokah - Do you know why I had to change this method from:
In order for the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
} |
Uh oh!
There was an error while loading. Please reload this page.