-
-
Save corasaurus-hex/2c03d8abc1c8827b44b59fbdc31a3622 to your computer and use it in GitHub Desktop.
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
class FooWithValueOfAndToString { | |
valueOf() { | |
return "FooWithValueOfAndToString.valueOf()"; | |
} | |
toString() { | |
return "FooWithValueOfAndToString.toString()"; | |
} | |
} | |
class BarWithToString { | |
toString() { | |
return "BarWithToString.toString()"; | |
} | |
} | |
function* bazGenerator() { | |
for (let i = 0; i < 10; i++) { | |
yield i; | |
} | |
return "baz iterator finished"; | |
} | |
async function* quxAsyncGenerator() { | |
yield await Promise.resolve('foo'); | |
yield await Promise.resolve('bar'); | |
yield await Promise.resolve('baz'); | |
} | |
class ToPrimitive { | |
[Symbol.toPrimitive](hint) { | |
return `hint === ${hint}` | |
} | |
} | |
(async () => { | |
const fooWithValueOfAndToString = new FooWithValueOfAndToString(); | |
const barWithToString = new BarWithToString(); | |
const toPrimitive = new ToPrimitive(); | |
const foobarArray = ["foo", "bar"]; | |
const date = new Date(); | |
const map = new Map([foobarArray]); | |
const weakmap = new WeakMap(); | |
weakmap.set({some: "weakmap object"}, "whatever") | |
const set = new Set(foobarArray); | |
const weakset = new WeakSet(); | |
weakset.add({some: "weakset object"}); | |
const array = foobarArray; | |
const object = {foo: "bar"}; | |
const boolean = true; | |
const NULL = null; | |
const UNDEFINED = undefined; | |
const integer = 1; | |
const float = 1.2; | |
const bigint = 123n; | |
const infinity = Infinity; | |
const nan = NaN; | |
const symbol = Symbol.for("symbol foo"); | |
const error = new Error("error"); | |
const typedArray = new Int8Array(8); | |
for (let i = 0; i < 9; i++) { | |
typedArray[i] = Math.floor(Math.random() * 255) | |
} | |
const fetchResponse = await fetch('https://api.ipify.org?format=json'); | |
const generator = bazGenerator(); | |
const asyncGenerator = quxAsyncGenerator(); | |
const promise = new Promise(() => {}); | |
const fn = () => {}; | |
const all = { | |
fooWithValueOfAndToString, | |
barWithToString, | |
toPrimitive, | |
date, | |
map, | |
weakmap, | |
set, | |
weakset, | |
array, | |
object, | |
boolean, | |
NULL, | |
UNDEFINED, | |
integer, | |
float, | |
bigint, | |
infinity, | |
nan, | |
symbol, | |
error, | |
typedArray, | |
fetchResponse, | |
generator, | |
asyncGenerator, | |
promise, | |
fn | |
}; | |
const plus = []; | |
const interpolated = []; | |
Object.entries(all).forEach(([k, v]) => { | |
try { | |
plus.push(`+${k} === ` + v); | |
} catch (e) { | |
plus.push(`+${k} === whoopsie! ` + e); | |
} | |
try { | |
interpolated.push(`\${${k}} === ${v}`) | |
} catch (e) { | |
interpolated.push(`\${${k}} === whoopsie! ${e}`) | |
} | |
}); | |
console.log(plus.join("\n")); | |
console.log(interpolated.join("\n")); | |
})() |
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
❯ node --version && node str_test.js | |
v18.6.0 | |
(node:90539) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time | |
(Use `node --trace-warnings ...` to show where the warning was created) | |
+fooWithValueOfAndToString === FooWithValueOfAndToString.valueOf() | |
+barWithToString === BarWithToString.toString() | |
+toPrimitive === hint === default | |
+date === Tue Aug 16 2022 22:22:21 GMT-0500 (Central Daylight Time) | |
+map === [object Map] | |
+weakmap === [object WeakMap] | |
+set === [object Set] | |
+weakset === [object WeakSet] | |
+array === foo,bar | |
+object === [object Object] | |
+boolean === true | |
+NULL === null | |
+UNDEFINED === undefined | |
+integer === 1 | |
+float === 1.2 | |
+bigint === 123 | |
+infinity === Infinity | |
+nan === NaN | |
+symbol === whoopsie! TypeError: Cannot convert a Symbol value to a string | |
+error === Error: error | |
+typedArray === -19,75,32,-115,-67,47,31,-124 | |
+fetchResponse === [object Response] | |
+generator === [object Generator] | |
+asyncGenerator === [object AsyncGenerator] | |
+promise === [object Promise] | |
+fn === () => {} | |
${fooWithValueOfAndToString} === FooWithValueOfAndToString.toString() | |
${barWithToString} === BarWithToString.toString() | |
${toPrimitive} === hint === string | |
${date} === Tue Aug 16 2022 22:22:21 GMT-0500 (Central Daylight Time) | |
${map} === [object Map] | |
${weakmap} === [object WeakMap] | |
${set} === [object Set] | |
${weakset} === [object WeakSet] | |
${array} === foo,bar | |
${object} === [object Object] | |
${boolean} === true | |
${NULL} === null | |
${UNDEFINED} === undefined | |
${integer} === 1 | |
${float} === 1.2 | |
${bigint} === 123 | |
${infinity} === Infinity | |
${nan} === NaN | |
${symbol} === whoopsie! TypeError: Cannot convert a Symbol value to a string | |
${error} === Error: error | |
${typedArray} === -19,75,32,-115,-67,47,31,-124 | |
${fetchResponse} === [object Response] | |
${generator} === [object Generator] | |
${asyncGenerator} === [object AsyncGenerator] | |
${promise} === [object Promise] | |
${fn} === () => {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment