๐Ÿฆธโ€โ™‚๏ธ ์Šˆํผํžˆ์–ด๋กœ์ฒ˜๋Ÿผ ์ฝ”๋”ฉํ•˜๋Š” 11๊ฐ€์ง€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒ๊ณผ ์š”๋ น (Vol.2)

21996 ๋‹จ์–ด javascriptwebdevbeginnerstutorial
์•ˆ๋…•ํ•˜์„ธ์š” JSํžˆ์–ด๋กœ์ฆˆ์ž…๋‹ˆ๋‹ค! ์ด ์ƒˆ ๊ธฐ์‚ฌ๋กœ ๋งˆ์นจ๋‚ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ๊ฐ€์ž โœจ

1. ๋‚œ์ˆ˜๋กœ ๋ชฉ๋ก ์ƒ์„ฑ



๋‹ค์–‘ํ•œ ์ด์œ ๋กœ ๊ฐ€์งœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Array.from({ length: 1000 }, Math.random)
// [ 0.6163093133259432,โ€ˆ0.8877401276499153,โ€ˆ0.4094354756035987,โ€ˆ...] - 1000 items


2. ์ˆซ์ž๋กœ ๋ชฉ๋ก ์ƒ์„ฑ



๋„ค, ์ˆซ์ž๊ฐ€ ์žˆ๋Š” ๋ชฉ๋ก์„ ์ƒ์„ฑํ•˜๋Š” ํŠธ๋ฆญ์ด ํ•˜๋‚˜ ๋” ์žˆ์Šต๋‹ˆ๋‹ค.

Array.from({ length: 1000 }, (v, i) => i)
// [0, 1, 2, 3, 4, 5, 6....999]


1-2 ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋•๋ถ„์—

3. RGB โ†’ HEX ๋ณ€ํ™˜



๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์—†์ด RGB๋ฅผ HEX๋กœ ๋ณ€ํ™˜ํ•˜์‹ญ์‹œ์˜ค!

const rgb2hex = ([r, g, b]) =>
  '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).substr(1);

rgb2hex([76, 11, 181]);
// #4c0bb5


4. HEX โ†’ RGB ๋ณ€ํ™˜



๋‹ค์‹œ ๋ณ€ํ™˜์— ๋Œ€ํ•ด ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?! ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

const hex2rgb = hex =>
  [1, 3, 5].map((h) => parseInt(hex.substring(h, h + 2), 16));

hex2rgb("#4c0bb5");
// [76, 11, 181]


5. ํ™€์ˆ˜ ๋˜๋Š” ์ง์ˆ˜



๋˜ ๋‹ค๋ฅธ ํ™€์ˆ˜/์ง์ˆ˜ ๊ฒ€์‚ฌ.

const value = 232;  

if (value & 1) console.log("odd");
else console.log("even");
// even


6. ์œ ํšจํ•œ URL ํ™•์ธ



๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ์šฉ์ž๊ฐ€ ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ URL์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์–ด์จŒ๋“  ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ๊ณต์œ ํ•˜์ž

const isValidURL = (url) => {
  try {
    new URL(url);
    return true;
  } catch (error) {
    return false;
  }
};

isValidURL("https://dev.to");
// true

isValidURL("https//invalidto");
// false


7. N ๋ญ”๊ฐ€ ์ „



๋•Œ๋•Œ๋กœ ๋‚ ์งœ๋ฅผ 6 minute(s) ago ๋กœ ์ธ์‡„ํ•  ๊ฒƒ์ด ํ•„์š”ํ•˜์ง€๋งŒ ๊ดด๋ฌผ ํฌ๊ธฐ์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์ž‘์€ ์Šค๋‹ˆํŽซ์ž…๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ๋Œ€๋กœ ์‰ฝ๊ฒŒ ์ˆ˜์ •ํ•˜๊ณ  ๊ณ„์† ์ง„ํ–‰ํ•˜์‹ญ์‹œ์˜ค.

const fromAgo = (date) => {
  const ms = Date.now() - date.getTime();
  const seconds = Math.round(ms / 1000);
  const minutes = Math.round(ms / 60000);
  const hours = Math.round(ms / 3600000);
  const days = Math.round(ms / 86400000);
  const months = Math.round(ms / 2592000000);
  const years = Math.round(ms / 31104000000);

  switch (true) {
    case seconds < 60:
      return `${seconds} second(s) ago"`;
    case minutes < 60:
      return `${minutes} minute(s) ago"`;
    case hours < 24:
      return `${hours} hour(s) ago"`;
    case days < 30:
      return `${days} day(s) ago`;
    case months < 12:
      return `${months} month(s) ago`;
    default:
      return `${years} year(s) ago`;
  }
};

const createdAt = new Date(2021, 0, 5);
fromAgo(createdAt); // 14 day(s) ago;


8. ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฒฝ๋กœ ์ƒ์„ฑ



์šฐ๋ฆฌ๋Š” ๊ฒฝ๋กœ/๊ฒฝ๋กœ๋กœ ๋งŽ์€ ์ž‘์—…์„ ํ•˜๊ณ  ํ•ญ์ƒ ๊ทธ๊ฒƒ๋“ค์„ ์กฐ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๊ฑฐ๊ธฐ์— ํ‘ธ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•  ๋•Œ generatePath๊ฐ€ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค!

const generatePath = (path, obj) =>
    path.replace(/(\:[a-z]+)/g, (v) => obj[v.substr(1)]);

const route = "/app/:page/:id";
generatePath(route, {
  page: "products",
  id: 85,
});
// /app/products/123


9. ๊ฒฝ๋กœ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ



์˜ˆ! ์ด์ œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ง๋ ฌ ๋ณ€ํ™˜๊ธฐ๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๊ตฌ๋ฌธ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const getPathParams = (path, pathMap, serializer) => {
  path = path.split("/");
  pathMap = pathMap.split("/");
  return pathMap.reduce((acc, crr, i) => {
    if (crr[0] === ":") {
      const param = crr.substr(1);
      acc[param] = serializer && serializer[param]
        ? serializer[param](path[i])
        : path[i];
    }
    return acc;
  }, {});
};

getPathParams("/app/products/123", "/app/:page/:id");
// { page: 'products', id: '123' }

getPathParams("/items/2/id/8583212", "/items/:category/id/:id", {
  category: v => ['Car', 'Mobile', 'Home'][v],
  id: v => +v
});
// { category: 'Home', id: 8583212 }


10. ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด๋กœ ๊ฒฝ๋กœ ์ƒ์„ฑ



๋ฌผ๋ก  ์šฐ๋ฆฌ๋Š” ๊ฒฝ๋กœ๋กœ ์ž‘์—…ํ•˜๊ณ  ์ฟผ๋ฆฌ๋กœ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

const generatePathQuery = (path, obj) =>
  path +
  Object.entries(obj)
    .reduce((total, [k, v]) => (total += `${k}=${encodeURIComponent(v)}&`), "?")
    .slice(0, -1);

generatePathQuery("/user", { name: "Orkhan", age: 30 }); 
// "/user?name=Orkhan&age=30"


11. ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ



์ด์ œ ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค.

const getQueryParams = url =>
  url.match(/([^?=&]+)(=([^&]*))/g).reduce((total, crr) => {
    const [key, value] = crr.split("=");
    total[key] = value;
    return total;
  }, {});

getQueryParams("/user?name=Orkhan&age=30");
// { name: 'Orkhan', age: '30' }


๊ฒฐ๋ก 



๋ชจ๋“  ๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ํ›Œ๋ฅญํ•˜๊ณ  ๊นจ๋—ํ•˜๊ฒŒ ์œ ์ง€ํ•˜์‹ญ์‹œ์˜ค.

JS ์˜์›…์˜ ๊ธฐ์ˆ ์„ ๋ฐ›์•„ ์„ธ์ƒ์„ ๊ตฌํ•˜์‹ญ์‹œ์˜ค! โœจ๐Ÿฆธโ€โ™‚๏ธ โœจ

github code-like์— ํŒ/ํŠธ๋ฆญ์„ ์ €์žฅํ•˜๊ณ  ์ œ๊ณตํ•˜์‹ญ์‹œ์˜ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ