-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7d16640
add String type
bbernays fb2cac2
Add tests for string/text
bbernays 205dff6
linting and formatting
bbernays ca7d276
Merge branch 'main' into scalar-string
bbernays c46377b
linting
bbernays 9291ac9
Updated tsconfig to access Object`.hasOwn`
bbernays 343c39f
tests passing and pr review comments
bbernays b968074
linting
bbernays 8708186
linting/formatting
bbernays d48bf8a
fix: Review comments
erezrokah ab7a6e5
Merge branch 'main' into scalar-string
erezrokah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,3 +131,4 @@ dist | |
|
||
# Editor settings | ||
.idea | ||
.vscode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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)); | ||
}); | ||
}); | ||
|
||
[ | ||
{ value: '', expected: '' }, | ||
{ value: 'test string', expected: 'test string' }, | ||
{ value: String('new string object'), expected: 'new string object' }, | ||
{ value: new Text('new text object'), expected: 'new text object' }, | ||
{ value: new TextEncoder().encode('test'), expected: 'test' }, | ||
].forEach(({ value, expected }, index) => { | ||
test(`valid strings: '${value}' (${index})`, (t) => { | ||
const s = new Text(value); | ||
t.is(s.valid, true); | ||
t.is(s.value, expected); | ||
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" }); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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; | ||
} | ||
|
||
if (typeof value === 'string') { | ||
this._value = value; | ||
this._valid = true; | ||
return; | ||
} | ||
|
||
if (value instanceof Uint8Array) { | ||
this._value = new TextDecoder().decode(value); | ||
this._valid = true; | ||
return; | ||
} | ||
|
||
if (typeof value!.toString === 'function' && value!.toString !== Object.prototype.toString) { | ||
this._value = value!.toString(); | ||
this._valid = true; | ||
return; | ||
} | ||
|
||
throw new Error(`Unable to set '${value}' as Text`); | ||
} | ||
|
||
public toString() { | ||
if (this._valid) { | ||
return this._value.toString(); | ||
} | ||
|
||
return NULL_VALUE; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.