Javascript/재사용 함수

⭐️ Loop 함수import { isFunction } from "./typeOf.js"; import { throwTypeError } from "./throwError.js"; export function loop(callback, repeatCount = 10) { if (!isFunction(callback)) { throwTypeError("callback 인자 유형은 함수여야 합니다."); } Array(repeatCount) .fill(null) .forEach((_, i) => callback(i)); }loop 함수는 반복 횟수와 콜백 함수를 인자로 받아서, 주어진 횟수만큼 콜백 함수를 반복 호출하는 함수입니다. 만약 인자로 전달된 콜백 함수가 함수 유형인지 체크하고, 아니라면 예외를 ..
⭐️ Memoization(메모이제이션) 패턴을 구현한 memo 함수import { isFunction, isString } from "./typeOf.js"; import { throwTypeError } from "./throwError.js"; export const memo = (() => { const cache = {}; return (memoized, key, showLog = false) => { if (!isFunction(memoized)) { throwTypeError("memoized 인자는 함수 유형이여야 합니다"); } if (!isString(key)) { throwTypeError("key 인자는 문자 유형이여야 합니다"); } if (cache[key]) { return c..
⭐️ throttle 유틸리티 함수 export const throttle = (callback, delay = 300) => { let isThrottling = false; return (...args) => { if (!isThrottling) { callback.apply(this, args); isThrottling = true; setTimeout(() => (isThrottling = false), delay); } }; }; //! Test // window.addEventListener( // "mousemove", // throttle(() => { // console.log("throttling"); // }, 300) // ); 위의 함수는 지정된 시간 간격(delay) 동안 여러 ..
⭐️ 객체나 배열의 각 요소를 처리하는 유틸리티 함수객체 또는 배열에서 각 요소를 반복적으로 처리하는 each 함수를 제공하며, 유효하지 않은 인자가 전달되었을 경우 예외를 발생시키는 재사용 가능한 함수import { throwTypeError } from "./throwError.js"; import { isArray, isObject, isFunction } from "./typeOf.js"; /** * * @param {Array | Object} arrayOrObject 순환할 객체(배열 또는 객체) * @param {Function} callback 반복 순환 처리할 함수 * @returns {Array} 집합 객체 반환 */ export function each(arrayOrObject, ca..
⭐️ 에러 유틸리티 함수 export const throwError = (message) => { throw new Error(message); }; export const throwSyntaxError = (message) => { throw new SyntaxError(message); }; export const throwReferenceError = (message) => { throw new ReferenceError(message); }; export const throwTypeError = (message) => { throw new TypeError(message); }; Error, SyntaxError, ReferenceError, TypeError 객체를 생성하고, 이들 객체에 인수로..
⭐️ 데이터 유형을 확인하기 위한 여러 가지 유틸리티 함수export const typeOf = (data) => { return Object.prototype.toString.call(data).slice(8, -1).toLowerCase(); }; export const isNull = (data) => typeOf(data) === "null"; export const isUndefined = (data) => typeOf(data) === "undefined"; export const isNumber = (data) => typeOf(data) === "number" && !isNaN(data); export const isBigInt = (data) => typeOf(data) === "bigi..
HYEBEEN
'Javascript/재사용 함수' 카테고리의 글 목록