Convert a Restricted Function to an Arrow Function
Problem statement
Convert the supplied restricted JavaScript function declaration source into one canonical arrow-function code string.
For this exercise, assume the input follows function NAME(PARAM) { return PARAM OP INTEGER; }. NAME and PARAM are distinct, nonreserved identifiers containing only lowercase ASCII letters. The same parameter appears in the declaration and return expression. OP is exactly +, - or *. INTEGER is a canonical nonnegative decimal literal from 0 through 999, with no leading zeroes except the single literal 0.
For this exercise, whitespace consists only of ASCII space, tab and newline. It may surround the declaration or occur between tokens. At least one whitespace character separates function from NAME, and return from PARAM; punctuation can otherwise abut tokens.
Return exactly const NAME = (PARAM) => PARAM OP INTEGER;, with one ASCII space at each displayed space and no leading or trailing whitespace. Preserve the name, parameter, operator and literal text. Do not execute the code or change the input.
The admitted input has only this one pure return expression. It contains no calls, comments, strings, nested statements, this, arguments, super, asynchronous or generator functions, or caller context that depends on construction or declaration hoisting.
Function
convertRestrictedFunctionToArrow(source: String) → StringExamples
Example 1
source = "function add(x) { return x + 1; }"return = "const add = (x) => x + 1;"Authored example: Keep the name, parameter and addition, and emit exactly the canonical arrow layout.
Example 2
source = "function lower(value){return value-999;}"return = "const lower = (value) => value - 999;"Authored example: Punctuation may abut tokens; the canonical result inserts its prescribed spaces.
Example 3
source = "\tfunction\nscale ( amount )\n{\treturn\tamount*0 ;\n}\t"return = "const scale = (amount) => amount * 0;"Authored example: Space, tab and newline separators are normalized; multiplication by zero is preserved as text.
Constraints
- For this exercise, assume
1 <= source.length <= 200, measured in ASCII characters. sourceis a valid declaration under the grammar and context promise in the statement.1 <= NAME.length, PARAM.length <= 10; names contain onlyathroughz, are distinct, and are not reserved JavaScript keywords.- Whitespace characters have ASCII codes
32,9and10. INTEGERis0or a one-, two- or three-digit decimal literal beginning with1through9.