2

I have a javascript file in which I have defined a custom function which take some input object and perform some action.
This file is called from many files.
Now I want to priint all the params name used in this function.

May be I need to read whole file as a string and print the desired output

 function customFunction(param){
       if(param.isOK == yes){}
       if(param.hasMenu == no){}
       ..
       ..
       ..
       if(param.cancelText == "cancel"){}

    }

I want to write a program which reads this file and output all the params name. e.g.

isOK | hasMenu | ...... | CancelText ..

1
  • param is object or string?? Commented Sep 19, 2015 at 10:21

2 Answers 2

2

You can use Regular Expression.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace _32666940
{

    class Program
    {
        public static String str = "function customFunction(param){ " +
            "if(param.isOK == yes){}" +
            "if(param.hasMenu == no){}" +
            "if(param.cancelText == \"cancel\"){}" +    // I had to add \ before "
            "if(param[  'isOK'  ] == yes){}" +
            "if(param[\"isOK\"] == yes){}" +
            "}";
        static void Main()
        {
            //@"param\.[a-zA-Z_]+[a-zA-Z0-9_]*"
            Regex regex = new Regex(@"param\.[a-zA-Z_]+[a-zA-Z0-9_]*");
            Match match = regex.Match(str);
            while (match.Success)
            {
                Console.WriteLine(match.Value);
                match = match.NextMatch();
            }

            regex = new Regex(@"\bparam\[\s*(['""])(.+?)\1\s*]");
            match = regex.Match(str);
            while (match.Success)
            {
                Console.WriteLine(match.Value);
                match = match.NextMatch();
            }

            Console.ReadKey();

        }
    }
}

Edit: updated the code to match param['some-key']

Edit updated the code to match param["some-key"]

Sign up to request clarification or add additional context in comments.

3 Comments

This will not get properties which are accessed using array notation: e.g. param['this-is-another-property']
This solve my one problem. Can you please provide me the regex for situation like param['this-is-another-property']
I've updated the code again. The code is now working in all cases. You can also have a glance here ... stackoverflow.com/questions/32676695/…
0

If param is object, then you can use jQuery each() method. $.each() is an object iterator utility.

You can loop through this object using each() method as following

$.each( param, function( key, value ) {
     console.log(value);
    //Add your logic as per requirement
});

It will print all the values(in console) available in the param object.

Ref: jQuery each()

1 Comment

In this way I need to pass all the param's during function call.. This is the main issue .. I have to calculate the number of parameter that I am supporting in this function .. It is around 1000 . I can not pass all the params at a time.. Different combinations of param are passed from different file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.