notes

#javascript

2023. 7. 7.
Case Insensitive Sort

arr.sort((x, y) => x.toLowerCase().localeCompare(y.toLowerCase()))
//                                ^^^^^^^^^^^^^^^ ๐Ÿคฏ
arr.sort((x, y) => x.toLowerCase().localeCompare(y.toLowerCase()))
//                                ^^^^^^^^^^^^^^^ ๐Ÿคฏ

2023. 6. 15.
JavaScript 1.2 Reference

  1. ๋‚˜๋Š” ๋ถ„๋ช…ํžˆ rsc ๊ธ€์„ ์“ฐ๋˜ ์ค‘์ด์—ˆ๋Š”๋ฐ
  2. ์–ด์ฉŒ๋‹ค๋ณด๋‹ˆ ์ดˆ์ฐฝ๊ธฐ js ์ฝ”๋“œ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ์—ˆ์„๊นŒ ๊ถ๊ธˆํ•ด์ ธ์„œ
  3. ๊ฒ€์ƒ‰๋„ ํ•ด๋ณด๊ณ  gptํ•œํ…Œ ๋ฌผ์–ด๋„ ๋ณด๊ณ  ํ–ˆ๋Š”๋ฐ ์ ์ ˆํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์—†์—ˆ๋Š”๋ฐ
  4. ๊ทธ๋Ÿฌ๋‹ค ์•„ ์ดˆ๊ธฐ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜ ํ•˜๋Š” ์ƒ๊ฐ์ด ๋“ค์–ด ์ฐพ์•„๋ณด๋‹ˆ
  5. JavaScript 1.2 Reference ๊ฐ€ ์žˆ์—ˆ๋‹ค

image

"10/31/97" โ˜œ ๊ฐ„์ง€ ๋‚œ๋‹ค...

wayback machine์— ์•„์นด์ด๋น™๋œ ์˜ค๋ฆฌ์ง€๋„ ๋งํฌ๋„ ์žˆ์—ˆ๋‹ค.

2023. 6. 15.
`for (; amount--; )`

react-scrollbars-custom์˜ ์˜ˆ์ œ๋ฅผ ๋ณด๋‹ค๊ฐ€ ์ถฉ๊ฒฉ์ ์ธ ๊ฑธ ๋ด๊ฐ–๊ณ 

export function renderAmountOfParagraphs(amount = 5, paragraphsProps) {
  const result = [];

  for (; amount--; ) { // ??
    result.push(
      <p {...paragraphsProps} className="ExampleCard-Paragraph" key={`paragraph_${amount}`}>
        {getRandomParagraphText()}
      </p>
    );
  }

  return result;
}
export function renderAmountOfParagraphs(amount = 5, paragraphsProps) {
  const result = [];

  for (; amount--; ) { // ??
    result.push(
      <p {...paragraphsProps} className="ExampleCard-Paragraph" key={`paragraph_${amount}`}>
        {getRandomParagraphText()}
      </p>
    );
  }

  return result;
}

?

  for (; amount--; ) {
  for (; amount--; ) {

?? ๐Ÿคฏ

mdn์˜ for ๊ด€๋ จ ๋ฌธ์„œ๋ฅผ ๋ณด๋‹ˆ

All three expressions in the head of the for loop are optional. For example, it is not required to use the initialization block to initialize variables

๐Ÿคฏ๐Ÿคฏ

์ด ์™ธ์—๋„ ์ถฉ๊ฒฉ์ ์ธ ๊ฒŒ ๋” ์žˆ์—ˆ๋Š”๋ฐ:

Using for without a body

๐Ÿคฏ๐Ÿคฏ๐Ÿคฏ

Using for with two iterating variables

๐Ÿคฏ๐Ÿคฏ๐Ÿคฏ๐Ÿคฏ

2023. 6. 5.
`URL` object

const url = new URL('https://sehyunchung.dev')

url.toString() // 'https://sehyunchung.dev/' <- trailing slash ์ƒ๊ธฐ๋Š” ๊ฒƒ์— ์œ ์˜

url.pathname = 'post/1'

url.toString() // 'https://sehyunchung.dev/post/1' <- trailing slash ์—†์Œ

url.searchParams.set('key', 'value')

url.toString() // 'https://sehyunchung.dev/post/1?key=value'
const url = new URL('https://sehyunchung.dev')

url.toString() // 'https://sehyunchung.dev/' <- trailing slash ์ƒ๊ธฐ๋Š” ๊ฒƒ์— ์œ ์˜

url.pathname = 'post/1'

url.toString() // 'https://sehyunchung.dev/post/1' <- trailing slash ์—†์Œ

url.searchParams.set('key', 'value')

url.toString() // 'https://sehyunchung.dev/post/1?key=value'
// console.log(url)
{
  hash: ""
  host: "sehyunchung.dev"
  hostname: "sehyunchung.dev"
  href: "https://sehyunchung.dev/post/1?key=value"
  origin: "https://sehyunchung.dev"
  password: ""
  pathname: "/post/1"
  port: ""
  protocol: "https:"
  search: "?key=value"
  searchParams: URLSearchParams {size: 1}
  username: ""
}
// console.log(url)
{
  hash: ""
  host: "sehyunchung.dev"
  hostname: "sehyunchung.dev"
  href: "https://sehyunchung.dev/post/1?key=value"
  origin: "https://sehyunchung.dev"
  password: ""
  pathname: "/post/1"
  port: ""
  protocol: "https:"
  search: "?key=value"
  searchParams: URLSearchParams {size: 1}
  username: ""
}

url.pathname = ''

url.toString() // "https://sehyunchung.dev/"

url.pathname = 1

url.toString() // "https://sehyunchung.dev/1"

url.pathname = null

url.toString() // "https://sehyunchung.dev/null" ?!

url.pathname = undefined

url.toString() // "https://sehyunchung.dev/undefined" ?!
url.pathname = ''

url.toString() // "https://sehyunchung.dev/"

url.pathname = 1

url.toString() // "https://sehyunchung.dev/1"

url.pathname = null

url.toString() // "https://sehyunchung.dev/null" ?!

url.pathname = undefined

url.toString() // "https://sehyunchung.dev/undefined" ?!

2020. 11. 19.
Beware! Array methods will do shallow copies

const obj1 = { id: 1, name: "obj1" };
const obj2 = { id: 2, name: "obj2" };
const obj3 = { id: 3, name: "obj3" };

const arr = [obj1, obj2, obj3];

const mappedArr = arr.map((obj) => obj);
const filteredArr = arr.filter((obj) => obj.id !== 3);
const reducedArr = arr.reduce((acc, item) => {
  if (item.id === 3) {
    return acc;
  }
  return [...acc, item];
}, []);

const mappedAndSpreadedArr = arr.map((obj) => ({ ...obj }));
const reducedAndSpreadedArr = arr.reduce((acc, item) => {
  if (item.id === 3) {
    return acc;
  }
  return [...acc, { ...item }];
}, []);

console.log(obj1 === mappedArr[0]); // true
console.log(obj1 === mappedAndSpreadedArr[0]); // false

console.log(obj1 === filteredArr[0]); // true

console.log(obj1 === reducedArr[0]); // true
console.log(obj1 === reducedAndSpreadedArr[0]); // false

const findedObj1 = mappedArr.find((obj) => obj.id === 1);
findedObj1.name = "lol";
console.log(obj1.name); // 'lol'

const findedObj2 = mappedAndSpreadedArr.find((obj) => obj.id === 2);
findedObj2.name = "lol";
console.log(obj2.name); // 'obj2'
const obj1 = { id: 1, name: "obj1" };
const obj2 = { id: 2, name: "obj2" };
const obj3 = { id: 3, name: "obj3" };

const arr = [obj1, obj2, obj3];

const mappedArr = arr.map((obj) => obj);
const filteredArr = arr.filter((obj) => obj.id !== 3);
const reducedArr = arr.reduce((acc, item) => {
  if (item.id === 3) {
    return acc;
  }
  return [...acc, item];
}, []);

const mappedAndSpreadedArr = arr.map((obj) => ({ ...obj }));
const reducedAndSpreadedArr = arr.reduce((acc, item) => {
  if (item.id === 3) {
    return acc;
  }
  return [...acc, { ...item }];
}, []);

console.log(obj1 === mappedArr[0]); // true
console.log(obj1 === mappedAndSpreadedArr[0]); // false

console.log(obj1 === filteredArr[0]); // true

console.log(obj1 === reducedArr[0]); // true
console.log(obj1 === reducedAndSpreadedArr[0]); // false

const findedObj1 = mappedArr.find((obj) => obj.id === 1);
findedObj1.name = "lol";
console.log(obj1.name); // 'lol'

const findedObj2 = mappedAndSpreadedArr.find((obj) => obj.id === 2);
findedObj2.name = "lol";
console.log(obj2.name); // 'obj2'

2020. 10. 23.
Promise.allSettled

๋ชจ๋“  promise๊ฐ€ resolve/reject ๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ๊ฐ promise์˜ result๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Promise.all๊ณผ ๋‹ค๋ฅธ์ 

  1. promise ์ค‘ reject๊ฐ€ ์žˆ์–ด๋„ ์ผ๋‹จ ๋ชจ๋‘ pending ์ƒํƒœ๋ฅผ ๋ฒ—์–ด๋‚˜๊ธธ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  2. reject๋ฅผ ๋”ฐ๋กœ ํ•˜์ง€ ์•Š๊ณ  ๋ฐ˜ํ™˜๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฏ€๋กœ catch๋ฅผ ํƒ€์ง€ ์•Š๋Š”๋‹ค.
  3. ๋Œ€์‹  ๋ฐ˜ํ™˜ ๊ฒฐ๊ณผ๊ฐ’์ด ๋ณดํ†ต์˜ promise์™€ ๋‹ค๋ฅด๋‹ค. Promise<T>๊ฐ€ ์•„๋‹ˆ๊ณ  PromiseSettledResult<T>๋ฅผ ๋ฐ˜ํ™˜.
  4. PromiseSettledResult๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ๊ฒผ๋‹ค.
// fullfiled
{
  status: "fullfiled",
  value: T
}

// rejected
{
  status: "rejected",
  reason: any
}
// fullfiled
{
  status: "fullfiled",
  value: T
}

// rejected
{
  status: "rejected",
  reason: any
}

์‹ค์ œ๋กœ ๋Œ๋ ค๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

Screen Shot 2020-10-23 at 12 04 57 PM Playground

Ref

Promise.allSettled