Skip to content

Instantly share code, notes, and snippets.

@simonplend
Last active October 30, 2020 22:21
Show Gist options
  • Save simonplend/be1f26a83a1b0769bd374e14d244aa07 to your computer and use it in GitHub Desktop.
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 !!
✅ 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
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