Skip to content

Commit

Permalink
fix: test and improve boolify
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelDeimos committed Jan 9, 2025
1 parent f60c008 commit 44ad3c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/backend/src/util/hl_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/backend/src/util/hl_types.test.js
Original file line number Diff line number Diff line change
@@ -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;
})
});

0 comments on commit 44ad3c5

Please sign in to comment.