Replies: 3 comments
-
This approach seems to work, testing the value against true. Not sure if it's the best solution, though.
|
Beta Was this translation helpful? Give feedback.
0 replies
-
I usually use this mapper export const booleanMapper: ValueMapper<boolean> = value => Boolean(Number.parseFloat(value)); Unit test, which shows what values are mapped into: https://github.com/Siemienik/XToolset/blob/master/packages/xlsx-import/tests/unit/mappers/booleanMapper.test.ts#L7-L20 { inValue: null, expectedResult: false },
{ inValue: '', expectedResult: false },
{ inValue: ' ', expectedResult: false },
{ inValue: '0', expectedResult: false },
{ inValue: '000', expectedResult: false },
{ inValue: 'true', expectedResult: false },
{ inValue: 'false', expectedResult: false },
{ inValue: 'a0.1', expectedResult: false },
{ inValue: '001', expectedResult: true },
{ inValue: '123', expectedResult: true },
{ inValue: '0.1', expectedResult: true },
{ inValue: '-1', expectedResult: true },
{ inValue: ' -1.2123asd', expectedResult: true }, |
Beta Was this translation helpful? Give feedback.
0 replies
-
A const GetBooleanFromCell = (cell: Cell): boolean => {
if (typeof cell.value === 'boolean') {
return cell.value;
}
return false;
} B const GetBooleanFromCell = (cell: Cell): boolean => {
if (cell.type === ValueType.Boolean) {
return cell.value as boolean;
}
return false;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
If I'm writing a Typescript helper function like this:
How can I test for boolean content and then return it? While debugging I do see that
.value
is indeed a boolean... but how do I tell my Typescript compiler this fact?Thank you!
Beta Was this translation helpful? Give feedback.
All reactions