Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add helper.ts#77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
averyjennings wants to merge1 commit intomainfromfeatureBranch4
Closed
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletionshelper.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
exportfunctionformatString(input:string):string{
returninput.trim().toLowerCase().replace(/\s+/g,'-');
}

exportfunctioncalculateSum(numbers:number[]):number{
returnnumbers.reduce((acc,num)=>acc+num,0);
}

exportfunctionisValidEmail(email:string):boolean{
constemailRegex=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;
returnemailRegex.test(email);
}

exportfunctiondebounce<Textends(...args:any[])=>any>(
func:T,
delay:number
):(...args:Parameters<T>)=>void{
lettimeoutId:NodeJS.Timeout;
return(...args:Parameters<T>)=>{
clearTimeout(timeoutId);
timeoutId=setTimeout(()=>func(...args),delay);
};
}
Comment on lines +14 to +23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

🛠️ Refactor suggestion

Fix timeout initialization and consider return value handling.

Two potential improvements:

  1. InitializetimeoutId to avoid callingclearTimeout on undefined
  2. The debounced function doesn't return the original function's return value, which may be unexpected
 export function debounce<T extends (...args: any[]) => any>(   func: T,   delay: number ): (...args: Parameters<T>) => void {-  let timeoutId: NodeJS.Timeout;+  let timeoutId: NodeJS.Timeout | undefined;   return (...args: Parameters<T>) => {-    clearTimeout(timeoutId);+    if (timeoutId) clearTimeout(timeoutId);     timeoutId = setTimeout(() => func(...args), delay);   }; }

Alternatively, if you need the return value, consider returning a Promise or changing the return type signature.

📝 Committable suggestion

‼️IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
exportfunctiondebounce<Textends(...args:any[])=>any>(
func:T,
delay:number
):(...args:Parameters<T>)=>void{
lettimeoutId:NodeJS.Timeout;
return(...args:Parameters<T>)=>{
clearTimeout(timeoutId);
timeoutId=setTimeout(()=>func(...args),delay);
};
}
exportfunctiondebounce<Textends(...args:any[])=>any>(
func:T,
delay:number
):(...args:Parameters<T>)=>void{
lettimeoutId:NodeJS.Timeout|undefined;
return(...args:Parameters<T>)=>{
if(timeoutId)clearTimeout(timeoutId);
timeoutId=setTimeout(()=>func(...args),delay);
};
}
🤖 Prompt for AI Agents
In helper.ts around lines 14 to 23, initialize timeoutId to undefined or nullbefore using it to avoid calling clearTimeout on an uninitialized variable.Also, update the debounced function to handle and return the original function'sreturn value properly, either by returning a Promise that resolves with thefunction's result or by adjusting the return type signature to reflect thedebounced function's behavior.


exportfunctionchunk<T>(array:T[],size:number):T[][]{
constchunks:T[][]=[];
for(leti=0;i<array.length;i+=size){
chunks.push(array.slice(i,i+size));
}
returnchunks;
}

[8]ページ先頭

©2009-2025 Movatter.jp