Skip to content

Commit

Permalink
Carve out cast logic in a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
vdestraitt authored and adimascio committed Jul 27, 2019
1 parent a745da6 commit 0134973
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 17 deletions.
7 changes: 3 additions & 4 deletions src/components/stepforms/FillnaStepForm.vue
Expand Up @@ -26,6 +26,7 @@ import ColumnPicker from '@/components/stepforms/ColumnPicker.vue';
import BaseStepForm from './StepForm.vue';
import { FillnaStep } from '@/lib/steps';
import { DataSetColumn } from '@/lib/dataset';
import { castFromString } from '@/lib/helpers';
@StepFormComponent({
vqbstep: 'fillna',
Expand Down Expand Up @@ -58,10 +59,8 @@ export default class FillnaStepForm extends BaseStepForm<FillnaStep> {
submit() {
const type = this.columnHeaders.filter(h => h.name === this.editedStep.column)[0].type;
if ((type === 'integer' || type === 'float') && !isNaN(Number(this.editedStep.value))) {
this.editedStep.value = Number(this.editedStep.value);
} else if (type === 'boolean') {
this.editedStep.value = this.editedStep.value === 'true';
if (type !== undefined) {
this.editedStep.value = castFromString(this.editedStep.value as string, type);
}
this.$$super.submit();
}
Expand Down
18 changes: 5 additions & 13 deletions src/components/stepforms/ReplaceStepForm.vue
Expand Up @@ -37,6 +37,7 @@ import WidgetToReplace from '@/components/stepforms/WidgetToReplace.vue';
import BaseStepForm from './StepForm.vue';
import { ReplaceStep } from '@/lib/steps';
import { DataSetColumn } from '@/lib/dataset';
import { castFromString } from '@/lib/helpers';
@StepFormComponent({
vqbstep: 'replace',
Expand Down Expand Up @@ -81,19 +82,10 @@ export default class ReplaceStepForm extends BaseStepForm<ReplaceStep> {
submit() {
const type = this.columnHeaders.filter(h => h.name === this.editedStep.search_column)[0].type;
if (type === 'integer' || type === 'float') {
for (const tuple of this.editedStep.to_replace) {
if (!isNaN(Number(tuple[0]))) {
tuple[0] = Number(tuple[0]);
}
if (!isNaN(Number(tuple[1]))) {
tuple[1] = Number(tuple[1]);
}
}
} else if (type === 'boolean') {
for (const tuple of this.editedStep.to_replace) {
tuple[0] = tuple[0] === 'true';
tuple[1] = tuple[1] === 'true';
for (const tuple of this.editedStep.to_replace) {
if (type !== undefined) {
tuple[0] = castFromString(tuple[0], type);
tuple[1] = castFromString(tuple[1], type);
}
}
this.$$super.submit();
Expand Down
29 changes: 29 additions & 0 deletions src/lib/helpers.ts
@@ -0,0 +1,29 @@
import { DataSetColumnType } from './dataset';

function issStringBoolean(string: string): boolean {
return (
string === 'true' ||
string === 'false' ||
string === 'True' ||
string === 'False' ||
string === 'TRUE' ||
string === 'FALSE' ||
string === '1' ||
string === '0'
);
}

/**
* Determines if a value canto be be cast to number
*
* @param value the value to be tested
* @returns a boolean to determine if the value can be connverted to a number
*/
export function castFromString(value: string, type: DataSetColumnType) {
if ((type === 'integer' || type === 'float') && !isNaN(Number(value))) {
return Number(value);
} else if (type === 'boolean' && issStringBoolean(value)) {
return value === 'true' || value === 'True' || value === 'TRUE' || value === '1';
}
return value;
}
40 changes: 40 additions & 0 deletions tests/unit/helpers.spec.ts
@@ -0,0 +1,40 @@
import { castFromString } from '@/lib/helpers';

describe('castFromString', () => {
it('should cast numeric string to number type', () => {
const string1 = '42';
const string2 = '4.2';
expect(castFromString(string1, 'integer')).toEqual(42);
expect(castFromString(string2, 'float')).toEqual(4.2);
});

it('should not cast a string that does not convert to number type', () => {
const string = 'Hey';
expect(castFromString(string, 'integer')).toEqual('Hey');
expect(castFromString(string, 'float')).toEqual('Hey');
});

it('should cast numeric string to boolean type', () => {
const string1 = 'true';
const string2 = 'True';
const string3 = 'TRUE';
const string4 = '1';
const string5 = 'false';
const string6 = 'False';
const string7 = 'FALSE';
const string8 = '0';
expect(castFromString(string1, 'boolean')).toEqual(true);
expect(castFromString(string2, 'boolean')).toEqual(true);
expect(castFromString(string3, 'boolean')).toEqual(true);
expect(castFromString(string4, 'boolean')).toEqual(true);
expect(castFromString(string5, 'boolean')).toEqual(false);
expect(castFromString(string6, 'boolean')).toEqual(false);
expect(castFromString(string7, 'boolean')).toEqual(false);
expect(castFromString(string8, 'boolean')).toEqual(false);
});

it('should not cast a string that does not convert to boolean type', () => {
const string = 'FaLsE';
expect(castFromString(string, 'boolean')).toEqual('FaLsE');
});
});

0 comments on commit 0134973

Please sign in to comment.