-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix deprecated boolean package warning (#597)
* feat: Implement isBooleanable function * test: Add tests for isBooleanable function * feat: Implement boolean function * test: Add tests for boolean function * refactor: remove boolean package * Update lib/boolean.ts Co-authored-by: Bryan Mishkin <[email protected]> --------- Co-authored-by: Bryan Mishkin <[email protected]>
- Loading branch information
Showing
6 changed files
with
119 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Originally from: https://www.npmjs.com/package/boolean | ||
|
||
export function boolean(value: unknown): boolean { | ||
switch (typeof value) { | ||
case 'string': { | ||
return ['true', 't', 'yes', 'y', 'on', '1'].includes( | ||
value.trim().toLowerCase(), | ||
); | ||
} | ||
|
||
case 'number': { | ||
return value.valueOf() === 1; | ||
} | ||
|
||
case 'boolean': { | ||
return value.valueOf(); | ||
} | ||
|
||
default: { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
export function isBooleanable(value: unknown): boolean { | ||
switch (typeof value) { | ||
case 'string': { | ||
return [ | ||
'true', | ||
't', | ||
'yes', | ||
'y', | ||
'on', | ||
'1', | ||
'false', | ||
'f', | ||
'no', | ||
'n', | ||
'off', | ||
'0', | ||
].includes(value.trim().toLowerCase()); | ||
} | ||
|
||
case 'number': { | ||
return [0, 1].includes(Number(value).valueOf()); | ||
} | ||
|
||
case 'boolean': { | ||
return true; | ||
} | ||
|
||
default: { | ||
return false; | ||
} | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,61 @@ | ||
import { boolean, isBooleanable } from '../../lib/boolean.js'; | ||
|
||
describe('boolean', function () { | ||
describe('#boolean', function () { | ||
it.each(['true', 't', 'yes', 'y', 'on', '1'])( | ||
'returns true when string is %s', | ||
function (value) { | ||
expect(boolean(value)).toBe(true); | ||
}, | ||
); | ||
|
||
it('returns true when number is 1', function () { | ||
expect(boolean(1)).toBe(true); | ||
}); | ||
|
||
it('returns true when boolean is true', function () { | ||
expect(boolean(true)).toBe(true); | ||
}); | ||
|
||
it.each(['foo', 2, undefined])( | ||
'returns false when value is %p', | ||
function (value) { | ||
expect(boolean(value)).toBe(false); | ||
}, | ||
); | ||
}); | ||
|
||
describe('#isBooleanable', function () { | ||
it.each([ | ||
'true', | ||
't', | ||
'yes', | ||
'y', | ||
'on', | ||
'1', | ||
'false', | ||
'f', | ||
'no', | ||
'n', | ||
'off', | ||
'0', | ||
])('returns true when string is %s', function (value) { | ||
expect(isBooleanable(value)).toBe(true); | ||
}); | ||
|
||
it.each([0, 1])('returns true when number is %i', function (value) { | ||
expect(isBooleanable(value)).toBe(true); | ||
}); | ||
|
||
it.each([true, false])('returns when boolean is %s', function (value) { | ||
expect(isBooleanable(value)).toBe(true); | ||
}); | ||
|
||
it.each(['foo', 2, undefined])( | ||
'returns false when value is %p', | ||
function (value) { | ||
expect(isBooleanable(value)).toBe(false); | ||
}, | ||
); | ||
}); | ||
}); |