Skip to content

Instantly share code, notes, and snippets.

View shisama's full-sized avatar

Masashi Hirano shisama

View GitHub Profile
const sourceCode = "{data: 'Hello, \u2028World'}";
eval(sourceCode) // 'Hello, World'
try {
someFunc() // thorws Error
} catch {
console.log('Error occurs')
}
const sourceCode = "{data: 'Hello, \u2028World'}";
eval(sourceCode) // SyntaxError: Invalid or unexpected token
@shisama
shisama / export.js
Last active November 28, 2019 17:53
export default () => {
console.log("default export")
}
export function printArg(arg) {
console.log(arg)
}
export function throwError() {
throw Error("error!");
}
console.log(2 ** 53)
// 9007199254740992
console.log(2 ** 53 + 1)
// 9007199254740992
console.log(2n ** 53n + 1n)
// 9007199254740993n
console.log(2n ** 53n * 1000n)
// 9007199254740992000n
const promise1 = Promise.resolve('foo');
const promise2 = Promise.reject('bar')
const promises = [promise1, promise2];
Promise.allSettled(promises).
then((results) => {
for (const result of results) {
if (result.status === 'fulfilled') console.log(result.value, 'is fulfilled');
if (result.status === 'rejected') console.log(result.reason, 'is rejected');
}
let regexp = /t(e)(st(\d?))/g;
let str = 'test1test2';
str.match(regexp);
// Array ['test1', 'test2']
let array = [...str.matchAll(regexp)];
console.log(array[0]);
// Array ["test1", "e", "st1", "1"]
@shisama
shisama / console_timeEnd.js
Created October 23, 2019 09:13
difference between Node.js v12 and v13 about console.timeEnd
const label = 'test';
console.time(label);
setTimeout(() => {
console.timeEnd(label);
}, 3000);
// 3001.732ms v12
// 3.001s v13
@shisama
shisama / intl_v12_v13.js
Created October 23, 2019 08:53
difference between Node.js v12 and v13 about Intl
const january = new Date(9e8);
const spanish = new Intl.DateTimeFormat('es', { month: 'long' });
console.log(spanish.format(january))
// 'M01' v12
// 'enero` v13
@shisama
shisama / assert.throws.v12.js
Last active October 23, 2019 08:42
show change of assert.throws since Node.js v13
// v12
const assert = require('assert');
const SomeError = class extends Error{};
try {
// 第一引数でthrowされたエラーがSomeErrorか判定
assert.throws(() => {throw new TypeError({})}, SomeError)
} catch (e) {
// eはTypeError
assert.ok(e instanceof TypeError)