Last active
October 30, 2020 22:21
-
-
Save simonplend/be1f26a83a1b0769bd374e14d244aa07 to your computer and use it in GitHub Desktop.
Script and results for testing casting of ECMAScript standard defined types to booleans with Boolean() and !!
This file contains hidden or 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
✅ Casting "" (type: string) with Boolean() === false | |
✅ Casting "" (type: string) with !! === false | |
✅ Casting "a" (type: string) with Boolean() === true | |
✅ Casting "a" (type: string) with !! === true | |
✅ Casting 0 (type: number) with Boolean() === false | |
✅ Casting 0 (type: number) with !! === false | |
✅ Casting 0.5 (type: number) with Boolean() === true | |
✅ Casting 0.5 (type: number) with !! === true | |
✅ Casting 9007199254740992 (type: bigint) with Boolean() === true | |
✅ Casting 9007199254740992 (type: bigint) with !! === true | |
✅ Casting Symbol(symbol) (type: symbol) with Boolean() === true | |
✅ Casting Symbol(symbol) (type: symbol) with !! === true | |
✅ Casting true (type: boolean) with Boolean() === true | |
✅ Casting true (type: boolean) with !! === true | |
✅ Casting false (type: boolean) with Boolean() === false | |
✅ Casting false (type: boolean) with !! === false | |
✅ Casting null (type: object) with Boolean() === false | |
✅ Casting null (type: object) with !! === false | |
✅ Casting undefined (type: undefined) with Boolean() === false | |
✅ Casting undefined (type: undefined) with !! === false | |
✅ Casting {} (type: object) with Boolean() === true | |
✅ Casting {} (type: object) with !! === true | |
✅ Casting [] (type: object) with Boolean() === true | |
✅ Casting [] (type: object) with !! === true | |
✅ Casting function () {} (type: function) with Boolean() === true | |
✅ Casting function () {} (type: function) with !! === true |
This file contains hidden or 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
const testCases = [ | |
// String (empty) | |
{ value: "", expectation: false }, | |
// String | |
{ value: "a", expectation: true }, | |
// Number (integer) | |
{ value: 0, expectation: false }, | |
// Number (floating point) | |
{ value: 0.5, expectation: true }, | |
// BigInt | |
{ value: 9007199254740992n, expectation: true }, | |
// Symbol | |
{ value: Symbol("symbol"), expectation: true }, | |
// Boolean | |
{ value: true, expectation: true }, | |
// Boolean | |
{ value: false, expectation: false }, | |
// null | |
{ value: null, expectation: false }, | |
// undefined | |
{ value: undefined, expectation: false }, | |
// Object | |
{ value: {}, expectation: true }, | |
// Object | |
{ value: [], expectation: true }, | |
// Function | |
{ value: function () {}, expectation: true }, | |
]; | |
function outputTestResult({ result, value, expectation, strategy }) { | |
const resultIcon = result === true ? "✅" : "❌"; | |
let valueString = value ? value.toString() : value; | |
if (["string", "object", "array"].includes(typeof value)) { | |
valueString = JSON.stringify(value); | |
} | |
console.log( | |
`${resultIcon} Casting ${valueString} (type: ${typeof value}) with ${strategy} === ${expectation.toString()}` | |
); | |
} | |
for (let { value, expectation } of testCases) { | |
outputTestResult({ | |
result: Boolean(value) === expectation, | |
value, | |
expectation, | |
strategy: "Boolean()", | |
}); | |
outputTestResult({ | |
result: !!value === expectation, | |
value, | |
expectation, | |
strategy: "!!", | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment