Use deno lint and AI to clean and optimize the code #36
Replies: 5 comments 1 reply
-
Example: % deno lint model.ts
error[no-explicit-any]: `any` type is not allowed
--> /Users/jduan/UnixStuff/devel/whereis-api-v0/main/model.ts:34:53
|
34 | public static initialize(record: Record<string, any>): void {
| ^^^
= hint: Use a specific type other than `any`
docs: https://docs.deno.com/lint/rules/no-explicit-any
|
Beta Was this translation helpful? Give feedback.
-
Here is the file changed with claude-3-7-sonnet-latest, using prompt "review and optimize the typescript code"
|
Beta Was this translation helpful? Give feedback.
-
AI code refactoring examplesOrginal code in model.ts/**
* Parses a tracking ID string into a TrackingID object or returns an error code.
* @param {string} strTrackingID - The tracking ID string to parse (e.g., "fdx-123456789012").
* @returns [string, TrackingID] An array containing an error code (if any) and the parsed TrackingID object (or undefined).
*/
static parse(strTrackingID: string): TrackingID {
const array = strTrackingID.split("-");
if ("" == strTrackingID.trim()) {
throw new UserError("400-01");
} else if (array.length != 2) {
throw new UserError("400-05");
} else {
const operator: string = array[0];
const trackingNum: string = array[1];
if (!this.operators.includes(operator)) {
throw new UserError("400-04");
}
if ("fdx" == operator) {
this.checkFedExTrackingNum(trackingNum);
}
if ("sfex" == operator) {
this.checkSFTrackingNum(trackingNum);
}
return new TrackingID(operator, trackingNum);
}
} VSCode with Github copilot/**
* Parses a tracking ID string into a TrackingID object or returns an error code.
* @param {string} strTrackingID - The tracking ID string to parse (e.g., "fdx-123456789012").
* @returns [string, TrackingID] An array containing an error code (if any) and the parsed TrackingID object (or undefined).
*/
static parse(strTrackingID: string): [string, TrackingID | undefined] {
const NO_ERROR = "NO_ERROR";
const array = strTrackingID.split("-");
if (strTrackingID == null || strTrackingID.trim() === "") {
return ["400-01", undefined];
} else if (!Array.isArray(array) || array.length !== 2 || array.some(item => typeof item !== "string" || item.trim() === "")) {
return ["400-05", undefined];
} else {
const operator: string = array[0];
const trackingNum: string = array[1];
if (!this.operators.includes(operator.toLowerCase())) {
return ["400-04", undefined];
}
if (operator === "fdx") {
const errorCode = this.checkFedExTrackingNum(trackingNum);
if (errorCode != undefined) {
return [errorCode, undefined];
}
}
if (operator === "sfex") {
const errorCode = this.checkSFTrackingNum(trackingNum);
if (errorCode != undefined) {
return [errorCode, undefined];
}
}
return [NO_ERROR, new TrackingID(operator, trackingNum)];
}
} Zed with Claude 3.5 /**
* Parses a tracking ID string into a TrackingID object or returns an error code.
* @param {string} strTrackingID - The tracking ID string to parse (e.g., "fdx-123456789012").
* @returns [string, TrackingID] An array containing an error code (if any) and the parsed TrackingID object (or undefined).
*/
static parse(strTrackingID: string): [string, TrackingID | undefined] {
if (strTrackingID.trim() === "") {
return ["400-01", undefined];
}
const array = strTrackingID.split("-");
if (array.length !== 2) {
return ["400-05", undefined];
}
const [operator, trackingNum] = array;
if (!this.operators.includes(operator)) {
return ["400-04", undefined];
}
// Currently only support two operators
const validationMap = {
"fdx": this.checkFedExTrackingNum,
"sfex": this.checkSFTrackingNum
};
const validateTrackingNum = validationMap[operator as keyof typeof validationMap];
const errorCode = validateTrackingNum(trackingNum);
if (errorCode) {
return [errorCode, undefined];
}
return ["", new TrackingID(operator, trackingNum)];
} |
Beta Was this translation helpful? Give feedback.
-
Claude 3.7. Cleaner.Here's a refactored version of the /**
* Parses a tracking ID string into a TrackingID object.
* @param {string} strTrackingID - The tracking ID string to parse (e.g., "fdx-123456789012").
* @returns {TrackingID} The parsed TrackingID object.
* @throws {UserError} If the tracking ID format is invalid.
*/
static parse(strTrackingID: string): TrackingID {
// Check for empty tracking ID
if (!strTrackingID?.trim()) {
throw new UserError("400-01");
}
const array = strTrackingID.split("-");
// Check for correct format (operatorCode-trackingNum)
if (array.length !== 2) {
throw new UserError("400-05");
}
const [operator, trackingNum] = array;
// Validate operator
if (!this.operators.includes(operator)) {
throw new UserError("400-04");
}
// Validate tracking number based on operator
switch (operator) {
case "fdx":
this.checkFedExTrackingNum(trackingNum);
break;
case "sfex":
this.checkSFTrackingNum(trackingNum);
break;
}
return new TrackingID(operator, trackingNum);
} Improvements made:
|
Beta Was this translation helpful? Give feedback.
-
Claude 3.7 -- further improvementsHere's a refactored version of your /**
* Parses a tracking ID string into a TrackingID object.
* @param {string} strTrackingID - The tracking ID string to parse (e.g., "fdx-123456789012").
* @returns {TrackingID} The parsed TrackingID object.
* @throws {UserError} If the tracking ID format is invalid.
*/
static parse(strTrackingID: string): TrackingID {
// Check for empty tracking ID
if (!strTrackingID?.trim()) {
throw new UserError("400-01");
}
const array = strTrackingID.split("-");
// Check for correct format (operator-number)
if (array.length !== 2) {
throw new UserError("400-05");
}
const [operator, trackingNum] = array;
// Validate operator
if (!this.operators.includes(operator)) {
throw new UserError("400-04");
}
// Validate tracking number based on operator using object literal
const trackingValidators: Record<string, (trackingNum: string) => void> = {
"fdx": this.checkFedExTrackingNum,
"sfex": this.checkSFTrackingNum
};
// Call the appropriate validator if it exists
if (trackingValidators[operator]) {
trackingValidators[operator].call(this, trackingNum);
}
return new TrackingID(operator, trackingNum);
} This approach:
The code is now more maintainable and follows a more declarative pattern. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
deno lint
to clean up typescript code: https://www.perplexity.ai/search/8ee4209b-f3d2-429e-8f5e-4c20f47c41c4Beta Was this translation helpful? Give feedback.
All reactions