Skip to content

Awesome collection of useful javascript snippet ๐Ÿ’ก

Notifications You must be signed in to change notification settings

le2sky/js-snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

8 Commits
ย 
ย 
ย 
ย 

Repository files navigation

useful-js-snippets

์œ ์šฉํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์กฐ๊ฐ๋“ค์„ ๋ชจ์•„ ๋†“์•˜์Šต๋‹ˆ๋‹ค.

TOC

  1. Reference

  2. Snippets usage

Reference

Snippets usage

Get Value

ํ•จ์ˆ˜๋Š” ์ง€์ •๋œ ๊ฒฝ๋กœ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ฐ’์ด ์—†์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

getValue({ a: { b: { c: "d" } } }, "a.b.c"); // = d
Function Parameter Description
getValue obj: object, path: string ์œ ํšจํ•œ ๊ฒฝ๋กœ๋ผ๋ฉด ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๊ณ  ์•„๋‹ˆ๋ฉด null์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

clamp

๊ฐ’์ด ์ง€์ •๋œ ๋ฒ”์œ„ ๋‚ด์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์ง€์ •๋œ ๋ฒ”์œ„์— ๊ฐ’์ด ์—†๋‹ค๋ฉด ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฐ’์„ ๋ฐ˜ํ™˜(ํด๋žจํ”„)ํ•ฉ๋‹ˆ๋‹ค.

clamp(0, 10, -5); // = 0
Function Parameter Description
clamp min: number, max: number, value:number ์ฃผ์–ด์ง„ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๊ฑฐ๋‚˜ ์ตœ์†Œ๊ฐ’ ํ˜น์€ ์ตœ๋Œ€๊ฐ’์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

Sleep

๋‹ค์Œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— ์ง€์ •๋œ ๊ธฐ๊ฐ„(๋ฐ€๋ฆฌ์ดˆ)์„ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.

await sleep(1000); // waits 1 sec
Function Parameter Description
sleep duration: number ์ฃผ์–ด์ง„ ๊ธฐ๊ฐ„๋™์•ˆ ๋”œ๋ ˆ์ด ๋ฉ๋‹ˆ๋‹ค.

Group By

keying-function์— ๋”ฐ๋ผ ๊ฐœ์ฒด์˜ ๊ด€๋ จ ํ•ญ๋ชฉ์„ ๊ทธ๋ฃนํ™”ํ•˜๊ณ  ์ƒ‰์ธํ™”ํ•ฉ๋‹ˆ๋‹ค.

groupBy(
  (vehicle) => vehicle.make,
  [
    { make: "tesla", model: "3" },
    { make: "tesla", model: "y" },
    { make: "ford", model: "mach-e" },
  ]
);
// {
//   tesla: [ { make: 'tesla', ... }, { make: 'tesla', ... } ],
//   ford: [ { make: 'ford', ... } ],
Function Parameter Description
groupBy fn:Function list: Array ๊ทธ๋ฃนํ™”๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Collect By

keying-function์— ๋”ฐ๋ผ ๊ด€๋ จ ํ•ญ๋ชฉ์„ ํฌํ•จํ•˜๋Š” ํ•˜์œ„ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

collectBy(
  (vehicle) => vehicle.make,
  [
    { make: "tesla", model: "3" },
    { make: "tesla", model: "y" },
    { make: "ford", model: "mach-e" },
  ]
);

// [
//   [ { make: 'tesla', ... }, { make: 'tesla', ... } ],
//   [ { make: 'ford', ... } ],
// ]
Function Parameter Description
collectBy fn:Function list: Array groupBy์— ์˜์กด์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Head

๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๊นจ๋—ํ•˜๊ณ  ์ฝ๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

head([1, 2, 3]); // = 1
head([]); // = undefined
Function Parameter Description
head list: Array ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๊ฑฐ๋‚˜ undefiend๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

Tail

๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์š”์†Œ๋ฅผ โ€‹โ€‹๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๊นจ๋—ํ•˜๊ณ  ์ฝ๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

tail([1, 2, 3]); // = [2, 3]
tail([]); // = []
Function Parameter Description
tail list: Array ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€์˜ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

Flatten

์ค‘์ฒฉ๋œ ํ•˜์œ„ ๋ฆฌ์ŠคํŠธ์—์„œ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ์žฌ๊ท€์ ์œผ๋กœ ๊ฐ€์ ธ์™€์„œ ํ”Œ๋žซ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

flatten([[1, 2, [3, 4], 5, [6, [7, 8]]]]); // = [1, 2, 3, 4, 5, 6, 7, 8]
Function Parameter Description
flatten list: Array ๋‹ค์ฐจ์› ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ 1์ฐจ์› ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

Intersection By

keying-function์— ์˜ํ•ด ์ •์˜๋œ ๋Œ€๋กœ ๋‘ ๋ชฉ๋ก์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๊ฐ’์„ ์ฐพ์Šต๋‹ˆ๋‹ค. (๊ต์ง‘ํ•ฉ)

intersectionBy((v) => v, [1, 2, 3], [2, 3, 4]); // = [2, 3]
intersectionBy(
  (v) => v.a,
  [{ a: 1 }, { a: 2 }],
  [{ a: 2 }, { a: 3 }, { a: 4 }]
); // = [{ a: 2 }];
Function Parameter Description
intersectionBy fn: Function listA: Array listB: Array ๊ต์ง‘ํ•ฉ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

Index By

keying-function์— ์˜ํ•ด ๊ฒฐ์ •๋œ ๊ฐ’์œผ๋กœ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๋ฅผ ์ธ๋ฑ์‹ฑํ•ฉ๋‹ˆ๋‹ค.

indexBy((val) => val.a, [{ a: 1 }, { a: 2 }, { a: 3 }]);
// = { 1: { a: 1 }, 2: { a:2 }, 3: { a: 3 } }
Function Parameter Description
indexBy fn: Function list: Array ์ธ๋ฑ์Šค๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Difference By

์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ ๋ฆฌ์ŠคํŠธ์—์„œ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ ๋ฆฌ์ŠคํŠธ์— ์—†๋Š” ๋ชจ๋“  ํ•ญ๋ชฉ์„ ์ฐพ์•„์„œ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

keying-function์— ์˜ํ•ด ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.

differenceBy((val) => val, [1, 2, 3], [3, 4, 5]); // = [1,2]
differenceBy(
  (vehicle) => vehicle.make,
  [{ make: "tesla" }, { make: "ford" }, { make: "gm" }],
  [{ make: "tesla" }, { make: "bmw" }, { make: "audi" }]
); // = [{ make: 'ford' }, { make: 'gm' }]
Function Parameter Description
differenceBy fn: Function listA: Array listB: Array indexBy์— ์˜์กดํ•ฉ๋‹ˆ๋‹ค.

Recover With

์ฃผ์–ด์ง„ ํ•จ์ˆ˜์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๊ธฐ๋ณธ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

recoverWith("A", (val) => val, "B"); // = B
recoverWith("A", () => {
  throw new Error();
}); // = 'A'
Function Parameter Description
recoverWith defaultValue: any fn: Function ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ดํ›„์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ฃผ์–ด์ง„ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.

Distance

๋‘ ์  p1๊ณผ p2 ์‚ฌ์ด์˜ ์œ ํด๋ฆฌ๋“œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. (๋‘ ์ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ)

distance([0, 1], [5, 4]); // = 5.8309518948453
Function Parameter Description
distance p1: Array<number> p2: Array<number> ๋‘ ์ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ

Drop While

์ฒซ ๋ฒˆ์งธ ์š”์†Œ์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์กฐ๊ฑด ํ•จ์ˆ˜๊ฐ€ ์ถฉ์กฑ๋  ๋•Œ๊นŒ์ง€ ๋ชฉ๋ก์—์„œ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

dropWhile((val) => val < 5, [1, 2, 3, 4, 5, 6, 7]); // = [5,6,7]
Function Parameter Description
dropWhile pred: Function list: Array ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

Sum By

๊ฐ ์š”์†Œ์˜ ๊ฐœ๋ณ„ ๊ฐ’์„ ๋ฐœํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ๋ชจ๋“  ์š”์†Œ์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

sumBy(
  (product) => product.price,
  [
    { name: "pizza", price: 10 },
    { name: "pepsi", price: 5 },
    { name: "salad", price: 5 },
  ]
); // = 20
Function Parameter Description
sumBy fn: Function list: Array ์ดํ•ฉ(number)์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

Ascending

ํ‰๊ฐ€ ํ•จ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

const byPrice = ascending((val) => val.price);
[{ price: 300 }, { price: 100 }, { price: 200 }].sort(byPrice);
// = [{ price: 100 }, { price: 200 }, { price: 300 }]
Function Parameter Description
ascending fn: Function ํ‰๊ฐ€ํ•จ์ˆ˜๋Š” ๋น„๊ตํ•  ์†์„ฑ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Descending

ํ‰๊ฐ€ ํ•จ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

const byPrice = descending((val) => val.price);
[{ price: 300 }, { price: 100 }, { price: 200 }].sort(byPrice);
// = [{ price: 300 }, { price: 200 }, { price: 100 }]
Function Parameter Description
descending fn: Function ํ‰๊ฐ€ํ•จ์ˆ˜๋Š” ๋น„๊ตํ•  ์†์„ฑ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Find Key

์ธ๋ฑ์Šค ๋‚ด์—์„œ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ํ‚ค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

findKey((car) => !car.available, {
  tesla: { available: true },
  ford: { available: false },
  gm: { available: true },
}); // = "ford"
Function Parameter Description
findKey predicate: Function, index: Object ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ํ‚ค ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Bifurcate By

์ฃผ์–ด์ง„ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ’์„ ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ์œผ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” ํ‰๊ฐ€ ํ•จ์ˆ˜๊ฐ€ ์ฐธ์œผ๋กœ ํ‰๊ฐ€๋˜๋Š” ๊ฐ’์„ ํฌํ•จํ•˜๊ณ  ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋Š” ๊ฑฐ์ง“์ธ ๊ฐ’๋“ค์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

bifurcateBy((val) => val > 0, [-1, 2, -3, 4]);
// = [[2, 4], [-1, -3]]
Function Parameter Description
bifurcateBy predicate: Function, list: Array 2์ฐจ์› ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

Pipe

์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•จ์ˆ˜ ํ•ฉ์„ฑ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋Š” ๋‘ ๋ฒˆ์งธ ํ•จ์ˆ˜์— ์ „๋‹ฌ๋˜๊ณ  ๋‘ ๋ฒˆ์งธ ๊ฒฐ๊ณผ๋Š” ์„ธ ๋ฒˆ์งธ ํ•จ์ˆ˜์— ์ „๋‹ฌ๋˜๋Š” ์‹์œผ๋กœ ๋ชจ๋“  ๊ธฐ๋Šฅ์ด ์ฒ˜๋ฆฌ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์†๋ฉ๋‹ˆ๋‹ค.

pipe([Math.abs, Math.floor, (val) => -val], 4.2); // = -4
pipe([(a, b) => a - b, Math.abs], 5, 10); // = 5
Function Parameter Description
pipe functions: Array<Function>, [args...] ํ•จ์ˆ˜ ํ•ฉ์„ฑ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

isValidJSON

์ฃผ์–ด์ง„ JSON ๋ฌธ์ž์—ด์ด ์œ ํšจํ•œ์ง€ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

isValidJSON('{"name":"Adam","age":20}'); // true
isValidJSON('{"name":"Adam",age:"20"}'); // false
isValidJSON(null); // true
Function Parameter Description
isValidJSON str: string JSON ๋ฌธ์ž์—ด์ด ์œ ํšจํ•œ์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

isValidDate

์ฃผ์–ด์ง„ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์œ ํšจํ•œ date ๊ฐ’์ธ์ง€ ํŒ๋‹จํ•ฉ๋‹ˆ๋‹ค.

isValidDate("December 17, 1995 03:24:00"); // true
isValidDate("1995-12-17T03:24:00"); // true
isValidDate("1995-12-17 T03:24:00"); // false
isValidDate("Duck"); // false
isValidDate(1995, 11, 17); // true
isValidDate(1995, 11, 17, "Duck"); // false
isValidDate({}); // false
Function Parameter Description
isValidDate [args..] ์ฃผ์–ด์ง„ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์œ ํšจํ•œ date์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

Memoization

memoization ํ…Œํฌ๋‹‰์„ ์ด์šฉํ•ด์„œ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฏธ๋ฆฌ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

const fibonacci = (n) => (n <= 1 ? 1 : fibonacci(n - 1) + fibonacci(n - 2));
const memoizedFibonacci = memoize(fibonacci);

for (let i = 0; i < 100; i++) fibonacci(30); // ~5000ms
for (let i = 0; i < 100; i++) memoizedFibonacci(30); // ~50ms
Function Parameter Description
memoize fn: Function ์ฃผ์–ด์ง„ ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•ด๋‘ก๋‹ˆ๋‹ค.

Curry

๋‹ค์ค‘ ์ธ์ž๋ฅผ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ์ปค๋ง๋œ ํ•จ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

let curriedSum = curry(sum);

curriedSum(1, 2, 3); // 6
curriedSum(1)(2, 3); // 6
curriedSum(1)(2)(3); // 6
Function Parameter Description
curry fn: Function ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ์ปค๋งํ•ฉ๋‹ˆ๋‹ค.

About

Awesome collection of useful javascript snippet ๐Ÿ’ก

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published