Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Carve out cast logic in a helper function
- Loading branch information
1 parent
a745da6
commit 0134973
Showing
4 changed files
with
77 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,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; | ||
} |
This file contains 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,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'); | ||
}); | ||
}); |