diff --git a/src/backend/src/util/hl_types.js b/src/backend/src/util/hl_types.js index 942c0d4c3c..6110a595a5 100644 --- a/src/backend/src/util/hl_types.js +++ b/src/backend/src/util/hl_types.js @@ -44,6 +44,12 @@ const hl_type_definitions = { return v; } + if ( v === 1 ) return true; + if ( v === 0 ) return false + if ( typeof v === 'object' ) { + return v !== null; + } + throw new Error(`could not adapt value to boolean: ${quot(v)}`); } } diff --git a/src/backend/src/util/hl_types.test.js b/src/backend/src/util/hl_types.test.js new file mode 100644 index 0000000000..f2ad93e4ca --- /dev/null +++ b/src/backend/src/util/hl_types.test.js @@ -0,0 +1,17 @@ +const { expect } = require('chai'); +const { boolify } = require('./hl_types'); + +describe('hl_types', () => { + it('boolify falsy values', () => { + expect(boolify(undefined)).to.be.false; + expect(boolify(0)).to.be.false; + expect(boolify('')).to.be.false; + expect(boolify(null)).to.be.false; + }) + it('boolify truthy values', () => { + expect(boolify(true)).to.be.true; + expect(boolify(1)).to.be.true; + expect(boolify('1')).to.be.true; + expect(boolify({})).to.be.true; + }) +}); \ No newline at end of file