Skip to content

Commit

Permalink
feat: create a JS helpers class (#190)
Browse files Browse the repository at this point in the history
* feat: create js helpers

* capture arrow functions

* make everything more readable
  • Loading branch information
a2937 authored Apr 16, 2024
1 parent 970713f commit d0db232
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/__fixtures__/curriculum-helpers-javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ myFunc();
*/
`;

const functionDeclaration =
"function myFunction(param1, param2 = 'default', param3) {";
const constFunction =
"const myFunction = (param1, param2 = 'default', param3) => {";
const letFunction =
"let myFunction = function(param1, param2 = 'default', param3) {";

const arrowFunction = `const myFunc = name => console.log("Name")`;

const testValues = {
jsCodeWithSingleAndMultLineComments,
jsCodeWithSingleAndMultLineCommentsRemoved,
Expand All @@ -65,6 +74,10 @@ const testValues = {
jsCodeWithNoArgCall,
jsCodeWithArgCall,
jsCodeWithCommentedCall,
functionDeclaration,
constFunction,
letFunction,
arrowFunction,
};

export default testValues;
36 changes: 36 additions & 0 deletions lib/__tests__/javascript-helper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import jsTestValues from "../__fixtures__/curriculum-helpers-javascript";

import { getFunctionParams } from "../index";

const { functionDeclaration, constFunction, letFunction, arrowFunction } =
jsTestValues;

describe("js-help", () => {
describe("getFunctionArgs", () => {
it("gets arguments from function declarations", function () {
const parameters = getFunctionParams(functionDeclaration);
expect(parameters[0].name).toBe("param1");
expect(parameters[1].defaultValue).toBe("default");
expect(parameters[1].name).toBe("param2");
expect(parameters[2].name).toBe("param3");
});
it("gets arguments from const function variables", function () {
const parameters = getFunctionParams(constFunction);
expect(parameters[0].name).toBe("param1");
expect(parameters[1].defaultValue).toBe("default");
expect(parameters[1].name).toBe("param2");
expect(parameters[2].name).toBe("param3");
});
it("gets arguments from let function variables", function () {
const parameters = getFunctionParams(letFunction);
expect(parameters[0].name).toBe("param1");
expect(parameters[1].defaultValue).toBe("default");
expect(parameters[1].name).toBe("param2");
expect(parameters[2].name).toBe("param3");
});
it("gets arguments from arrow functions", function () {
const parameters = getFunctionParams(arrowFunction);
expect(parameters[0].name).toBe("name");
});
});
});
44 changes: 44 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,47 @@ export class CSSHelp {
return [...new Set(allSelectors)];
}
}

/**
* Extracts all function parameters and default values from a function
* @param functionObject A function in string form
* @returns {{name:String,defaultValue: String | undefined}}
*/
export function getFunctionParams(code: string) {
// Regular expression to match function declarations, arrow functions, and function expressions
const functionDeclareRegex = /(?:function\s*[^(]*\(([^)]*)\))/;

const functionVariableRegex =
/(?:\b(?:const|let|var)\s*\w+\s*=\s*(?:function)?\s*\(([^)]*)\))/;

const arrowFunctionRegex = /=\s+([^)]*)=>/;

// Match the function parameters
const paramMatch =
code.match(functionDeclareRegex) ||
code.match(functionVariableRegex) ||
code.match(arrowFunctionRegex);

if (paramMatch) {
// Find the captured group containing the parameters
const paramString =
paramMatch[1] || paramMatch[2] || paramMatch[3] || paramMatch[4];
// Split the parameter string by commas to get individual parameters
const params = paramString.split(",").map((param: string) => {
// Split each parameter by '=' to separate name and default value
const parts = param.trim().split("=");
// If the parameter has a default value, extract it, otherwise set it to undefined
const defaultValue =
parts.length > 1 ? parts[1].replace(/['"]/g, "").trim() : undefined;
// Return an object with the parameter name and default value
return {
name: parts[0].trim(),
defaultValue: defaultValue,
};
});
return params;
}

// Return an empty array if no function parameters are found
return [];
}

0 comments on commit d0db232

Please sign in to comment.